[go: up one dir, main page]
More Web Proxy on the site http://driver.im/

Attachment #8723066: 1-add-js-apis for bug #1240072

View | Details | Raw Unified | Return to bug 1240072
Collapse All | Expand All

(-)a/js/src/builtin/Module.js (+2 lines)
Line     Link Here 
 Lines 236-251   function ModuleDeclarationInstantiation( Link Here 
236
                ThrowSyntaxError(JSMSG_AMBIGUOUS_IMPORT, imp.importName);
236
                ThrowSyntaxError(JSMSG_AMBIGUOUS_IMPORT, imp.importName);
237
            CreateImportBinding(env, imp.localName, resolution.module, resolution.bindingName);
237
            CreateImportBinding(env, imp.localName, resolution.module, resolution.bindingName);
238
        }
238
        }
239
    }
239
    }
240
240
241
    // Step 16.iv
241
    // Step 16.iv
242
    InstantiateModuleFunctionDeclarations(module);
242
    InstantiateModuleFunctionDeclarations(module);
243
}
243
}
244
_SetCanonicalName(ModuleDeclarationInstantiation, "ModuleDeclarationInstantiation");
244
245
245
// 15.2.1.16.5 ModuleEvaluation()
246
// 15.2.1.16.5 ModuleEvaluation()
246
function ModuleEvaluation()
247
function ModuleEvaluation()
247
{
248
{
248
    if (!IsObject(this) || !IsModule(this))
249
    if (!IsObject(this) || !IsModule(this))
249
        return callFunction(CallModuleMethodIfWrapped, this, "ModuleEvaluation");
250
        return callFunction(CallModuleMethodIfWrapped, this, "ModuleEvaluation");
250
251
251
    // Step 1
252
    // Step 1
 Lines 263-278   function ModuleEvaluation() Link Here 
263
    for (let i = 0; i < requestedModules.length; i++) {
264
    for (let i = 0; i < requestedModules.length; i++) {
264
        let required = requestedModules[i];
265
        let required = requestedModules[i];
265
        let requiredModule = HostResolveImportedModule(module, required);
266
        let requiredModule = HostResolveImportedModule(module, required);
266
        callFunction(requiredModule.evaluation, requiredModule);
267
        callFunction(requiredModule.evaluation, requiredModule);
267
    }
268
    }
268
269
269
    return EvaluateModule(module);
270
    return EvaluateModule(module);
270
}
271
}
272
_SetCanonicalName(ModuleEvaluation, "ModuleEvaluation");
271
273
272
function ModuleNamespaceEnumerate()
274
function ModuleNamespaceEnumerate()
273
{
275
{
274
    if (!IsObject(this) || !IsModuleNamespace(this))
276
    if (!IsObject(this) || !IsModuleNamespace(this))
275
        return callFunction(CallModuleMethodIfWrapped, this, "ModuleNamespaceEnumerate");
277
        return callFunction(CallModuleMethodIfWrapped, this, "ModuleNamespaceEnumerate");
276
278
277
    return CreateListIterator(ModuleNamespaceExports(this));
279
    return CreateListIterator(ModuleNamespaceExports(this));
278
}
280
}
(-)a/js/src/builtin/ModuleObject.cpp (-9 / +39 lines)
Line     Link Here 
 Lines 770-785   ModuleObject::script() const Link Here 
770
}
770
}
771
771
772
bool
772
bool
773
ModuleObject::evaluated() const
773
ModuleObject::evaluated() const
774
{
774
{
775
    return getReservedSlot(EvaluatedSlot).toBoolean();
775
    return getReservedSlot(EvaluatedSlot).toBoolean();
776
}
776
}
777
777
778
Value
779
ModuleObject::hostDefinedField() const
780
{
781
    return getReservedSlot(HostDefined);
782
}
783
784
void
785
ModuleObject::setHostDefinedField(JS::Value value)
786
{
787
    setReservedSlot(HostDefined, value);
788
}
789
778
ModuleEnvironmentObject&
790
ModuleEnvironmentObject&
779
ModuleObject::initialEnvironment() const
791
ModuleObject::initialEnvironment() const
780
{
792
{
781
    return getReservedSlot(InitialEnvironmentSlot).toObject().as<ModuleEnvironmentObject>();
793
    return getReservedSlot(InitialEnvironmentSlot).toObject().as<ModuleEnvironmentObject>();
782
}
794
}
783
795
784
JSObject*
796
JSObject*
785
ModuleObject::enclosingStaticScope() const
797
ModuleObject::enclosingStaticScope() const
 Lines 900-915   ModuleObject::createNamespace(JSContext* Link Here 
900
    }
912
    }
901
913
902
    self->initReservedSlot(NamespaceSlot, ObjectValue(*ns));
914
    self->initReservedSlot(NamespaceSlot, ObjectValue(*ns));
903
    self->initReservedSlot(NamespaceExportsSlot, ObjectValue(*exports));
915
    self->initReservedSlot(NamespaceExportsSlot, ObjectValue(*exports));
904
    self->initReservedSlot(NamespaceBindingsSlot, PrivateValue(bindings));
916
    self->initReservedSlot(NamespaceBindingsSlot, PrivateValue(bindings));
905
    return ns;
917
    return ns;
