JSObjectGetProxyTargetTest.cpp [plain text]
#include "config.h"
#include "JSObjectGetProxyTargetTest.h"
#include "APICast.h"
#include "InitializeThreading.h"
#include "JSCInlines.h"
#include "JSObjectRefPrivate.h"
#include "JSProxy.h"
#include "JavaScript.h"
#include "Options.h"
#include "ProxyObject.h"
using namespace JSC;
int testJSObjectGetProxyTarget()
{
bool overallResult = true;
printf("JSObjectGetProxyTargetTest:\n");
auto test = [&] (const char* description, bool currentResult) {
printf(" %s: %s\n", description, currentResult ? "PASS" : "FAIL");
overallResult &= currentResult;
};
JSContextGroupRef group = JSContextGroupCreate();
JSContextRef context = JSGlobalContextCreateInGroup(group, nullptr);
ExecState* exec = toJS(context);
VM& vm = *toJS(group);
JSObjectRef globalObjectProxy = JSContextGetGlobalObject(context);
JSProxy* globalObjectProxyObject = jsCast<JSProxy*>(toJS(globalObjectProxy));
JSGlobalObject* globalObjectObject = jsCast<JSGlobalObject*>(globalObjectProxyObject->target());
Structure* proxyStructure = JSProxy::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), PureForwardingProxyType);
JSObjectRef globalObject = toRef(globalObjectObject);
JSProxy* jsProxyObject = JSProxy::create(vm, proxyStructure);
JSObjectRef array = JSObjectMakeArray(context, 0, nullptr, nullptr);
Structure* emptyObjectStructure = JSFinalObject::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), 0);
JSObject* handler = JSFinalObject::create(vm, emptyObjectStructure);
ProxyObject* proxyObjectObject = ProxyObject::create(exec, globalObjectObject, toJS(array), handler);
JSObjectRef jsProxy = toRef(jsProxyObject);
JSObjectRef proxyObject = toRef(proxyObjectObject);
test("proxy target of null is null", !JSObjectGetProxyTarget(nullptr));
test("proxy target of non-proxy is null", !JSObjectGetProxyTarget(array));
test("proxy target of uninitialized JSProxy is null", !JSObjectGetProxyTarget(jsProxy));
jsProxyObject->setTarget(vm, globalObjectObject);
test("proxy target of initialized JSProxy works", JSObjectGetProxyTarget(jsProxy) == globalObject);
test("proxy target of ProxyObject works", JSObjectGetProxyTarget(proxyObject) == array);
test("proxy target of GlobalObject is the globalObject", JSObjectGetProxyTarget(globalObjectProxy) == globalObject);
JSContextGroupRelease(group);
printf("JSObjectGetProxyTargetTest: %s\n", overallResult ? "PASS" : "FAIL");
return !overallResult;
}