Bug 784620 - Support reading self-hosted JS from a file set via environment variable. r=terrence
authorTill Schneidereit <tschneidereit@gmail.com>
Thu, 11 Oct 2012 11:19:43 +0200
changeset 110280 5f1d0cfe99ace6c6383bd98230ea2ee986471c51
parent 110279 0ea416c21d176c96e9a792128a806245606e9fde
child 110281 dc34d5773895741ec71f1970fbb4b1236b162917
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersterrence
bugs784620
milestone19.0a1
Bug 784620 - Support reading self-hosted JS from a file set via environment variable. r=terrence
js/src/Makefile.in
js/src/builtin/embedjs.py
js/src/jscntxt.cpp
--- a/js/src/Makefile.in
+++ b/js/src/Makefile.in
@@ -877,17 +877,18 @@ ifdef HAVE_DTRACE
 $(addsuffix .$(OBJ_SUFFIX),jsprobes jsinterp jsobj): $(CURDIR)/javascript-trace.h
 endif
 
 ifdef HAVE_LINUX_PERF_EVENT_H
 pm_linux.$(OBJ_SUFFIX): CXXFLAGS += $(LINUX_HEADERS_INCLUDES)
 endif
 
 # Prepare self-hosted JS code for embedding
-export:: selfhosted.out.h
+export:: selfhosting
+selfhosting:: selfhosted.out.h
 
 selfhosting_srcs := \
   $(srcdir)/builtin/array.js \
   $(NULL)
 
 selfhosted_out_h_deps := \
   $(selfhosting_srcs) \
   $(srcdir)/js.msg \
--- a/js/src/builtin/embedjs.py
+++ b/js/src/builtin/embedjs.py
@@ -38,15 +38,14 @@ def replaceMessages(line, messages):
 def main():
     debug = sys.argv[1] == '-d'
     if debug:
         sys.argv.pop(1)
     output_file = sys.argv[1]
     messages_file = sys.argv[2]
     macros_file = sys.argv[3]
     source_files = sys.argv[4:]
-    combined_file = 'combined.js'
+    combined_file = 'selfhosted.js'
     replaceErrorMsgs(source_files, messages_file, combined_file)
     js2c.JS2C([combined_file, macros_file], [output_file], { 'TYPE': 'CORE', 'COMPRESSION': 'off', 'DEBUG':debug })
-    os.remove(combined_file)
 
 if __name__ == "__main__":
     main()
\ No newline at end of file
--- a/js/src/jscntxt.cpp
+++ b/js/src/jscntxt.cpp
@@ -252,33 +252,41 @@ static JSClass self_hosting_global_class
 bool
 JSRuntime::initSelfHosting(JSContext *cx)
 {
     JS_ASSERT(!selfHostedGlobal_);
     RootedObject savedGlobal(cx, JS_GetGlobalObject(cx));
     if (!(selfHostedGlobal_ = JS_NewGlobalObject(cx, &self_hosting_global_class, NULL)))
         return false;
     JS_SetGlobalObject(cx, selfHostedGlobal_);
-
-    const char *src = selfhosted::raw_sources;
-    uint32_t srcLen = selfhosted::GetRawScriptsSize();
+    RootedObject shg(cx, selfHostedGlobal_);
 
     CompileOptions options(cx);
     options.setFileAndLine("self-hosted", 1);
     options.setSelfHostingMode(true);
 
-    RootedObject shg(cx, selfHostedGlobal_);
-    Value rv;
     /*
      * Set a temporary error reporter printing to stderr because it is too
      * early in the startup process for any other reporter to be registered
      * and we don't want errors in self-hosted code to be silently swallowed.
      */
     JSErrorReporter oldReporter = JS_SetErrorReporter(cx, selfHosting_ErrorReporter);
-    bool ok = Evaluate(cx, shg, options, src, srcLen, &rv);
+    Value rv;
+    bool ok;
+
+    char *filename = getenv("MOZ_SELFHOSTEDJS");
+    if (filename) {
+        RootedScript script(cx, Compile(cx, shg, options, filename));
+        if (script)
+            ok = Execute(cx, script, *shg.get(), &rv);
+    } else {
+        const char *src = selfhosted::raw_sources;
+        uint32_t srcLen = selfhosted::GetRawScriptsSize();
+        ok = Evaluate(cx, shg, options, src, srcLen, &rv);
+    }
     JS_SetErrorReporter(cx, oldReporter);
     JS_SetGlobalObject(cx, savedGlobal);
     return ok;
 }
 
 void
 JSRuntime::markSelfHostedGlobal(JSTracer *trc)
 {