906
}
918
}
907
919
920
static bool
921
InvokeSelfHostedMethod(JSContext* cx, HandleModuleObject self, HandlePropertyName name)
922
{
923
    RootedValue value(cx);
924
    if (!GlobalObject::getSelfHostedFunction(cx, cx->global(), name, name, 0, &value))
925
        return false;
926
927
    InvokeArgs args(cx);
928
    if (!args.init(0))
929
        return false;
930
    args.setCallee(value);
931
    args.setThis(ObjectValue(*self));
932
    return Invoke(cx, args);
933
}
934
935
/* static */ bool
936
ModuleObject::DeclarationInstantiation(JSContext* cx, HandleModuleObject self)
937
{
938
    return InvokeSelfHostedMethod(cx, self, cx->names().ModuleDeclarationInstantiation);
939
}
940
941
/* static */ bool
942
ModuleObject::Evaluation(JSContext* cx, HandleModuleObject self, MutableHandleValue result)
943
{
944
    return InvokeSelfHostedMethod(cx, self, cx->names().ModuleEvaluation);
945
}
946
908
DEFINE_GETTER_FUNCTIONS(ModuleObject, namespace_, NamespaceSlot)
947
DEFINE_GETTER_FUNCTIONS(ModuleObject, namespace_, NamespaceSlot)
909
DEFINE_GETTER_FUNCTIONS(ModuleObject, evaluated, EvaluatedSlot)
948
DEFINE_GETTER_FUNCTIONS(ModuleObject, evaluated, EvaluatedSlot)
910
DEFINE_GETTER_FUNCTIONS(ModuleObject, requestedModules, RequestedModulesSlot)
949
DEFINE_GETTER_FUNCTIONS(ModuleObject, requestedModules, RequestedModulesSlot)
911
DEFINE_GETTER_FUNCTIONS(ModuleObject, importEntries, ImportEntriesSlot)
950
DEFINE_GETTER_FUNCTIONS(ModuleObject, importEntries, ImportEntriesSlot)
912
DEFINE_GETTER_FUNCTIONS(ModuleObject, localExportEntries, LocalExportEntriesSlot)
951
DEFINE_GETTER_FUNCTIONS(ModuleObject, localExportEntries, LocalExportEntriesSlot)
913
DEFINE_GETTER_FUNCTIONS(ModuleObject, indirectExportEntries, IndirectExportEntriesSlot)
952
DEFINE_GETTER_FUNCTIONS(ModuleObject, indirectExportEntries, IndirectExportEntriesSlot)
914
DEFINE_GETTER_FUNCTIONS(ModuleObject, starExportEntries, StarExportEntriesSlot)
953
DEFINE_GETTER_FUNCTIONS(ModuleObject, starExportEntries, StarExportEntriesSlot)
915
954
 Lines 941-965   GlobalObject::initModuleProto(JSContext* Link Here 
941
980
942
    if (!DefinePropertiesAndFunctions(cx, proto, protoAccessors, protoFunctions))
981
    if (!DefinePropertiesAndFunctions(cx, proto, protoAccessors, protoFunctions))
943
        return false;
982
        return false;
944
983
945
    global->setReservedSlot(MODULE_PROTO, ObjectValue(*proto));
984
    global->setReservedSlot(MODULE_PROTO, ObjectValue(*proto));
946
    return true;
985
    return true;
947
}
986
}
948
987
949
bool
950
js::InitModuleClasses(JSContext* cx, HandleObject obj)
951
{
952
    Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>());
953
    return GlobalObject::initModuleProto(cx, global) &&
954
           GlobalObject::initImportEntryProto(cx, global) &&
955
           GlobalObject::initExportEntryProto(cx, global);
956
}
957
958
#undef DEFINE_GETTER_FUNCTIONS
988
#undef DEFINE_GETTER_FUNCTIONS
959
#undef DEFINE_STRING_ACCESSOR_METHOD
989
#undef DEFINE_STRING_ACCESSOR_METHOD
960
#undef DEFINE_ARRAY_SLOT_ACCESSOR
990
#undef DEFINE_ARRAY_SLOT_ACCESSOR
961
991
962
///////////////////////////////////////////////////////////////////////////
992
///////////////////////////////////////////////////////////////////////////
963
// ModuleBuilder
993
// ModuleBuilder
964
994
965
ModuleBuilder::ModuleBuilder(ExclusiveContext* cx, HandleModuleObject module)
995
ModuleBuilder::ModuleBuilder(ExclusiveContext* cx, HandleModuleObject module)
(-)a/js/src/builtin/ModuleObject.h (-2 / +15 lines)
Line     Link Here 
 Lines 203-218   class ModuleObject : public NativeObject Link Here 
203
    enum
203
    enum
