2017-05-08 09:24:44 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2018-12-07 00:52:16 +03:00
|
|
|
#include "LoadedScript.h"
|
2018-12-07 00:52:15 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2018-12-07 00:52:15 +03:00
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2019-05-21 03:57:38 +03:00
|
|
|
#include "js/Modules.h" // JS::{Get,Set}ModulePrivate
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// LoadedScript
|
|
|
|
//////////////////////////////////////////////////////////////
|
2017-05-08 09:24:44 +03:00
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LoadedScript)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(LoadedScript)
|
2017-05-08 09:24:44 +03:00
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(LoadedScript)
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchOptions)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL)
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(LoadedScript)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(LoadedScript)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(LoadedScript)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(LoadedScript)
|
|
|
|
|
2018-12-07 00:52:18 +03:00
|
|
|
LoadedScript::LoadedScript(ScriptKind aKind, ScriptFetchOptions* aFetchOptions,
|
|
|
|
nsIURI* aBaseURL)
|
2018-12-07 00:52:15 +03:00
|
|
|
: mKind(aKind), mFetchOptions(aFetchOptions), mBaseURL(aBaseURL) {
|
|
|
|
MOZ_ASSERT(mFetchOptions);
|
|
|
|
MOZ_ASSERT(mBaseURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadedScript::~LoadedScript() { DropJSObjects(this); }
|
|
|
|
|
2018-12-07 00:52:17 +03:00
|
|
|
void LoadedScript::AssociateWithScript(JSScript* aScript) {
|
2019-01-18 16:37:43 +03:00
|
|
|
// Set a JSScript's private value to point to this object. The JS engine will
|
|
|
|
// increment our reference count by calling HostAddRefTopLevelScript(). This
|
|
|
|
// is decremented by HostReleaseTopLevelScript() below when the JSScript dies.
|
2018-12-07 00:52:17 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(JS::GetScriptPrivate(aScript).isUndefined());
|
|
|
|
JS::SetScriptPrivate(aScript, JS::PrivateValue(this));
|
|
|
|
}
|
|
|
|
|
2019-01-18 16:37:43 +03:00
|
|
|
inline void CheckModuleScriptPrivate(LoadedScript* script,
|
|
|
|
const JS::Value& aPrivate) {
|
2018-12-07 00:52:17 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (script->IsModuleScript()) {
|
|
|
|
JSObject* module = script->AsModuleScript()->mModuleRecord.unbarrieredGet();
|
2019-01-03 13:06:01 +03:00
|
|
|
MOZ_ASSERT_IF(module, JS::GetModulePrivate(module) == aPrivate);
|
2018-12-07 00:52:17 +03:00
|
|
|
}
|
|
|
|
#endif
|
2019-01-18 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostAddRefTopLevelScript(const JS::Value& aPrivate) {
|
|
|
|
// Increment the reference count of a LoadedScript object that is now pointed
|
|
|
|
// to by a JSScript. The reference count is decremented by
|
|
|
|
// HostReleaseTopLevelScript() below.
|
2018-12-07 00:52:17 +03:00
|
|
|
|
2019-01-18 16:37:43 +03:00
|
|
|
auto script = static_cast<LoadedScript*>(aPrivate.toPrivate());
|
|
|
|
CheckModuleScriptPrivate(script, aPrivate);
|
|
|
|
script->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HostReleaseTopLevelScript(const JS::Value& aPrivate) {
|
|
|
|
// Decrement the reference count of a LoadedScript object that was pointed to
|
|
|
|
// by a JSScript. The reference count was originally incremented by
|
|
|
|
// HostAddRefTopLevelScript() above.
|
|
|
|
|
|
|
|
auto script = static_cast<LoadedScript*>(aPrivate.toPrivate());
|
|
|
|
CheckModuleScriptPrivate(script, aPrivate);
|
2018-12-07 00:52:17 +03:00
|
|
|
script->Release();
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// ClassicScript
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2018-12-07 00:52:18 +03:00
|
|
|
ClassicScript::ClassicScript(ScriptFetchOptions* aFetchOptions,
|
2018-12-07 00:52:15 +03:00
|
|
|
nsIURI* aBaseURL)
|
2018-12-07 00:52:18 +03:00
|
|
|
: LoadedScript(ScriptKind::eClassic, aFetchOptions, aBaseURL) {}
|
2018-12-07 00:52:15 +03:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// ModuleScript
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleScript)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(LoadedScript)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ModuleScript)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ModuleScript, LoadedScript)
|
2018-10-04 13:50:55 +03:00
|
|
|
tmp->UnlinkModuleRecord();
|
2017-12-06 17:54:58 +03:00
|
|
|
tmp->mParseError.setUndefined();
|
|
|
|
tmp->mErrorToRethrow.setUndefined();
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ModuleScript, LoadedScript)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(ModuleScript, LoadedScript)
|
2018-10-04 13:50:55 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mModuleRecord)
|
2017-12-06 17:54:58 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mParseError)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mErrorToRethrow)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ModuleScript, LoadedScript)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ModuleScript, LoadedScript)
|
2017-05-08 09:24:44 +03:00
|
|
|
|
2018-12-07 00:52:18 +03:00
|
|
|
ModuleScript::ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
|
|
|
|
: LoadedScript(ScriptKind::eModule, aFetchOptions, aBaseURL),
|
2019-02-01 13:26:14 +03:00
|
|
|
mDebuggerDataInitialized(false) {
|
2018-12-07 00:52:15 +03:00
|
|
|
MOZ_ASSERT(!ModuleRecord());
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!HasParseError());
|
|
|
|
MOZ_ASSERT(!HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:50:55 +03:00
|
|
|
void ModuleScript::UnlinkModuleRecord() {
|
2018-12-07 00:52:15 +03:00
|
|
|
// Remove the module record's pointer to this object if present and
|
|
|
|
// decrement our reference count. The reference is added by
|
|
|
|
// SetModuleRecord() below.
|
2018-10-04 13:50:55 +03:00
|
|
|
if (mModuleRecord) {
|
2018-10-16 15:44:12 +03:00
|
|
|
MOZ_ASSERT(JS::GetModulePrivate(mModuleRecord).toPrivate() == this);
|
|
|
|
JS::SetModulePrivate(mModuleRecord, JS::UndefinedValue());
|
2018-10-04 13:50:55 +03:00
|
|
|
mModuleRecord = nullptr;
|
2017-08-22 12:34:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleScript::~ModuleScript() {
|
|
|
|
// The object may be destroyed without being unlinked first.
|
2018-10-04 13:50:55 +03:00
|
|
|
UnlinkModuleRecord();
|
2017-08-22 12:34:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:50:55 +03:00
|
|
|
void ModuleScript::SetModuleRecord(JS::Handle<JSObject*> aModuleRecord) {
|
|
|
|
MOZ_ASSERT(!mModuleRecord);
|
2018-12-07 00:52:15 +03:00
|
|
|
MOZ_ASSERT_IF(IsModuleScript(), !AsModuleScript()->HasParseError());
|
|
|
|
MOZ_ASSERT_IF(IsModuleScript(), !AsModuleScript()->HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2018-10-04 13:50:55 +03:00
|
|
|
mModuleRecord = aModuleRecord;
|
2017-05-08 09:24:44 +03:00
|
|
|
|
2019-01-18 16:37:43 +03:00
|
|
|
// Make module's host defined field point to this object. The JS engine will
|
|
|
|
// increment our reference count by calling HostAddRefTopLevelScript(). This
|
|
|
|
// is decremented when the field is cleared in UnlinkModuleRecord() above or
|
|
|
|
// when the module record dies.
|
2018-12-07 00:52:15 +03:00
|
|
|
MOZ_ASSERT(JS::GetModulePrivate(mModuleRecord).isUndefined());
|
2018-10-16 15:44:12 +03:00
|
|
|
JS::SetModulePrivate(mModuleRecord, JS::PrivateValue(this));
|
2018-12-07 00:52:15 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
HoldJSObjects(this);
|
2018-12-07 00:52:15 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:54:58 +03:00
|
|
|
void ModuleScript::SetParseError(const JS::Value& aError) {
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(!aError.isUndefined());
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!HasParseError());
|
|
|
|
MOZ_ASSERT(!HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2018-10-04 13:50:55 +03:00
|
|
|
UnlinkModuleRecord();
|
2017-12-06 17:54:58 +03:00
|
|
|
mParseError = aError;
|
2017-08-22 12:34:14 +03:00
|
|
|
HoldJSObjects(this);
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:54:58 +03:00
|
|
|
void ModuleScript::SetErrorToRethrow(const JS::Value& aError) {
|
|
|
|
MOZ_ASSERT(!aError.isUndefined());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2018-10-04 13:50:55 +03:00
|
|
|
// This is only called after SetModuleRecord() or SetParseError() so we don't
|
2017-12-21 19:59:12 +03:00
|
|
|
// need to call HoldJSObjects() here.
|
2018-12-07 00:52:15 +03:00
|
|
|
MOZ_ASSERT(ModuleRecord() || HasParseError());
|
2017-12-21 19:59:12 +03:00
|
|
|
|
2017-12-06 17:54:58 +03:00
|
|
|
mErrorToRethrow = aError;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
2019-02-01 13:26:14 +03:00
|
|
|
void ModuleScript::SetDebuggerDataInitialized() {
|
2018-12-07 00:52:15 +03:00
|
|
|
MOZ_ASSERT(ModuleRecord());
|
2019-02-01 13:26:14 +03:00
|
|
|
MOZ_ASSERT(!mDebuggerDataInitialized);
|
2018-02-15 16:18:59 +03:00
|
|
|
|
2019-02-01 13:26:14 +03:00
|
|
|
mDebuggerDataInitialized = true;
|
2018-02-15 16:18:59 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|