Skip to content

Commit

Permalink
Bug 1481196 - Compile module scripts to a JSScript like we do for cla…
Browse files Browse the repository at this point in the history
…ssic scripts r=jandem r=baku
  • Loading branch information
jonco3 committed Aug 8, 2018
1 parent 47ff646 commit 9d62550
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 149 deletions.
21 changes: 10 additions & 11 deletions dom/base/nsJSUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ nsJSUtils::CompileModule(JSContext* aCx,
JS::SourceBufferHolder& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions &aCompileOptions,
JS::MutableHandle<JSObject*> aModule)
JS::MutableHandle<JSScript*> aScript)
{
AUTO_PROFILER_LABEL("nsJSUtils::CompileModule", JS);

Expand All @@ -487,7 +487,7 @@ nsJSUtils::CompileModule(JSContext* aCx,

NS_ENSURE_TRUE(xpc::Scriptability::Get(aEvaluationGlobal).Allowed(), NS_OK);

if (!JS::CompileModule(aCx, aCompileOptions, aSrcBuf, aModule)) {
if (!JS::CompileModule(aCx, aCompileOptions, aSrcBuf, aScript)) {
return NS_ERROR_FAILURE;
}

Expand All @@ -496,7 +496,7 @@ nsJSUtils::CompileModule(JSContext* aCx,

nsresult
nsJSUtils::InitModuleSourceElement(JSContext* aCx,
JS::Handle<JSObject*> aModule,
JS::Handle<JSScript*> aScript,
nsIScriptElement* aElement)
{
JS::Rooted<JS::Value> value(aCx);
Expand All @@ -509,16 +509,15 @@ nsJSUtils::InitModuleSourceElement(JSContext* aCx,
MOZ_ASSERT(value.isObject());
JS::Rooted<JSObject*> object(aCx, &value.toObject());

JS::Rooted<JSScript*> script(aCx, JS::GetModuleScript(aModule));
if (!JS::InitScriptSourceElement(aCx, script, object, nullptr)) {
if (!JS::InitScriptSourceElement(aCx, aScript, object, nullptr)) {
return NS_ERROR_FAILURE;
}

return NS_OK;
}

nsresult
nsJSUtils::ModuleInstantiate(JSContext* aCx, JS::Handle<JSObject*> aModule)
nsJSUtils::ModuleInstantiate(JSContext* aCx, JS::Handle<JSScript*> aScript)
{
AUTO_PROFILER_LABEL("nsJSUtils::ModuleInstantiate", JS);

Expand All @@ -527,17 +526,17 @@ nsJSUtils::ModuleInstantiate(JSContext* aCx, JS::Handle<JSObject*> aModule)
MOZ_ASSERT(CycleCollectedJSContext::Get() &&
CycleCollectedJSContext::Get()->MicroTaskLevel());

NS_ENSURE_TRUE(xpc::Scriptability::Get(aModule).Allowed(), NS_OK);
NS_ENSURE_TRUE(xpc::Scriptability::Get(aScript).Allowed(), NS_OK);

if (!JS::ModuleInstantiate(aCx, aModule)) {
if (!JS::ModuleInstantiate(aCx, aScript)) {
return NS_ERROR_FAILURE;
}

return NS_OK;
}

nsresult
nsJSUtils::ModuleEvaluate(JSContext* aCx, JS::Handle<JSObject*> aModule)
nsJSUtils::ModuleEvaluate(JSContext* aCx, JS::Handle<JSScript*> aScript)
{
AUTO_PROFILER_LABEL("nsJSUtils::ModuleEvaluate", JS);

Expand All @@ -546,9 +545,9 @@ nsJSUtils::ModuleEvaluate(JSContext* aCx, JS::Handle<JSObject*> aModule)
MOZ_ASSERT(CycleCollectedJSContext::Get() &&
CycleCollectedJSContext::Get()->MicroTaskLevel());

NS_ENSURE_TRUE(xpc::Scriptability::Get(aModule).Allowed(), NS_OK);
NS_ENSURE_TRUE(xpc::Scriptability::Get(aScript).Allowed(), NS_OK);

if (!JS::ModuleEvaluate(aCx, aModule)) {
if (!JS::ModuleEvaluate(aCx, aScript)) {
return NS_ERROR_FAILURE;
}

Expand Down
8 changes: 4 additions & 4 deletions dom/base/nsJSUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,17 @@ class nsJSUtils
JS::SourceBufferHolder& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions &aCompileOptions,
JS::MutableHandle<JSObject*> aModule);
JS::MutableHandle<JSScript*> aScript);

static nsresult InitModuleSourceElement(JSContext* aCx,
JS::Handle<JSObject*> aModule,
JS::Handle<JSScript*> aScript,
nsIScriptElement* aElement);

static nsresult ModuleInstantiate(JSContext* aCx,
JS::Handle<JSObject*> aModule);
JS::Handle<JSScript*> aScript);

static nsresult ModuleEvaluate(JSContext* aCx,
JS::Handle<JSObject*> aModule);
JS::Handle<JSScript*> aScript);

// Returns false if an exception got thrown on aCx. Passing a null
// aElement is allowed; that wil produce an empty aScopeChain.
Expand Down
36 changes: 18 additions & 18 deletions dom/script/ModuleScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(ModuleScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ModuleScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLoader)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL)
tmp->UnlinkModuleRecord();
tmp->UnlinkScript();
tmp->mParseError.setUndefined();
tmp->mErrorToRethrow.setUndefined();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
Expand All @@ -31,7 +31,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ModuleScript)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ModuleScript)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mModuleRecord)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mScript)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mParseError)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mErrorToRethrow)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
Expand All @@ -46,42 +46,42 @@ ModuleScript::ModuleScript(ScriptLoader* aLoader, nsIURI* aBaseURL)
{
MOZ_ASSERT(mLoader);
MOZ_ASSERT(mBaseURL);
MOZ_ASSERT(!mModuleRecord);
MOZ_ASSERT(!mScript);
MOZ_ASSERT(!HasParseError());
MOZ_ASSERT(!HasErrorToRethrow());
}