204
    {
204
    {
205
        ScriptSlot = 0,
205
        ScriptSlot = 0,
206
        StaticScopeSlot,
206
        StaticScopeSlot,
207
        InitialEnvironmentSlot,
207
        InitialEnvironmentSlot,
208
        EnvironmentSlot,
208
        EnvironmentSlot,
209
        NamespaceSlot,
209
        NamespaceSlot,
210
        EvaluatedSlot,
210
        EvaluatedSlot,
211
        HostDefined,
211
        RequestedModulesSlot,
212
        RequestedModulesSlot,
212
        ImportEntriesSlot,
213
        ImportEntriesSlot,
213
        LocalExportEntriesSlot,
214
        LocalExportEntriesSlot,
214
        IndirectExportEntriesSlot,
215
        IndirectExportEntriesSlot,
215
        StarExportEntriesSlot,
216
        StarExportEntriesSlot,
216
        ImportBindingsSlot,
217
        ImportBindingsSlot,
217
        NamespaceExportsSlot,
218
        NamespaceExportsSlot,
218
        NamespaceBindingsSlot,
219
        NamespaceBindingsSlot,
 Lines 237-269   class ModuleObject : public NativeObject Link Here 
237
    void fixScopesAfterCompartmentMerge(JSContext* cx);
238
    void fixScopesAfterCompartmentMerge(JSContext* cx);
238
239
239
    JSScript* script() const;
240
    JSScript* script() const;
240
    JSObject* enclosingStaticScope() const;
241
    JSObject* enclosingStaticScope() const;
241
    ModuleEnvironmentObject& initialEnvironment() const;
242
    ModuleEnvironmentObject& initialEnvironment() const;
242
    ModuleEnvironmentObject* environment() const;
243
    ModuleEnvironmentObject* environment() const;
243
    ModuleNamespaceObject* namespace_();
244
    ModuleNamespaceObject* namespace_();
244
    bool evaluated() const;
245
    bool evaluated() const;
246
    Value hostDefinedField() const;
245
    ArrayObject& requestedModules() const;
247
    ArrayObject& requestedModules() const;
246
    ArrayObject& importEntries() const;
248
    ArrayObject& importEntries() const;
247
    ArrayObject& localExportEntries() const;
249
    ArrayObject& localExportEntries() const;
248
    ArrayObject& indirectExportEntries() const;
250
    ArrayObject& indirectExportEntries() const;
249
    ArrayObject& starExportEntries() const;
251
    ArrayObject& starExportEntries() const;
250
    IndirectBindingMap& importBindings();
252
    IndirectBindingMap& importBindings();
251
    JSObject* namespaceExports();
253
    JSObject* namespaceExports();
252
    IndirectBindingMap* namespaceBindings();
254
    IndirectBindingMap* namespaceBindings();
253
255
256
    static bool DeclarationInstantiation(JSContext* cx, HandleModuleObject self);
257
    static bool Evaluation(JSContext* cx, HandleModuleObject self, MutableHandleValue result);
258
259
    void setHostDefinedField(JS::Value value);
260
261
    // For intrinsic_CreateModuleEnvironment.
254
    void createEnvironment();
262
    void createEnvironment();
255
263
264
    // For BytecodeEmitter.
256
    bool noteFunctionDeclaration(ExclusiveContext* cx, HandleAtom name, HandleFunction fun);
265
    bool noteFunctionDeclaration(ExclusiveContext* cx, HandleAtom name, HandleFunction fun);
266
267
    // For intrinsic_InstantiateModuleFunctionDeclarations.
257
    static bool instantiateFunctionDeclarations(JSContext* cx, HandleModuleObject self);
268
    static bool instantiateFunctionDeclarations(JSContext* cx, HandleModuleObject self);
258
269
270
    // For intrinsic_SetModuleEvaluated.
259
    void setEvaluated();
271
    void setEvaluated();
272
273
    // For intrinsic_EvaluateModule.
260
    static bool evaluate(JSContext* cx, HandleModuleObject self, MutableHandleValue rval);
274
    static bool evaluate(JSContext* cx, HandleModuleObject self, MutableHandleValue rval);
261
275
276
    // For intrinsic_NewModuleNamespace.
262
    static ModuleNamespaceObject* createNamespace(JSContext* cx, HandleModuleObject self,
277
    static ModuleNamespaceObject* createNamespace(JSContext* cx, HandleModuleObject self,
263
                                                  HandleObject exports);
278
                                                  HandleObject exports);
264
279
265
  private:
280
  private:
266
    static void trace(JSTracer* trc, JSObject* obj);
281
    static void trace(JSTracer* trc, JSObject* obj);
267
    static void finalize(js::FreeOp* fop, JSObject* obj);
282
    static void finalize(js::FreeOp* fop, JSObject* obj);
268
283
269
    bool hasScript() const;
284
    bool hasScript() const;
 Lines 316-333   class MOZ_STACK_CLASS ModuleBuilder Link Here 
316
                               HandleAtom importName);
331
                               HandleAtom importName);
317
332
318
    bool maybeAppendRequestedModule(HandleAtom module);
333
    bool maybeAppendRequestedModule(HandleAtom module);
319
334
320
    template <typename T>
335
    template <typename T>
321
    ArrayObject* createArray(const GCVector<T>& vector);
336
    ArrayObject* createArray(const GCVector<T>& vector);
