[Git][ghc/ghc][wip/js-staging] Factor JS platform constants and h$programArgs/h$rtsArgs into functions with init

Josh Meredith (@JoshMeredith) gitlab at gitlab.haskell.org
Mon Oct 10 15:42:12 UTC 2022



Josh Meredith pushed to branch wip/js-staging at Glasgow Haskell Compiler / GHC


Commits:
42256338 by Josh Meredith at 2022-10-10T15:40:51+00:00
Factor JS platform constants and h$programArgs/h$rtsArgs into functions with init

- - - - -


7 changed files:

- libraries/base/jsbits/base.js
- libraries/base/jsbits/platform.js
- rts/js/debug.js
- rts/js/environment.js
- rts/js/gc.js
- rts/js/profiling.js
- rts/js/thread.js


Changes:

=====================================
libraries/base/jsbits/base.js
=====================================
@@ -15,7 +15,7 @@ function h$logIO() { h$log.apply(h$log, arguments); }
 function h$base_access(file, file_off, mode, c) {
     TRACE_IO("base_access");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.stat(fd, function(err, fs) {
             if(err) {
                 h$handleErrnoC(err, -1, 0, c);
@@ -31,7 +31,7 @@ function h$base_access(file, file_off, mode, c) {
 function h$base_chmod(file, file_off, mode, c) {
     TRACE_IO("base_chmod");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.chmod(h$decodeUtf8z(file, file_off), mode, function(err) {
             h$handleErrnoC(err, -1, 0, c);
         });
@@ -110,7 +110,7 @@ function h$base_dup2(fd, new_fd, c) {
 function h$base_fstat(fd, stat, stat_off, c) {
     TRACE_IO("base_stat");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.fstat(fd, function(err, fs) {
             if(err) {
                 h$handleErrnoC(err, -1, 0, c);
@@ -175,7 +175,7 @@ function h$long_from_number(f,c) {
 function h$base_lseek(fd, pos_h, pos_l, whence, c) {
     TRACE_IO("base_lseek");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         var p = CLOSEST_FLOAT_NUMBER(pos_h,pos_l);
         var o = h$base_fds[fd];
         if(!o) {
@@ -219,7 +219,7 @@ function h$base_lseek(fd, pos_h, pos_l, whence, c) {
 function h$base_lstat(file, file_off, stat, stat_off, c) {
     TRACE_IO("base_lstat");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.lstat(h$decodeUtf8z(file, file_off), function(err, fs) {
             if(err) {
                 h$handleErrnoC(err, -1, 0, c);
@@ -234,7 +234,7 @@ function h$base_lstat(file, file_off, stat, stat_off, c) {
 }
 function h$base_open(file, file_off, how, mode, c) {
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         var flags, off;
         var fp   = h$decodeUtf8z(file, file_off);
         TRACE_IO("base_open: " + fp);
@@ -294,7 +294,7 @@ function h$base_read(fd, buf, buf_off, n, c) {
 function h$base_stat(file, file_off, stat, stat_off, c) {
     TRACE_IO("base_stat");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.stat(h$decodeUtf8z(file, file_off), function(err, fs) {
             if(err) {
                 h$handleErrnoC(err, -1, 0, c);
@@ -310,7 +310,7 @@ function h$base_stat(file, file_off, stat, stat_off, c) {
 function h$base_umask(mode) {
     TRACE_IO("base_umask: " + mode);
 #ifndef GHCJS_BROWSER
-    if(h$isNode) return process.umask(mode);
+    if(h$isNode()) return process.umask(mode);
 #endif
     return 0;
 }
@@ -337,7 +337,7 @@ function h$base_write(fd, buf, buf_off, n, c) {
 function h$base_ftruncate(fd, pos_h, pos_l, c) {
     TRACE_IO("base_ftruncate");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.ftruncate(fd, CLOSEST_FLOAT_NUMBER(pos_h,pos_l), function(err) {
             h$handleErrnoC(err, -1, 0, c);
         });
@@ -348,7 +348,7 @@ function h$base_ftruncate(fd, pos_h, pos_l, c) {
 function h$base_unlink(file, file_off, c) {
     TRACE_IO("base_unlink");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.unlink(h$decodeUtf8z(file, file_off), function(err) {
             h$handleErrnoC(err, -1, 0, c);
         });
@@ -359,14 +359,14 @@ function h$base_unlink(file, file_off, c) {
 function h$base_getpid() {
     TRACE_IO("base_getpid");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) return process.pid;
+    if(h$isNode()) return process.pid;
 #endif
     return 0;
 }
 function h$base_link(file1, file1_off, file2, file2_off, c) {
     TRACE_IO("base_link");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.link(h$decodeUtf8z(file1, file1_off), h$decodeUtf8z(file2, file2_off), function(err) {
             h$handleErrnoC(err, -1, 0, c);
         });
@@ -398,7 +398,7 @@ function h$base_tcsetattr(attr, val, termios, termios_off) {
 function h$base_utime(file, file_off, timbuf, timbuf_off, c) {
     TRACE_IO("base_utime");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         h$fs.fstat(h$decodeUtf8z(file, file_off), function(err, fs) {
             if(err) {
                 h$handleErrnoC(err, 0, -1, c); // fixme
@@ -588,7 +588,7 @@ var h$base_process_stdin = function() {
     c.processing = false;
 }
 
-if(h$isNode) {
+if(h$isNode()) {
     h$base_closeFile = function(fd, fdo, c) {
         TRACE_IO("base_closeFile: " + fd + " (" + fdo.fd + ")");
         var real_fd = typeof fdo.fd === 'number' ? fdo.fd : fd;
@@ -677,7 +677,7 @@ if(h$isNode) {
     h$base_isattyStdout = function() { return process.stdout.isTTY; };
     h$base_isattyStderr = function() { return process.stderr.isTTY; };
 
-} else if (h$isJsShell) {
+} else if (h$isJsShell()) {
     h$base_readStdin = function(fd, fdo, buf, buf_offset, n, c) {
         c(0);
     }
@@ -759,8 +759,8 @@ var h$base_fds = [h$base_stdin_fd, h$base_stdout_fd, h$base_stderr_fd];
 function h$shutdownHaskellAndExit(code, fast) {
 #ifndef GHCJS_BROWSER
 #ifdef GHCJS_LOG_BUFFER
-    if(h$isNode) console.log(h$logBuffer);
-    if(h$isJsShell || h$isJsCore) print(h$logBuffer);
+    if(h$isNode()) console.log(h$logBuffer);
+    if(h$isJsShell() || h$isJsCore) print(h$logBuffer);
 #endif
 #endif
     h$exitProcess(code);


=====================================
libraries/base/jsbits/platform.js
=====================================
@@ -13,17 +13,41 @@
    more platforms should be added here in the future
 */
 #ifndef GHCJS_BROWSER
-var h$isNode    = false; // runtime is node.js
-var h$isJvm     = false; // runtime is JVM
-var h$isJsShell = false; // runtime is SpiderMonkey jsshell
-var h$isJsCore  = false; // runtime is JavaScriptCore jsc
-var h$isBrowser = false; // running in browser or everything else
+var h$isNode_    = false; // runtime is node.js
+var h$isJvm_     = false; // runtime is JVM
+var h$isJsShell_ = false; // runtime is SpiderMonkey jsshell
+var h$isJsCore_  = false; // runtime is JavaScriptCore jsc
+var h$isBrowser_ = false; // running in browser or everything else
 
-var h$isGHCJSi  = false; // Code is GHCJSi (browser or node)
+var h$isGHCJSi_  = false; // Code is GHCJSi (browser or node)
+
+function h$isNode() {
+  return h$isNode_;
+}
+
+function h$isJvm() {
+  return h$isJvm_;
+}
+
+function h$isJsShell() {
+  return h$isJsShell_;
+}
+
+function h$isJsCore() {
+  return h$isJsCore_;
+}
+
+function h$isBrowser() {
+  return h$isBrowser_;
+}
+
+function h$isGHCJSi() {
+  return h$isGHCJSi_;
+}
 
 // load all required node.js modules
 if(typeof process !== 'undefined' && (typeof h$TH !== 'undefined' || (typeof require !== 'undefined' && typeof module !== 'undefined' && module.exports))) {
-    h$isNode = true;
+    h$isNode_ = true;
     // we have to use these names for the closure compiler externs to work
     var fs            = require('fs');
     var path          = require('path');
@@ -53,22 +77,22 @@ if(typeof process !== 'undefined' && (typeof h$TH !== 'undefined' || (typeof req
     }
     var h$processConstants = h$getProcessConstants();
 } else if(typeof Java !== 'undefined') {
-    h$isJvm = true;
+    h$isJvm_ = true;
     this.console = {
       log: function(s) {
         java.lang.System.out.print(s);
       }
     };
 } else if(typeof snarf !== 'undefined' && typeof print !== 'undefined' && typeof quit !== 'undefined') {
-    h$isJsShell = true;
+    h$isJsShell_ = true;
     this.console = { log: this.print };
 } else if(typeof numberOfDFGCompiles !== 'undefined' && typeof jscStack !== 'undefined') {
-    h$isJsCore = true;
+    h$isJsCore_ = true;
 } else {
-    h$isBrowser = true;
+    h$isBrowser_ = true;
 }
 if(typeof global !== 'undefined' && global.h$GHCJSi) {
-  h$isGHCJSi = true;
+  h$isGHCJSi_ = true;
 }
 #endif
 


=====================================
rts/js/debug.js
=====================================
@@ -73,7 +73,7 @@ function h$debugAlloc_init_internal() {
     };
 
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
       try {
         // the 'weak' package provides the same functionality, but segfaults
         // try this first
@@ -237,7 +237,7 @@ var h$debug = {};
 
 function h$loadDebugTools() {
   function load(m, p) {
-    if(h$isNode) {
+    if(h$isNode()) {
       try {
         var t = require(m);
         h$debug[p] = t;
@@ -247,7 +247,7 @@ function h$loadDebugTools() {
   load('gc-stats', 'gcStats');
   load('v8-natives', 'v8');
   var t;
-  if(h$isNode) {
+  if(h$isNode()) {
     try {
       t = require('gc-stats');
       h$debug.gcStats = t;


=====================================
rts/js/environment.js
=====================================
@@ -74,78 +74,95 @@ function h$collectProps(o) {
 
 // load the command line arguments in h$programArgs
 // the first element is the program name
-var h$programArgs;
-#ifdef GHCJS_BROWSER
-h$programArgs = [ "a.js" ];
-#else
-if(h$isNode) {
-    h$programArgs = process.argv.slice(1);
-} else if(h$isJvm) {
-    h$programArgs = h$getGlobal(this).arguments.slice(0);
-    h$programArgs.unshift("a.js");
-} else if(h$isJsShell && typeof h$getGlobal(this).scriptArgs !== 'undefined') {
-    h$programArgs = h$getGlobal(this).scriptArgs.slice(0);
-    h$programArgs.unshift("a.js");
-} else if((h$isJsShell || h$isJsCore) && typeof h$getGlobal(this).arguments !== 'undefined') {
-    h$programArgs = h$getGlobal(this).arguments.slice(0);
-    h$programArgs.unshift("a.js");
-} else {
-    h$programArgs = [ "a.js" ];
+var h$programArgs_;
+var h$rtsArgs_;
+
+function h$programArgs() {
+  if (!h$programArgs_) {
+    h$initArgs();
+  }
+  return h$programArgs_;
 }
-#endif
 
-//filter RTS arguments
-var h$rtsArgs = [];
-{
-    var prog_args = [];
-    var rts_args = [];
-    var in_rts = false;
-    var i = 0;
-    for(i=0;i<h$programArgs.length;i++) {
-        var a = h$programArgs[i];
-        // The '--RTS' argument disables all future
-        // +RTS ... -RTS processing.
-        if (a === "--RTS") {
-            break;
-        }
-        // The '--' argument is passed through to the program, but
-        // disables all further +RTS ... -RTS processing.
-        else if (a === "--") {
-            break;
-        }
-        else if (a === "+RTS") {
-            in_rts = true;
-        }
-        else if (a === "-RTS") {
-            in_rts = false;
-        }
-        else if (in_rts) {
-            rts_args.push(a);
-        }
-        else {
-            prog_args.push(a);
-        }
-    }
-    // process remaining program arguments
-    for (;i<h$programArgs.length;i++) {
-        prog_args.push(h$programArgs[i]);
-    }
-    //set global variables
-    h$programArgs = prog_args;
-    h$rtsArgs     = rts_args;
+function h$rtsArgs() {
+  if (!h$rtsArgs_) {
+    h$initArgs();
+  }
+  return h$rtsArgs_;
+}
+
+function h$initArgs() {
+  #ifdef GHCJS_BROWSER
+  h$programArgs_ = [ "a.js" ];
+  #else
+  if(h$isNode()) {
+      h$programArgs_ = process.argv.slice(1);
+  } else if(h$isJvm()) {
+      h$programArgs_ = h$getGlobal(this).arguments.slice(0);
+      h$programArgs_.unshift("a.js");
+  } else if(h$isJsShell() && typeof h$getGlobal(this).scriptArgs !== 'undefined') {
+      h$programArgs_ = h$getGlobal(this).scriptArgs.slice(0);
+      h$programArgs_.unshift("a.js");
+  } else if((h$isJsShell() || h$isJsCore()) && typeof h$getGlobal(this).arguments !== 'undefined') {
+      h$programArgs_ = h$getGlobal(this).arguments.slice(0);
+      h$programArgs_.unshift("a.js");
+  } else {
+      h$programArgs_ = [ "a.js" ];
+  }
+  #endif
+
+  //filter RTS arguments
+  {
+      var prog_args = [];
+      var rts_args = [];
+      var in_rts = false;
+      var i = 0;
+      for(i=0;i<h$programArgs_.length;i++) {
+          var a = h$programArgs_[i];
+          // The '--RTS' argument disables all future
+          // +RTS ... -RTS processing.
+          if (a === "--RTS") {
+              break;
+          }
+          // The '--' argument is passed through to the program, but
+          // disables all further +RTS ... -RTS processing.
+          else if (a === "--") {
+              break;
+          }
+          else if (a === "+RTS") {
+              in_rts = true;
+          }
+          else if (a === "-RTS") {
+              in_rts = false;
+          }
+          else if (in_rts) {
+              rts_args.push(a);
+          }
+          else {
+              prog_args.push(a);
+          }
+      }
+      // process remaining program arguments
+      for (;i<h$programArgs_.length;i++) {
+          prog_args.push(h$programArgs_[i]);
+      }
+      //set global variables
+      h$programArgs_ = prog_args;
+      h$rtsArgs_     = rts_args;
+  }
 }
 
 function h$getProgArgv(argc_v,argc_off,argv_v,argv_off) {
   TRACE_ENV("getProgArgV");
-  var c = h$programArgs.length;
+  var c = h$programArgs().length;
   if(c === 0) {
     argc_v.dv.setInt32(argc_off, 0, true);
   } else {
     argc_v.dv.setInt32(argc_off, c, true);
     var argv = h$newByteArray(4*c);
     argv.arr = [];
-    for(var i=0;i<h$programArgs.length;i++) {
-      argv.arr[4*i] = [ h$encodeUtf8(h$programArgs[i]), 0 ];
+    for(var i=0;i<h$programArgs().length;i++) {
+      argv.arr[4*i] = [ h$encodeUtf8(h$programArgs()[i]), 0 ];
     }
     if(!argv_v.arr) { argv_v.arr = []; }
     argv_v.arr[argv_off] = [argv, 0];
@@ -159,12 +176,12 @@ function h$setProgArgv(n, ptr_d, ptr_o) {
     var arg = h$decodeUtf8z(p[0], p[1]);
     args.push(arg);
   }
-  h$programArgs = args;
+  h$programArgs() = args;
 }
 
 function h$getpid() {
 #ifndef GHCJS_BROWSER
-  if(h$isNode) return process.id;
+  if(h$isNode()) return process.id;
 #endif
   return 0;
 }
@@ -177,7 +194,7 @@ var h$fakeCpuTime = 1.0;
 
 function h$getCPUTime() {
 #ifndef GHCJS_BROWSER
-if(h$isNode) {
+if(h$isNode()) {
   var t = process.cpuUsage();
   var cput = t.user + t.system;
   TRACE_ENV("getCPUTime: " + cput);
@@ -194,7 +211,7 @@ if(h$isNode) {
 function h$__hscore_environ() {
     TRACE_ENV("hscore_environ");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         var env = [], i;
         for(i in process.env) {
           var envv = i + '=' + process.env[i];
@@ -219,7 +236,7 @@ function h$__hsbase_unsetenv(name, name_off) {
 function h$getenv(name, name_off) {
     TRACE_ENV("getenv");
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
         var n = h$decodeUtf8z(name, name_off);
         TRACE_ENV("getenv (node): " + n);
         if(typeof process.env[n] !== 'undefined') {
@@ -240,7 +257,7 @@ function h$setenv(name, name_off, val, val_off, overwrite) {
     return -1;
   }
 #ifndef GHCJS_BROWSER
-  if(h$isNode) {
+  if(h$isNode()) {
     if(overwrite || typeof process.env[n] !== 'undefined') process.env[n] = v;
   }
 #endif
@@ -255,7 +272,7 @@ function h$unsetenv(name, name_off) {
     return -1;
   }
 #ifndef GHCJS_BROWSER
-  if(h$isNode) delete process.env[n];
+  if(h$isNode()) delete process.env[n];
 #endif
   return 0;
 }
@@ -275,12 +292,12 @@ function h$putenv(str, str_off) {
   TRACE_ENV("putenv: " + x);
   if(i === -1) { // remove the value
     TRACE_ENV("putenv unset: " + x);
-    if(h$isNode) delete process.env[x];
+    if(h$isNode()) delete process.env[x];
   } else { // set the value
     var name = x.substring(0, i)
     var val = x.substring(i+1);
     TRACE_ENV("putenv set: " + name + " -> " + val);
-    if(h$isNode) process.env[name] = val;
+    if(h$isNode()) process.env[name] = val;
   }
 #endif
   return 0;
@@ -303,7 +320,7 @@ function h$append_prog_name(str) {
   }
 
   // only works for node for now
-  if(h$isNode) {
+  if(h$isNode()) {
     return basename(process.argv[1]) + ": " + str;
   }
 
@@ -326,15 +343,15 @@ function h$errorMsg(pat) {
     str = str.replace(/%s/, arguments[i]);
   }
 #ifndef GHCJS_BROWSER
-  if(h$isGHCJSi) {
+  if(h$isGHCJSi()) {
     // ignore message
-  } else if(h$isNode) {
+  } else if(h$isNode()) {
     process.stderr.write(str);
-  } else if (h$isJsShell && typeof printErr !== 'undefined') {
+  } else if (h$isJsShell() && typeof printErr !== 'undefined') {
     if(str.length) printErr(stripTrailingNewline(str));
-  } else if (h$isJsShell && typeof putstr !== 'undefined') {
+  } else if (h$isJsShell() && typeof putstr !== 'undefined') {
     putstr(str);
-  } else if (h$isJsCore) {
+  } else if (h$isJsCore()) {
     if(str.length) {
 	if(h$base_stderrLeftover.val !== null) {
 	    debug(h$base_stderrLeftover.val + stripTrailingNewline(str));


=====================================
rts/js/gc.js
=====================================
@@ -175,7 +175,7 @@ function h$gc(t) {
 #ifndef GHCJS_BROWSER
     // fixme, should enable again later when proper CAF management
     // and retention of the standard handles in GHCJSi work
-    if(h$isGHCJSi) return;
+    if(h$isGHCJSi()) return;
 #endif
 
     if(h$currentThread !== null) throw "h$gc: GC can only be run when no thread is running";


=====================================
rts/js/profiling.js
=====================================
@@ -27,7 +27,7 @@ var h$registerCC = null, h$registerCCS = null, h$setCCS = null;
 var h$runProf = function(f) {
     f();
 }
-if(h$isNode) {
+if(h$isNode()) {
     (function() {
 	try {
             var p = require('ghcjs-profiling');
@@ -331,4 +331,4 @@ function h$buildCCSPtr(o) {
 // run the action with an empty CCS
 function h$clearCCS(a) {
   throw new Error("ClearCCSOp not implemented");
-}
\ No newline at end of file
+}


=====================================
rts/js/thread.js
=====================================
@@ -812,7 +812,7 @@ function h$reportMainLoopException(e, isMainThread) {
   var main = isMainThread ? " main" : "";
   h$log("uncaught exception in Haskell" + main + " thread: " + e.toString());
   if(e.stack) h$log(e.stack);
-  if (h$isNode) {
+  if (h$isNode()) {
     process.exit(1);
   }
 }
@@ -1112,7 +1112,7 @@ function h$main(a) {
   //TRACE_SCHEDULER("sched: starting main thread");
     t.stack[0] = h$doneMain_e;
 #ifndef GHCJS_BROWSER
-  if(!h$isBrowser && !h$isGHCJSi) {
+  if(!h$isBrowser() && !h$isGHCJSi()) {
     t.stack[2] = h$baseZCGHCziTopHandlerzitopHandler;
   }
 #endif
@@ -1131,7 +1131,7 @@ function h$main(a) {
 
 function h$doneMain() {
 #ifndef GHCJS_BROWSER
-  if(h$isGHCJSi) {
+  if(h$isGHCJSi()) {
     if(h$currentThread.stack) {
       global.h$GHCJSi.done(h$currentThread);
     }
@@ -1156,13 +1156,13 @@ h$ThreadAbortedError.prototype.toString = function() {
 
 function h$exitProcess(code) {
 #ifndef GHCJS_BROWSER
-    if(h$isNode) {
+    if(h$isNode()) {
 	process.exit(code);
-    } else if(h$isJvm) {
+    } else if(h$isJvm()) {
         java.lang.System.exit(code);
-    } else if(h$isJsShell) {
+    } else if(h$isJsShell()) {
         quit(code);
-    } else if(h$isJsCore) {
+    } else if(h$isJsCore()) {
         if(h$base_stdoutLeftover.val !== null) print(h$base_stdoutLeftover.val);
         if(h$base_stderrLeftover.val !== null) debug(h$base_stderrLeftover.val);
         // jsc does not support returning a nonzero value, print it instead



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/42256338080732fd8be10c8c2dfee438826897b9

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/42256338080732fd8be10c8c2dfee438826897b9
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20221010/8504342a/attachment-0001.html>


More information about the ghc-commits mailing list