1/*
2 * Copyright (C) 2015-2019 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
37namespace JSC {
38
39const ClassInfo JSModuleEnvironment::s_info = { "JSModuleEnvironment", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSModuleEnvironment) };
40
41JSModuleEnvironment* 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
64void JSModuleEnvironment::finishCreation(VM& vm, JSValue initialValue, AbstractModuleRecord* moduleRecord)
65{
66 Base::finishCreation(vm, initialValue);
67 this->moduleRecordSlot().set(vm, this, moduleRecord);
68}
69
70void JSModuleEnvironment::visitChildren(JSCell* cell, SlotVisitor& visitor)
71{
72 JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell);
73 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
74 Base::visitChildren(thisObject, visitor);
75 visitor.appendValues(thisObject->variables(), thisObject->symbolTable()->scopeSize());
76 visitor.append(thisObject->moduleRecordSlot());
77}
78
79bool JSModuleEnvironment::getOwnPropertySlot(JSObject* cell, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
80{
81 VM& vm = globalObject->vm();
82 auto scope = DECLARE_THROW_SCOPE(vm);
83 JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell);
84 AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(globalObject, Identifier::fromUid(vm, propertyName.uid()));
85 RETURN_IF_EXCEPTION(scope, false);
86 if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved) {
87 // When resolveImport resolves the resolution, the imported module environment must have the binding.
88 JSModuleEnvironment* importedModuleEnvironment = resolution.moduleRecord->moduleEnvironment();
89 PropertySlot redirectSlot(importedModuleEnvironment, PropertySlot::InternalMethodType::Get);
90 bool result = importedModuleEnvironment->methodTable(vm)->getOwnPropertySlot(importedModuleEnvironment, globalObject, resolution.localName, redirectSlot);
91 ASSERT_UNUSED(result, result);
92 ASSERT(redirectSlot.isValue());
93 JSValue value = redirectSlot.getValue(globalObject, resolution.localName);
94 scope.assertNoException();
95 slot.setValue(thisObject, redirectSlot.attributes(), value);
96 return true;
97 }
98 return Base::getOwnPropertySlot(thisObject, globalObject, propertyName, slot);
99}
100
101void JSModuleEnvironment::getOwnNonIndexPropertyNames(JSObject* cell, JSGlobalObject* globalObject, PropertyNameArray& propertyNamesArray, EnumerationMode mode)
102{
103 JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell);
104 if (propertyNamesArray.includeStringProperties()) {
105 for (const auto& pair : thisObject->moduleRecord()->importEntries()) {
106 const AbstractModuleRecord::ImportEntry& importEntry = pair.value;
107 if (importEntry.type == AbstractModuleRecord::ImportEntryType::Single)
108 propertyNamesArray.add(importEntry.localName);
109 }
110 }
111 return Base::getOwnNonIndexPropertyNames(thisObject, globalObject, propertyNamesArray, mode);
112}
113
114bool JSModuleEnvironment::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
115{
116 VM& vm = globalObject->vm();
117 auto scope = DECLARE_THROW_SCOPE(vm);
118
119 JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell);
120 // All imported bindings are immutable.
121 AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(globalObject, Identifier::fromUid(vm, propertyName.uid()));
122 RETURN_IF_EXCEPTION(scope, false);
123 if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved) {
124 throwTypeError(globalObject, scope, ReadonlyPropertyWriteError);
125 return false;
126 }
127 RELEASE_AND_RETURN(scope, Base::put(thisObject, globalObject, propertyName, value, slot));
128}
129
130bool JSModuleEnvironment::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName)
131{
132 VM& vm = globalObject->vm();
133 auto scope = DECLARE_THROW_SCOPE(vm);
134
135 JSModuleEnvironment* thisObject = jsCast<JSModuleEnvironment*>(cell);
136 // All imported bindings are immutable.
137 AbstractModuleRecord::Resolution resolution = thisObject->moduleRecord()->resolveImport(globalObject, Identifier::fromUid(vm, propertyName.uid()));
138 RETURN_IF_EXCEPTION(scope, false);
139 if (resolution.type == AbstractModuleRecord::Resolution::Type::Resolved)
140 return false;
141 return Base::deleteProperty(thisObject, globalObject, propertyName);
142}
143
144} // namespace JSC
145