322
};
337
};
323
338
324
bool InitModuleClasses(JSContext* cx, HandleObject obj);
325
326
} // namespace js
339
} // namespace js
327
340
328
template<>
341
template<>
329
inline bool
342
inline bool
330
JSObject::is<js::ModuleNamespaceObject>() const
343
JSObject::is<js::ModuleNamespaceObject>() const
331
{
344
{
332
    return js::IsDerivedProxyObject(this, &js::ModuleNamespaceObject::proxyHandler);
345
    return js::IsDerivedProxyObject(this, &js::ModuleNamespaceObject::proxyHandler);
333
}
346
}
(-)a/js/src/builtin/ReflectParse.cpp (+4 lines)
Line     Link Here 
 Lines 3742-3757   reflect_parse(JSContext* cx, uint32_t ar Link Here 
3742
    serialize.setParser(&parser);
3742
    serialize.setParser(&parser);
3743
3743
3744
    ParseNode* pn;
3744
    ParseNode* pn;
3745
    if (target == ParseTarget::Script) {
3745
    if (target == ParseTarget::Script) {
3746
        pn = parser.parse();
3746
        pn = parser.parse();
3747
        if (!pn)
3747
        if (!pn)
3748
            return false;
3748
            return false;
3749
    } else {
3749
    } else {
3750
        Rooted<GlobalObject*> global(cx, cx->global());
3751
        if (!GlobalObject::ensureModulePrototypesCreated(cx, global))
3752
            return false;
3753
3750
        Rooted<ModuleObject*> module(cx, ModuleObject::create(cx, nullptr));
3754
        Rooted<ModuleObject*> module(cx, ModuleObject::create(cx, nullptr));
3751
        if (!module)
3755
        if (!module)
3752
            return false;
3756
            return false;
3753
3757
3754
        ModuleBuilder builder(cx, module);
3758
        ModuleBuilder builder(cx, module);
3755
        pn = parser.standaloneModule(module, builder);
3759
        pn = parser.standaloneModule(module, builder);
3756
        if (!pn)
3760
        if (!pn)
3757
            return false;
3761
            return false;
(-)a/js/src/frontend/BytecodeCompiler.cpp (-10 / +21 lines)
Line     Link Here 
 Lines 759-800   frontend::CompileScript(ExclusiveContext Link Here 
759
}
759
}
760
760
761
ModuleObject*
761
ModuleObject*
762
frontend::CompileModule(ExclusiveContext* cx, const ReadOnlyCompileOptions& optionsInput,
762
frontend::CompileModule(ExclusiveContext* cx, const ReadOnlyCompileOptions& optionsInput,
763
                        SourceBufferHolder& srcBuf, LifoAlloc* alloc,
763
                        SourceBufferHolder& srcBuf, LifoAlloc* alloc,
764
                        ScriptSourceObject** sourceObjectOut /* = nullptr */)
764
                        ScriptSourceObject** sourceObjectOut /* = nullptr */)
765
{
765
{
766
    MOZ_ASSERT(srcBuf.get());
766
    MOZ_ASSERT(srcBuf.get());
767
    MOZ_ASSERT(cx->isJSContext() == (alloc == nullptr));
767
    MOZ_ASSERT(alloc);
768
769
    if (!alloc)
770
        alloc = &cx->asJSContext()->tempLifoAlloc();
771
    MOZ_ASSERT_IF(sourceObjectOut, *sourceObjectOut == nullptr);
768
    MOZ_ASSERT_IF(sourceObjectOut, *sourceObjectOut == nullptr);
772
769
773
    CompileOptions options(cx, optionsInput);
770
    CompileOptions options(cx, optionsInput);
774
    options.maybeMakeStrictMode(true); // ES6 10.2.1 Module code is always strict mode code.
771
    options.maybeMakeStrictMode(true); // ES6 10.2.1 Module code is always strict mode code.
775
    options.setIsRunOnce(true);
772
    options.setIsRunOnce(true);
776
773
777
    Rooted<StaticScope*> staticScope(cx, &cx->global()->lexicalScope().staticBlock());
774
    Rooted<StaticScope*> staticScope(cx, &cx->global()->lexicalScope().staticBlock());
778
    BytecodeCompiler compiler(cx, alloc, options, srcBuf, staticScope,
775
    BytecodeCompiler compiler(cx, alloc, options, srcBuf, staticScope,
779
                              TraceLogger_ParserCompileModule);
776
                              TraceLogger_ParserCompileModule);
780
    RootedModuleObject module(cx, compiler.compileModule());
777
    ModuleObject* module = compiler.compileModule();
778
779
    // See the comment about sourceObjectOut above.
780
    if (sourceObjectOut)
781
        *sourceObjectOut = compiler.sourceObjectPtr();
782
783
    return module;
784
}
785
786
ModuleObject*
787
frontend::CompileModule(JSContext* cx, const ReadOnlyCompileOptions& options,
788
                        SourceBufferHolder& srcBuf)
789
{
790
    Rooted<GlobalObject*> global(cx, cx->global());
791
    if (!GlobalObject::ensureModulePrototypesCreated(cx, global))
792
        return nullptr;
793
794
    LifoAlloc* alloc = &cx->asJSContext()->tempLifoAlloc();
795
    RootedModuleObject module(cx, CompileModule(cx, options, srcBuf, alloc));
781
    if (!module)
796
    if (!module)
782
        return nullptr;
797
        return nullptr;
783
798
784
    // This happens in GlobalHelperThreadState::finishModuleParseTask() when a
799
    // This happens in GlobalHelperThreadState::finishModuleParseTask() when a
785
    // module is compiled off main thread.
800
    // module is compiled off main thread.
786
    if (cx->isJSContext() && !ModuleObject::FreezeArrayProperties(cx->asJSContext(), module))
801
    if (!ModuleObject::FreezeArrayProperties(cx->asJSContext(), module))
787
        return nullptr;
802
        return nullptr;
788
803
789
    // See the comment about sourceObjectOut above.
790
    if (sourceObjectOut)
791
        *sourceObjectOut = compiler.sourceObjectPtr();
792
793
    return module;
804
    return module;
794
}
805
}
795
806
796
bool
807
bool
797
frontend::CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const char16_t* chars, size_t length)
808
frontend::CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const char16_t* chars, size_t length)
798
{
809
{
799
    MOZ_ASSERT(cx->compartment() == lazy->functionNonDelazifying()->compartment());
810
    MOZ_ASSERT(cx->compartment() == lazy->functionNonDelazifying()->compartment());
800
811
(-)a/js/src/frontend/BytecodeCompiler.h (-1 / +5 lines)
Line     Link Here 
 Lines 28-45   JSScript* Link Here 
28
CompileScript(ExclusiveContext* cx, LifoAlloc* alloc,
28
CompileScript(ExclusiveContext* cx, LifoAlloc* alloc,
29
              HandleObject scopeChain, Handle<StaticScope*> enclosingStaticScope,
29
              HandleObject scopeChain, Handle<StaticScope*> enclosingStaticScope,
30
              HandleScript evalCaller, const ReadOnlyCompileOptions& options,
30
              HandleScript evalCaller, const ReadOnlyCompileOptions& options,
31
              SourceBufferHolder& srcBuf, JSString* source_ = nullptr,
31
              SourceBufferHolder& srcBuf, JSString* source_ = nullptr,
32
              SourceCompressionTask* extraSct = nullptr,
32
              SourceCompressionTask* extraSct = nullptr,
33
              ScriptSourceObject** sourceObjectOut = nullptr);
33
              ScriptSourceObject** sourceObjectOut = nullptr);
34
34
35
ModuleObject*
35
ModuleObject*
36
CompileModule(JSContext *cx, const ReadOnlyCompileOptions &options,
37
              SourceBufferHolder &srcBuf);
38
39
ModuleObject*
36
CompileModule(ExclusiveContext *cx, const ReadOnlyCompileOptions &options,
40
CompileModule(ExclusiveContext *cx, const ReadOnlyCompileOptions &options,
37
              SourceBufferHolder &srcBuf, LifoAlloc* alloc = nullptr,
41
              SourceBufferHolder &srcBuf, LifoAlloc* alloc,
38
              ScriptSourceObject** sourceObjectOut = nullptr);
42
              ScriptSourceObject** sourceObjectOut = nullptr);
39
43
40
bool
44
bool
41
CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const char16_t* chars, size_t length);
45
CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const char16_t* chars, size_t length);
42
46
43
/*
47
/*
44
 * enclosingStaticScope is a static enclosing scope (e.g. a StaticWithScope).
48
 * enclosingStaticScope is a static enclosing scope (e.g. a StaticWithScope).
45
 * Must be null if the enclosing scope is a global.
49
 * Must be null if the enclosing scope is a global.
(-)a/js/src/jsapi.cpp (+130 lines)
Line     Link Here 
 Lines 4137-4152   JS::FinishOffThreadScript(JSContext* may Link Here 
4137
        }
4137
        }
4138
        return script;
4138
        return script;
4139
    } else {
4139
    } else {
4140
        return HelperThreadState().finishScriptParseTask(maybecx, rt, token);
4140
        return HelperThreadState().finishScriptParseTask(maybecx, rt, token);
4141
    }
4141
    }
4142
}
4142
}
4143
4143
4144
JS_PUBLIC_API(bool)
4144
JS_PUBLIC_API(bool)
4145
JS::CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options,
4146
                           const char16_t* chars, size_t length,
4147
                           OffThreadCompileCallback callback, void* callbackData)
4148
{
4149
    MOZ_ASSERT(CanCompileOffThread(cx, options, length));
4150
    return StartOffThreadParseModule(cx, options, chars, length, callback, callbackData);
4151
}
4152
4153
JS_PUBLIC_API(JSObject*)
4154
JS::FinishOffThreadModule(JSContext* maybecx, JSRuntime* rt, void* token)
4155
{
4156
    MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt));
4157
4158
    if (maybecx) {
4159
        RootedObject module(maybecx);
4160
        {
4161
            AutoLastFrameCheck lfc(maybecx);
4162
            module = HelperThreadState().finishModuleParseTask(maybecx, rt, token);
4163
        }
4164
        return module;
4165
    } else {
4166
        return HelperThreadState().finishModuleParseTask(maybecx, rt, token);
4167
    }
4168
}
4169
4170
JS_PUBLIC_API(bool)
4145
JS_CompileScript(JSContext* cx, const char* ascii, size_t length,
4171
JS_CompileScript(JSContext* cx, const char* ascii, size_t length,
4146
                 const JS::CompileOptions& options, MutableHandleScript script)
4172
                 const JS::CompileOptions& options, MutableHandleScript script)
4147
{
4173
{
4148
    return Compile(cx, options, ascii, length, script);
4174
    return Compile(cx, options, ascii, length, script);
4149
}
4175
}
4150
4176
4151
JS_PUBLIC_API(bool)
4177
JS_PUBLIC_API(bool)
4152
JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length,
4178
JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length,
 Lines 4565-4580   JS::Evaluate(JSContext* cx, AutoObjectVe Link Here 
4565
4591
4566
JS_PUBLIC_API(bool)
4592
JS_PUBLIC_API(bool)
4567
JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
4593
JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
4568
             const char* filename, MutableHandleValue rval)
4594
             const char* filename, MutableHandleValue rval)
4569
{
4595
{
4570
    return ::Evaluate(cx, optionsArg, filename, rval);
4596
    return ::Evaluate(cx, optionsArg, filename, rval);
4571
}
4597
}
4572
4598
4599
JS_PUBLIC_API(JSFunction*)
4600
JS::GetModuleResolveHook(JSContext* cx)
4601
{
4602
    AssertHeapIsIdle(cx);
4603
    CHECK_REQUEST(cx);
4604
    return cx->global()->moduleResolveHook();
4605
}
4606
4607
JS_PUBLIC_API(void)
4608
JS::SetModuleResolveHook(JSContext* cx, HandleFunction func)
4609
{
4610
    AssertHeapIsIdle(cx);
4611
    CHECK_REQUEST(cx);
4612
    assertSameCompartment(cx, func);
4613
    cx->global()->setModuleResolveHook(func);
4614
}
4615
4616
JS_PUBLIC_API(bool)
4617
JS::ParseModule(JSContext* cx, const ReadOnlyCompileOptions& options,
4618
                SourceBufferHolder& srcBuf, JS::MutableHandleObject module)
4619
{
4620
    MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
4621
    AssertHeapIsIdle(cx);
4622
    CHECK_REQUEST(cx);
4623
4624
    AutoLastFrameCheck lfc(cx);
4625
4626
    SourceCompressionTask sct(cx);
4627
    RootedObject obj(cx, frontend::CompileModule(cx, options, srcBuf));
4628
    if (!obj)
4629
        return false;
4630
4631
    module.set(obj);
4632
    return true;
4633
}
4634
4635
JS_PUBLIC_API(void)
4636
JS::SetModuleHostDefinedField(JSObject* module, JS::Value value)
4637
{
4638
    MOZ_ASSERT(module->is<ModuleObject>());
4639
    module->as<ModuleObject>().setHostDefinedField(value);
4640
}
4641
4642
JS_PUBLIC_API(JS::Value)
4643
JS::GetModuleHostDefinedField(JSObject* module)
4644
{
4645
    MOZ_ASSERT(module->is<ModuleObject>());
4646
    return module->as<ModuleObject>().hostDefinedField();
4647
}
4648
4649
JS_PUBLIC_API(bool)
4650
JS::ModuleDeclarationInstantiation(JSContext* cx, JS::HandleObject moduleArg)
4651
{
4652
    AssertHeapIsIdle(cx);
4653
    CHECK_REQUEST(cx);
4654
    assertSameCompartment(cx, moduleArg);
4655
    if (!moduleArg->is<ModuleObject>())
4656
        return false;
4657
4658
    RootedModuleObject module(cx, &moduleArg->as<ModuleObject>());
4659
    return ModuleObject::DeclarationInstantiation(cx, module);
4660
}
4661
4662
JS_PUBLIC_API(bool)
4663
JS::ModuleEvaluation(JSContext* cx, JS::HandleObject moduleArg)
4664
{
4665
    AssertHeapIsIdle(cx);
4666
    CHECK_REQUEST(cx);
4667
    assertSameCompartment(cx, moduleArg);
4668
    if (!moduleArg->is<ModuleObject>())
4669
        return false;
4670
4671
    RootedModuleObject module(cx, &moduleArg->as<ModuleObject>());
4672
    RootedValue result(cx);
4673
    return ModuleObject::Evaluation(cx, module, &result);
4674
}
4675
4676
JS_PUBLIC_API(bool)
4677
JS::GetRequestedModules(JSContext* cx, JS::HandleObject moduleArg, JS::MutableHandleObject rval)
4678
{
4679
    AssertHeapIsIdle(cx);
4680
    CHECK_REQUEST(cx);
4681
    assertSameCompartment(cx, moduleArg);
4682
    if (!moduleArg->is<ModuleObject>())
4683
        return false;
4684
4685
    RootedModuleObject module(cx, &moduleArg->as<ModuleObject>());
4686
    rval.set(&module->requestedModules());
4687
    return true;
4688
}
4689
4690
JS_PUBLIC_API(JSScript*)
4691
JS::GetModuleScript(JSContext* cx, JS::HandleObject moduleArg)
4692
{
4693
    AssertHeapIsIdle(cx);
4694
    CHECK_REQUEST(cx);
4695
    assertSameCompartment(cx, moduleArg);
4696
    if (!moduleArg->is<ModuleObject>())
4697
        return nullptr;
4698
4699
    RootedModuleObject module(cx, &moduleArg->as<ModuleObject>());
4700
    return module->script();
4701
}
4702
4573
static JSObject*
4703
static JSObject*
4574
JS_NewHelper(JSContext* cx, HandleObject ctor, const JS::HandleValueArray& inputArgs)
4704
JS_NewHelper(JSContext* cx, HandleObject ctor, const JS::HandleValueArray& inputArgs)
4575
{
4705
{
4576
    AssertHeapIsIdle(cx);
4706
    AssertHeapIsIdle(cx);
4577
    CHECK_REQUEST(cx);
4707
    CHECK_REQUEST(cx);
4578
    assertSameCompartment(cx, ctor, inputArgs);
4708
    assertSameCompartment(cx, ctor, inputArgs);
4579
4709
4580
    RootedValue ctorVal(cx, ObjectValue(*ctor));
4710
    RootedValue ctorVal(cx, ObjectValue(*ctor));
(-)a/js/src/jsapi.h (+92 lines)
Line     Link Here 
 Lines 757-772   class MOZ_STACK_CLASS SourceBufferHolder Link Here 
757
        static const char16_t NullChar_ = 0;
757
        static const char16_t NullChar_ = 0;
758
        if (!get()) {
758
        if (!get()) {
759
            data_ = &NullChar_;
759
            data_ = &NullChar_;
760
            length_ = 0;
760
            length_ = 0;
761
            ownsChars_ = false;
761
            ownsChars_ = false;
762
        }
762
        }
763
    }
763
    }
764
764
765
    SourceBufferHolder(SourceBufferHolder&& other)
766
      : data_(other.data_),
767
        length_(other.length_),
768
        ownsChars_(other.ownsChars_)
769
    {
770
        other.data_ = nullptr;
771
        other.length_ = 0;
772
        other.ownsChars_ = false;
773
    }
774
765
    ~SourceBufferHolder() {
775
    ~SourceBufferHolder() {
766
        if (ownsChars_)
776
        if (ownsChars_)
767
            js_free(const_cast<char16_t*>(data_));
777
            js_free(const_cast<char16_t*>(data_));
768
    }
778
    }
769
779
770
    // Access the underlying source buffer without affecting ownership.
780
    // Access the underlying source buffer without affecting ownership.
771
    const char16_t* get() const { return data_; }
781
    const char16_t* get() const { return data_; }
772
782
 Lines 4112-4127   CanCompileOffThread(JSContext* cx, const Link Here 
4112
extern JS_PUBLIC_API(bool)
4122
extern JS_PUBLIC_API(bool)
4113
CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options,
4123
CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options,
4114
                 const char16_t* chars, size_t length,
4124
                 const char16_t* chars, size_t length,
4115
                 OffThreadCompileCallback callback, void* callbackData);
4125
                 OffThreadCompileCallback callback, void* callbackData);
4116
4126
4117
extern JS_PUBLIC_API(JSScript*)
4127
extern JS_PUBLIC_API(JSScript*)
4118
FinishOffThreadScript(JSContext* maybecx, JSRuntime* rt, void* token);
4128
FinishOffThreadScript(JSContext* maybecx, JSRuntime* rt, void* token);
4119
4129
4130
extern JS_PUBLIC_API(bool)
4131
CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options,
4132
                       const char16_t* chars, size_t length,
4133
                       OffThreadCompileCallback callback, void* callbackData);
4134
4135
extern JS_PUBLIC_API(JSObject*)
4136
FinishOffThreadModule(JSContext* maybecx, JSRuntime* rt, void* token);
4137
4120
/**
4138
/**
4121
 * Compile a function with scopeChain plus the global as its scope chain.
4139
 * Compile a function with scopeChain plus the global as its scope chain.
4122
 * scopeChain must contain objects in the current compartment of cx.  The actual
4140
 * scopeChain must contain objects in the current compartment of cx.  The actual
4123
 * scope chain used for the function will consist of With wrappers for those
4141
 * scope chain used for the function will consist of With wrappers for those
4124
 * objects, followed by the current global of the compartment cx is in.  This
4142
 * objects, followed by the current global of the compartment cx is in.  This
4125
 * global must not be explicitly included in the scope chain.
4143
 * global must not be explicitly included in the scope chain.
4126
 */
