1 | /* |
2 | * Copyright (C) 2016 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "ProxyConstructor.h" |
28 | |
29 | #include "Error.h" |
30 | #include "IdentifierInlines.h" |
31 | #include "JSCInlines.h" |
32 | #include "ObjectConstructor.h" |
33 | #include "ObjectPrototype.h" |
34 | #include "ProxyObject.h" |
35 | #include "ProxyRevoke.h" |
36 | #include "StructureInlines.h" |
37 | |
38 | namespace JSC { |
39 | |
40 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ProxyConstructor); |
41 | |
42 | const ClassInfo ProxyConstructor::s_info = { "Proxy" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ProxyConstructor) }; |
43 | |
44 | ProxyConstructor* ProxyConstructor::create(VM& vm, Structure* structure) |
45 | { |
46 | ProxyConstructor* constructor = new (NotNull, allocateCell<ProxyConstructor>(vm.heap)) ProxyConstructor(vm, structure); |
47 | constructor->finishCreation(vm, "Proxy" , structure->globalObject()); |
48 | return constructor; |
49 | } |
50 | |
51 | static EncodedJSValue JSC_HOST_CALL callProxy(ExecState*); |
52 | static EncodedJSValue JSC_HOST_CALL constructProxyObject(ExecState*); |
53 | |
54 | ProxyConstructor::ProxyConstructor(VM& vm, Structure* structure) |
55 | : Base(vm, structure, callProxy, constructProxyObject) |
56 | { |
57 | } |
58 | |
59 | static EncodedJSValue JSC_HOST_CALL makeRevocableProxy(ExecState* exec) |
60 | { |
61 | VM& vm = exec->vm(); |
62 | auto scope = DECLARE_THROW_SCOPE(vm); |
63 | if (exec->argumentCount() < 2) |
64 | return throwVMTypeError(exec, scope, "Proxy.revocable needs to be called with two arguments: the target and the handler"_s ); |
65 | |
66 | ArgList args(exec); |
67 | JSValue target = args.at(0); |
68 | JSValue handler = args.at(1); |
69 | ProxyObject* proxy = ProxyObject::create(exec, exec->lexicalGlobalObject(), target, handler); |
70 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
71 | ProxyRevoke* revoke = ProxyRevoke::create(vm, exec->lexicalGlobalObject()->proxyRevokeStructure(), proxy); |
72 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
73 | |
74 | JSObject* result = constructEmptyObject(exec); |
75 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
76 | result->putDirect(vm, makeIdentifier(vm, "proxy" ), proxy, static_cast<unsigned>(PropertyAttribute::None)); |
77 | result->putDirect(vm, makeIdentifier(vm, "revoke" ), revoke, static_cast<unsigned>(PropertyAttribute::None)); |
78 | |
79 | return JSValue::encode(result); |
80 | } |
81 | |
82 | static EncodedJSValue JSC_HOST_CALL proxyRevocableConstructorThrowError(ExecState* exec) |
83 | { |
84 | auto scope = DECLARE_THROW_SCOPE(exec->vm()); |
85 | return throwVMTypeError(exec, scope, "Proxy.revocable cannot be constructed. It can only be called"_s ); |
86 | } |
87 | |
88 | void ProxyConstructor::finishCreation(VM& vm, const char* name, JSGlobalObject* globalObject) |
89 | { |
90 | Base::finishCreation(vm, name, NameVisibility::Visible, NameAdditionMode::WithStructureTransition); |
91 | putDirect(vm, vm.propertyNames->length, jsNumber(2), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
92 | putDirect(vm, makeIdentifier(vm, "revocable" ), JSFunction::create(vm, globalObject, 2, "revocable"_s , makeRevocableProxy, NoIntrinsic, proxyRevocableConstructorThrowError)); |
93 | } |
94 | |
95 | static EncodedJSValue JSC_HOST_CALL constructProxyObject(ExecState* exec) |
96 | { |
97 | auto scope = DECLARE_THROW_SCOPE(exec->vm()); |
98 | if (exec->newTarget().isUndefined()) |
99 | return throwVMTypeError(exec, scope, "new.target of Proxy construct should not be undefined"_s ); |
100 | |
101 | ArgList args(exec); |
102 | JSValue target = args.at(0); |
103 | JSValue handler = args.at(1); |
104 | RELEASE_AND_RETURN(scope, JSValue::encode(ProxyObject::create(exec, exec->lexicalGlobalObject(), target, handler))); |
105 | } |
106 | |
107 | static EncodedJSValue JSC_HOST_CALL callProxy(ExecState* exec) |
108 | { |
109 | auto scope = DECLARE_THROW_SCOPE(exec->vm()); |
110 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "Proxy" )); |
111 | } |
112 | |
113 | } // namespace JSC |
114 | |