void
ModuleScript::UnlinkModuleRecord()
ModuleScript::UnlinkScript()
{
// Remove module's back reference to this object request if present.
if (mModuleRecord) {
MOZ_ASSERT(JS::GetModuleHostDefinedField(mModuleRecord).toPrivate() ==
if (mScript) {
MOZ_ASSERT(JS::GetModuleHostDefinedField(mScript).toPrivate() ==
this);
JS::SetModuleHostDefinedField(mModuleRecord, JS::UndefinedValue());
mModuleRecord = nullptr;
JS::SetModuleHostDefinedField(mScript, JS::UndefinedValue());
mScript = nullptr;
}
}

ModuleScript::~ModuleScript()
{
// The object may be destroyed without being unlinked first.
UnlinkModuleRecord();
UnlinkScript();
DropJSObjects(this);
}

void
ModuleScript::SetModuleRecord(JS::Handle<JSObject*> aModuleRecord)
ModuleScript::SetScript(JS::Handle<JSScript*> aScript)
{
MOZ_ASSERT(!mModuleRecord);
MOZ_ASSERT(!mScript);
MOZ_ASSERT(!HasParseError());
MOZ_ASSERT(!HasErrorToRethrow());

mModuleRecord = aModuleRecord;
mScript = aScript;

// Make module's host defined field point to this module script object.
// This is cleared in the UnlinkModuleRecord().
JS::SetModuleHostDefinedField(mModuleRecord, JS::PrivateValue(this));
// This is cleared in the UnlinkScript().
JS::SetModuleHostDefinedField(mScript, JS::PrivateValue(this));
HoldJSObjects(this);
}

Expand All @@ -92,7 +92,7 @@ ModuleScript::SetParseError(const JS::Value& aError)
MOZ_ASSERT(!HasParseError());
MOZ_ASSERT(!HasErrorToRethrow());

UnlinkModuleRecord();
UnlinkScript();
mParseError = aError;
HoldJSObjects(this);
}
Expand All @@ -103,17 +103,17 @@ ModuleScript::SetErrorToRethrow(const JS::Value& aError)
MOZ_ASSERT(!aError.isUndefined());
MOZ_ASSERT(!HasErrorToRethrow());

// This is only called after SetModuleRecord() or SetParseError() so we don't
// This is only called after SetScript() or SetParseError() so we don't
// need to call HoldJSObjects() here.
MOZ_ASSERT(mModuleRecord || HasParseError());
MOZ_ASSERT(mScript || HasParseError());

mErrorToRethrow = aError;
}

void
ModuleScript::SetSourceElementAssociated()
{
MOZ_ASSERT(mModuleRecord);
MOZ_ASSERT(mScript);
MOZ_ASSERT(!mSourceElementAssociated);

mSourceElementAssociated = true;
Expand Down
8 changes: 4 additions & 4 deletions dom/script/ModuleScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModuleScript final : public nsISupports
{
RefPtr<ScriptLoader> mLoader;
nsCOMPtr<nsIURI> mBaseURL;
JS::Heap<JSObject*> mModuleRecord;
JS::Heap<JSScript*> mScript;
JS::Heap<JS::Value> mParseError;
JS::Heap<JS::Value> mErrorToRethrow;
bool mSourceElementAssociated;
Expand All @@ -36,21 +36,21 @@ class ModuleScript final : public nsISupports
ModuleScript(ScriptLoader* aLoader,
nsIURI* aBaseURL);

void SetModuleRecord(JS::Handle<JSObject*> aModuleRecord);
void SetScript(JS::Handle<JSScript*> aScript);
void SetParseError(const JS::Value& aError);
void SetErrorToRethrow(const JS::Value& aError);
void SetSourceElementAssociated();

ScriptLoader* Loader() const { return mLoader; }
JSObject* ModuleRecord() const { return mModuleRecord; }
JSScript* Script() const { return mScript; }
nsIURI* BaseURL() const { return mBaseURL; }
JS::Value ParseError() const { return mParseError; }
JS::Value ErrorToRethrow() const { return mErrorToRethrow; }
bool HasParseError() const { return !mParseError.isUndefined(); }
bool HasErrorToRethrow() const { return !mErrorToRethrow.isUndefined(); }
bool SourceElementAssociated() const { return mSourceElementAssociated; }

void UnlinkModuleRecord();
void UnlinkScript();
};

} // dom namespace
Expand Down
Loading

0 comments on commit 9d62550

Please sign in to comment.