4144
 */
4127
extern JS_PUBLIC_API(bool)
4145
extern JS_PUBLIC_API(bool)
 Lines 4255-4270   Evaluate(JSContext* cx, const ReadOnlyCo Link Here 
4255
4273
4256
/**
4274
/**
4257
 * Evaluate the given file in the scope of the current global of cx.
4275
 * Evaluate the given file in the scope of the current global of cx.
4258
 */
4276
 */
4259
extern JS_PUBLIC_API(bool)
4277
extern JS_PUBLIC_API(bool)
4260
Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options,
4278
Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options,
4261
         const char* filename, JS::MutableHandleValue rval);
4279
         const char* filename, JS::MutableHandleValue rval);
4262
4280
4281
/**
4282
 * Get the HostResolveImportedModule hook for a global.
4283
 */
4284
extern JS_PUBLIC_API(JSFunction*)
4285
GetModuleResolveHook(JSContext* cx);
4286
4287
/**
4288
 * Set the HostResolveImportedModule hook for a global to the given function.
4289
 */
4290
extern JS_PUBLIC_API(void)
4291
SetModuleResolveHook(JSContext* cx, JS::HandleFunction func);
4292
4293
/**
4294
 * Parse the given source buffer as a module in the scope of the current global
4295
 * of cx and return a source text module record.
4296
 */
