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 | |
38 | using namespace JSC; |
39 | |
40 | int 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 | VM& vm = *toJS(group); |
55 | JSObjectRef globalObjectProxy = JSContextGetGlobalObject(context); |
56 | |
57 | JSGlobalObject* globalObjectObject; |
58 | JSObjectRef globalObjectRef; |
59 | JSProxy* jsProxyObject; |
60 | |
61 | { |
62 | JSLockHolder locker(vm); |
63 | JSProxy* globalObjectProxyObject = jsCast<JSProxy*>(toJS(globalObjectProxy)); |
64 | globalObjectObject = jsCast<JSGlobalObject*>(globalObjectProxyObject->target()); |
65 | Structure* proxyStructure = JSProxy::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), PureForwardingProxyType); |
66 | globalObjectRef = toRef(jsCast<JSObject*>(globalObjectObject)); |
67 | jsProxyObject = JSProxy::create(vm, proxyStructure); |
68 | } |
69 | |
70 | JSObjectRef array = JSObjectMakeArray(context, 0, nullptr, nullptr); |
71 | |
72 | ProxyObject* proxyObjectObject; |
73 | |
74 | { |
75 | JSLockHolder locker(vm); |
76 | Structure* emptyObjectStructure = JSFinalObject::createStructure(vm, globalObjectObject, globalObjectObject->objectPrototype(), 0); |
77 | JSObject* handler = JSFinalObject::create(vm, emptyObjectStructure); |
78 | proxyObjectObject = ProxyObject::create(globalObjectObject, toJS(array), handler); |
79 | } |
80 | |
81 | JSObjectRef jsProxy = toRef(jsProxyObject); |
82 | JSObjectRef proxyObject = toRef(proxyObjectObject); |
83 | |
84 | test("proxy target of null is null" , !JSObjectGetProxyTarget(nullptr)); |
85 | test("proxy target of non-proxy is null" , !JSObjectGetProxyTarget(array)); |
86 | test("proxy target of uninitialized JSProxy is null" , !JSObjectGetProxyTarget(jsProxy)); |
87 | |
88 | { |
89 | JSLockHolder locker(vm); |
90 | jsProxyObject->setTarget(vm, globalObjectObject); |
91 | } |
92 | |
93 | test("proxy target of initialized JSProxy works" , JSObjectGetProxyTarget(jsProxy) == globalObjectRef); |
94 | |
95 | test("proxy target of ProxyObject works" , JSObjectGetProxyTarget(proxyObject) == array); |
96 | |
97 | test("proxy target of GlobalObject is the globalObject" , JSObjectGetProxyTarget(globalObjectProxy) == globalObjectRef); |
98 | |
99 | JSGlobalContextRelease(context); |
100 | JSContextGroupRelease(group); |
101 | |
102 | printf("JSObjectGetProxyTargetTest: %s\n" , overallResult ? "PASS" : "FAIL" ); |
103 | return !overallResult; |
104 | } |
105 | |
106 | |