From 22f300f7c431bbf4de20437d2ebd7bff38284efb Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Thu, 27 Aug 2020 09:14:33 +0000 Subject: Issue #618 - Add JS API to associate scripts with DOM elements after compilation Ref BZ 1342416 --- js/src/jsapi.cpp | 11 +++++++++++ js/src/jsapi.h | 9 +++++++++ js/src/jsscript.cpp | 35 +++++++++++++++++++++++------------ js/src/jsscript.h | 2 ++ 4 files changed, 45 insertions(+), 12 deletions(-) (limited to 'js/src') diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 69a3ba2ac2..dd1b254864 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4432,6 +4432,17 @@ JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain, chars.get(), length, fun); } +JS_PUBLIC_API(bool) +JS::InitScriptSourceElement(JSContext* cx, HandleScript script, + HandleObject element, HandleString elementAttrName) +{ + MOZ_ASSERT(cx); + MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); + + RootedScriptSource sso(cx, &script->sourceObject()->as()); + return ScriptSourceObject::initElementProperties(cx, sso, element, elementAttrName); +} + JS_PUBLIC_API(JSString*) JS_DecompileScript(JSContext* cx, HandleScript script, const char* name, unsigned indent) { diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 5cdfd958e7..476bc03b9b 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -4212,6 +4212,15 @@ CompileFunction(JSContext* cx, AutoObjectVector& envChain, const char* name, unsigned nargs, const char* const* argnames, const char* bytes, size_t length, JS::MutableHandleFunction fun); +/* + * Associate an element wrapper and attribute name with a previously compiled + * script, for debugging purposes. Calling this function is optional, but should + * be done before script execution if it is required. + */ +extern JS_PUBLIC_API(bool) +InitScriptSourceElement(JSContext* cx, HandleScript script, + HandleObject element, HandleString elementAttrName = nullptr); + } /* namespace JS */ extern JS_PUBLIC_API(JSString*) diff --git a/js/src/jsscript.cpp b/js/src/jsscript.cpp index bdd411d043..ba2e540e41 100644 --- a/js/src/jsscript.cpp +++ b/js/src/jsscript.cpp @@ -1407,19 +1407,10 @@ ScriptSourceObject::initFromOptions(JSContext* cx, HandleScriptSource source, MOZ_ASSERT(source->getReservedSlot(ELEMENT_PROPERTY_SLOT).isMagic(JS_GENERIC_MAGIC)); MOZ_ASSERT(source->getReservedSlot(INTRODUCTION_SCRIPT_SLOT).isMagic(JS_GENERIC_MAGIC)); - RootedValue element(cx, ObjectOrNullValue(options.element())); - if (!cx->compartment()->wrap(cx, &element)) + RootedObject element(cx, options.element()); + RootedString elementAttributeName(cx, options.elementAttributeName()); + if (!initElementProperties(cx, source, element, elementAttributeName)) return false; - source->setReservedSlot(ELEMENT_SLOT, element); - - RootedValue elementAttributeName(cx); - if (options.elementAttributeName()) - elementAttributeName = StringValue(options.elementAttributeName()); - else - elementAttributeName = UndefinedValue(); - if (!cx->compartment()->wrap(cx, &elementAttributeName)) - return false; - source->setReservedSlot(ELEMENT_PROPERTY_SLOT, elementAttributeName); // There is no equivalent of cross-compartment wrappers for scripts. If the // introduction script and ScriptSourceObject are in different compartments, @@ -1437,6 +1428,26 @@ ScriptSourceObject::initFromOptions(JSContext* cx, HandleScriptSource source, return true; } +bool +ScriptSourceObject::initElementProperties(JSContext* cx, HandleScriptSource source, + HandleObject element, HandleString elementAttrName) +{ + RootedValue elementValue(cx, ObjectOrNullValue(element)); + if (!cx->compartment()->wrap(cx, &elementValue)) + return false; + + RootedValue nameValue(cx); + if (elementAttrName) + nameValue = StringValue(elementAttrName); + if (!cx->compartment()->wrap(cx, &nameValue)) + return false; + + source->setReservedSlot(ELEMENT_SLOT, elementValue); + source->setReservedSlot(ELEMENT_PROPERTY_SLOT, nameValue); + + return true; +} + /* static */ bool JSScript::loadSource(JSContext* cx, ScriptSource* ss, bool* worked) { diff --git a/js/src/jsscript.h b/js/src/jsscript.h index c19fbfc71d..74803c0f68 100644 --- a/js/src/jsscript.h +++ b/js/src/jsscript.h @@ -620,6 +620,8 @@ class ScriptSourceObject : public NativeObject // are provided by |options|, re-wrapping as necessary. static bool initFromOptions(JSContext* cx, HandleScriptSource source, const ReadOnlyCompileOptions& options); + static bool initElementProperties(JSContext* cx, HandleScriptSource source, + HandleObject element, HandleString elementAttrName); ScriptSource* source() const { return static_cast(getReservedSlot(SOURCE_SLOT).toPrivate()); -- cgit v1.2.3 From e4fb3f48aee341424fac593abe2e24a791a8da00 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 27 Aug 2020 19:25:46 +0000 Subject: Revert "Issue #618 - Add JS API to associate scripts with DOM elements after compilation" This reverts commit 22f300f7c431bbf4de20437d2ebd7bff38284efb. --- js/src/jsapi.cpp | 11 ----------- js/src/jsapi.h | 9 --------- js/src/jsscript.cpp | 35 ++++++++++++----------------------- js/src/jsscript.h | 2 -- 4 files changed, 12 insertions(+), 45 deletions(-) (limited to 'js/src') diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index dd1b254864..69a3ba2ac2 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4432,17 +4432,6 @@ JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain, chars.get(), length, fun); } -JS_PUBLIC_API(bool) -JS::InitScriptSourceElement(JSContext* cx, HandleScript script, - HandleObject element, HandleString elementAttrName) -{ - MOZ_ASSERT(cx); - MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); - - RootedScriptSource sso(cx, &script->sourceObject()->as()); - return ScriptSourceObject::initElementProperties(cx, sso, element, elementAttrName); -} - JS_PUBLIC_API(JSString*) JS_DecompileScript(JSContext* cx, HandleScript script, const char* name, unsigned indent) { diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 476bc03b9b..5cdfd958e7 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -4212,15 +4212,6 @@ CompileFunction(JSContext* cx, AutoObjectVector& envChain, const char* name, unsigned nargs, const char* const* argnames, const char* bytes, size_t length, JS::MutableHandleFunction fun); -/* - * Associate an element wrapper and attribute name with a previously compiled - * script, for debugging purposes. Calling this function is optional, but should - * be done before script execution if it is required. - */ -extern JS_PUBLIC_API(bool) -InitScriptSourceElement(JSContext* cx, HandleScript script, - HandleObject element, HandleString elementAttrName = nullptr); - } /* namespace JS */ extern JS_PUBLIC_API(JSString*) diff --git a/js/src/jsscript.cpp b/js/src/jsscript.cpp index ba2e540e41..bdd411d043 100644 --- a/js/src/jsscript.cpp +++ b/js/src/jsscript.cpp @@ -1407,10 +1407,19 @@ ScriptSourceObject::initFromOptions(JSContext* cx, HandleScriptSource source, MOZ_ASSERT(source->getReservedSlot(ELEMENT_PROPERTY_SLOT).isMagic(JS_GENERIC_MAGIC)); MOZ_ASSERT(source->getReservedSlot(INTRODUCTION_SCRIPT_SLOT).isMagic(JS_GENERIC_MAGIC)); - RootedObject element(cx, options.element()); - RootedString elementAttributeName(cx, options.elementAttributeName()); - if (!initElementProperties(cx, source, element, elementAttributeName)) + RootedValue element(cx, ObjectOrNullValue(options.element())); + if (!cx->compartment()->wrap(cx, &element)) return false; + source->setReservedSlot(ELEMENT_SLOT, element); + + RootedValue elementAttributeName(cx); + if (options.elementAttributeName()) + elementAttributeName = StringValue(options.elementAttributeName()); + else + elementAttributeName = UndefinedValue(); + if (!cx->compartment()->wrap(cx, &elementAttributeName)) + return false; + source->setReservedSlot(ELEMENT_PROPERTY_SLOT, elementAttributeName); // There is no equivalent of cross-compartment wrappers for scripts. If the // introduction script and ScriptSourceObject are in different compartments, @@ -1428,26 +1437,6 @@ ScriptSourceObject::initFromOptions(JSContext* cx, HandleScriptSource source, return true; } -bool -ScriptSourceObject::initElementProperties(JSContext* cx, HandleScriptSource source, - HandleObject element, HandleString elementAttrName) -{ - RootedValue elementValue(cx, ObjectOrNullValue(element)); - if (!cx->compartment()->wrap(cx, &elementValue)) - return false; - - RootedValue nameValue(cx); - if (elementAttrName) - nameValue = StringValue(elementAttrName); - if (!cx->compartment()->wrap(cx, &nameValue)) - return false; - - source->setReservedSlot(ELEMENT_SLOT, elementValue); - source->setReservedSlot(ELEMENT_PROPERTY_SLOT, nameValue); - - return true; -} - /* static */ bool JSScript::loadSource(JSContext* cx, ScriptSource* ss, bool* worked) { diff --git a/js/src/jsscript.h b/js/src/jsscript.h index 74803c0f68..c19fbfc71d 100644 --- a/js/src/jsscript.h +++ b/js/src/jsscript.h @@ -620,8 +620,6 @@ class ScriptSourceObject : public NativeObject // are provided by |options|, re-wrapping as necessary. static bool initFromOptions(JSContext* cx, HandleScriptSource source, const ReadOnlyCompileOptions& options); - static bool initElementProperties(JSContext* cx, HandleScriptSource source, - HandleObject element, HandleString elementAttrName); ScriptSource* source() const { return static_cast(getReservedSlot(SOURCE_SLOT).toPrivate()); -- cgit v1.2.3 From df55ce90372c71ec9cb186677044aacc96c87187 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Tue, 8 Sep 2020 11:00:27 +0000 Subject: Issue #618 - Clear the module map when changing a Document's global and add release build assertions for mismatching compartments. --- js/src/jsapi.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'js/src') diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 69a3ba2ac2..c0f0e61cc6 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4708,7 +4708,7 @@ JS::ModuleInstantiate(JSContext* cx, JS::HandleObject moduleArg) { AssertHeapIsIdle(cx); CHECK_REQUEST(cx); - assertSameCompartment(cx, moduleArg); + releaseAssertSameCompartment(cx, moduleArg); return ModuleObject::Instantiate(cx, moduleArg.as()); } @@ -4717,7 +4717,7 @@ JS::ModuleEvaluate(JSContext* cx, JS::HandleObject moduleArg) { AssertHeapIsIdle(cx); CHECK_REQUEST(cx); - assertSameCompartment(cx, moduleArg); + releaseAssertSameCompartment(cx, moduleArg); return ModuleObject::Evaluate(cx, moduleArg.as()); } @@ -6204,7 +6204,7 @@ JS_SetPendingException(JSContext* cx, HandleValue value) { AssertHeapIsIdle(cx); CHECK_REQUEST(cx); - assertSameCompartment(cx, value); + releaseAssertSameCompartment(cx, value); cx->setPendingException(value); } -- cgit v1.2.3