4297
extern JS_PUBLIC_API(bool)
4298
ParseModule(JSContext* cx, const ReadOnlyCompileOptions& options,
4299
            SourceBufferHolder& srcBuf, JS::MutableHandleObject moduleRecord);
4300
4301
/**
4302
 * Set the [[HostDefined]] field of a source text module record to the given
4303
 * value.
4304
 */
4305
extern JS_PUBLIC_API(void)
4306
SetModuleHostDefinedField(JSObject* module, JS::Value value);
4307
4308
/**
4309
 * Get the [[HostDefined]] field of a source text module record.
4310
 */
4311
extern JS_PUBLIC_API(JS::Value)
4312
GetModuleHostDefinedField(JSObject* module);
4313
4314
/*
4315
 * Perform the ModuleDeclarationInstantiation operation on on the give source
4316
 * text module record.
4317
 *
4318
 * This transitively resolves all module dependencies (calling the
4319
 * HostResolveImportedModule hook) and initializes the environment record for
4320
 * the module.
4321
 */
4322
extern JS_PUBLIC_API(bool)
4323
ModuleDeclarationInstantiation(JSContext* cx, JS::HandleObject moduleRecord);
4324
4325
/*
4326
 * Perform the ModuleEvaluation operation on on the give source text module
4327
 * record.
4328
 *
4329
 * This does nothing if this module has already been evaluated. Otherwise, it
4330
 * transitively evaluates all dependences of this module and then evaluates this
4331
 * module.
4332
 *
4333
 * ModuleDeclarationInstantiation must have completed prior to calling this.
4334
 */
