diff options
author | Jon Coppeard <jcoppeard@mozilla.com> | 2020-08-27 09:14:33 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-08-27 09:14:33 +0000 |
commit | 22f300f7c431bbf4de20437d2ebd7bff38284efb (patch) | |
tree | af0678b528562e6ff4875e41d8bf07deadde44ec /js/src/jsscript.cpp | |
parent | 8924e4ddd3d0b463ed3694fb788df9928d4e7dd5 (diff) | |
download | UXP-22f300f7c431bbf4de20437d2ebd7bff38284efb.tar UXP-22f300f7c431bbf4de20437d2ebd7bff38284efb.tar.gz UXP-22f300f7c431bbf4de20437d2ebd7bff38284efb.tar.lz UXP-22f300f7c431bbf4de20437d2ebd7bff38284efb.tar.xz UXP-22f300f7c431bbf4de20437d2ebd7bff38284efb.zip |
Issue #618 - Add JS API to associate scripts with DOM elements after compilation
Ref BZ 1342416
Diffstat (limited to 'js/src/jsscript.cpp')
-rw-r--r-- | js/src/jsscript.cpp | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/js/src/jsscript.cpp b/js/src/jsscript.cpp index bdd411d04..ba2e540e4 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) { |