Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "builtin/ModuleObject.h"
8 :
9 : #include "builtin/SelfHostingDefines.h"
10 : #include "frontend/ParseNode.h"
11 : #include "frontend/SharedContext.h"
12 : #include "gc/Policy.h"
13 : #include "gc/Tracer.h"
14 :
15 : #include "jsobjinlines.h"
16 : #include "jsscriptinlines.h"
17 :
18 : using namespace js;
19 : using namespace js::frontend;
20 :
21 : static_assert(MODULE_STATE_FAILED < MODULE_STATE_PARSED &&
22 : MODULE_STATE_PARSED < MODULE_STATE_INSTANTIATED &&
23 : MODULE_STATE_INSTANTIATED < MODULE_STATE_EVALUATED,
24 : "Module states are ordered incorrectly");
25 :
26 : template<typename T, Value ValueGetter(const T* obj)>
27 : static bool
28 0 : ModuleValueGetterImpl(JSContext* cx, const CallArgs& args)
29 : {
30 0 : args.rval().set(ValueGetter(&args.thisv().toObject().as<T>()));
31 0 : return true;
32 : }
33 :
34 : template<typename T, Value ValueGetter(const T* obj)>
35 : static bool
36 0 : ModuleValueGetter(JSContext* cx, unsigned argc, Value* vp)
37 : {
38 0 : CallArgs args = CallArgsFromVp(argc, vp);
39 0 : return CallNonGenericMethod<T::isInstance, ModuleValueGetterImpl<T, ValueGetter>>(cx, args);
40 : }
41 :
42 : #define DEFINE_GETTER_FUNCTIONS(cls, name, slot) \
43 : static Value \
44 : cls##_##name##Value(const cls* obj) { \
45 : return obj->getFixedSlot(cls::slot); \
46 : } \
47 : \
48 : static bool \
49 : cls##_##name##Getter(JSContext* cx, unsigned argc, Value* vp) \
50 : { \
51 : return ModuleValueGetter<cls, cls##_##name##Value>(cx, argc, vp); \
52 : }
53 :
54 : #define DEFINE_ATOM_ACCESSOR_METHOD(cls, name) \
55 : JSAtom* \
56 : cls::name() const \
57 : { \
58 : Value value = cls##_##name##Value(this); \
59 : return &value.toString()->asAtom(); \
60 : }
61 :
62 : #define DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(cls, name) \
63 : JSAtom* \
64 : cls::name() const \
65 : { \
66 : Value value = cls##_##name##Value(this); \
67 : if (value.isNull()) \
68 : return nullptr; \
69 : return &value.toString()->asAtom(); \
70 : }
71 :
72 : ///////////////////////////////////////////////////////////////////////////
73 : // ImportEntryObject
74 :
75 : /* static */ const Class
76 : ImportEntryObject::class_ = {
77 : "ImportEntry",
78 : JSCLASS_HAS_RESERVED_SLOTS(ImportEntryObject::SlotCount) |
79 : JSCLASS_IS_ANONYMOUS
80 : };
81 :
82 0 : DEFINE_GETTER_FUNCTIONS(ImportEntryObject, moduleRequest, ModuleRequestSlot)
83 0 : DEFINE_GETTER_FUNCTIONS(ImportEntryObject, importName, ImportNameSlot)
84 0 : DEFINE_GETTER_FUNCTIONS(ImportEntryObject, localName, LocalNameSlot)
85 :
86 0 : DEFINE_ATOM_ACCESSOR_METHOD(ImportEntryObject, moduleRequest)
87 0 : DEFINE_ATOM_ACCESSOR_METHOD(ImportEntryObject, importName)
88 0 : DEFINE_ATOM_ACCESSOR_METHOD(ImportEntryObject, localName)
89 :
90 : /* static */ bool
91 0 : ImportEntryObject::isInstance(HandleValue value)
92 : {
93 0 : return value.isObject() && value.toObject().is<ImportEntryObject>();
94 : }
95 :
96 : /* static */ bool
97 0 : GlobalObject::initImportEntryProto(JSContext* cx, Handle<GlobalObject*> global)
98 : {
99 : static const JSPropertySpec protoAccessors[] = {
100 : JS_PSG("moduleRequest", ImportEntryObject_moduleRequestGetter, 0),
101 : JS_PSG("importName", ImportEntryObject_importNameGetter, 0),
102 : JS_PSG("localName", ImportEntryObject_localNameGetter, 0),
103 : JS_PS_END
104 : };
105 :
106 0 : RootedObject proto(cx, GlobalObject::createBlankPrototype<PlainObject>(cx, global));
107 0 : if (!proto)
108 0 : return false;
109 :
110 0 : if (!DefinePropertiesAndFunctions(cx, proto, protoAccessors, nullptr))
111 0 : return false;
112 :
113 0 : global->setReservedSlot(IMPORT_ENTRY_PROTO, ObjectValue(*proto));
114 0 : return true;
115 : }
116 :
117 : /* static */ ImportEntryObject*
118 0 : ImportEntryObject::create(JSContext* cx,
119 : HandleAtom moduleRequest,
120 : HandleAtom importName,
121 : HandleAtom localName)
122 : {
123 0 : RootedObject proto(cx, cx->global()->getImportEntryPrototype());
124 0 : RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
125 0 : if (!obj)
126 0 : return nullptr;
127 :
128 0 : RootedImportEntryObject self(cx, &obj->as<ImportEntryObject>());
129 0 : self->initReservedSlot(ModuleRequestSlot, StringValue(moduleRequest));
130 0 : self->initReservedSlot(ImportNameSlot, StringValue(importName));
131 0 : self->initReservedSlot(LocalNameSlot, StringValue(localName));
132 0 : return self;
133 : }
134 :
135 : ///////////////////////////////////////////////////////////////////////////
136 : // ExportEntryObject
137 :
138 : /* static */ const Class
139 : ExportEntryObject::class_ = {
140 : "ExportEntry",
141 : JSCLASS_HAS_RESERVED_SLOTS(ExportEntryObject::SlotCount) |
142 : JSCLASS_IS_ANONYMOUS
143 : };
144 :
145 0 : DEFINE_GETTER_FUNCTIONS(ExportEntryObject, exportName, ExportNameSlot)
146 0 : DEFINE_GETTER_FUNCTIONS(ExportEntryObject, moduleRequest, ModuleRequestSlot)
147 0 : DEFINE_GETTER_FUNCTIONS(ExportEntryObject, importName, ImportNameSlot)
148 0 : DEFINE_GETTER_FUNCTIONS(ExportEntryObject, localName, LocalNameSlot)
149 :
150 0 : DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(ExportEntryObject, exportName)
151 0 : DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(ExportEntryObject, moduleRequest)
152 0 : DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(ExportEntryObject, importName)
153 0 : DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(ExportEntryObject, localName)
154 :
155 : /* static */ bool
156 0 : ExportEntryObject::isInstance(HandleValue value)
157 : {
158 0 : return value.isObject() && value.toObject().is<ExportEntryObject>();
159 : }
160 :
161 : /* static */ bool
162 0 : GlobalObject::initExportEntryProto(JSContext* cx, Handle<GlobalObject*> global)
163 : {
164 : static const JSPropertySpec protoAccessors[] = {
165 : JS_PSG("exportName", ExportEntryObject_exportNameGetter, 0),
166 : JS_PSG("moduleRequest", ExportEntryObject_moduleRequestGetter, 0),
167 : JS_PSG("importName", ExportEntryObject_importNameGetter, 0),
168 : JS_PSG("localName", ExportEntryObject_localNameGetter, 0),
169 : JS_PS_END
170 : };
171 :
172 0 : RootedObject proto(cx, GlobalObject::createBlankPrototype<PlainObject>(cx, global));
173 0 : if (!proto)
174 0 : return false;
175 :
176 0 : if (!DefinePropertiesAndFunctions(cx, proto, protoAccessors, nullptr))
177 0 : return false;
178 :
179 0 : global->setReservedSlot(EXPORT_ENTRY_PROTO, ObjectValue(*proto));
180 0 : return true;
181 : }
182 :
183 : static Value
184 0 : StringOrNullValue(JSString* maybeString)
185 : {
186 0 : return maybeString ? StringValue(maybeString) : NullValue();
187 : }
188 :
189 : /* static */ ExportEntryObject*
190 0 : ExportEntryObject::create(JSContext* cx,
191 : HandleAtom maybeExportName,
192 : HandleAtom maybeModuleRequest,
193 : HandleAtom maybeImportName,
194 : HandleAtom maybeLocalName)
195 : {
196 0 : RootedObject proto(cx, cx->global()->getExportEntryPrototype());
197 0 : RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
198 0 : if (!obj)
199 0 : return nullptr;
200 :
201 0 : RootedExportEntryObject self(cx, &obj->as<ExportEntryObject>());
202 0 : self->initReservedSlot(ExportNameSlot, StringOrNullValue(maybeExportName));
203 0 : self->initReservedSlot(ModuleRequestSlot, StringOrNullValue(maybeModuleRequest));
204 0 : self->initReservedSlot(ImportNameSlot, StringOrNullValue(maybeImportName));
205 0 : self->initReservedSlot(LocalNameSlot, StringOrNullValue(maybeLocalName));
206 0 : return self;
207 : }
208 :
209 : ///////////////////////////////////////////////////////////////////////////
210 : // IndirectBindingMap
211 :
212 0 : IndirectBindingMap::Binding::Binding(ModuleEnvironmentObject* environment, Shape* shape)
213 0 : : environment(environment), shape(shape)
214 0 : {}
215 :
216 0 : IndirectBindingMap::IndirectBindingMap(Zone* zone)
217 0 : : map_(ZoneAllocPolicy(zone))
218 : {
219 0 : }
220 :
221 : bool
222 0 : IndirectBindingMap::init()
223 : {
224 0 : return map_.init();
225 : }
226 :
227 : void
228 0 : IndirectBindingMap::trace(JSTracer* trc)
229 : {
230 0 : for (Map::Enum e(map_); !e.empty(); e.popFront()) {
231 0 : Binding& b = e.front().value();
232 0 : TraceEdge(trc, &b.environment, "module bindings environment");
233 0 : TraceEdge(trc, &b.shape, "module bindings shape");
234 0 : jsid bindingName = e.front().key();
235 0 : TraceManuallyBarrieredEdge(trc, &bindingName, "module bindings binding name");
236 0 : MOZ_ASSERT(bindingName == e.front().key());
237 : }
238 0 : }
239 :
240 : bool
241 0 : IndirectBindingMap::putNew(JSContext* cx, HandleId name,
242 : HandleModuleEnvironmentObject environment, HandleId localName)
243 : {
244 0 : RootedShape shape(cx, environment->lookup(cx, localName));
245 0 : MOZ_ASSERT(shape);
246 0 : if (!map_.putNew(name, Binding(environment, shape))) {
247 0 : ReportOutOfMemory(cx);
248 0 : return false;
249 : }
250 :
251 0 : return true;
252 : }
253 :
254 : bool
255 0 : IndirectBindingMap::lookup(jsid name, ModuleEnvironmentObject** envOut, Shape** shapeOut) const
256 : {
257 0 : auto ptr = map_.lookup(name);
258 0 : if (!ptr)
259 0 : return false;
260 :
261 0 : const Binding& binding = ptr->value();
262 0 : MOZ_ASSERT(binding.environment);
263 0 : MOZ_ASSERT(!binding.environment->inDictionaryMode());
264 0 : MOZ_ASSERT(binding.environment->containsPure(binding.shape));
265 0 : *envOut = binding.environment;
266 0 : *shapeOut = binding.shape;
267 0 : return true;
268 : }
269 :
270 : ///////////////////////////////////////////////////////////////////////////
271 : // ModuleNamespaceObject
272 :
273 3 : /* static */ const ModuleNamespaceObject::ProxyHandler ModuleNamespaceObject::proxyHandler;
274 :
275 : /* static */ bool
276 0 : ModuleNamespaceObject::isInstance(HandleValue value)
277 : {
278 0 : return value.isObject() && value.toObject().is<ModuleNamespaceObject>();
279 : }
280 :
281 : /* static */ ModuleNamespaceObject*
282 0 : ModuleNamespaceObject::create(JSContext* cx, HandleModuleObject module)
283 : {
284 0 : RootedValue priv(cx, ObjectValue(*module));
285 0 : ProxyOptions options;
286 0 : options.setLazyProto(true);
287 0 : options.setSingleton(true);
288 0 : RootedObject object(cx, NewProxyObject(cx, &proxyHandler, priv, nullptr, options));
289 0 : if (!object)
290 0 : return nullptr;
291 :
292 0 : return &object->as<ModuleNamespaceObject>();
293 : }
294 :
295 : ModuleObject&
296 0 : ModuleNamespaceObject::module()
297 : {
298 0 : return GetProxyPrivate(this).toObject().as<ModuleObject>();
299 : }
300 :
301 : JSObject&
302 0 : ModuleNamespaceObject::exports()
303 : {
304 0 : JSObject* exports = module().namespaceExports();
305 0 : MOZ_ASSERT(exports);
306 0 : return *exports;
307 : }
308 :
309 : IndirectBindingMap&
310 0 : ModuleNamespaceObject::bindings()
311 : {
312 0 : IndirectBindingMap* bindings = module().namespaceBindings();
313 0 : MOZ_ASSERT(bindings);
314 0 : return *bindings;
315 : }
316 :
317 : bool
318 0 : ModuleNamespaceObject::addBinding(JSContext* cx, HandleAtom exportedName,
319 : HandleModuleObject targetModule, HandleAtom localName)
320 : {
321 0 : IndirectBindingMap* bindings(this->module().namespaceBindings());
322 0 : MOZ_ASSERT(bindings);
323 :
324 0 : RootedModuleEnvironmentObject environment(cx, &targetModule->initialEnvironment());
325 0 : RootedId exportedNameId(cx, AtomToId(exportedName));
326 0 : RootedId localNameId(cx, AtomToId(localName));
327 0 : return bindings->putNew(cx, exportedNameId, environment, localNameId);
328 : }
329 :
330 : const char ModuleNamespaceObject::ProxyHandler::family = 0;
331 :
332 3 : ModuleNamespaceObject::ProxyHandler::ProxyHandler()
333 3 : : BaseProxyHandler(&family, false)
334 3 : {}
335 :
336 : bool
337 0 : ModuleNamespaceObject::ProxyHandler::getPrototype(JSContext* cx, HandleObject proxy,
338 : MutableHandleObject protop) const
339 : {
340 0 : protop.set(nullptr);
341 0 : return true;
342 : }
343 :
344 : bool
345 0 : ModuleNamespaceObject::ProxyHandler::setPrototype(JSContext* cx, HandleObject proxy,
346 : HandleObject proto, ObjectOpResult& result) const
347 : {
348 0 : if (!proto)
349 0 : return result.succeed();
350 0 : return result.failCantSetProto();
351 : }
352 :
353 : bool
354 0 : ModuleNamespaceObject::ProxyHandler::getPrototypeIfOrdinary(JSContext* cx, HandleObject proxy,
355 : bool* isOrdinary,
356 : MutableHandleObject protop) const
357 : {
358 0 : *isOrdinary = false;
359 0 : return true;
360 : }
361 :
362 : bool
363 0 : ModuleNamespaceObject::ProxyHandler::setImmutablePrototype(JSContext* cx, HandleObject proxy,
364 : bool* succeeded) const
365 : {
366 0 : *succeeded = true;
367 0 : return true;
368 : }
369 :
370 : bool
371 0 : ModuleNamespaceObject::ProxyHandler::isExtensible(JSContext* cx, HandleObject proxy,
372 : bool* extensible) const
373 : {
374 0 : *extensible = false;
375 0 : return true;
376 : }
377 :
378 : bool
379 0 : ModuleNamespaceObject::ProxyHandler::preventExtensions(JSContext* cx, HandleObject proxy,
380 : ObjectOpResult& result) const
381 : {
382 0 : result.succeed();
383 0 : return true;
384 : }
385 :
386 : bool
387 0 : ModuleNamespaceObject::ProxyHandler::getOwnPropertyDescriptor(JSContext* cx, HandleObject proxy,
388 : HandleId id,
389 : MutableHandle<PropertyDescriptor> desc) const
390 : {
391 0 : Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
392 0 : if (JSID_IS_SYMBOL(id)) {
393 0 : if (JSID_TO_SYMBOL(id) == cx->wellKnownSymbols().toStringTag) {
394 0 : RootedValue value(cx, StringValue(cx->names().Module));
395 0 : desc.object().set(proxy);
396 0 : desc.setWritable(false);
397 0 : desc.setEnumerable(false);
398 0 : desc.setConfigurable(false);
399 0 : desc.setValue(value);
400 0 : return true;
401 : }
402 :
403 0 : return true;
404 : }
405 :
406 0 : const IndirectBindingMap& bindings = ns->bindings();
407 : ModuleEnvironmentObject* env;
408 : Shape* shape;
409 0 : if (!bindings.lookup(id, &env, &shape))
410 0 : return true;
411 :
412 0 : RootedValue value(cx, env->getSlot(shape->slot()));
413 0 : if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
414 0 : ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
415 0 : return false;
416 : }
417 :
418 0 : desc.object().set(env);
419 0 : desc.setConfigurable(false);
420 0 : desc.setEnumerable(true);
421 0 : desc.setValue(value);
422 0 : return true;
423 : }
424 :
425 : bool
426 0 : ModuleNamespaceObject::ProxyHandler::defineProperty(JSContext* cx, HandleObject proxy, HandleId id,
427 : Handle<PropertyDescriptor> desc,
428 : ObjectOpResult& result) const
429 : {
430 0 : return result.failReadOnly();
431 : }
432 :
433 : bool
434 0 : ModuleNamespaceObject::ProxyHandler::has(JSContext* cx, HandleObject proxy, HandleId id,
435 : bool* bp) const
436 : {
437 0 : Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
438 0 : if (JSID_IS_SYMBOL(id)) {
439 0 : *bp = JSID_TO_SYMBOL(id) == cx->wellKnownSymbols().toStringTag;
440 0 : return true;
441 : }
442 :
443 0 : *bp = ns->bindings().has(id);
444 0 : return true;
445 : }
446 :
447 : bool
448 0 : ModuleNamespaceObject::ProxyHandler::get(JSContext* cx, HandleObject proxy, HandleValue receiver,
449 : HandleId id, MutableHandleValue vp) const
450 : {
451 0 : Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
452 0 : if (JSID_IS_SYMBOL(id)) {
453 0 : if (JSID_TO_SYMBOL(id) == cx->wellKnownSymbols().toStringTag) {
454 0 : vp.setString(cx->names().Module);
455 0 : return true;
456 : }
457 :
458 0 : vp.setUndefined();
459 0 : return true;
460 : }
461 :
462 : ModuleEnvironmentObject* env;
463 : Shape* shape;
464 0 : if (!ns->bindings().lookup(id, &env, &shape)) {
465 0 : vp.setUndefined();
466 0 : return true;
467 : }
468 :
469 0 : RootedValue value(cx, env->getSlot(shape->slot()));
470 0 : if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
471 0 : ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
472 0 : return false;
473 : }
474 :
475 0 : vp.set(value);
476 0 : return true;
477 : }
478 :
479 : bool
480 0 : ModuleNamespaceObject::ProxyHandler::set(JSContext* cx, HandleObject proxy, HandleId id, HandleValue v,
481 : HandleValue receiver, ObjectOpResult& result) const
482 : {
483 0 : return result.failReadOnly();
484 : }
485 :
486 : bool
487 0 : ModuleNamespaceObject::ProxyHandler::delete_(JSContext* cx, HandleObject proxy, HandleId id,
488 : ObjectOpResult& result) const
489 : {
490 0 : Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
491 0 : if (JSID_IS_SYMBOL(id)) {
492 0 : if (JSID_TO_SYMBOL(id) == cx->wellKnownSymbols().toStringTag)
493 0 : return result.failCantDelete();
494 :
495 0 : return result.succeed();
496 : }
497 :
498 0 : if (ns->bindings().has(id))
499 0 : return result.failCantDelete();
500 :
501 0 : return result.succeed();
502 : }
503 :
504 : bool
505 0 : ModuleNamespaceObject::ProxyHandler::ownPropertyKeys(JSContext* cx, HandleObject proxy,
506 : AutoIdVector& props) const
507 : {
508 0 : Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
509 0 : RootedObject exports(cx, &ns->exports());
510 : uint32_t count;
511 0 : if (!GetLengthProperty(cx, exports, &count) || !props.reserve(props.length() + count + 1))
512 0 : return false;
513 :
514 0 : Rooted<ValueVector> names(cx, ValueVector(cx));
515 0 : if (!names.resize(count) || !GetElements(cx, exports, count, names.begin()))
516 0 : return false;
517 :
518 0 : for (uint32_t i = 0; i < count; i++)
519 0 : props.infallibleAppend(AtomToId(&names[i].toString()->asAtom()));
520 :
521 0 : props.infallibleAppend(SYMBOL_TO_JSID(cx->wellKnownSymbols().toStringTag));
522 :
523 0 : return true;
524 : }
525 :
526 : ///////////////////////////////////////////////////////////////////////////
527 : // FunctionDeclaration
528 :
529 0 : FunctionDeclaration::FunctionDeclaration(HandleAtom name, HandleFunction fun)
530 0 : : name(name), fun(fun)
531 0 : {}
532 :
533 0 : void FunctionDeclaration::trace(JSTracer* trc)
534 : {
535 0 : TraceEdge(trc, &name, "FunctionDeclaration name");
536 0 : TraceEdge(trc, &fun, "FunctionDeclaration fun");
537 0 : }
538 :
539 : ///////////////////////////////////////////////////////////////////////////
540 : // ModuleObject
541 :
542 : /* static */ const ClassOps
543 : ModuleObject::classOps_ = {
544 : nullptr, /* addProperty */
545 : nullptr, /* delProperty */
546 : nullptr, /* getProperty */
547 : nullptr, /* setProperty */
548 : nullptr, /* enumerate */
549 : nullptr, /* newEnumerate */
550 : nullptr, /* resolve */
551 : nullptr, /* mayResolve */
552 : ModuleObject::finalize,
553 : nullptr, /* call */
554 : nullptr, /* hasInstance */
555 : nullptr, /* construct */
556 : ModuleObject::trace
557 : };
558 :
559 : /* static */ const Class
560 : ModuleObject::class_ = {
561 : "Module",
562 : JSCLASS_HAS_RESERVED_SLOTS(ModuleObject::SlotCount) |
563 : JSCLASS_IS_ANONYMOUS |
564 : JSCLASS_BACKGROUND_FINALIZE,
565 : &ModuleObject::classOps_
566 : };
567 :
568 : #define DEFINE_ARRAY_SLOT_ACCESSOR(cls, name, slot) \
569 : ArrayObject& \
570 : cls::name() const \
571 : { \
572 : return getFixedSlot(cls::slot).toObject().as<ArrayObject>(); \
573 : }
574 :
575 0 : DEFINE_ARRAY_SLOT_ACCESSOR(ModuleObject, requestedModules, RequestedModulesSlot)
576 0 : DEFINE_ARRAY_SLOT_ACCESSOR(ModuleObject, importEntries, ImportEntriesSlot)
577 0 : DEFINE_ARRAY_SLOT_ACCESSOR(ModuleObject, localExportEntries, LocalExportEntriesSlot)
578 0 : DEFINE_ARRAY_SLOT_ACCESSOR(ModuleObject, indirectExportEntries, IndirectExportEntriesSlot)
579 0 : DEFINE_ARRAY_SLOT_ACCESSOR(ModuleObject, starExportEntries, StarExportEntriesSlot)
580 :
581 : /* static */ bool
582 0 : ModuleObject::isInstance(HandleValue value)
583 : {
584 0 : return value.isObject() && value.toObject().is<ModuleObject>();
585 : }
586 :
587 : /* static */ ModuleObject*
588 0 : ModuleObject::create(JSContext* cx)
589 : {
590 0 : RootedObject proto(cx, cx->global()->getModulePrototype());
591 0 : RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
592 0 : if (!obj)
593 0 : return nullptr;
594 :
595 0 : RootedModuleObject self(cx, &obj->as<ModuleObject>());
596 :
597 0 : Zone* zone = cx->zone();
598 0 : IndirectBindingMap* bindings = zone->new_<IndirectBindingMap>(zone);
599 0 : if (!bindings || !bindings->init()) {
600 0 : ReportOutOfMemory(cx);
601 0 : js_delete<IndirectBindingMap>(bindings);
602 0 : return nullptr;
603 : }
604 :
605 0 : self->initReservedSlot(ImportBindingsSlot, PrivateValue(bindings));
606 :
607 0 : FunctionDeclarationVector* funDecls = zone->new_<FunctionDeclarationVector>(zone);
608 0 : if (!funDecls) {
609 0 : ReportOutOfMemory(cx);
610 0 : return nullptr;
611 : }
612 :
613 0 : self->initReservedSlot(FunctionDeclarationsSlot, PrivateValue(funDecls));
614 0 : return self;
615 : }
616 :
617 : /* static */ void
618 0 : ModuleObject::finalize(js::FreeOp* fop, JSObject* obj)
619 : {
620 0 : MOZ_ASSERT(fop->maybeOnHelperThread());
621 0 : ModuleObject* self = &obj->as<ModuleObject>();
622 0 : if (self->hasImportBindings())
623 0 : fop->delete_(&self->importBindings());
624 0 : if (IndirectBindingMap* bindings = self->namespaceBindings())
625 0 : fop->delete_(bindings);
626 0 : if (FunctionDeclarationVector* funDecls = self->functionDeclarations())
627 0 : fop->delete_(funDecls);
628 0 : }
629 :
630 : ModuleEnvironmentObject*
631 0 : ModuleObject::environment() const
632 : {
633 0 : Value value = getReservedSlot(EnvironmentSlot);
634 0 : if (value.isUndefined())
635 0 : return nullptr;
636 :
637 0 : return &value.toObject().as<ModuleEnvironmentObject>();
638 : }
639 :
640 : bool
641 0 : ModuleObject::hasImportBindings() const
642 : {
643 : // Import bindings may not be present if we hit OOM in initialization.
644 0 : return !getReservedSlot(ImportBindingsSlot).isUndefined();
645 : }
646 :
647 : IndirectBindingMap&
648 0 : ModuleObject::importBindings()
649 : {
650 0 : return *static_cast<IndirectBindingMap*>(getReservedSlot(ImportBindingsSlot).toPrivate());
651 : }
652 :
653 : JSObject*
654 0 : ModuleObject::namespaceExports()
655 : {
656 0 : Value value = getReservedSlot(NamespaceExportsSlot);
657 0 : if (value.isUndefined())
658 0 : return nullptr;
659 :
660 0 : return &value.toObject();
661 : }
662 :
663 : IndirectBindingMap*
664 0 : ModuleObject::namespaceBindings()
665 : {
666 0 : Value value = getReservedSlot(NamespaceBindingsSlot);
667 0 : if (value.isUndefined())
668 0 : return nullptr;
669 :
670 0 : return static_cast<IndirectBindingMap*>(value.toPrivate());
671 : }
672 :
673 : ModuleNamespaceObject*
674 0 : ModuleObject::namespace_()
675 : {
676 0 : Value value = getReservedSlot(NamespaceSlot);
677 0 : if (value.isUndefined())
678 0 : return nullptr;
679 0 : return &value.toObject().as<ModuleNamespaceObject>();
680 : }
681 :
682 : FunctionDeclarationVector*
683 0 : ModuleObject::functionDeclarations()
684 : {
685 0 : Value value = getReservedSlot(FunctionDeclarationsSlot);
686 0 : if (value.isUndefined())
687 0 : return nullptr;
688 :
689 0 : return static_cast<FunctionDeclarationVector*>(value.toPrivate());
690 : }
691 :
692 : void
693 0 : ModuleObject::init(HandleScript script)
694 : {
695 0 : initReservedSlot(ScriptSlot, PrivateValue(script));
696 0 : initReservedSlot(StateSlot, Int32Value(MODULE_STATE_FAILED));
697 0 : }
698 :
699 : void
700 0 : ModuleObject::setInitialEnvironment(HandleModuleEnvironmentObject initialEnvironment)
701 : {
702 0 : initReservedSlot(InitialEnvironmentSlot, ObjectValue(*initialEnvironment));
703 0 : }
704 :
705 : void
706 0 : ModuleObject::initImportExportData(HandleArrayObject requestedModules,
707 : HandleArrayObject importEntries,
708 : HandleArrayObject localExportEntries,
709 : HandleArrayObject indirectExportEntries,
710 : HandleArrayObject starExportEntries)
711 : {
712 0 : initReservedSlot(RequestedModulesSlot, ObjectValue(*requestedModules));
713 0 : initReservedSlot(ImportEntriesSlot, ObjectValue(*importEntries));
714 0 : initReservedSlot(LocalExportEntriesSlot, ObjectValue(*localExportEntries));
715 0 : initReservedSlot(IndirectExportEntriesSlot, ObjectValue(*indirectExportEntries));
716 0 : initReservedSlot(StarExportEntriesSlot, ObjectValue(*starExportEntries));
717 0 : setReservedSlot(StateSlot, Int32Value(MODULE_STATE_PARSED));
718 0 : }
719 :
720 : static bool
721 0 : FreezeObjectProperty(JSContext* cx, HandleNativeObject obj, uint32_t slot)
722 : {
723 0 : RootedObject property(cx, &obj->getSlot(slot).toObject());
724 0 : return FreezeObject(cx, property);
725 : }
726 :
727 : /* static */ bool
728 0 : ModuleObject::Freeze(JSContext* cx, HandleModuleObject self)
729 : {
730 0 : return FreezeObjectProperty(cx, self, RequestedModulesSlot) &&
731 0 : FreezeObjectProperty(cx, self, ImportEntriesSlot) &&
732 0 : FreezeObjectProperty(cx, self, LocalExportEntriesSlot) &&
733 0 : FreezeObjectProperty(cx, self, IndirectExportEntriesSlot) &&
734 0 : FreezeObjectProperty(cx, self, StarExportEntriesSlot) &&
735 0 : FreezeObject(cx, self);
736 : }
737 :
738 : #ifdef DEBUG
739 :
740 : static inline bool
741 0 : IsObjectFrozen(JSContext* cx, HandleObject obj)
742 : {
743 0 : bool frozen = false;
744 0 : MOZ_ALWAYS_TRUE(TestIntegrityLevel(cx, obj, IntegrityLevel::Frozen, &frozen));
745 0 : return frozen;
746 : }
747 :
748 : static inline bool
749 0 : IsObjectPropertyFrozen(JSContext* cx, HandleNativeObject obj, uint32_t slot)
750 : {
751 0 : RootedObject property(cx, &obj->getSlot(slot).toObject());
752 0 : return IsObjectFrozen(cx, property);
753 : }
754 :
755 : /* static */ inline bool
756 0 : ModuleObject::IsFrozen(JSContext* cx, HandleModuleObject self)
757 : {
758 0 : return IsObjectPropertyFrozen(cx, self, RequestedModulesSlot) &&
759 0 : IsObjectPropertyFrozen(cx, self, ImportEntriesSlot) &&
760 0 : IsObjectPropertyFrozen(cx, self, LocalExportEntriesSlot) &&
761 0 : IsObjectPropertyFrozen(cx, self, IndirectExportEntriesSlot) &&
762 0 : IsObjectPropertyFrozen(cx, self, StarExportEntriesSlot) &&
763 0 : IsObjectFrozen(cx, self);
764 : }
765 :
766 : #endif
767 :
768 : inline static void
769 0 : AssertModuleScopesMatch(ModuleObject* module)
770 : {
771 0 : MOZ_ASSERT(module->enclosingScope()->is<GlobalScope>());
772 0 : MOZ_ASSERT(IsGlobalLexicalEnvironment(&module->initialEnvironment().enclosingEnvironment()));
773 0 : }
774 :
775 : void
776 0 : ModuleObject::fixEnvironmentsAfterCompartmentMerge()
777 : {
778 0 : AssertModuleScopesMatch(this);
779 0 : initialEnvironment().fixEnclosingEnvironmentAfterCompartmentMerge(script()->global());
780 0 : AssertModuleScopesMatch(this);
781 0 : }
782 :
783 : bool
784 0 : ModuleObject::hasScript() const
785 : {
786 : // When modules are parsed via the Reflect.parse() API, the module object
787 : // doesn't have a script.
788 0 : return !getReservedSlot(ScriptSlot).isUndefined();
789 : }
790 :
791 : JSScript*
792 0 : ModuleObject::script() const
793 : {
794 0 : return static_cast<JSScript*>(getReservedSlot(ScriptSlot).toPrivate());
795 : }
796 :
797 : static inline void
798 0 : AssertValidModuleState(ModuleState state)
799 : {
800 0 : MOZ_ASSERT(state >= MODULE_STATE_FAILED && state <= MODULE_STATE_EVALUATED);
801 0 : }
802 :
803 : ModuleState
804 0 : ModuleObject::state() const
805 : {
806 0 : ModuleState state = getReservedSlot(StateSlot).toInt32();
807 0 : AssertValidModuleState(state);
808 0 : return state;
809 : }
810 :
811 : Value
812 0 : ModuleObject::hostDefinedField() const
813 : {
814 0 : return getReservedSlot(HostDefinedSlot);
815 : }
816 :
817 : void
818 0 : ModuleObject::setHostDefinedField(const JS::Value& value)
819 : {
820 0 : setReservedSlot(HostDefinedSlot, value);
821 0 : }
822 :
823 : ModuleEnvironmentObject&
824 0 : ModuleObject::initialEnvironment() const
825 : {
826 0 : return getReservedSlot(InitialEnvironmentSlot).toObject().as<ModuleEnvironmentObject>();
827 : }
828 :
829 : Scope*
830 0 : ModuleObject::enclosingScope() const
831 : {
832 0 : return script()->enclosingScope();
833 : }
834 :
835 : /* static */ void
836 0 : ModuleObject::trace(JSTracer* trc, JSObject* obj)
837 : {
838 0 : ModuleObject& module = obj->as<ModuleObject>();
839 0 : if (module.hasScript()) {
840 0 : JSScript* script = module.script();
841 0 : TraceManuallyBarrieredEdge(trc, &script, "Module script");
842 0 : module.setReservedSlot(ScriptSlot, PrivateValue(script));
843 : }
844 :
845 0 : if (module.hasImportBindings())
846 0 : module.importBindings().trace(trc);
847 0 : if (IndirectBindingMap* bindings = module.namespaceBindings())
848 0 : bindings->trace(trc);
849 :
850 0 : if (FunctionDeclarationVector* funDecls = module.functionDeclarations())
851 0 : funDecls->trace(trc);
852 0 : }
853 :
854 : void
855 0 : ModuleObject::createEnvironment()
856 : {
857 : // The environment has already been created, we just neet to set it in the
858 : // right slot.
859 0 : MOZ_ASSERT(!getReservedSlot(InitialEnvironmentSlot).isUndefined());
860 0 : MOZ_ASSERT(getReservedSlot(EnvironmentSlot).isUndefined());
861 0 : setReservedSlot(EnvironmentSlot, getReservedSlot(InitialEnvironmentSlot));
862 0 : }
863 :
864 : bool
865 0 : ModuleObject::noteFunctionDeclaration(JSContext* cx, HandleAtom name, HandleFunction fun)
866 : {
867 0 : FunctionDeclarationVector* funDecls = functionDeclarations();
868 0 : if (!funDecls->emplaceBack(name, fun)) {
869 0 : ReportOutOfMemory(cx);
870 0 : return false;
871 : }
872 :
873 0 : return true;
874 : }
875 :
876 : /* static */ bool
877 0 : ModuleObject::instantiateFunctionDeclarations(JSContext* cx, HandleModuleObject self)
878 : {
879 0 : MOZ_ASSERT(IsFrozen(cx, self));
880 :
881 0 : FunctionDeclarationVector* funDecls = self->functionDeclarations();
882 0 : if (!funDecls) {
883 0 : JS_ReportErrorASCII(cx, "Module function declarations have already been instantiated");
884 0 : return false;
885 : }
886 :
887 0 : RootedModuleEnvironmentObject env(cx, &self->initialEnvironment());
888 0 : RootedFunction fun(cx);
889 0 : RootedValue value(cx);
890 :
891 0 : for (const auto& funDecl : *funDecls) {
892 0 : fun = funDecl.fun;
893 0 : RootedObject obj(cx, Lambda(cx, fun, env));
894 0 : if (!obj)
895 0 : return false;
896 :
897 0 : value = ObjectValue(*fun);
898 0 : if (!SetProperty(cx, env, funDecl.name->asPropertyName(), value))
899 0 : return false;
900 : }
901 :
902 0 : js_delete(funDecls);
903 0 : self->setReservedSlot(FunctionDeclarationsSlot, UndefinedValue());
904 0 : return true;
905 : }
906 :
907 : void
908 0 : ModuleObject::setState(int32_t newState)
909 : {
910 0 : AssertValidModuleState(newState);
911 0 : MOZ_ASSERT(state() != MODULE_STATE_FAILED);
912 0 : MOZ_ASSERT(newState == MODULE_STATE_FAILED || newState > state());
913 0 : setReservedSlot(StateSlot, Int32Value(newState));
914 0 : }
915 :
916 : /* static */ bool
917 0 : ModuleObject::evaluate(JSContext* cx, HandleModuleObject self, MutableHandleValue rval)
918 : {
919 0 : MOZ_ASSERT(IsFrozen(cx, self));
920 :
921 0 : RootedScript script(cx, self->script());
922 0 : RootedModuleEnvironmentObject scope(cx, self->environment());
923 0 : if (!scope) {
924 0 : JS_ReportErrorASCII(cx, "Module declarations have not yet been instantiated");
925 0 : return false;
926 : }
927 :
928 0 : return Execute(cx, script, *scope, rval.address());
929 : }
930 :
931 : /* static */ ModuleNamespaceObject*
932 0 : ModuleObject::createNamespace(JSContext* cx, HandleModuleObject self, HandleObject exports)
933 : {
934 0 : MOZ_ASSERT(!self->namespace_());
935 0 : MOZ_ASSERT(exports->is<ArrayObject>() || exports->is<UnboxedArrayObject>());
936 :
937 0 : RootedModuleNamespaceObject ns(cx, ModuleNamespaceObject::create(cx, self));
938 0 : if (!ns)
939 0 : return nullptr;
940 :
941 0 : Zone* zone = cx->zone();
942 0 : IndirectBindingMap* bindings = zone->new_<IndirectBindingMap>(zone);
943 0 : if (!bindings || !bindings->init()) {
944 0 : ReportOutOfMemory(cx);
945 0 : js_delete<IndirectBindingMap>(bindings);
946 0 : return nullptr;
947 : }
948 :
949 0 : self->initReservedSlot(NamespaceSlot, ObjectValue(*ns));
950 0 : self->initReservedSlot(NamespaceExportsSlot, ObjectValue(*exports));
951 0 : self->initReservedSlot(NamespaceBindingsSlot, PrivateValue(bindings));
952 0 : return ns;
953 : }
954 :
955 : static bool
956 0 : InvokeSelfHostedMethod(JSContext* cx, HandleModuleObject self, HandlePropertyName name)
957 : {
958 0 : RootedValue fval(cx);
959 0 : if (!GlobalObject::getSelfHostedFunction(cx, cx->global(), name, name, 0, &fval))
960 0 : return false;
961 :
962 0 : RootedValue ignored(cx);
963 0 : return Call(cx, fval, self, &ignored);
964 : }
965 :
966 : /* static */ bool
967 0 : ModuleObject::DeclarationInstantiation(JSContext* cx, HandleModuleObject self)
968 : {
969 0 : return InvokeSelfHostedMethod(cx, self, cx->names().ModuleDeclarationInstantiation);
970 : }
971 :
972 : /* static */ bool
973 0 : ModuleObject::Evaluation(JSContext* cx, HandleModuleObject self)
974 : {
975 0 : return InvokeSelfHostedMethod(cx, self, cx->names().ModuleEvaluation);
976 : }
977 :
978 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, namespace_, NamespaceSlot)
979 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, state, StateSlot)
980 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, requestedModules, RequestedModulesSlot)
981 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, importEntries, ImportEntriesSlot)
982 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, localExportEntries, LocalExportEntriesSlot)
983 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, indirectExportEntries, IndirectExportEntriesSlot)
984 0 : DEFINE_GETTER_FUNCTIONS(ModuleObject, starExportEntries, StarExportEntriesSlot)
985 :
986 : /* static */ bool
987 0 : GlobalObject::initModuleProto(JSContext* cx, Handle<GlobalObject*> global)
988 : {
989 : static const JSPropertySpec protoAccessors[] = {
990 : JS_PSG("namespace", ModuleObject_namespace_Getter, 0),
991 : JS_PSG("state", ModuleObject_stateGetter, 0),
992 : JS_PSG("requestedModules", ModuleObject_requestedModulesGetter, 0),
993 : JS_PSG("importEntries", ModuleObject_importEntriesGetter, 0),
994 : JS_PSG("localExportEntries", ModuleObject_localExportEntriesGetter, 0),
995 : JS_PSG("indirectExportEntries", ModuleObject_indirectExportEntriesGetter, 0),
996 : JS_PSG("starExportEntries", ModuleObject_starExportEntriesGetter, 0),
997 : JS_PS_END
998 : };
999 :
1000 : static const JSFunctionSpec protoFunctions[] = {
1001 : JS_SELF_HOSTED_FN("getExportedNames", "ModuleGetExportedNames", 1, 0),
1002 : JS_SELF_HOSTED_FN("resolveExport", "ModuleResolveExport", 2, 0),
1003 : JS_SELF_HOSTED_FN("declarationInstantiation", "ModuleDeclarationInstantiation", 0, 0),
1004 : JS_SELF_HOSTED_FN("evaluation", "ModuleEvaluation", 0, 0),
1005 : JS_FS_END
1006 : };
1007 :
1008 0 : RootedObject proto(cx, GlobalObject::createBlankPrototype<PlainObject>(cx, global));
1009 0 : if (!proto)
1010 0 : return false;
1011 :
1012 0 : if (!DefinePropertiesAndFunctions(cx, proto, protoAccessors, protoFunctions))
1013 0 : return false;
1014 :
1015 0 : global->setReservedSlot(MODULE_PROTO, ObjectValue(*proto));
1016 0 : return true;
1017 : }
1018 :
1019 : #undef DEFINE_GETTER_FUNCTIONS
1020 : #undef DEFINE_STRING_ACCESSOR_METHOD
1021 : #undef DEFINE_ARRAY_SLOT_ACCESSOR
1022 :
1023 : ///////////////////////////////////////////////////////////////////////////
1024 : // ModuleBuilder
1025 :
1026 0 : ModuleBuilder::ModuleBuilder(JSContext* cx, HandleModuleObject module)
1027 : : cx_(cx),
1028 : module_(cx, module),
1029 0 : requestedModules_(cx, AtomVector(cx)),
1030 0 : importedBoundNames_(cx, AtomVector(cx)),
1031 0 : importEntries_(cx, ImportEntryVector(cx)),
1032 0 : exportEntries_(cx, ExportEntryVector(cx)),
1033 0 : localExportEntries_(cx, ExportEntryVector(cx)),
1034 0 : indirectExportEntries_(cx, ExportEntryVector(cx)),
1035 0 : starExportEntries_(cx, ExportEntryVector(cx))
1036 0 : {}
1037 :
1038 : bool
1039 0 : ModuleBuilder::buildTables()
1040 : {
1041 0 : for (const auto& e : exportEntries_) {
1042 0 : RootedExportEntryObject exp(cx_, e);
1043 0 : if (!exp->moduleRequest()) {
1044 0 : RootedImportEntryObject importEntry(cx_, importEntryFor(exp->localName()));
1045 0 : if (!importEntry) {
1046 0 : if (!localExportEntries_.append(exp))
1047 0 : return false;
1048 : } else {
1049 0 : if (importEntry->importName() == cx_->names().star) {
1050 0 : if (!localExportEntries_.append(exp))
1051 0 : return false;
1052 : } else {
1053 0 : RootedAtom exportName(cx_, exp->exportName());
1054 0 : RootedAtom moduleRequest(cx_, importEntry->moduleRequest());
1055 0 : RootedAtom importName(cx_, importEntry->importName());
1056 0 : RootedExportEntryObject exportEntry(cx_);
1057 0 : exportEntry = ExportEntryObject::create(cx_,
1058 : exportName,
1059 : moduleRequest,
1060 : importName,
1061 0 : nullptr);
1062 0 : if (!exportEntry || !indirectExportEntries_.append(exportEntry))
1063 0 : return false;
1064 : }
1065 : }
1066 0 : } else if (exp->importName() == cx_->names().star) {
1067 0 : if (!starExportEntries_.append(exp))
1068 0 : return false;
1069 : } else {
1070 0 : if (!indirectExportEntries_.append(exp))
1071 0 : return false;
1072 : }
1073 : }
1074 :
1075 0 : return true;
1076 : }
1077 :
1078 : bool
1079 0 : ModuleBuilder::initModule()
1080 : {
1081 0 : RootedArrayObject requestedModules(cx_, createArray<JSAtom*>(requestedModules_));
1082 0 : if (!requestedModules)
1083 0 : return false;
1084 :
1085 0 : RootedArrayObject importEntries(cx_, createArray<ImportEntryObject*>(importEntries_));
1086 0 : if (!importEntries)
1087 0 : return false;
1088 :
1089 0 : RootedArrayObject localExportEntries(cx_, createArray<ExportEntryObject*>(localExportEntries_));
1090 0 : if (!localExportEntries)
1091 0 : return false;
1092 :
1093 0 : RootedArrayObject indirectExportEntries(cx_);
1094 0 : indirectExportEntries = createArray<ExportEntryObject*>(indirectExportEntries_);
1095 0 : if (!indirectExportEntries)
1096 0 : return false;
1097 :
1098 0 : RootedArrayObject starExportEntries(cx_, createArray<ExportEntryObject*>(starExportEntries_));
1099 0 : if (!starExportEntries)
1100 0 : return false;
1101 :
1102 0 : module_->initImportExportData(requestedModules,
1103 : importEntries,
1104 : localExportEntries,
1105 : indirectExportEntries,
1106 0 : starExportEntries);
1107 :
1108 0 : return true;
1109 : }
1110 :
1111 : bool
1112 0 : ModuleBuilder::processImport(frontend::ParseNode* pn)
1113 : {
1114 0 : MOZ_ASSERT(pn->isKind(PNK_IMPORT));
1115 0 : MOZ_ASSERT(pn->isArity(PN_BINARY));
1116 0 : MOZ_ASSERT(pn->pn_left->isKind(PNK_IMPORT_SPEC_LIST));
1117 0 : MOZ_ASSERT(pn->pn_right->isKind(PNK_STRING));
1118 :
1119 0 : RootedAtom module(cx_, pn->pn_right->pn_atom);
1120 0 : if (!maybeAppendRequestedModule(module))
1121 0 : return false;
1122 :
1123 0 : for (ParseNode* spec = pn->pn_left->pn_head; spec; spec = spec->pn_next) {
1124 0 : MOZ_ASSERT(spec->isKind(PNK_IMPORT_SPEC));
1125 0 : MOZ_ASSERT(spec->pn_left->isArity(PN_NAME));
1126 0 : MOZ_ASSERT(spec->pn_right->isArity(PN_NAME));
1127 :
1128 0 : RootedAtom importName(cx_, spec->pn_left->pn_atom);
1129 0 : RootedAtom localName(cx_, spec->pn_right->pn_atom);
1130 :
1131 0 : if (!importedBoundNames_.append(localName))
1132 0 : return false;
1133 :
1134 0 : RootedImportEntryObject importEntry(cx_);
1135 0 : importEntry = ImportEntryObject::create(cx_, module, importName, localName);
1136 0 : if (!importEntry || !importEntries_.append(importEntry))
1137 0 : return false;
1138 : }
1139 :
1140 0 : return true;
1141 : }
1142 :
1143 : bool
1144 0 : ModuleBuilder::processExport(frontend::ParseNode* pn)
1145 : {
1146 0 : MOZ_ASSERT(pn->isKind(PNK_EXPORT) || pn->isKind(PNK_EXPORT_DEFAULT));
1147 0 : MOZ_ASSERT(pn->getArity() == (pn->isKind(PNK_EXPORT) ? PN_UNARY : PN_BINARY));
1148 :
1149 0 : bool isDefault = pn->getKind() == PNK_EXPORT_DEFAULT;
1150 0 : ParseNode* kid = isDefault ? pn->pn_left : pn->pn_kid;
1151 :
1152 0 : if (isDefault && pn->pn_right) {
1153 : // This is an export default containing an expression.
1154 0 : RootedAtom localName(cx_, cx_->names().starDefaultStar);
1155 0 : RootedAtom exportName(cx_, cx_->names().default_);
1156 0 : return appendExportEntry(exportName, localName);
1157 : }
1158 :
1159 0 : switch (kid->getKind()) {
1160 : case PNK_EXPORT_SPEC_LIST:
1161 0 : MOZ_ASSERT(!isDefault);
1162 0 : for (ParseNode* spec = kid->pn_head; spec; spec = spec->pn_next) {
1163 0 : MOZ_ASSERT(spec->isKind(PNK_EXPORT_SPEC));
1164 0 : RootedAtom localName(cx_, spec->pn_left->pn_atom);
1165 0 : RootedAtom exportName(cx_, spec->pn_right->pn_atom);
1166 0 : if (!appendExportEntry(exportName, localName))
1167 0 : return false;
1168 : }
1169 0 : break;
1170 :
1171 : case PNK_CLASS: {
1172 0 : const ClassNode& cls = kid->as<ClassNode>();
1173 0 : MOZ_ASSERT(cls.names());
1174 0 : RootedAtom localName(cx_, cls.names()->innerBinding()->pn_atom);
1175 0 : RootedAtom exportName(cx_, isDefault ? cx_->names().default_ : localName.get());
1176 0 : if (!appendExportEntry(exportName, localName))
1177 0 : return false;
1178 0 : break;
1179 : }
1180 :
1181 : case PNK_VAR:
1182 : case PNK_CONST:
1183 : case PNK_LET: {
1184 0 : MOZ_ASSERT(kid->isArity(PN_LIST));
1185 0 : for (ParseNode* var = kid->pn_head; var; var = var->pn_next) {
1186 0 : if (var->isKind(PNK_ASSIGN))
1187 0 : var = var->pn_left;
1188 0 : MOZ_ASSERT(var->isKind(PNK_NAME));
1189 0 : RootedAtom localName(cx_, var->pn_atom);
1190 0 : RootedAtom exportName(cx_, isDefault ? cx_->names().default_ : localName.get());
1191 0 : if (!appendExportEntry(exportName, localName))
1192 0 : return false;
1193 : }
1194 0 : break;
1195 : }
1196 :
1197 : case PNK_FUNCTION: {
1198 0 : RootedFunction func(cx_, kid->pn_funbox->function());
1199 0 : MOZ_ASSERT(!func->isArrow());
1200 0 : RootedAtom localName(cx_, func->explicitName());
1201 0 : RootedAtom exportName(cx_, isDefault ? cx_->names().default_ : localName.get());
1202 0 : MOZ_ASSERT_IF(isDefault, localName);
1203 0 : if (!appendExportEntry(exportName, localName))
1204 0 : return false;
1205 0 : break;
1206 : }
1207 :
1208 : default:
1209 0 : MOZ_CRASH("Unexpected parse node");
1210 : }
1211 :
1212 0 : return true;
1213 : }
1214 :
1215 : bool
1216 0 : ModuleBuilder::processExportFrom(frontend::ParseNode* pn)
1217 : {
1218 0 : MOZ_ASSERT(pn->isKind(PNK_EXPORT_FROM));
1219 0 : MOZ_ASSERT(pn->isArity(PN_BINARY));
1220 0 : MOZ_ASSERT(pn->pn_left->isKind(PNK_EXPORT_SPEC_LIST));
1221 0 : MOZ_ASSERT(pn->pn_right->isKind(PNK_STRING));
1222 :
1223 0 : RootedAtom module(cx_, pn->pn_right->pn_atom);
1224 0 : if (!maybeAppendRequestedModule(module))
1225 0 : return false;
1226 :
1227 0 : for (ParseNode* spec = pn->pn_left->pn_head; spec; spec = spec->pn_next) {
1228 0 : if (spec->isKind(PNK_EXPORT_SPEC)) {
1229 0 : RootedAtom bindingName(cx_, spec->pn_left->pn_atom);
1230 0 : RootedAtom exportName(cx_, spec->pn_right->pn_atom);
1231 0 : if (!appendExportFromEntry(exportName, module, bindingName))
1232 0 : return false;
1233 : } else {
1234 0 : MOZ_ASSERT(spec->isKind(PNK_EXPORT_BATCH_SPEC));
1235 0 : RootedAtom importName(cx_, cx_->names().star);
1236 0 : if (!appendExportFromEntry(nullptr, module, importName))
1237 0 : return false;
1238 : }
1239 : }
1240 :
1241 0 : return true;
1242 : }
1243 :
1244 : ImportEntryObject*
1245 0 : ModuleBuilder::importEntryFor(JSAtom* localName) const
1246 : {
1247 0 : for (auto import : importEntries_) {
1248 0 : if (import->localName() == localName)
1249 0 : return import;
1250 : }
1251 0 : return nullptr;
1252 : }
1253 :
1254 : bool
1255 0 : ModuleBuilder::hasExportedName(JSAtom* name) const
1256 : {
1257 0 : for (auto entry : exportEntries_) {
1258 0 : if (entry->exportName() == name)
1259 0 : return true;
1260 : }
1261 0 : return false;
1262 : }
1263 :
1264 : bool
1265 0 : ModuleBuilder::appendExportEntry(HandleAtom exportName, HandleAtom localName)
1266 : {
1267 0 : Rooted<ExportEntryObject*> exportEntry(cx_);
1268 0 : exportEntry = ExportEntryObject::create(cx_, exportName, nullptr, nullptr, localName);
1269 0 : return exportEntry && exportEntries_.append(exportEntry);
1270 : }
1271 :
1272 : bool
1273 0 : ModuleBuilder::appendExportFromEntry(HandleAtom exportName, HandleAtom moduleRequest,
1274 : HandleAtom importName)
1275 : {
1276 0 : Rooted<ExportEntryObject*> exportEntry(cx_);
1277 0 : exportEntry = ExportEntryObject::create(cx_, exportName, moduleRequest, importName, nullptr);
1278 0 : return exportEntry && exportEntries_.append(exportEntry);
1279 : }
1280 :
1281 : bool
1282 0 : ModuleBuilder::maybeAppendRequestedModule(HandleAtom module)
1283 : {
1284 0 : for (auto m : requestedModules_) {
1285 0 : if (m == module)
1286 0 : return true;
1287 : }
1288 0 : return requestedModules_.append(module);
1289 : }
1290 :
1291 : static Value
1292 0 : MakeElementValue(JSString *string)
1293 : {
1294 0 : return StringValue(string);
1295 : }
1296 :
1297 : static Value
1298 0 : MakeElementValue(JSObject *object)
1299 : {
1300 0 : return ObjectValue(*object);
1301 : }
1302 :
1303 : template <typename T>
1304 0 : ArrayObject* ModuleBuilder::createArray(const GCVector<T>& vector)
1305 : {
1306 0 : uint32_t length = vector.length();
1307 0 : RootedArrayObject array(cx_, NewDenseFullyAllocatedArray(cx_, length));
1308 0 : if (!array)
1309 0 : return nullptr;
1310 :
1311 0 : array->setDenseInitializedLength(length);
1312 0 : for (uint32_t i = 0; i < length; i++)
1313 0 : array->initDenseElement(i, MakeElementValue(vector[i]));
1314 :
1315 0 : return array;
1316 9 : }
|