forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleNamespace.cpp
More file actions
400 lines (360 loc) · 18.2 KB
/
Copy pathModuleNamespace.cpp
File metadata and controls
400 lines (360 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLanguagePch.h"
#include "Types/PropertyIndexRanges.h"
#include "Types/SimpleDictionaryPropertyDescriptor.h"
#include "Types/SimpleDictionaryTypeHandler.h"
#include "ModuleNamespace.h"
#include "ModuleNamespaceEnumerator.h"
namespace Js
{
ModuleNamespace::ModuleNamespace(ModuleRecordBase* moduleRecord, DynamicType* type) :
moduleRecord(moduleRecord), DynamicObject(type), unambiguousNonLocalExports(nullptr),
sortedExportedNames(nullptr), nsSlots(nullptr)
{
}
ModuleNamespace* ModuleNamespace::GetModuleNamespace(ModuleRecordBase* requestModule)
{
Assert(requestModule->IsSourceTextModuleRecord());
SourceTextModuleRecord* moduleRecord = SourceTextModuleRecord::FromHost(requestModule);
ModuleNamespace* nsObject = moduleRecord->GetNamespace();
if (nsObject != nullptr)
{
return nsObject;
}
ScriptContext* scriptContext = moduleRecord->GetRealm()->GetScriptContext();
Recycler* recycler = scriptContext->GetRecycler();
nsObject = RecyclerNew(recycler, ModuleNamespace, moduleRecord, scriptContext->GetLibrary()->GetModuleNamespaceType());
nsObject->Initialize();
moduleRecord->SetNamespace(nsObject);
return nsObject;
}
void ModuleNamespace::Initialize()
{
ScriptContext* scriptContext = moduleRecord->GetRealm()->GetScriptContext();
Recycler* recycler = scriptContext->GetRecycler();
SourceTextModuleRecord* sourceTextModuleRecord = static_cast<SourceTextModuleRecord*>(
static_cast<ModuleRecordBase*>(moduleRecord));
JavascriptLibrary* library = GetLibrary();
if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
{
DynamicObject::SetPropertyWithAttributes(PropertyIds::_symbolToStringTag, library->GetModuleTypeDisplayString(),
PropertyNone, nullptr);
}
ModuleImportOrExportEntryList* localExportList = sourceTextModuleRecord->GetLocalExportEntryList();
// We don't have a type handler that can handle ModuleNamespace object. We have properties that could be aliased
// like {export foo as foo1, foo2, foo3}, and external properties as reexport from current module. The problem with aliasing
// is that multiple propertyId can be associated with one slotIndex. We need to build from PropertyMap directly here.
// there is one instance of ModuleNamespace per module file; we can always use the BigPropertyIndex for security.
propertyMap = RecyclerNew(recycler, SimplePropertyDescriptorMap, recycler, sourceTextModuleRecord->GetLocalExportCount());
if (localExportList != nullptr)
{
localExportList->Map([=](ModuleImportOrExportEntry exportEntry) {
PropertyId exportNameId = exportEntry.exportName->GetPropertyId();
PropertyId localNameId = exportEntry.localName->GetPropertyId();
const Js::PropertyRecord* exportNameRecord = scriptContext->GetThreadContext()->GetPropertyName(exportNameId);
ModuleNameRecord* importRecord = nullptr;
AssertMsg(exportNameId != Js::Constants::NoProperty, "should have been initialized already");
// ignore local exports that are actually indirect exports.
if (sourceTextModuleRecord->GetImportEntryList() == nullptr ||
!sourceTextModuleRecord->ResolveImport(localNameId, &importRecord))
{
BigPropertyIndex index = sourceTextModuleRecord->GetLocalExportSlotIndexByExportName(exportNameId);
Assert((uint)index < sourceTextModuleRecord->GetLocalExportCount());
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor = { index, PropertyModuleNamespaceDefault };
propertyMap->Add(exportNameRecord, propertyDescriptor);
}
});
}
// update the local slot to use the storage for local exports.
SetNSSlotsForModuleNS(sourceTextModuleRecord->GetLocalExportSlots());
// For items that are not in the local export list, we need to resolve them to get it
ExportedNames* exportedNames = sourceTextModuleRecord->GetExportedNames(nullptr);
ModuleNameRecord* moduleNameRecord = nullptr;
sortedExportedNames = ListForListIterator::New(scriptContext->GetRecycler());
#if DBG
uint unresolvableExportsCount = 0;
uint localExportCount = 0;
#endif
if (exportedNames != nullptr)
{
exportedNames->Map([&](PropertyId propertyId) {
JavascriptString* propertyString = scriptContext->GetPropertyString(propertyId);
sortedExportedNames->Add(propertyString);
if (!moduleRecord->ResolveExport(propertyId, nullptr, &moduleNameRecord))
{
// ignore ambiguous resolution.
#if DBG
unresolvableExportsCount++;
#endif
return;
}
// non-ambiguous resolution.
if (moduleNameRecord == nullptr)
{
JavascriptError::ThrowSyntaxError(scriptContext, JSERR_ResolveExportFailed, scriptContext->GetPropertyName(propertyId)->GetBuffer());
}
if (moduleNameRecord->module == moduleRecord)
{
// skip local exports as they are covered in the localExportSlots.
// have to check the property map to avoid filtering out aliased local re-exports
// which are not covered in localExportSlots
if (propertyMap->ContainsKey(scriptContext->GetThreadContext()->GetPropertyName(propertyId)))
{
#if DBG
localExportCount++;
#endif
return;
}
}
this->AddUnambiguousNonLocalExport(propertyId, moduleNameRecord);
});
}
sortedExportedNames->Sort([](void* context, const void* left, const void* right) ->int {
JavascriptString** leftString = (JavascriptString**)(left);
JavascriptString** rightString = (JavascriptString**)(right);
return JavascriptString::strcmp(*leftString, *rightString);
}, nullptr);
#if DBG
uint totalExportCount = exportedNames != nullptr ? exportedNames->Count() : 0;
uint unambiguousNonLocalCount = (this->GetUnambiguousNonLocalExports() != nullptr) ? this->GetUnambiguousNonLocalExports()->Count() : 0;
Assert(totalExportCount == localExportCount + unambiguousNonLocalCount + unresolvableExportsCount);
#endif
BOOL result = this->PreventExtensions();
Assert(result);
}
void ModuleNamespace::AddUnambiguousNonLocalExport(PropertyId propertyId, ModuleNameRecord* nonLocalExportNameRecord)
{
Recycler* recycler = GetScriptContext()->GetRecycler();
if (unambiguousNonLocalExports == nullptr)
{
unambiguousNonLocalExports = RecyclerNew(recycler, UnambiguousExportMap, recycler, 4);
}
// keep a local copy of the module/
unambiguousNonLocalExports->AddNew(propertyId, *nonLocalExportNameRecord);
}
PropertyQueryFlags ModuleNamespace::HasPropertyQuery(PropertyId propertyId, _Inout_opt_ PropertyValueInfo* info)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
const Js::PropertyRecord* propertyRecord = GetScriptContext()->GetThreadContext()->GetPropertyName(propertyId);
if (propertyRecord->IsSymbol())
{
return this->DynamicObject::HasPropertyQuery(propertyId, info);
}
if (propertyMap != nullptr && propertyMap->TryGetValue(propertyRecord, &propertyDescriptor))
{
return PropertyQueryFlags::Property_Found;
}
if (unambiguousNonLocalExports != nullptr)
{
return JavascriptConversion::BooleanToPropertyQueryFlags(unambiguousNonLocalExports->ContainsKey(propertyId));
}
return PropertyQueryFlags::Property_NotFound;
}
BOOL ModuleNamespace::HasOwnProperty(PropertyId propertyId)
{
return HasProperty(propertyId);
}
BOOL ModuleNamespace::IsConfigurable(PropertyId propertyId)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
const Js::PropertyRecord* propertyRecord = GetScriptContext()->GetThreadContext()->GetPropertyName(propertyId);
if (propertyRecord->IsSymbol())
{
return this->DynamicObject::IsConfigurable(propertyId);
}
if (propertyMap != nullptr && propertyMap->TryGetValue(propertyRecord, &propertyDescriptor))
{
return !!(propertyDescriptor.Attributes & PropertyConfigurable);
}
if (unambiguousNonLocalExports != nullptr)
{
ModuleNameRecord moduleNameRecord;
if (unambiguousNonLocalExports->TryGetValue(propertyId, &moduleNameRecord))
{
return !!(PropertyModuleNamespaceDefault & PropertyConfigurable);
}
}
return DynamicObject::IsConfigurable(propertyId);
}
BOOL ModuleNamespace::IsEnumerable(PropertyId propertyId)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
const Js::PropertyRecord* propertyRecord = GetScriptContext()->GetThreadContext()->GetPropertyName(propertyId);
if (propertyRecord->IsSymbol())
{
return this->DynamicObject::IsEnumerable(propertyId);
}
if (propertyMap != nullptr && propertyMap->TryGetValue(propertyRecord, &propertyDescriptor))
{
return !!(propertyDescriptor.Attributes & PropertyEnumerable);
}
if (unambiguousNonLocalExports != nullptr)
{
ModuleNameRecord moduleNameRecord;
if (unambiguousNonLocalExports->TryGetValue(propertyId, &moduleNameRecord))
{
return !!(PropertyModuleNamespaceDefault & PropertyEnumerable);
}
}
return DynamicObject::IsEnumerable(propertyId);
}
BOOL ModuleNamespace::IsWritable(PropertyId propertyId)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
const Js::PropertyRecord* propertyRecord = GetScriptContext()->GetThreadContext()->GetPropertyName(propertyId);
if (propertyRecord->IsSymbol())
{
return this->DynamicObject::IsWritable(propertyId);
}
if (propertyMap != nullptr && propertyMap->TryGetValue(propertyRecord, &propertyDescriptor))
{
return !!(propertyDescriptor.Attributes & PropertyWritable);
}
if (unambiguousNonLocalExports != nullptr)
{
ModuleNameRecord moduleNameRecord;
if (unambiguousNonLocalExports->TryGetValue(propertyId, &moduleNameRecord))
{
return !!(PropertyModuleNamespaceDefault & PropertyWritable);
}
}
return DynamicObject::IsWritable(propertyId);
}
PropertyQueryFlags ModuleNamespace::GetPropertyQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
const Js::PropertyRecord* propertyRecord = requestContext->GetThreadContext()->GetPropertyName(propertyId);
if (propertyRecord->IsSymbol())
{
return this->DynamicObject::GetPropertyQuery(originalInstance, propertyId, value, info, requestContext);
}
if (propertyMap != nullptr && propertyMap->TryGetValue(propertyRecord, &propertyDescriptor))
{
Assert((uint)propertyDescriptor.propertyIndex < ((SourceTextModuleRecord*)static_cast<ModuleRecordBase*>(moduleRecord))->GetLocalExportCount());
PropertyValueInfo::SetNoCache(info, this); // Disable inlinecache for localexport slot for now.
//if ((PropertyIndex)propertyDescriptor.propertyIndex == propertyDescriptor.propertyIndex)
//{
// PropertyValueInfo::Set(info, this, (PropertyIndex)propertyDescriptor.propertyIndex, propertyDescriptor.Attributes);
//}
*value = this->GetNSSlot(propertyDescriptor.propertyIndex);
return PropertyQueryFlags::Property_Found;
}
if (unambiguousNonLocalExports != nullptr)
{
ModuleNameRecord moduleNameRecord;
// TODO: maybe we can cache the slot address & offset, instead of looking up everytime? We do need to look up the reference everytime.
if (unambiguousNonLocalExports->TryGetValue(propertyId, &moduleNameRecord))
{
// special case for export * as ns
if (moduleNameRecord.bindingName == Js::PropertyIds::star_)
{
*value = static_cast<Var>(moduleNameRecord.module->GetNamespace());
return PropertyQueryFlags::Property_Found;
}
return JavascriptConversion::BooleanToPropertyQueryFlags(moduleNameRecord.module->GetNamespace()->GetProperty(originalInstance, moduleNameRecord.bindingName, value, info, requestContext));
}
}
return PropertyQueryFlags::Property_NotFound;
}
PropertyQueryFlags ModuleNamespace::GetPropertyQuery(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
const PropertyRecord* propertyRecord = nullptr;
GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
return JavascriptConversion::BooleanToPropertyQueryFlags(GetProperty(originalInstance, propertyRecord->GetPropertyId(), value, info, requestContext));
}
BOOL ModuleNamespace::GetInternalProperty(Var instance, PropertyId internalPropertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
return FALSE;
}
PropertyQueryFlags ModuleNamespace::GetPropertyReferenceQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
return JavascriptConversion::BooleanToPropertyQueryFlags(GetProperty(originalInstance, propertyId, value, info, requestContext));
}
BOOL ModuleNamespace::GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, EnumeratorCache * enumeratorCache)
{
ModuleNamespaceEnumerator* moduleEnumerator = ModuleNamespaceEnumerator::New(this, flags, requestContext, enumeratorCache);
if (moduleEnumerator == nullptr)
{
return FALSE;
}
return enumerator->Initialize(moduleEnumerator, nullptr, nullptr, flags, requestContext, nullptr);
}
BOOL ModuleNamespace::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
{
//Assert: IsPropertyKey(P) is true.
//Let exports be O.[[Exports]].
//If P is an element of exports, return false.
//Return true.
return !HasProperty(propertyId);
}
BOOL ModuleNamespace::DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags)
{
//Assert: IsPropertyKey(P) is true.
//Let exports be O.[[Exports]].
//If P is an element of exports, return false.
//Return true.
PropertyRecord const *propertyRecord = nullptr;
if (JavascriptOperators::ShouldTryDeleteProperty(this, propertyNameString, &propertyRecord))
{
Assert(propertyRecord);
return DeleteProperty(propertyRecord->GetPropertyId(), flags);
}
return TRUE;
}
BOOL ModuleNamespace::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
{
stringBuilder->AppendCppLiteral(_u("{ModuleNamespaceObject}"));
return TRUE;
}
BOOL ModuleNamespace::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
{
stringBuilder->AppendCppLiteral(_u("Object, (ModuleNamespaceObject)"));
return TRUE;
}
Var ModuleNamespace::GetNSSlot(BigPropertyIndex propertyIndex)
{
Assert((uint)propertyIndex < static_cast<SourceTextModuleRecord*>(static_cast<ModuleRecordBase*>(moduleRecord))->GetLocalExportCount());
return this->nsSlots[propertyIndex];
}
PropertyId ModuleNamespace::GetPropertyId(BigPropertyIndex index)
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor;
if (propertyMap->TryGetValueAt(index, &propertyDescriptor))
{
const PropertyRecord* propertyRecord = propertyMap->GetKeyAt(index);
return propertyRecord->GetPropertyId();
}
return Constants::NoProperty;
}
BOOL ModuleNamespace::FindNextProperty(BigPropertyIndex& index, JavascriptString** propertyString, PropertyId* propertyId, PropertyAttributes* attributes, ScriptContext * requestContext) const
{
if (index < propertyMap->Count())
{
SimpleDictionaryPropertyDescriptor<BigPropertyIndex> propertyDescriptor(propertyMap->GetValueAt(index));
Assert(propertyDescriptor.Attributes == PropertyModuleNamespaceDefault);
const PropertyRecord* propertyRecord = propertyMap->GetKeyAt(index);
*propertyId = propertyRecord->GetPropertyId();
if (propertyString != nullptr)
{
*propertyString = requestContext->GetPropertyString(*propertyId);
}
if (attributes != nullptr)
{
*attributes = propertyDescriptor.Attributes;
}
return TRUE;
}
else
{
*propertyId = Constants::NoProperty;
if (propertyString != nullptr)
{
*propertyString = nullptr;
}
}
return FALSE;
}
}