Bug 1498320 - Move module-compilation's createSourceAndParser call into a separate prepareModuleParse function, anticipating when only that function will have to deal with multiple kinds of SourceBufferHolder. r=tcampbell
authorJeff Walden <jwalden@mit.edu>
Thu, 11 Oct 2018 13:54:42 -0700
changeset 445971 2052c58dfe9835eb4451a1cc02ff9b566419e2e2
parent 445970 e6b6326c593d9a8c1095b2c872056df7f8ff088f
child 445972 d9e71df3d1d7a6dbf8effeb426273cd901d7700b
push id35030
push usercsabou@mozilla.com
push dateTue, 13 Nov 2018 04:24:01 +0000
treeherdermozilla-central@f6df375b8698 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstcampbell
bugs1498320
milestone65.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 1498320 - Move module-compilation's createSourceAndParser call into a separate prepareModuleParse function, anticipating when only that function will have to deal with multiple kinds of SourceBufferHolder. r=tcampbell
js/src/frontend/BytecodeCompiler.cpp
--- a/js/src/frontend/BytecodeCompiler.cpp
+++ b/js/src/frontend/BytecodeCompiler.cpp
@@ -46,24 +46,38 @@ class MOZ_STACK_CLASS BytecodeCompiler
     // Construct an object passing mandatory arguments.
     BytecodeCompiler(JSContext* cx,
                      const ReadOnlyCompileOptions& options,
                      SourceBufferHolder& sourceBuffer,
                      HandleScope enclosingScope);
 
     JSScript* compileGlobalScript(ScopeKind scopeKind);
     JSScript* compileEvalScript(HandleObject environment, HandleScope enclosingScope);
+
+    // Call this before calling compileModule.
+    MOZ_MUST_USE bool prepareModuleParse() {
+        return createSourceAndParser(ParseGoal::Module);
+    }
+
     ModuleObject* compileModule();
+
     bool compileStandaloneFunction(MutableHandleFunction fun, GeneratorKind generatorKind,
                                    FunctionAsyncKind asyncKind,
                                    const Maybe<uint32_t>& parameterListEnd);
 
     ScriptSourceObject* sourceObjectPtr() const;
 
   private:
+    void assertSourceAndParserCreated() const {
+        MOZ_ASSERT(sourceObject != nullptr);
+        MOZ_ASSERT(scriptSource != nullptr);
+        MOZ_ASSERT(usedNames.isSome());
+        MOZ_ASSERT(parser.isSome());
+    }
+
     JSScript* compileScript(HandleObject environment, SharedContext* sc);
     bool checkLength();
     bool createScriptSource(const Maybe<uint32_t>& parameterListEnd);
     bool canLazilyParse();
     bool createParser(ParseGoal goal);
     bool createSourceAndParser(ParseGoal goal,
                                const Maybe<uint32_t>& parameterListEnd = Nothing());
 
@@ -224,19 +238,20 @@ BytecodeCompiler::canLazilyParse()
 }
 
 bool
 BytecodeCompiler::createParser(ParseGoal goal)
 {
     usedNames.emplace(cx);
 
     if (canLazilyParse()) {
-        syntaxParser.emplace(cx, cx->tempLifoAlloc(), options, sourceBuffer.get(),
-                             sourceBuffer.length(), /* foldConstants = */ false,
-                             *usedNames, nullptr, nullptr, sourceObject, goal);
+        syntaxParser.emplace(cx, cx->tempLifoAlloc(), options,
+                             sourceBuffer.get(), sourceBuffer.length(),
+                             /* foldConstants = */ false, *usedNames, nullptr, nullptr,
+                             sourceObject, goal);
         if (!syntaxParser->checkOptions()) {
             return false;
         }
     }
 
     parser.emplace(cx, cx->tempLifoAlloc(), options, sourceBuffer.get(), sourceBuffer.length(),
                    /* foldConstants = */ true, *usedNames, syntaxParser.ptrOr(nullptr), nullptr,
                    sourceObject, goal);
@@ -405,19 +420,17 @@ BytecodeCompiler::compileEvalScript(Hand
     EvalSharedContext evalsc(cx, environment, enclosingScope,
                              directives, options.extraWarningsOption);
     return compileScript(environment, &evalsc);
 }
 
 ModuleObject*
 BytecodeCompiler::compileModule()
 {
-    if (!createSourceAndParser(ParseGoal::Module)) {
-        return nullptr;
-    }
+    assertSourceAndParserCreated();
 
     if (!createScript()) {
         return nullptr;
     }
 
     Rooted<ModuleObject*> module(cx, ModuleObject::create(cx));
     if (!module) {
         return nullptr;
@@ -751,16 +764,21 @@ frontend::CompileModule(JSContext* cx, c
     CompileOptions options(cx, optionsInput);
     options.maybeMakeStrictMode(true); // ES6 10.2.1 Module code is always strict mode code.
     options.setIsRunOnce(true);
     options.allowHTMLComments = false;
 
     RootedScope emptyGlobalScope(cx, &cx->global()->emptyGlobalScope());
     BytecodeCompiler compiler(cx, options, srcBuf, emptyGlobalScope);
     AutoInitializeSourceObject autoSSO(compiler, sourceObjectOut);
+
+    if (!compiler.prepareModuleParse()) {
+        return nullptr;
+    }
+
     ModuleObject* module = compiler.compileModule();
     if (!module) {
         return nullptr;
     }
 
     assertException.reset();
     return module;
 }