1 | /* |
2 | * Copyright (C) 2015-2017 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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | #include "JSModuleEnvironment.h" |
31 | |
32 | #include "AbstractModuleRecord.h" |
33 | #include "Interpreter.h" |
34 | #include "JSCInlines.h" |
35 | #include "JSFunction.h" |
36 | |
37 | namespace JSC { |
38 | |
39 | const ClassInfo JSModuleEnvironment::s_info = { "JSModuleEnvironment" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSModuleEnvironment) }; |
40 | |
41 | JSModuleEnvironment* JSModuleEnvironment::create( |
42 | VM& vm, Structure* structure, JSScope* currentScope, SymbolTable* symbolTable, JSValue initialValue, AbstractModuleRecord* moduleRecord) |
43 | { |
44 | // JSLexicalEnvironment has the storage to store the variable slots after the its class storage. |
45 | // Because the offset of the variable slots are fixed in the JSLexicalEnvironment, inheritting these class and adding new member field is not allowed, |
46 | // the new member will overlap the variable slots. |
47 | // To keep the JSModuleEnvironment compatible to the JSLexicalEnvironment but add the new member to store the AbstractModuleRecord, we additionally allocate |
48 | // the storage after the variable slots. |
49 | // |
50 | // JSLexicalEnvironment: |
51 | // [ JSLexicalEnvironment ][ variable slots ] |
52 | // |
53 | // JSModuleEnvironment: |
54 | // [ JSLexicalEnvironment ][ variable slots ][ additional slots for JSModuleEnvironment ] |
55 | JSModuleEnvironment* result = |
56 | new ( |
57 | NotNull, |
58 | allocateCell<JSModuleEnvironment>(vm.heap, JSModuleEnvironment::allocationSize(symbolTable))) |
59 | JSModuleEnvironment(vm, structure, currentScope, symbolTable); |
60 | result->finishCreation(vm, initialValue, moduleRecord); |
61 | return result; |
62 | } |
63 | |
64 | void JSModuleEnvironment::finishCreation(VM& vm, JSValue initialValue, AbstractModuleRecord* moduleRecord) |
65 | { |
66 | Base::finishCreation(vm, initialValue); |
67 | this->moduleRecordSlot().set(vm, this, moduleRecord); |
68 | } |
69 | |
70 | void JSModuleEnvironment::visitChildren(JSCell* cell, SlotVisitor& visitor) |
71 | { |
72 | JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell); |
73 | Base::visitChildren(thisObject, visitor); |
74 | visitor.appendValues(thisObject->variables(), thisObject->symbolTable()->scopeSize()); |
75 | visitor.append(thisObject->moduleRecordSlot()); |
76 | } |
77 | |
78 | bool JSModuleEnvironment::getOwnPropertySlot(JSObject* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot) |
79 | { |
80 | VM& vm = exec->vm(); |
81 | auto scope = DECLARE_THROW_SCOPE(vm); |
82 | JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell); |
83 | AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(exec, Identifier::fromUid(exec, propertyName.uid())); |
84 | RETURN_IF_EXCEPTION(scope, false); |
85 | if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved) { |
86 | // When resolveImport resolves the resolution, the imported module environment must have the binding. |
87 | JSModuleEnvironment* importedModuleEnvironment = resolution.moduleRecord->moduleEnvironment(); |
88 | PropertySlot redirectSlot(importedModuleEnvironment, PropertySlot::InternalMethodType::Get); |
89 | bool result = importedModuleEnvironment->methodTable(vm)->getOwnPropertySlot(importedModuleEnvironment, exec, resolution.localName, redirectSlot); |
90 | ASSERT_UNUSED(result, result); |
91 | ASSERT(redirectSlot.isValue()); |
92 | JSValue value = redirectSlot.getValue(exec, resolution.localName); |
93 | scope.assertNoException(); |
94 | slot.setValue(thisObject, redirectSlot.attributes(), value); |
95 | return true; |
96 | } |
97 | return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot); |
98 | } |
99 | |
100 | void JSModuleEnvironment::getOwnNonIndexPropertyNames(JSObject* cell, ExecState* exec, PropertyNameArray& propertyNamesArray, EnumerationMode mode) |
101 | { |
102 | JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell); |
103 | if (propertyNamesArray.includeStringProperties()) { |
104 | for (const auto& pair : thisObject->moduleRecord()->importEntries()) { |
105 | const AbstractModuleRecord::ImportEntry& importEntry = pair.value; |
106 | if (importEntry.type == AbstractModuleRecord::ImportEntryType::Single) |
107 | propertyNamesArray.add(importEntry.localName); |
108 | } |
109 | } |
110 | return Base::getOwnNonIndexPropertyNames(thisObject, exec, propertyNamesArray, mode); |
111 | } |
112 | |
113 | bool JSModuleEnvironment::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) |
114 | { |
115 | VM& vm = exec->vm(); |
116 | auto scope = DECLARE_THROW_SCOPE(vm); |
117 | |
118 | JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell); |
119 | // All imported bindings are immutable. |
120 | AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(exec, Identifier::fromUid(exec, propertyName.uid())); |
121 | RETURN_IF_EXCEPTION(scope, false); |
122 | if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved) { |
123 | throwTypeError(exec, scope, ReadonlyPropertyWriteError); |
124 | return false; |
125 | } |
126 | RELEASE_AND_RETURN(scope, Base::put(thisObject, exec, propertyName, value, slot)); |
127 | } |
128 | |
129 | bool JSModuleEnvironment::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName) |
130 | { |
131 | VM& vm = exec->vm(); |
132 | auto scope = DECLARE_THROW_SCOPE(vm); |
133 | |
134 | JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell); |
135 | // All imported bindings are immutable. |
136 | AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(exec, Identifier::fromUid(exec, propertyName.uid())); |
137 | RETURN_IF_EXCEPTION(scope, false); |
138 | if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved) |
139 | return false; |
140 | return Base::deleteProperty(thisObject, exec, propertyName); |
141 | } |
142 | |
143 | } // namespace JSC |
144 | |