Bug 1544882 - Make |JSScript::loadSource| applicable to any |ScriptSource| by folding its precondition into its behavior. r=arai
authorJeff Walden <jwalden@mit.edu>
Thu, 11 Apr 2019 18:22:56 -0700
changeset 530251 2af8656bd3c0362939cda478005e9eb2238ed9b5
parent 530250 5279ac14ae4898b1d2d771f14891b395b068626a
child 530252 74774a7f1a4dd13925354df4e3963068fc4cd28d
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1544882
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1544882 - Make |JSScript::loadSource| applicable to any |ScriptSource| by folding its precondition into its behavior. r=arai Differential Revision: https://phabricator.services.mozilla.com/D27766
js/src/jsapi.cpp
js/src/vm/Debugger.cpp
js/src/vm/JSFunction.cpp
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
js/src/wasm/AsmJS.cpp
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -3654,19 +3654,18 @@ JS_PUBLIC_API JSString* JS_DecompileScri
 
   AssertHeapIsIdle();
   CHECK_THREAD(cx);
   script->ensureNonLazyCanonicalFunction();
   RootedFunction fun(cx, script->functionNonDelazifying());
   if (fun) {
     return JS_DecompileFunction(cx, fun);
   }
-  bool haveSource = script->scriptSource()->hasSourceText();
-  if (!haveSource &&
-      !JSScript::loadSource(cx, script->scriptSource(), &haveSource)) {
+  bool haveSource;
+  if (!JSScript::tryLoadSource(cx, script->scriptSource(), &haveSource)) {
     return nullptr;
   }
   return haveSource ? JSScript::sourceData(cx, script)
                     : NewStringCopyZ<CanGC>(cx, "[no source]");
 }
 
 JS_PUBLIC_API JSString* JS_DecompileFunction(JSContext* cx,
                                              HandleFunction fun) {
--- a/js/src/vm/Debugger.cpp
+++ b/js/src/vm/Debugger.cpp
@@ -8406,19 +8406,18 @@ class DebuggerSourceGetTextMatcher {
 
  public:
   explicit DebuggerSourceGetTextMatcher(JSContext* cx) : cx_(cx) {}
 
   using ReturnType = JSString*;
 
   ReturnType match(HandleScriptSourceObject sourceObject) {
     ScriptSource* ss = sourceObject->source();
-    bool hasSourceText = ss->hasSourceText();
-    if (!ss->hasSourceText() &&
-        !JSScript::loadSource(cx_, ss, &hasSourceText)) {
+    bool hasSourceText;
+    if (!JSScript::tryLoadSource(cx_, ss, &hasSourceText)) {
       return nullptr;
     }
     if (!hasSourceText) {
       return NewStringCopyZ<CanGC>(cx_, "[no source]");
     }
 
     if (ss->isFunctionBody()) {
       return ss->functionBodyString(cx_);
--- a/js/src/vm/JSFunction.cpp
+++ b/js/src/vm/JSFunction.cpp
@@ -864,19 +864,20 @@ JSString* js::FunctionToString(JSContext
   bool haveSource = fun->isInterpreted() &&
                     (fun->isClassConstructor() || !fun->isSelfHostedBuiltin());
 
   // If we're in toSource mode, put parentheses around lambda functions so
   // that eval returns lambda, not function statement.
   bool addParentheses =
       haveSource && isToSource && (fun->isLambda() && !fun->isArrow());
 
-  if (haveSource && !script->scriptSource()->hasSourceText() &&
-      !JSScript::loadSource(cx, script->scriptSource(), &haveSource)) {
-    return nullptr;
+  if (haveSource) {
+    if (!JSScript::tryLoadSource(cx, script->scriptSource(), &haveSource)) {
+      return nullptr;
+    }
   }
 
   // Fast path for the common case, to avoid StringBuffer overhead.
   if (!addParentheses && haveSource) {
     FunctionToStringCache& cache = cx->zone()->functionToStringCache();
     if (JSString* str = cache.lookup(script)) {
       return str;
     }
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -1648,19 +1648,22 @@ void ScriptSourceObject::setPrivate(JSRu
   JS::AutoSuppressGCAnalysis nogc;
   Value prevValue = getReservedSlot(PRIVATE_SLOT);
   rt->releaseScriptPrivate(prevValue);
   setReservedSlot(PRIVATE_SLOT, value);
   rt->addRefScriptPrivate(value);
 }
 
 /* static */
-bool JSScript::loadSource(JSContext* cx, ScriptSource* ss, bool* worked) {
-  MOZ_ASSERT(!ss->hasSourceText());
-  *worked = false;
+bool JSScript::tryLoadSource(JSContext* cx, ScriptSource* ss, bool* loaded) {
+  *loaded = ss->hasSourceText();
+  if (*loaded) {
+    return true;
+  }
+
   if (!cx->runtime()->sourceHook.ref() || !ss->sourceRetrievable()) {
     return true;
   }
   char16_t* src = nullptr;
   size_t length;
   if (!cx->runtime()->sourceHook->load(cx, ss->filename(), &src, &length)) {
     return false;
   }
@@ -1669,17 +1672,17 @@ bool JSScript::loadSource(JSContext* cx,
   }
 
   // XXX On-demand source is currently only UTF-16.  Perhaps it should be
   //     changed to UTF-8, or UTF-8 be allowed in addition to UTF-16?
   if (!ss->setSource(cx, EntryUnits<char16_t>(src), length)) {
     return false;
   }
 
-  *worked = true;
+  *loaded = true;
   return true;
 }
 
 /* static */
 JSFlatString* JSScript::sourceData(JSContext* cx, HandleScript script) {
   MOZ_ASSERT(script->scriptSource()->hasSourceText());
   return script->scriptSource()->substring(cx, script->sourceStart(),
                                            script->sourceEnd());
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -2489,17 +2489,17 @@ class JSScript : public js::gc::TenuredC
   // directly, via lazy arguments or a rest parameter.
   bool mayReadFrameArgsDirectly();
 
   static JSFlatString* sourceData(JSContext* cx, JS::HandleScript script);
 
   MOZ_MUST_USE bool appendSourceDataForToString(JSContext* cx,
                                                 js::StringBuffer& buf);
 
-  static bool loadSource(JSContext* cx, js::ScriptSource* ss, bool* worked);
+  static bool tryLoadSource(JSContext* cx, js::ScriptSource* ss, bool* loaded);
 
   void setSourceObject(js::ScriptSourceObject* object);
   js::ScriptSourceObject* sourceObject() const { return sourceObject_; }
   js::ScriptSource* scriptSource() const;
   js::ScriptSource* maybeForwardedScriptSource() const;
 
   void setDefaultClassConstructorSpan(js::ScriptSourceObject* sourceObject,
                                       uint32_t start, uint32_t end,
--- a/js/src/wasm/AsmJS.cpp
+++ b/js/src/wasm/AsmJS.cpp
@@ -6881,18 +6881,18 @@ static bool HandleInstantiationFailure(J
   if (cx->isExceptionPending()) {
     return false;
   }
 
   ScriptSource* source = metadata.scriptSource.get();
 
   // Source discarding is allowed to affect JS semantics because it is never
   // enabled for normal JS content.
-  bool haveSource = source->hasSourceText();
-  if (!haveSource && !JSScript::loadSource(cx, source, &haveSource)) {
+  bool haveSource;
+  if (!JSScript::tryLoadSource(cx, source, &haveSource)) {
     return false;
   }
   if (!haveSource) {
     JS_ReportErrorASCII(cx,
                         "asm.js link failure with source discarding enabled");
     return false;
   }
 
@@ -7213,18 +7213,18 @@ JSString* js::AsmJSModuleToString(JSCont
   ScriptSource* source = metadata.scriptSource.get();
 
   JSStringBuilder out(cx);
 
   if (isToSource && fun->isLambda() && !out.append("(")) {
     return nullptr;
   }
 
-  bool haveSource = source->hasSourceText();
-  if (!haveSource && !JSScript::loadSource(cx, source, &haveSource)) {
+  bool haveSource;
+  if (!JSScript::tryLoadSource(cx, source, &haveSource)) {
     return nullptr;
   }
 
   if (!haveSource) {
     if (!out.append("function ")) {
       return nullptr;
     }
     if (fun->explicitName() && !out.append(fun->explicitName())) {
@@ -7264,18 +7264,18 @@ JSString* js::AsmJSFunctionToString(JSCo
 
   ScriptSource* source = metadata.scriptSource.get();
   JSStringBuilder out(cx);
 
   if (!out.append("function ")) {
     return nullptr;
   }
 
-  bool haveSource = source->hasSourceText();
-  if (!haveSource && !JSScript::loadSource(cx, source, &haveSource)) {
+  bool haveSource;
+  if (!JSScript::tryLoadSource(cx, source, &haveSource)) {
     return nullptr;
   }
 
   if (!haveSource) {
     // asm.js functions can't be anonymous
     MOZ_ASSERT(fun->explicitName());
     if (!out.append(fun->explicitName())) {
       return nullptr;