Bug 1469004 - Add an API to create a module namespace object r=anba
authorJon Coppeard <jcoppeard@mozilla.com>
Mon, 13 Aug 2018 16:39:14 +0100
changeset 431430 08bf805f6f0ef61f68686ef1ca2cc6f750a2cfa0
parent 431429 5660b9d6ef1edb68673d2fd8abc75873ab08f4f7
child 431431 7846bdd3762cf494ec24efc9cfc3472dc715ce4f
push id34443
push usercsabou@mozilla.com
push dateWed, 15 Aug 2018 00:53:32 +0000
treeherdermozilla-central@b80906e2fbc9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersanba
bugs1469004
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1469004 - Add an API to create a module namespace object r=anba
js/src/builtin/Module.js
js/src/builtin/ModuleObject.cpp
js/src/builtin/ModuleObject.h
js/src/vm/CommonPropertyNames.h
--- a/js/src/builtin/Module.js
+++ b/js/src/builtin/Module.js
@@ -181,17 +181,17 @@ function IsResolvedBinding(resolution)
            "Bad module resolution result");
     return typeof resolution === "object" && resolution !== null;
 }
 
 // 15.2.1.18 GetModuleNamespace(module)
 function GetModuleNamespace(module)
 {
     // Step 1
-    assert(IsModule(module), "GetModuleNamespace called with non-module");
+    assert(IsObject(module) && IsModule(module), "GetModuleNamespace called with non-module");
 
     // Until issue https://github.com/tc39/ecma262/issues/1155 is resolved,
     // violate the spec here and throw if called on an errored module.
     if (module.status === MODULE_STATUS_EVALUATED_ERROR)
         throw GetModuleEvaluationError(module);
 
     // Steps 2-3
     assert(module.status !== MODULE_STATUS_UNINSTANTIATED,
@@ -233,23 +233,23 @@ function ModuleNamespaceCreate(module, e
         AddModuleNamespaceBinding(ns, name, binding.module, binding.bindingName);
     }
 
     return ns;
 }
 
 function GetModuleEnvironment(module)
 {
-    assert(IsModule(module), "Non-module passed to GetModuleEnvironment");
+    assert(IsObject(module) && IsModule(module), "Non-module passed to GetModuleEnvironment");
 
     assert(module.status >= MODULE_STATUS_INSTANTIATING,
            "Attempt to access module environement before instantation");
 
     let env = UnsafeGetReservedSlot(module, MODULE_OBJECT_ENVIRONMENT_SLOT);
-    assert(IsModuleEnvironment(env),
+    assert(IsObject(env) && IsModuleEnvironment(env),
            "Module environment slot contains unexpected value");
 
     return env;
 }
 
 function CountArrayValues(array, value)
 {
     let count = 0;
--- a/js/src/builtin/ModuleObject.cpp
+++ b/js/src/builtin/ModuleObject.cpp
@@ -1152,16 +1152,32 @@ ModuleObject::Instantiate(JSContext* cx,
 }
 
 /* static */ bool
 ModuleObject::Evaluate(JSContext* cx, HandleModuleObject self)
 {
     return InvokeSelfHostedMethod(cx, self, cx->names().ModuleEvaluate);
 }
 
+/* static */ ModuleNamespaceObject*
+ModuleObject::GetOrCreateModuleNamespace(JSContext* cx, HandleModuleObject self)
+{
+    FixedInvokeArgs<1> args(cx);
+    args[0].setObject(*self);
+
+    RootedValue result(cx);
+    if (!CallSelfHostedFunction(cx, cx->names().GetModuleNamespace, UndefinedHandleValue, args,
+                                &result))
+    {
+        return nullptr;
+    }
+
+    return &result.toObject().as<ModuleNamespaceObject>();
+}
+
 DEFINE_GETTER_FUNCTIONS(ModuleObject, namespace_, NamespaceSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, status, StatusSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, evaluationError, EvaluationErrorSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, requestedModules, RequestedModulesSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, importEntries, ImportEntriesSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, localExportEntries, LocalExportEntriesSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, indirectExportEntries, IndirectExportEntriesSlot)
 DEFINE_GETTER_FUNCTIONS(ModuleObject, starExportEntries, StarExportEntriesSlot)
--- a/js/src/builtin/ModuleObject.h
+++ b/js/src/builtin/ModuleObject.h
@@ -312,16 +312,19 @@ class ModuleObject : public NativeObject
     ArrayObject& localExportEntries() const;
     ArrayObject& indirectExportEntries() const;
     ArrayObject& starExportEntries() const;
     IndirectBindingMap& importBindings();
 
     static bool Instantiate(JSContext* cx, HandleModuleObject self);
     static bool Evaluate(JSContext* cx, HandleModuleObject self);
 
+    static ModuleNamespaceObject* GetOrCreateModuleNamespace(JSContext* cx,
+                                                             HandleModuleObject self);
+
     void setMetaObject(JSObject* obj);
 
     void setHostDefinedField(const JS::Value& value);
 
     // For BytecodeEmitter.
     bool noteFunctionDeclaration(JSContext* cx, HandleAtom name, HandleFunction fun);
 
     // For intrinsic_InstantiateModuleFunctionDeclarations.
--- a/js/src/vm/CommonPropertyNames.h
+++ b/js/src/vm/CommonPropertyNames.h
@@ -155,16 +155,17 @@
     macro(Generator, Generator, "Generator") \
     macro(GeneratorFunction, GeneratorFunction, "GeneratorFunction") \
     macro(GeneratorNext, GeneratorNext, "GeneratorNext") \
     macro(GeneratorReturn, GeneratorReturn, "GeneratorReturn") \
     macro(GeneratorThrow, GeneratorThrow, "GeneratorThrow") \
     macro(get, get, "get") \
     macro(GetInternalError, GetInternalError, "GetInternalError") \
     macro(getInternals, getInternals, "getInternals") \
+    macro(GetModuleNamespace, GetModuleNamespace, "GetModuleNamespace") \
     macro(getOwnPropertyDescriptor, getOwnPropertyDescriptor, "getOwnPropertyDescriptor") \
     macro(getOwnPropertyNames, getOwnPropertyNames, "getOwnPropertyNames") \
     macro(getPrefix, getPrefix, "get ") \
     macro(getPropertyDescriptor, getPropertyDescriptor, "getPropertyDescriptor") \
     macro(getPropertySuper, getPropertySuper, "getPropertySuper") \
     macro(getPrototypeOf, getPrototypeOf, "getPrototypeOf") \
     macro(GetTypeError, GetTypeError, "GetTypeError") \
     macro(global, global, "global") \