1/*
2 * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSObjectGetProxyTargetTest.h"
28
29#include "APICast.h"
30#include "InitializeThreading.h"
31#include "JSCInlines.h"
32#include "JSObjectRefPrivate.h"
33#include "JSProxy.h"
34#include "JavaScript.h"
35#include "Options.h"
36#include "ProxyObject.h"
37
38using namespace JSC;
39
40int testJSObjectGetProxyTarget()
41{
42 bool overallResult = true;
43
44 printf("JSObjectGetProxyTargetTest:\n");
45
46 auto test = [&] (const char* description, bool currentResult) {
47 printf(" %s: %s\n", description, currentResult ? "PASS" : "FAIL");
48 overallResult &= currentResult;
49 };
50
51 JSContextGroupRef group = JSContextGroupCreate();
52 JSGlobalContextRef context = JSGlobalContextCreateInGroup(group, nullptr);
53
54 ExecState* exec = toJS(context);
55 VM& vm = *toJS(group);
56 JSObjectRef globalObjectProxy = JSContextGetGlobalObject(context);
57
58 JSGlobalObject* globalObjectObject;
59 JSObjectRef globalObject;
60 JSProxy* jsProxyObject;
61
62 {
63 JSLockHolder locker(vm);
64 JSProxy* globalObjectProxyObject = jsCast<JSProxy*>(toJS(globalObjectProxy));
65 globalObjectObject = jsCast<JSGlobalObject*>(globalObjectProxyObject->target());
66 Structure* proxyStructure = JSProxy::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), PureForwardingProxyType);
67 globalObject = toRef(globalObjectObject);
68 jsProxyObject = JSProxy::create(vm, proxyStructure);
69 }
70
71 JSObjectRef array = JSObjectMakeArray(context, 0, nullptr, nullptr);
72
73 ProxyObject* proxyObjectObject;
74
75 {
76 JSLockHolder locker(vm);
77 Structure* emptyObjectStructure = JSFinalObject::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), 0);
78 JSObject* handler = JSFinalObject::create(vm, emptyObjectStructure);
79 proxyObjectObject = ProxyObject::create(exec, globalObjectObject, toJS(array), handler);
80 }
81
82 JSObjectRef jsProxy = toRef(jsProxyObject);
83 JSObjectRef proxyObject = toRef(proxyObjectObject);
84
85 test("proxy target of null is null", !JSObjectGetProxyTarget(nullptr));
86 test("proxy target of non-proxy is null", !JSObjectGetProxyTarget(array));
87 test("proxy target of uninitialized JSProxy is null", !JSObjectGetProxyTarget(jsProxy));
88
89 {
90 JSLockHolder locker(vm);
91 jsProxyObject->setTarget(vm, globalObjectObject);
92 }
93
94 test("proxy target of initialized JSProxy works", JSObjectGetProxyTarget(jsProxy) == globalObject);
95
96 test("proxy target of ProxyObject works", JSObjectGetProxyTarget(proxyObject) == array);
97
98 test("proxy target of GlobalObject is the globalObject", JSObjectGetProxyTarget(globalObjectProxy) == globalObject);
99
100 JSGlobalContextRelease(context);
101 JSContextGroupRelease(group);
102
103 printf("JSObjectGetProxyTargetTest: %s\n", overallResult ? "PASS" : "FAIL");
104 return !overallResult;
105}
106
107