4335
extern JS_PUBLIC_API(bool)
4336
ModuleEvaluation(JSContext* cx, JS::HandleObject moduleRecord);
4337
4338
/*
4339
 * Get a list of the module specifiers used by a source text module
4340
 * record to request importation of modules.
4341
 *
4342
 * The result is a JavaScript array of string values.  ForOfIterator can be used
4343
 * to extract the individual strings.
4344
 */
4345
extern JS_PUBLIC_API(bool)
4346
GetRequestedModules(JSContext* cx, JS::HandleObject moduleRecord,
4347
                    JS::MutableHandleObject rval);
4348
4349
/*
4350
 * Get a the script associated with a module.
4351
 */
4352
extern JS_PUBLIC_API(JSScript*)
4353
GetModuleScript(JSContext* cx, JS::HandleObject moduleRecord);
4354
4263
} /* namespace JS */
4355
} /* namespace JS */
4264
4356
4265
extern JS_PUBLIC_API(bool)
4357
extern JS_PUBLIC_API(bool)
4266
JS_CheckForInterrupt(JSContext* cx);
4358
JS_CheckForInterrupt(JSContext* cx);
4267
4359
4268
/*
4360
/*
4269
 * These functions allow setting an interrupt callback that will be called
4361
 * These functions allow setting an interrupt callback that will be called
4270
 * from the JS thread some time after any thread triggered the callback using
4362
 * from the JS thread some time after any thread triggered the callback using
(-)a/js/src/shell/js.cpp (-22 / +3 lines)
Line     Link Here 
 Lines 3782-3813   runOffThreadScript(JSContext* cx, unsign Link Here 
3782
    RootedScript script(cx, JS::FinishOffThreadScript(cx, rt, token));
3782
    RootedScript script(cx, JS::FinishOffThreadScript(cx, rt, token));
3783
    if (!script)
3783
    if (!script)
3784
        return false;
3784
        return false;
3785
3785
3786
    return JS_ExecuteScript(cx, script, args.rval());
3786
    return JS_ExecuteScript(cx, script, args.rval());
3787
}
3787
}
3788
3788
3789
static bool
3789
static bool
3790
CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options,
3791
                       const char16_t* chars, size_t length,
3792
                       JS::OffThreadCompileCallback callback, void* callbackData)
3793
{
3794
    MOZ_ASSERT(JS::CanCompileOffThread(cx, options, length));
3795
    return StartOffThreadParseModule(cx, options, chars, length, callback, callbackData);
3796
}
3797
3798
static JSObject*
3799
FinishOffThreadModule(JSContext* maybecx, JSRuntime* rt, void* token)
3800
{
3801
    MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt));
3802
    return HelperThreadState().finishModuleParseTask(maybecx, rt, token);
3803
}
3804
3805
static bool
3806
OffThreadCompileModule(JSContext* cx, unsigned argc, Value* vp)
3790
OffThreadCompileModule(JSContext* cx, unsigned argc, Value* vp)
3807
{
3791
{
3808
    CallArgs args = CallArgsFromVp(argc, vp);
3792
    CallArgs args = CallArgsFromVp(argc, vp);
3809
3793
3810
    if (args.length() != 1 || !args[0].isString()) {
3794
    if (args.length() != 1 || !args[0].isString()) {
3811
        JS_ReportErrorNumber(cx, my_GetErrorMessage, nullptr, JSSMSG_INVALID_ARGS,
3795
        JS_ReportErrorNumber(cx, my_GetErrorMessage, nullptr, JSSMSG_INVALID_ARGS,
3812
                             "offThreadCompileModule");
3796
                             "offThreadCompileModule");
3813
        return false;
3797
        return false;
 Lines 3850-3867   OffThreadCompileModule(JSContext* cx, un Link Here 
3850
    }
3834
    }
3851
3835
3852
    if (!offThreadState.startIfIdle(cx, ownedChars)) {
3836
    if (!offThreadState.startIfIdle(cx, ownedChars)) {
3853
        JS_ReportError(cx, "called offThreadCompileModule without receiving prior off-thread "
3837
        JS_ReportError(cx, "called offThreadCompileModule without receiving prior off-thread "
3854
                       "compilation");
3838
                       "compilation");
3855
        return false;
3839
        return false;
3856
    }
3840
    }
3857
3841
3858
    if (!CompileOffThreadModule(cx, options, chars, length,
3842
    if (!JS::CompileOffThreadModule(cx, options, chars, length,
3859
                                OffThreadCompileScriptCallback, nullptr))
3843
                                    OffThreadCompileScriptCallback, nullptr))
3860
    {
3844
    {
3861
        offThreadState.abandon(cx);
3845
        offThreadState.abandon(cx);
3862
        return false;
3846
        return false;
3863
    }
3847
    }
3864
3848
3865
    args.rval().setUndefined();
3849
    args.rval().setUndefined();
3866
    return true;
3850
    return true;
3867
}
3851
}
 Lines 3876-3892   FinishOffThreadModule(JSContext* cx, uns Link Here 
3876
        gc::AutoFinishGC finishgc(rt);
3860
        gc::AutoFinishGC finishgc(rt);
3877
3861
3878
    void* token = offThreadState.waitUntilDone(cx);
3862
    void* token = offThreadState.waitUntilDone(cx);
3879
    if (!token) {
3863
    if (!token) {
3880
        JS_ReportError(cx, "called finishOffThreadModule when no compilation is pending");
3864
        JS_ReportError(cx, "called finishOffThreadModule when no compilation is pending");
3881
        return false;
3865
        return false;
3882
    }
3866
    }
3883
3867
3884
    RootedObject module(cx, FinishOffThreadModule(cx, rt, token));
3868
    RootedObject module(cx, JS::FinishOffThreadModule(cx, rt, token));
3885
    if (!module)
3869
    if (!module)
3886
        return false;
3870
        return false;
3887
3871
3888
    args.rval().setObject(*module);
3872
    args.rval().setObject(*module);
3889
    return true;
3873
    return true;
3890
}
3874
}
3891
3875
3892
struct MOZ_RAII FreeOnReturn
3876
struct MOZ_RAII FreeOnReturn
 Lines 6417-6435   NewGlobalObject(JSContext* cx, JS::Compa Link Here 
6417
6401
6418
        RootedObject domProto(cx, JS_InitClass(cx, glob, nullptr, &dom_class, dom_constructor,
6402
        RootedObject domProto(cx, JS_InitClass(cx, glob, nullptr, &dom_class, dom_constructor,
6419
                                               0, dom_props, dom_methods, nullptr, nullptr));
6403
                                               0, dom_props, dom_methods, nullptr, nullptr));
6420
        if (!domProto)
6404
        if (!domProto)
6421
            return nullptr;
6405
            return nullptr;
6422
6406
6423
        /* Initialize FakeDOMObject.prototype */
