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/. */
|
|
|
|
|
|
|
|
#include "ModuleScript.h"
|
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// A single module script. May be used to satisfy multiple load requests.
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleScript)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
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();
|
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
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ModuleScript)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ModuleScript)
|
|
|
|
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
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ModuleScript)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ModuleScript)
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
ModuleScript::ModuleScript(ScriptLoader* aLoader, nsIURI* aBaseURL)
|
2017-05-08 09:24:44 +03:00
|
|
|
: mLoader(aLoader),
|
2018-02-15 16:18:59 +03:00
|
|
|
mBaseURL(aBaseURL),
|
|
|
|
mSourceElementAssociated(false)
|
2017-05-08 09:24:44 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mLoader);
|
|
|
|
MOZ_ASSERT(mBaseURL);
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(!mModuleRecord);
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!HasParseError());
|
|
|
|
MOZ_ASSERT(!HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleScript::UnlinkModuleRecord()
|
|
|
|
{
|
|
|
|
// Remove module's back reference to this object request if present.
|
|
|
|
if (mModuleRecord) {
|
|
|
|
MOZ_ASSERT(JS::GetModuleHostDefinedField(mModuleRecord).toPrivate() ==
|
|
|
|
this);
|
|
|
|
JS::SetModuleHostDefinedField(mModuleRecord, JS::UndefinedValue());
|
|
|
|
mModuleRecord = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleScript::~ModuleScript()
|
|
|
|
{
|
|
|
|
// The object may be destroyed without being unlinked first.
|
|
|
|
UnlinkModuleRecord();
|
|
|
|
DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleScript::SetModuleRecord(JS::Handle<JSObject*> aModuleRecord)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mModuleRecord);
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!HasParseError());
|
|
|
|
MOZ_ASSERT(!HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
|
|
|
mModuleRecord = aModuleRecord;
|
2017-05-08 09:24:44 +03:00
|
|
|
|
|
|
|
// Make module's host defined field point to this module script object.
|
|
|
|
// This is cleared in the UnlinkModuleRecord().
|
|
|
|
JS::SetModuleHostDefinedField(mModuleRecord, JS::PrivateValue(this));
|
|
|
|
HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-06 17:54:58 +03:00
|
|
|
ModuleScript::SetParseError(const JS::Value& aError)
|
2017-05-08 09:24:44 +03:00
|
|
|
{
|
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
|
|
|
|
|
|
|
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)
|
2017-12-06 21:14:18 +03:00
|
|
|
{
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!aError.isUndefined());
|
|
|
|
MOZ_ASSERT(!HasErrorToRethrow());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2017-12-21 19:59:12 +03:00
|
|
|
// This is only called after SetModuleRecord() or SetParseError() so we don't
|
|
|
|
// need to call HoldJSObjects() here.
|
|
|
|
MOZ_ASSERT(mModuleRecord || HasParseError());
|
|
|
|
|
2017-12-06 17:54:58 +03:00
|
|
|
mErrorToRethrow = aError;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 16:18:59 +03:00
|
|
|
void
|
|
|
|
ModuleScript::SetSourceElementAssociated()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mModuleRecord);
|
|
|
|
MOZ_ASSERT(!mSourceElementAssociated);
|
|
|
|
|
|
|
|
mSourceElementAssociated = true;
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|