Source code

Revision control

Copy as Markdown

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "builtin/ModuleObject.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/EnumSet.h"
#include "mozilla/ScopeExit.h"
#include "builtin/Promise.h"
#include "builtin/SelfHostingDefines.h"
#include "frontend/ParseNode.h"
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex, ParserAtomsTable, ParserAtom
#include "frontend/SharedContext.h"
#include "frontend/Stencil.h"
#include "gc/GCContext.h"
#include "gc/Tracer.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin, JS::LimitedColumnNumberOneOrigin
#include "js/friend/ErrorMessages.h" // JSMSG_*
#include "js/Modules.h" // JS::GetModulePrivate, JS::ModuleDynamicImportHook, JS::ModuleType
#include "vm/EqualityOperations.h" // js::SameValue
#include "vm/Interpreter.h" // Execute, Lambda, ReportRuntimeLexicalError
#include "vm/ModuleBuilder.h" // js::ModuleBuilder
#include "vm/Modules.h"
#include "vm/PlainObject.h" // js::PlainObject
#include "vm/PromiseObject.h" // js::PromiseObject
#include "vm/SharedStencil.h" // js::GCThingIndex
#include "builtin/HandlerFunction-inl.h" // js::ExtraValueFromHandler, js::NewHandler{,WithExtraValue}, js::TargetFromHandler
#include "gc/GCContext-inl.h"
#include "vm/EnvironmentObject-inl.h" // EnvironmentObject::setAliasedBinding
#include "vm/JSObject-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/List-inl.h"
#include "vm/NativeObject-inl.h"
using namespace js;
using mozilla::Maybe;
using mozilla::Nothing;
using mozilla::Some;
using mozilla::Span;
static_assert(ModuleStatus::Unlinked < ModuleStatus::Linking &&
ModuleStatus::Linking < ModuleStatus::Linked &&
ModuleStatus::Linked < ModuleStatus::Evaluating &&
ModuleStatus::Evaluating < ModuleStatus::EvaluatingAsync &&
ModuleStatus::EvaluatingAsync < ModuleStatus::Evaluated &&
ModuleStatus::Evaluated < ModuleStatus::Evaluated_Error,
"Module statuses are ordered incorrectly");
static Value StringOrNullValue(JSString* maybeString) {
return maybeString ? StringValue(maybeString) : NullValue();
}
#define DEFINE_ATOM_ACCESSOR_METHOD(cls, name, slot) \
JSAtom* cls::name() const { \
Value value = getReservedSlot(slot); \
return &value.toString()->asAtom(); \
}
#define DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(cls, name, slot) \
JSAtom* cls::name() const { \
Value value = getReservedSlot(slot); \
if (value.isNull()) { \
return nullptr; \
} \
return &value.toString()->asAtom(); \
}
#define DEFINE_UINT32_ACCESSOR_METHOD(cls, name, slot) \
uint32_t cls::name() const { \
Value value = getReservedSlot(slot); \
MOZ_ASSERT(value.toNumber() >= 0); \
if (value.isInt32()) { \
return value.toInt32(); \
} \
return JS::ToUint32(value.toDouble()); \
}
///////////////////////////////////////////////////////////////////////////
// ImportEntry
ImportEntry::ImportEntry(Handle<ModuleRequestObject*> moduleRequest,
Handle<JSAtom*> maybeImportName,
Handle<JSAtom*> localName, uint32_t lineNumber,
JS::ColumnNumberOneOrigin columnNumber)
: moduleRequest_(moduleRequest),
importName_(maybeImportName),
localName_(localName),
lineNumber_(lineNumber),
columnNumber_(columnNumber) {}
void ImportEntry::trace(JSTracer* trc) {
TraceEdge(trc, &moduleRequest_, "ImportEntry::moduleRequest_");
TraceNullableEdge(trc, &importName_, "ImportEntry::importName_");
TraceNullableEdge(trc, &localName_, "ImportEntry::localName_");
}
///////////////////////////////////////////////////////////////////////////
// ExportEntry
ExportEntry::ExportEntry(Handle<JSAtom*> maybeExportName,
Handle<ModuleRequestObject*> moduleRequest,
Handle<JSAtom*> maybeImportName,
Handle<JSAtom*> maybeLocalName, uint32_t lineNumber,
JS::ColumnNumberOneOrigin columnNumber)
: exportName_(maybeExportName),
moduleRequest_(moduleRequest),
importName_(maybeImportName),
localName_(maybeLocalName),
lineNumber_(lineNumber),
columnNumber_(columnNumber) {
// Line and column numbers are optional for export entries since direct
// entries are checked at parse time.
}
void ExportEntry::trace(JSTracer* trc) {
TraceNullableEdge(trc, &exportName_, "ExportEntry::exportName_");
TraceNullableEdge(trc, &moduleRequest_, "ExportEntry::moduleRequest_");
TraceNullableEdge(trc, &importName_, "ExportEntry::importName_");
TraceNullableEdge(trc, &localName_, "ExportEntry::localName_");
}
///////////////////////////////////////////////////////////////////////////
// RequestedModule
/* static */
RequestedModule::RequestedModule(Handle<ModuleRequestObject*> moduleRequest,
uint32_t lineNumber,
JS::ColumnNumberOneOrigin columnNumber)
: moduleRequest_(moduleRequest),
lineNumber_(lineNumber),
columnNumber_(columnNumber) {}
void RequestedModule::trace(JSTracer* trc) {
TraceEdge(trc, &moduleRequest_, "ExportEntry::moduleRequest_");
}
///////////////////////////////////////////////////////////////////////////
// ResolvedBindingObject
/* static */ const JSClass ResolvedBindingObject::class_ = {
"ResolvedBinding",
JSCLASS_HAS_RESERVED_SLOTS(ResolvedBindingObject::SlotCount)};
ModuleObject* ResolvedBindingObject::module() const {
Value value = getReservedSlot(ModuleSlot);
return &value.toObject().as<ModuleObject>();
}
JSAtom* ResolvedBindingObject::bindingName() const {
Value value = getReservedSlot(BindingNameSlot);
return &value.toString()->asAtom();
}
/* static */
bool ResolvedBindingObject::isInstance(HandleValue value) {
return value.isObject() && value.toObject().is<ResolvedBindingObject>();
}
/* static */
ResolvedBindingObject* ResolvedBindingObject::create(
JSContext* cx, Handle<ModuleObject*> module, Handle<JSAtom*> bindingName) {
ResolvedBindingObject* self =
NewObjectWithGivenProto<ResolvedBindingObject>(cx, nullptr);
if (!self) {
return nullptr;
}
self->initReservedSlot(ModuleSlot, ObjectValue(*module));
self->initReservedSlot(BindingNameSlot, StringValue(bindingName));
return self;
}
///////////////////////////////////////////////////////////////////////////
// ImportAttribute
ImportAttribute::ImportAttribute(Handle<JSAtom*> key, Handle<JSString*> value)
: key_(key), value_(value) {}
void ImportAttribute::trace(JSTracer* trc) {
TraceNullableEdge(trc, &key_, "ImportAttribute::key_");
TraceNullableEdge(trc, &value_, "ImportAttribute::value_");
}
///////////////////////////////////////////////////////////////////////////
// ModuleRequestObject
/* static */ const JSClass ModuleRequestObject::class_ = {
"ModuleRequest",
JSCLASS_HAS_RESERVED_SLOTS(ModuleRequestObject::SlotCount)};
DEFINE_ATOM_OR_NULL_ACCESSOR_METHOD(ModuleRequestObject, specifier,
SpecifierSlot)
Span<const ImportAttribute> ModuleRequestObject::attributes() const {
Value value = getReservedSlot(AttributesSlot);
if (value.isNullOrUndefined()) {
return Span<const ImportAttribute>();
}
void* ptr = value.toPrivate();
MOZ_ASSERT(ptr);
auto* vector = static_cast<ImportAttributeVector*>(ptr);
return *vector;
}
bool ModuleRequestObject::hasAttributes() const {
return !getReservedSlot(ModuleRequestObject::AttributesSlot)
.isNullOrUndefined();
}
/* static */
bool ModuleRequestObject::getModuleType(
JSContext* cx, const Handle<ModuleRequestObject*> moduleRequest,
JS::ModuleType& moduleType) {
if (!moduleRequest->hasAttributes()) {
moduleType = JS::ModuleType::JavaScript;
return true;
}
for (const ImportAttribute& importAttribute : moduleRequest->attributes()) {
if (importAttribute.key() == cx->names().type) {
int32_t isJsonString;
if (!js::CompareStrings(cx, cx->names().json, importAttribute.value(),
&isJsonString)) {
return false;
}
if (isJsonString == 0) {
moduleType = JS::ModuleType::JSON;
return true;
}
moduleType = JS::ModuleType::Unknown;
return true;
}
}
moduleType = JS::ModuleType::JavaScript;
return true;
}
/* static */
bool ModuleRequestObject::isInstance(HandleValue value) {
return value.isObject() && value.toObject().is<ModuleRequestObject>();
}
/* static */
ModuleRequestObject* ModuleRequestObject::create(
JSContext* cx, Handle<JSAtom*> specifier,
MutableHandle<UniquePtr<ImportAttributeVector>> maybeAttributes) {
ModuleRequestObject* self =
NewObjectWithGivenProto<ModuleRequestObject>(cx, nullptr);
if (!self) {
return nullptr;
}
self->initReservedSlot(SpecifierSlot, StringOrNullValue(specifier));
if (maybeAttributes) {
InitReservedSlot(self, AttributesSlot, maybeAttributes.get().release(),
MemoryUse::ModuleImportAttributes);
}
return self;
}
///////////////////////////////////////////////////////////////////////////
// IndirectBindingMap
IndirectBindingMap::Binding::Binding(ModuleEnvironmentObject* environment,
jsid targetName, PropertyInfo prop)
: environment(environment),
#ifdef DEBUG
targetName(targetName),
#endif
prop(prop) {
}
void IndirectBindingMap::trace(JSTracer* trc) {
if (!map_) {
return;
}
for (Map::Enum e(*map_); !e.empty(); e.popFront()) {
Binding& b = e.front().value();
TraceEdge(trc, &b.environment, "module bindings environment");
#ifdef DEBUG
TraceEdge(trc, &b.targetName, "module bindings target name");
#endif
mozilla::DebugOnly<jsid> prev(e.front().key());
TraceEdge(trc, &e.front().mutableKey(), "module bindings binding name");
MOZ_ASSERT(e.front().key() == prev);
}
}
bool IndirectBindingMap::put(JSContext* cx, HandleId name,
Handle<ModuleEnvironmentObject*> environment,
HandleId targetName) {
if (!map_) {
map_.emplace(cx->zone());
}
mozilla::Maybe<PropertyInfo> prop = environment->lookup(cx, targetName);
MOZ_ASSERT(prop.isSome());
if (!map_->put(name, Binding(environment, targetName, *prop))) {
ReportOutOfMemory(cx);
return false;
}
return true;
}
bool IndirectBindingMap::lookup(jsid name, ModuleEnvironmentObject** envOut,
mozilla::Maybe<PropertyInfo>* propOut) const {
if (!map_) {
return false;
}
auto ptr = map_->lookup(name);
if (!ptr) {
return false;
}
const Binding& binding = ptr->value();
MOZ_ASSERT(binding.environment);
MOZ_ASSERT(
binding.environment->containsPure(binding.targetName, binding.prop));
*envOut = binding.environment;
*propOut = Some(binding.prop);
return true;
}
///////////////////////////////////////////////////////////////////////////
// ModuleNamespaceObject
/* static */
const ModuleNamespaceObject::ProxyHandler ModuleNamespaceObject::proxyHandler;
/* static */
bool ModuleNamespaceObject::isInstance(HandleValue value) {
return value.isObject() && value.toObject().is<ModuleNamespaceObject>();
}
/* static */
ModuleNamespaceObject* ModuleNamespaceObject::create(
JSContext* cx, Handle<ModuleObject*> module,
MutableHandle<UniquePtr<ExportNameVector>> exports,
MutableHandle<UniquePtr<IndirectBindingMap>> bindings) {
RootedValue priv(cx, ObjectValue(*module));
ProxyOptions options;
options.setLazyProto(true);
RootedObject object(
cx, NewProxyObject(cx, &proxyHandler, priv, nullptr, options));
if (!object) {
return nullptr;
}
SetProxyReservedSlot(object, ExportsSlot,
PrivateValue(exports.get().release()));
AddCellMemory(object, sizeof(ExportNameVector), MemoryUse::ModuleExports);
SetProxyReservedSlot(object, BindingsSlot,
PrivateValue(bindings.get().release()));
AddCellMemory(object, sizeof(IndirectBindingMap),
MemoryUse::ModuleBindingMap);
return &object->as<ModuleNamespaceObject>();
}
ModuleObject& ModuleNamespaceObject::module() {
return GetProxyPrivate(this).toObject().as<ModuleObject>();
}
const ExportNameVector& ModuleNamespaceObject::exports() const {
Value value = GetProxyReservedSlot(this, ExportsSlot);
auto* exports = static_cast<ExportNameVector*>(value.toPrivate());
MOZ_ASSERT(exports);
return *exports;
}
ExportNameVector& ModuleNamespaceObject::mutableExports() {
// Get a non-const reference for tracing/destruction. Do not actually mutate
// this vector! This would be incorrect without adding barriers.
return const_cast<ExportNameVector&>(exports());
}
IndirectBindingMap& ModuleNamespaceObject::bindings() {
Value value = GetProxyReservedSlot(this, BindingsSlot);
auto* bindings = static_cast<IndirectBindingMap*>(value.toPrivate());
MOZ_ASSERT(bindings);
return *bindings;
}
bool ModuleNamespaceObject::hasExports() const {
// Exports may not be present if we hit OOM in initialization.
return !GetProxyReservedSlot(this, ExportsSlot).isUndefined();
}
bool ModuleNamespaceObject::hasBindings() const {
// Import bindings may not be present if we hit OOM in initialization.
return !GetProxyReservedSlot(this, BindingsSlot).isUndefined();
}
bool ModuleNamespaceObject::addBinding(JSContext* cx,
Handle<JSAtom*> exportedName,
Handle<ModuleObject*> targetModule,
Handle<JSAtom*> targetName) {
Rooted<ModuleEnvironmentObject*> environment(
cx, &targetModule->initialEnvironment());
RootedId exportedNameId(cx, AtomToId(exportedName));
RootedId targetNameId(cx, AtomToId(targetName));
return bindings().put(cx, exportedNameId, environment, targetNameId);
}
const char ModuleNamespaceObject::ProxyHandler::family = 0;
ModuleNamespaceObject::ProxyHandler::ProxyHandler()
: BaseProxyHandler(&family, false) {}
bool ModuleNamespaceObject::ProxyHandler::getPrototype(
JSContext* cx, HandleObject proxy, MutableHandleObject protop) const {
protop.set(nullptr);
return true;
}
bool ModuleNamespaceObject::ProxyHandler::setPrototype(
JSContext* cx, HandleObject proxy, HandleObject proto,
ObjectOpResult& result) const {
if (!proto) {
return result.succeed();
}
return result.failCantSetProto();
}
bool ModuleNamespaceObject::ProxyHandler::getPrototypeIfOrdinary(
JSContext* cx, HandleObject proxy, bool* isOrdinary,
MutableHandleObject protop) const {
*isOrdinary = false;
return true;
}
bool ModuleNamespaceObject::ProxyHandler::setImmutablePrototype(
JSContext* cx, HandleObject proxy, bool* succeeded) const {
*succeeded = true;
return true;
}
bool ModuleNamespaceObject::ProxyHandler::isExtensible(JSContext* cx,
HandleObject proxy,
bool* extensible) const {
*extensible = false;
return true;
}
bool ModuleNamespaceObject::ProxyHandler::preventExtensions(
JSContext* cx, HandleObject proxy, ObjectOpResult& result) const {
result.succeed();
return true;
}
bool ModuleNamespaceObject::ProxyHandler::getOwnPropertyDescriptor(
JSContext* cx, HandleObject proxy, HandleId id,
MutableHandle<mozilla::Maybe<PropertyDescriptor>> desc) const {
Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
if (id.isSymbol()) {
if (id.isWellKnownSymbol(JS::SymbolCode::toStringTag)) {
desc.set(Some(PropertyDescriptor::Data(StringValue(cx->names().Module))));
return true;
}
desc.reset();
return true;
}
const IndirectBindingMap& bindings = ns->bindings();
ModuleEnvironmentObject* env;
mozilla::Maybe<PropertyInfo> prop;
if (!bindings.lookup(id, &env, &prop)) {
// Not found.
desc.reset();
return true;
}
RootedValue value(cx, env->getSlot(prop->slot()));
if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
return false;
}
desc.set(
Some(PropertyDescriptor::Data(value, {JS::PropertyAttribute::Enumerable,
JS::PropertyAttribute::Writable})));
return true;
}
static bool ValidatePropertyDescriptor(
JSContext* cx, Handle<PropertyDescriptor> desc, bool expectedWritable,
bool expectedEnumerable, bool expectedConfigurable,
HandleValue expectedValue, ObjectOpResult& result) {
if (desc.isAccessorDescriptor()) {
return result.fail(JSMSG_CANT_REDEFINE_PROP);
}
if (desc.hasWritable() && desc.writable() != expectedWritable) {
return result.fail(JSMSG_CANT_REDEFINE_PROP);
}
if (desc.hasEnumerable() && desc.enumerable() != expectedEnumerable) {
return result.fail(JSMSG_CANT_REDEFINE_PROP);
}
if (desc.hasConfigurable() && desc.configurable() != expectedConfigurable) {
return result.fail(JSMSG_CANT_REDEFINE_PROP);
}
if (desc.hasValue()) {
bool same;
if (!SameValue(cx, desc.value(), expectedValue, &same)) {
return false;
}
if (!same) {
return result.fail(JSMSG_CANT_REDEFINE_PROP);
}
}
return result.succeed();
}
bool ModuleNamespaceObject::ProxyHandler::defineProperty(
JSContext* cx, HandleObject proxy, HandleId id,
Handle<PropertyDescriptor> desc, ObjectOpResult& result) const {
if (id.isSymbol()) {
if (id.isWellKnownSymbol(JS::SymbolCode::toStringTag)) {
RootedValue value(cx, StringValue(cx->names().Module));
return ValidatePropertyDescriptor(cx, desc, false, false, false, value,
result);
}
return result.fail(JSMSG_CANT_DEFINE_PROP_OBJECT_NOT_EXTENSIBLE);
}
const IndirectBindingMap& bindings =
proxy->as<ModuleNamespaceObject>().bindings();
ModuleEnvironmentObject* env;
mozilla::Maybe<PropertyInfo> prop;
if (!bindings.lookup(id, &env, &prop)) {
return result.fail(JSMSG_CANT_DEFINE_PROP_OBJECT_NOT_EXTENSIBLE);
}
RootedValue value(cx, env->getSlot(prop->slot()));
if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
return false;
}
return ValidatePropertyDescriptor(cx, desc, true, true, false, value, result);
}
bool ModuleNamespaceObject::ProxyHandler::has(JSContext* cx, HandleObject proxy,
HandleId id, bool* bp) const {
Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
if (id.isSymbol()) {
*bp = id.isWellKnownSymbol(JS::SymbolCode::toStringTag);
return true;
}
*bp = ns->bindings().has(id);
return true;
}
bool ModuleNamespaceObject::ProxyHandler::get(JSContext* cx, HandleObject proxy,
HandleValue receiver, HandleId id,
MutableHandleValue vp) const {
Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
if (id.isSymbol()) {
if (id.isWellKnownSymbol(JS::SymbolCode::toStringTag)) {
vp.setString(cx->names().Module);
return true;
}
vp.setUndefined();
return true;
}
ModuleEnvironmentObject* env;
mozilla::Maybe<PropertyInfo> prop;
if (!ns->bindings().lookup(id, &env, &prop)) {
vp.setUndefined();
return true;
}
RootedValue value(cx, env->getSlot(prop->slot()));
if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
return false;
}
vp.set(value);
return true;
}
bool ModuleNamespaceObject::ProxyHandler::set(JSContext* cx, HandleObject proxy,
HandleId id, HandleValue v,
HandleValue receiver,
ObjectOpResult& result) const {
return result.failReadOnly();
}
bool ModuleNamespaceObject::ProxyHandler::delete_(
JSContext* cx, HandleObject proxy, HandleId id,
ObjectOpResult& result) const {
Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
if (id.isSymbol()) {
if (id.isWellKnownSymbol(JS::SymbolCode::toStringTag)) {
return result.failCantDelete();
}
return result.succeed();
}
if (ns->bindings().has(id)) {
return result.failCantDelete();
}
return result.succeed();
}
bool ModuleNamespaceObject::ProxyHandler::ownPropertyKeys(
JSContext* cx, HandleObject proxy, MutableHandleIdVector props) const {
Rooted<ModuleNamespaceObject*> ns(cx, &proxy->as<ModuleNamespaceObject>());
uint32_t count = ns->exports().length();
if (!props.reserve(props.length() + count + 1)) {
return false;
}
for (JSAtom* atom : ns->exports()) {
props.infallibleAppend(AtomToId(atom));
}
props.infallibleAppend(
PropertyKey::Symbol(cx->wellKnownSymbols().toStringTag));
return true;
}
void ModuleNamespaceObject::ProxyHandler::trace(JSTracer* trc,
JSObject* proxy) const {
auto& self = proxy->as<ModuleNamespaceObject>();
if (self.hasExports()) {
self.mutableExports().trace(trc);
}
if (self.hasBindings()) {
self.bindings().trace(trc);
}
}
void ModuleNamespaceObject::ProxyHandler::finalize(JS::GCContext* gcx,
JSObject* proxy) const {
auto& self = proxy->as<ModuleNamespaceObject>();
if (self.hasExports()) {
gcx->delete_(proxy, &self.mutableExports(), MemoryUse::ModuleExports);
}
if (self.hasBindings()) {
gcx->delete_(proxy, &self.bindings(), MemoryUse::ModuleBindingMap);
}
}
///////////////////////////////////////////////////////////////////////////
// SyntheticModuleFields
// The fields of a synthetic module record, as described in:
class js::SyntheticModuleFields {
public:
ExportNameVector exportNames;
public:
void trace(JSTracer* trc);
};
void SyntheticModuleFields::trace(JSTracer* trc) { exportNames.trace(trc); }
///////////////////////////////////////////////////////////////////////////
// CyclicModuleFields
// The fields of a cyclic module record, as described in:
class js::CyclicModuleFields {
public:
ModuleStatus status = ModuleStatus::Unlinked;
bool hasTopLevelAwait : 1;
private:
// Flag bits that determine whether other fields are present.
bool hasDfsIndex : 1;
bool hasDfsAncestorIndex : 1;
bool isAsyncEvaluating : 1;
bool hasPendingAsyncDependencies : 1;
// Fields whose presence is conditional on the flag bits above.
uint32_t dfsIndex = 0;
uint32_t dfsAncestorIndex = 0;
uint32_t asyncEvaluatingPostOrder = 0;
uint32_t pendingAsyncDependencies = 0;
// Fields describing the layout of exportEntries.
uint32_t indirectExportEntriesStart = 0;
uint32_t starExportEntriesStart = 0;
public:
HeapPtr<Value> evaluationError;
HeapPtr<JSObject*> metaObject;
HeapPtr<ScriptSourceObject*> scriptSourceObject;
RequestedModuleVector requestedModules;
ImportEntryVector importEntries;
ExportEntryVector exportEntries;
IndirectBindingMap importBindings;
UniquePtr<FunctionDeclarationVector> functionDeclarations;
HeapPtr<PromiseObject*> topLevelCapability;
HeapPtr<ListObject*> asyncParentModules;
HeapPtr<ModuleObject*> cycleRoot;
public:
CyclicModuleFields();
void trace(JSTracer* trc);
void initExportEntries(MutableHandle<ExportEntryVector> allEntries,
uint32_t localExportCount,
uint32_t indirectExportCount,
uint32_t starExportCount);
Span<const ExportEntry> localExportEntries() const;
Span<const ExportEntry> indirectExportEntries() const;
Span<const ExportEntry> starExportEntries() const;
void setDfsIndex(uint32_t index);
Maybe<uint32_t> maybeDfsIndex() const;
void setDfsAncestorIndex(uint32_t index);
Maybe<uint32_t> maybeDfsAncestorIndex() const;
void clearDfsIndexes();
void setAsyncEvaluating(uint32_t postOrder);
bool getIsAsyncEvaluating() const;
Maybe<uint32_t> maybeAsyncEvaluatingPostOrder() const;
void clearAsyncEvaluatingPostOrder();
void setPendingAsyncDependencies(uint32_t newValue);
Maybe<uint32_t> maybePendingAsyncDependencies() const;
};
CyclicModuleFields::CyclicModuleFields()
: hasTopLevelAwait(false),
hasDfsIndex(false),
hasDfsAncestorIndex(false),
isAsyncEvaluating(false),
hasPendingAsyncDependencies(false) {}
void CyclicModuleFields::trace(JSTracer* trc) {
TraceEdge(trc, &evaluationError, "CyclicModuleFields::evaluationError");
TraceNullableEdge(trc, &metaObject, "CyclicModuleFields::metaObject");
TraceNullableEdge(trc, &scriptSourceObject,
"CyclicModuleFields::scriptSourceObject");
requestedModules.trace(trc);
importEntries.trace(trc);
exportEntries.trace(trc);
importBindings.trace(trc);
TraceNullableEdge(trc, &topLevelCapability,
"CyclicModuleFields::topLevelCapability");
TraceNullableEdge(trc, &asyncParentModules,
"CyclicModuleFields::asyncParentModules");
TraceNullableEdge(trc, &cycleRoot, "CyclicModuleFields::cycleRoot");
}
void CyclicModuleFields::initExportEntries(
MutableHandle<ExportEntryVector> allEntries, uint32_t localExportCount,
uint32_t indirectExportCount, uint32_t starExportCount) {
MOZ_ASSERT(allEntries.length() ==
localExportCount + indirectExportCount + starExportCount);
exportEntries = std::move(allEntries.get());
indirectExportEntriesStart = localExportCount;
starExportEntriesStart = indirectExportEntriesStart + indirectExportCount;
}
Span<const ExportEntry> CyclicModuleFields::localExportEntries() const {
MOZ_ASSERT(indirectExportEntriesStart <= exportEntries.length());
return Span(exportEntries.begin(),
exportEntries.begin() + indirectExportEntriesStart);
}
Span<const ExportEntry> CyclicModuleFields::indirectExportEntries() const {
MOZ_ASSERT(indirectExportEntriesStart <= starExportEntriesStart);
MOZ_ASSERT(starExportEntriesStart <= exportEntries.length());
return Span(exportEntries.begin() + indirectExportEntriesStart,
exportEntries.begin() + starExportEntriesStart);
}
Span<const ExportEntry> CyclicModuleFields::starExportEntries() const {
MOZ_ASSERT(starExportEntriesStart <= exportEntries.length());
return Span(exportEntries.begin() + starExportEntriesStart,
exportEntries.end());
}
void CyclicModuleFields::setDfsIndex(uint32_t index) {
dfsIndex = index;
hasDfsIndex = true;
}
Maybe<uint32_t> CyclicModuleFields::maybeDfsIndex() const {
return hasDfsIndex ? Some(dfsIndex) : Nothing();
}
void CyclicModuleFields::setDfsAncestorIndex(uint32_t index) {
dfsAncestorIndex = index;
hasDfsAncestorIndex = true;
}
Maybe<uint32_t> CyclicModuleFields::maybeDfsAncestorIndex() const {
return hasDfsAncestorIndex ? Some(dfsAncestorIndex) : Nothing();
}
void CyclicModuleFields::clearDfsIndexes() {
dfsIndex = 0;
hasDfsIndex = false;
dfsAncestorIndex = 0;
hasDfsAncestorIndex = false;
}
void CyclicModuleFields::setAsyncEvaluating(uint32_t postOrder) {
isAsyncEvaluating = true;
asyncEvaluatingPostOrder = postOrder;
}
bool CyclicModuleFields::getIsAsyncEvaluating() const {
return isAsyncEvaluating;
}
Maybe<uint32_t> CyclicModuleFields::maybeAsyncEvaluatingPostOrder() const {
if (!isAsyncEvaluating ||
asyncEvaluatingPostOrder == ASYNC_EVALUATING_POST_ORDER_CLEARED) {
return Nothing();
}
return Some(asyncEvaluatingPostOrder);
}
void CyclicModuleFields::clearAsyncEvaluatingPostOrder() {
asyncEvaluatingPostOrder = ASYNC_EVALUATING_POST_ORDER_CLEARED;
}
void CyclicModuleFields::setPendingAsyncDependencies(uint32_t newValue) {
pendingAsyncDependencies = newValue;
hasPendingAsyncDependencies = true;
}
Maybe<uint32_t> CyclicModuleFields::maybePendingAsyncDependencies() const {
return hasPendingAsyncDependencies ? Some(pendingAsyncDependencies)
: Nothing();
}
///////////////////////////////////////////////////////////////////////////
// ModuleObject
/* static */ const JSClassOps ModuleObject::classOps_ = {
nullptr, // addProperty
nullptr, // delProperty
nullptr, // enumerate
nullptr, // newEnumerate
nullptr, // resolve
nullptr, // mayResolve
ModuleObject::finalize, // finalize
nullptr, // call
nullptr, // construct
ModuleObject::trace, // trace
};
/* static */ const JSClass ModuleObject::class_ = {
"Module",
JSCLASS_HAS_RESERVED_SLOTS(ModuleObject::SlotCount) |
JSCLASS_BACKGROUND_FINALIZE,
&ModuleObject::classOps_};
/* static */
bool ModuleObject::isInstance(HandleValue value) {
return value.isObject() && value.toObject().is<ModuleObject>();
}
bool ModuleObject::hasCyclicModuleFields() const {
// This currently only returns false if we GC during initialization.
return !getReservedSlot(CyclicModuleFieldsSlot).isUndefined();
}
CyclicModuleFields* ModuleObject::cyclicModuleFields() {
void* ptr = getReservedSlot(CyclicModuleFieldsSlot).toPrivate();
MOZ_ASSERT(ptr);
return static_cast<CyclicModuleFields*>(ptr);
}
const CyclicModuleFields* ModuleObject::cyclicModuleFields() const {
return const_cast<ModuleObject*>(this)->cyclicModuleFields();
}
Span<const RequestedModule> ModuleObject::requestedModules() const {
return cyclicModuleFields()->requestedModules;
}
Span<const ImportEntry> ModuleObject::importEntries() const {
return cyclicModuleFields()->importEntries;
}
Span<const ExportEntry> ModuleObject::localExportEntries() const {
return cyclicModuleFields()->localExportEntries();
}
Span<const ExportEntry> ModuleObject::indirectExportEntries() const {
return cyclicModuleFields()->indirectExportEntries();
}
Span<const ExportEntry> ModuleObject::starExportEntries() const {
return cyclicModuleFields()->starExportEntries();
}
const ExportNameVector& ModuleObject::syntheticExportNames() const {
return syntheticModuleFields()->exportNames;
}
void ModuleObject::initFunctionDeclarations(
UniquePtr<FunctionDeclarationVector> decls) {
cyclicModuleFields()->functionDeclarations = std::move(decls);
}
/* static */
ModuleObject* ModuleObject::create(JSContext* cx) {
Rooted<UniquePtr<CyclicModuleFields>> fields(cx);
fields = cx->make_unique<CyclicModuleFields>();
if (!fields) {
return nullptr;
}
Rooted<ModuleObject*> self(
cx, NewObjectWithGivenProto<ModuleObject>(cx, nullptr));
if (!self) {
return nullptr;
}
InitReservedSlot(self, CyclicModuleFieldsSlot, fields.release(),
MemoryUse::ModuleCyclicFields);
return self;
}
/* static */
ModuleObject* ModuleObject::createSynthetic(
JSContext* cx, MutableHandle<ExportNameVector> exportNames) {
Rooted<UniquePtr<SyntheticModuleFields>> syntheticFields(cx);
syntheticFields = cx->make_unique<SyntheticModuleFields>();
if (!syntheticFields) {
return nullptr;
}
Rooted<ModuleObject*> self(
cx, NewObjectWithGivenProto<ModuleObject>(cx, nullptr));
if (!self) {
return nullptr;
}
InitReservedSlot(self, SyntheticModuleFieldsSlot, syntheticFields.release(),
MemoryUse::ModuleSyntheticFields);
self->syntheticModuleFields()->exportNames = std::move(exportNames.get());
return self;
}
/* static */
void ModuleObject::finalize(JS::GCContext* gcx, JSObject* obj) {
ModuleObject* self = &obj->as<ModuleObject>();
if (self->hasCyclicModuleFields()) {
gcx->delete_(obj, self->cyclicModuleFields(),
MemoryUse::ModuleCyclicFields);
}
if (self->hasSyntheticModuleFields()) {
gcx->delete_(obj, self->syntheticModuleFields(),
MemoryUse::ModuleSyntheticFields);
}
}
ModuleEnvironmentObject& ModuleObject::initialEnvironment() const {
Value value = getReservedSlot(EnvironmentSlot);
return value.toObject().as<ModuleEnvironmentObject>();
}
ModuleEnvironmentObject* ModuleObject::environment() const {
// Note that this it's valid to call this even if there was an error
// evaluating the module.
// According to the spec the environment record is created during linking, but
// we create it earlier than that.
if (status() < ModuleStatus::Linked) {
return nullptr;
}
return &initialEnvironment();
}
IndirectBindingMap& ModuleObject::importBindings() {
return cyclicModuleFields()->importBindings;
}
ModuleNamespaceObject* ModuleObject::namespace_() {
Value value = getReservedSlot(NamespaceSlot);
if (value.isUndefined()) {
return nullptr;
}
return &value.toObject().as<ModuleNamespaceObject>();
}
ScriptSourceObject* ModuleObject::scriptSourceObject() const {
return cyclicModuleFields()->scriptSourceObject;
}
void ModuleObject::initAsyncSlots(JSContext* cx, bool hasTopLevelAwait,
Handle<ListObject*> asyncParentModules) {
cyclicModuleFields()->hasTopLevelAwait = hasTopLevelAwait;
cyclicModuleFields()->asyncParentModules = asyncParentModules;
}
static uint32_t NextPostOrder(JSRuntime* rt) {
uint32_t ordinal = rt->moduleAsyncEvaluatingPostOrder;
MOZ_ASSERT(ordinal != ASYNC_EVALUATING_POST_ORDER_CLEARED);
MOZ_ASSERT(ordinal < MAX_UINT32);
rt->moduleAsyncEvaluatingPostOrder++;
return ordinal;
}
// Reset the runtime's moduleAsyncEvaluatingPostOrder counter when the last
// module that was async evaluating is finished.
//
// The graph is not re-entrant and any future modules will be independent from
// this one.
static void MaybeResetPostOrderCounter(JSRuntime* rt,
uint32_t finishedPostOrder) {
if (rt->moduleAsyncEvaluatingPostOrder == finishedPostOrder + 1) {
rt->moduleAsyncEvaluatingPostOrder = ASYNC_EVALUATING_POST_ORDER_INIT;
}
}
void ModuleObject::setAsyncEvaluating() {
MOZ_ASSERT(!isAsyncEvaluating());
uint32_t postOrder = NextPostOrder(runtimeFromMainThread());
cyclicModuleFields()->setAsyncEvaluating(postOrder);
}
void ModuleObject::initScriptSlots(HandleScript script) {
MOZ_ASSERT(script);
MOZ_ASSERT(script->sourceObject());
MOZ_ASSERT(script->filename());
initReservedSlot(ScriptSlot, PrivateGCThingValue(script));
cyclicModuleFields()->scriptSourceObject = script->sourceObject();
}
void ModuleObject::setInitialEnvironment(
Handle<ModuleEnvironmentObject*> initialEnvironment) {
initReservedSlot(EnvironmentSlot, ObjectValue(*initialEnvironment));
}
void ModuleObject::initImportExportData(
MutableHandle<RequestedModuleVector> requestedModules,
MutableHandle<ImportEntryVector> importEntries,
MutableHandle<ExportEntryVector> exportEntries, uint32_t localExportCount,
uint32_t indirectExportCount, uint32_t starExportCount) {
cyclicModuleFields()->requestedModules = std::move(requestedModules.get());
cyclicModuleFields()->importEntries = std::move(importEntries.get());
cyclicModuleFields()->initExportEntries(exportEntries, localExportCount,
indirectExportCount, starExportCount);
}
/* static */
bool ModuleObject::Freeze(JSContext* cx, Handle<ModuleObject*> self) {
return FreezeObject(cx, self);
}
#ifdef DEBUG
/* static */ inline bool ModuleObject::AssertFrozen(
JSContext* cx, Handle<ModuleObject*> self) {
bool frozen = false;
if (!TestIntegrityLevel(cx, self, IntegrityLevel::Frozen, &frozen)) {
return false;
}
MOZ_ASSERT(frozen);
return true;
}
#endif
JSScript* ModuleObject::maybeScript() const {
Value value = getReservedSlot(ScriptSlot);
if (value.isUndefined()) {
return nullptr;
}
BaseScript* script = value.toGCThing()->as<BaseScript>();
MOZ_ASSERT(script->hasBytecode(),
"Module scripts should always have bytecode");
return script->asJSScript();
}
JSScript* ModuleObject::script() const {
JSScript* ptr = maybeScript();
MOZ_RELEASE_ASSERT(ptr);
return ptr;
}
const char* ModuleObject::filename() const {
// The ScriptSlot will be cleared once the module is evaluated, so we try to
// get the filename from cyclicModuleFields().
// TODO: Bug 1885483: Provide filename for JSON modules
if (!hasCyclicModuleFields()) {
return "(JSON module)";
}
return cyclicModuleFields()->scriptSourceObject->source()->filename();
}
static inline void AssertValidModuleStatus(ModuleStatus status) {
MOZ_ASSERT(status >= ModuleStatus::Unlinked &&
status <= ModuleStatus::Evaluated_Error);
}
ModuleStatus ModuleObject::status() const {
// Always return `ModuleStatus::Evaluated` so we can assert a module's status
// without checking which kind it is, even though synthetic modules don't have
// this field according to the spec.
if (hasSyntheticModuleFields()) {
return ModuleStatus::Evaluated;
}
ModuleStatus status = cyclicModuleFields()->status;
AssertValidModuleStatus(status);
if (status == ModuleStatus::Evaluated_Error) {
return ModuleStatus::Evaluated;
}
return status;
}
void ModuleObject::setStatus(ModuleStatus newStatus) {
AssertValidModuleStatus(newStatus);
// Note that under OOM conditions we can fail the module linking process even
// after modules have been marked as linked.
MOZ_ASSERT((status() <= ModuleStatus::Linked &&
newStatus == ModuleStatus::Unlinked) ||
newStatus > status(),
"New module status inconsistent with current status");
cyclicModuleFields()->status = newStatus;
}
bool ModuleObject::hasTopLevelAwait() const {
return cyclicModuleFields()->hasTopLevelAwait;
}
bool ModuleObject::isAsyncEvaluating() const {
return cyclicModuleFields()->getIsAsyncEvaluating();
}
Maybe<uint32_t> ModuleObject::maybeDfsIndex() const {
return cyclicModuleFields()->maybeDfsIndex();
}
uint32_t ModuleObject::dfsIndex() const { return maybeDfsIndex().value(); }
void ModuleObject::setDfsIndex(uint32_t index) {
cyclicModuleFields()->setDfsIndex(index);
}
Maybe<uint32_t> ModuleObject::maybeDfsAncestorIndex() const {
return cyclicModuleFields()->maybeDfsAncestorIndex();
}
uint32_t ModuleObject::dfsAncestorIndex() const {
return maybeDfsAncestorIndex().value();
}
void ModuleObject::setDfsAncestorIndex(uint32_t index) {
cyclicModuleFields()->setDfsAncestorIndex(index);
}
void ModuleObject::clearDfsIndexes() {
cyclicModuleFields()->clearDfsIndexes();
}
PromiseObject* ModuleObject::maybeTopLevelCapability() const {
return cyclicModuleFields()->topLevelCapability;
}
PromiseObject* ModuleObject::topLevelCapability() const {
PromiseObject* capability = maybeTopLevelCapability();
MOZ_RELEASE_ASSERT(capability);
return capability;
}
// static
PromiseObject* ModuleObject::createTopLevelCapability(
JSContext* cx, Handle<ModuleObject*> module) {
MOZ_ASSERT(!module->maybeTopLevelCapability());
Rooted<PromiseObject*> resultPromise(cx, CreatePromiseObjectForAsync(cx));
if (!resultPromise) {
return nullptr;
}
module->setInitialTopLevelCapability(resultPromise);
return resultPromise;
}
void ModuleObject::setInitialTopLevelCapability(
Handle<PromiseObject*> capability) {
cyclicModuleFields()->topLevelCapability = capability;
}
ListObject* ModuleObject::asyncParentModules() const {
return cyclicModuleFields()->asyncParentModules;
}
bool ModuleObject::appendAsyncParentModule(JSContext* cx,
Handle<ModuleObject*> self,
Handle<ModuleObject*> parent) {
Rooted<Value> parentValue(cx, ObjectValue(*parent));
return self->asyncParentModules()->append(cx, parentValue);
}
Maybe<uint32_t> ModuleObject::maybePendingAsyncDependencies() const {
return cyclicModuleFields()->maybePendingAsyncDependencies();
}
uint32_t ModuleObject::pendingAsyncDependencies() const {
return maybePendingAsyncDependencies().value();
}
Maybe<uint32_t> ModuleObject::maybeAsyncEvaluatingPostOrder() const {
return cyclicModuleFields()->maybeAsyncEvaluatingPostOrder();
}
uint32_t ModuleObject::getAsyncEvaluatingPostOrder() const {
return cyclicModuleFields()->maybeAsyncEvaluatingPostOrder().value();
}
void ModuleObject::clearAsyncEvaluatingPostOrder() {
MOZ_ASSERT(status() == ModuleStatus::Evaluated);
JSRuntime* rt = runtimeFromMainThread();
MaybeResetPostOrderCounter(rt, getAsyncEvaluatingPostOrder());
cyclicModuleFields()->clearAsyncEvaluatingPostOrder();
}
void ModuleObject::setPendingAsyncDependencies(uint32_t newValue) {
cyclicModuleFields()->setPendingAsyncDependencies(newValue);
}
void ModuleObject::setCycleRoot(ModuleObject* cycleRoot) {
cyclicModuleFields()->cycleRoot = cycleRoot;
}
ModuleObject* ModuleObject::getCycleRoot() const {
MOZ_RELEASE_ASSERT(cyclicModuleFields()->cycleRoot);
return cyclicModuleFields()->cycleRoot;
}
bool ModuleObject::hasSyntheticModuleFields() const {
bool result = !getReservedSlot(SyntheticModuleFieldsSlot).isUndefined();
MOZ_ASSERT_IF(result, !hasCyclicModuleFields());
return result;
}
SyntheticModuleFields* ModuleObject::syntheticModuleFields() {
MOZ_ASSERT(!hasCyclicModuleFields());
void* ptr = getReservedSlot(SyntheticModuleFieldsSlot).toPrivate();
MOZ_ASSERT(ptr);
return static_cast<SyntheticModuleFields*>(ptr);
}
const SyntheticModuleFields* ModuleObject::syntheticModuleFields() const {
return const_cast<ModuleObject*>(this)->syntheticModuleFields();
}
bool ModuleObject::hasTopLevelCapability() const {
return cyclicModuleFields()->topLevelCapability;
}
bool ModuleObject::hadEvaluationError() const {
ModuleStatus fullStatus = cyclicModuleFields()->status;
return fullStatus == ModuleStatus::Evaluated_Error;
}
void ModuleObject::setEvaluationError(HandleValue newValue) {
MOZ_ASSERT(status() != ModuleStatus::Unlinked);
MOZ_ASSERT(!hadEvaluationError());
cyclicModuleFields()->status = ModuleStatus::Evaluated_Error;
cyclicModuleFields()->evaluationError = newValue;
MOZ_ASSERT(status() == ModuleStatus::Evaluated);
MOZ_ASSERT(hadEvaluationError());
}
Value ModuleObject::maybeEvaluationError() const {
return cyclicModuleFields()->evaluationError;
}
Value ModuleObject::evaluationError() const {
MOZ_ASSERT(hadEvaluationError());
return maybeEvaluationError();
}
JSObject* ModuleObject::metaObject() const {
return cyclicModuleFields()->metaObject;
}
void ModuleObject::setMetaObject(JSObject* obj) {
MOZ_ASSERT(obj);
MOZ_ASSERT(!metaObject());
cyclicModuleFields()->metaObject = obj;
}
/* static */
void ModuleObject::trace(JSTracer* trc, JSObject* obj) {
ModuleObject& module = obj->as<ModuleObject>();
if (module.hasCyclicModuleFields()) {
module.cyclicModuleFields()->trace(trc);
}
if (module.hasSyntheticModuleFields()) {
module.syntheticModuleFields()->trace(trc);
}
}
/* static */
bool ModuleObject::instantiateFunctionDeclarations(JSContext* cx,
Handle<ModuleObject*> self) {
#ifdef DEBUG
MOZ_ASSERT(self->status() == ModuleStatus::Linking);
if (!AssertFrozen(cx, self)) {
return false;
}
#endif
// |self| initially manages this vector.
UniquePtr<FunctionDeclarationVector>& funDecls =
self->cyclicModuleFields()->functionDeclarations;
if (!funDecls) {
JS_ReportErrorASCII(
cx, "Module function declarations have already been instantiated");
return false;
}
Rooted<ModuleEnvironmentObject*> env(cx, &self->initialEnvironment());
RootedObject obj(cx);
RootedValue value(cx);
RootedFunction fun(cx);
Rooted<PropertyName*> name(cx);
for (GCThingIndex funIndex : *funDecls) {
fun.set(self->script()->getFunction(funIndex));
obj = Lambda(cx, fun, env);
if (!obj) {
return false;
}
name = fun->fullExplicitName()->asPropertyName();
value = ObjectValue(*obj);
if (!SetProperty(cx, env, name, value)) {
return false;
}
}
// Free the vector, now its contents are no longer needed.
funDecls.reset();
return true;
}
/* static */
bool ModuleObject::execute(JSContext* cx, Handle<ModuleObject*> self) {
#ifdef DEBUG
MOZ_ASSERT(self->status() == ModuleStatus::Evaluating ||
self->status() == ModuleStatus::EvaluatingAsync ||
self->status() == ModuleStatus::Evaluated);
MOZ_ASSERT(!self->hadEvaluationError());
if (!AssertFrozen(cx, self)) {
return false;
}
#endif
RootedScript script(cx, self->script());
auto guardA = mozilla::MakeScopeExit([&] {
if (self->hasTopLevelAwait()) {
// Handled in AsyncModuleExecutionFulfilled and
// AsyncModuleExecutionRejected.
return;
}
ModuleObject::onTopLevelEvaluationFinished(self);
});
Rooted<ModuleEnvironmentObject*> env(cx, self->environment());
if (!env) {
JS_ReportErrorASCII(cx,
"Module declarations have not yet been instantiated");
return false;
}
Rooted<Value> ignored(cx);
return Execute(cx, script, env, &ignored);
}
/* static */
void ModuleObject::onTopLevelEvaluationFinished(ModuleObject* module) {
// ScriptSlot is used by debugger to access environments during evaluating
// the top-level script.
// Clear the reference at exit to prevent us keeping this alive unnecessarily.
module->setReservedSlot(ScriptSlot, UndefinedValue());
}
/* static */
ModuleNamespaceObject* ModuleObject::createNamespace(
JSContext* cx, Handle<ModuleObject*> self,
MutableHandle<UniquePtr<ExportNameVector>> exports) {
MOZ_ASSERT(!self->namespace_());
Rooted<UniquePtr<IndirectBindingMap>> bindings(cx);
bindings = cx->make_unique<IndirectBindingMap>();
if (!bindings) {
return nullptr;
}
auto* ns = ModuleNamespaceObject::create(cx, self, exports, &bindings);
if (!ns) {
return nullptr;
}
self->initReservedSlot(NamespaceSlot, ObjectValue(*ns));
return ns;
}
/* static */
bool ModuleObject::createEnvironment(JSContext* cx,
Handle<ModuleObject*> self) {
Rooted<ModuleEnvironmentObject*> env(
cx, ModuleEnvironmentObject::create(cx, self));
if (!env) {
return false;
}
self->setInitialEnvironment(env);
return true;
}
/*static*/
bool ModuleObject::createSyntheticEnvironment(JSContext* cx,
Handle<ModuleObject*> self,
Handle<GCVector<Value>> values) {
Rooted<ModuleEnvironmentObject*> env(
cx, ModuleEnvironmentObject::createSynthetic(cx, self));
if (!env) {
return false;
}
MOZ_ASSERT(env->shape()->propMapLength() == values.length());
for (uint32_t i = 0; i < values.length(); i++) {
env->setAliasedBinding(env->firstSyntheticValueSlot() + i, values[i]);
}
self->setInitialEnvironment(env);
return true;
}
///////////////////////////////////////////////////////////////////////////
// ModuleBuilder
ModuleBuilder::ModuleBuilder(FrontendContext* fc,
const frontend::EitherParser& eitherParser)
: fc_(fc),
eitherParser_(eitherParser),
requestedModuleSpecifiers_(fc),
importEntries_(fc),
exportEntries_(fc),
exportNames_(fc) {}
bool ModuleBuilder::noteFunctionDeclaration(FrontendContext* fc,
uint32_t funIndex) {
if (!functionDecls_.emplaceBack(funIndex)) {
js::ReportOutOfMemory(fc);
return false;
}
return true;
}
void ModuleBuilder::noteAsync(frontend::StencilModuleMetadata& metadata) {
metadata.isAsync = true;
}
bool ModuleBuilder::buildTables(frontend::StencilModuleMetadata& metadata) {
// 15.2.1.17.1 ParseModule, Steps 4-11.
// Step 4.
metadata.moduleRequests = std::move(moduleRequests_);
metadata.requestedModules = std::move(requestedModules_);
// Step 5.
if (!metadata.importEntries.reserve(