6407
        /* Initialize FakeDOMObject.prototype */
6424
        InitDOMObject(domProto);
6408
        InitDOMObject(domProto);
6425
6426
        if (!js::InitModuleClasses(cx, glob))
6427
            return nullptr;
6428
    }
6409
    }
6429
6410
6430
    JS_FireOnNewGlobalObject(cx, glob);
6411
    JS_FireOnNewGlobalObject(cx, glob);
6431
6412
6432
    return glob;
6413
    return glob;
6433
}
6414
}
6434
6415
6435
static bool
6416
static bool
(-)a/js/src/vm/CommonPropertyNames.h (+2 lines)
Line     Link Here 
 Lines 160-175    Link Here 
160
    macro(maximumSignificantDigits, maximumSignificantDigits, "maximumSignificantDigits") \
160
    macro(maximumSignificantDigits, maximumSignificantDigits, "maximumSignificantDigits") \
161
    macro(message, message, "message") \
161
    macro(message, message, "message") \
162
    macro(minimumFractionDigits, minimumFractionDigits, "minimumFractionDigits") \
162
    macro(minimumFractionDigits, minimumFractionDigits, "minimumFractionDigits") \
163
    macro(minimumIntegerDigits, minimumIntegerDigits, "minimumIntegerDigits") \
163
    macro(minimumIntegerDigits, minimumIntegerDigits, "minimumIntegerDigits") \
164
    macro(minimumSignificantDigits, minimumSignificantDigits, "minimumSignificantDigits") \
164
    macro(minimumSignificantDigits, minimumSignificantDigits, "minimumSignificantDigits") \
165
    macro(minute, minute, "minute") \
165
    macro(minute, minute, "minute") \
166
    macro(missingArguments, missingArguments, "missingArguments") \
166
    macro(missingArguments, missingArguments, "missingArguments") \
167
    macro(module, module, "module") \
167
    macro(module, module, "module") \
168
    macro(ModuleDeclarationInstantiation, ModuleDeclarationInstantiation, "ModuleDeclarationInstantiation") \
169
    macro(ModuleEvaluation, ModuleEvaluation, "ModuleEvaluation") \
168
    macro(month, month, "month") \
170
    macro(month, month, "month") \
169
    macro(multiline, multiline, "multiline") \
171
    macro(multiline, multiline, "multiline") \
170
    macro(name, name, "name") \
172
    macro(name, name, "name") \
171
    macro(NaN, NaN, "NaN") \
173
    macro(NaN, NaN, "NaN") \
172
    macro(new, new_, "new") \
174
    macro(new, new_, "new") \
173
    macro(next, next, "next") \
175
    macro(next, next, "next") \
174
    macro(NFC, NFC, "NFC") \
176
    macro(NFC, NFC, "NFC") \
175
    macro(NFD, NFD, "NFD") \
177
    macro(NFD, NFD, "NFD") \
(-)a/js/src/vm/GlobalObject.cpp (-10 / +3 lines)
Line     Link Here 
 Lines 758-776   GlobalObject::addIntrinsicValue(JSContex Link Here 
758
758
759
    holder->setSlot(shape->slot(), value);
759
    holder->setSlot(shape->slot(), value);
760
    return true;
760
    return true;
761
}
761
}
762
762
763
/* static */ bool
763
/* static */ bool
764
GlobalObject::ensureModulePrototypesCreated(JSContext *cx, Handle<GlobalObject*> global)
764
GlobalObject::ensureModulePrototypesCreated(JSContext *cx, Handle<GlobalObject*> global)
765
{
765
{
766
    if (global->getSlot(MODULE_PROTO).isUndefined()) {
766
    return global->getOrCreateObject(cx, MODULE_PROTO, initModuleProto) &&
767
        MOZ_ASSERT(global->getSlot(IMPORT_ENTRY_PROTO).isUndefined() &&
767
           global->getOrCreateObject(cx, IMPORT_ENTRY_PROTO, initImportEntryProto) &&
768
                   global->getSlot(EXPORT_ENTRY_PROTO).isUndefined());
768
           global->getOrCreateObject(cx, EXPORT_ENTRY_PROTO, initExportEntryProto);
769
        if (!js::InitModuleClasses(cx, global))
770
            return false;
771
    }
772
    MOZ_ASSERT(global->getSlot(MODULE_PROTO).isObject() &&
773
               global->getSlot(IMPORT_ENTRY_PROTO).isObject() &&
774
               global->getSlot(EXPORT_ENTRY_PROTO).isObject());
775
    return true;
776
}
769
}

Return to bug 1240072