Make it so JetStream2 can run with the d8 shell
https://bugs.webkit.org/show_bug.cgi?id=213856

Reviewed by Keith Miller.

We use d8's Realm API to create separate global objects for
each test to run in.

* JetStream2/JetStreamDriver.js:
(Driver.prototype.runCode.globalObject.loadString):
(Driver.prototype.runCode):
(prototype.async run):
* JetStream2/cli.js:
* JetStream2/index.html:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@263818 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/PerformanceTests/ChangeLog b/PerformanceTests/ChangeLog
index d3e90d8..93eaa50 100644
--- a/PerformanceTests/ChangeLog
+++ b/PerformanceTests/ChangeLog
@@ -1,3 +1,20 @@
+2020-07-01  Saam Barati  <sbarati@apple.com>
+
+        Make it so JetStream2 can run with the d8 shell
+        https://bugs.webkit.org/show_bug.cgi?id=213856
+
+        Reviewed by Keith Miller.
+
+        We use d8's Realm API to create separate global objects for
+        each test to run in.
+
+        * JetStream2/JetStreamDriver.js:
+        (Driver.prototype.runCode.globalObject.loadString):
+        (Driver.prototype.runCode):
+        (prototype.async run):
+        * JetStream2/cli.js:
+        * JetStream2/index.html:
+
 2020-06-29  Tadeu Zagallo  <tzagallo@apple.com>
 
         New API benchmark
diff --git a/PerformanceTests/JetStream2/JetStreamDriver.js b/PerformanceTests/JetStream2/JetStreamDriver.js
index cbe498c..8af41a2 100644
--- a/PerformanceTests/JetStream2/JetStreamDriver.js
+++ b/PerformanceTests/JetStream2/JetStreamDriver.js
@@ -284,7 +284,18 @@
     {
         if (!isInBrowser) {
             let scripts = string;
-            let globalObject = runString("");
+            let globalObject;
+            let realm;
+            if (isD8) {
+                realm = Realm.createAllowCrossRealmAccess();
+                globalObject = Realm.global(realm);
+                globalObject.loadString = function(s) {
+                    return Realm.eval(realm, s);
+                };
+                globalObject.readFile = read;
+            } else
+                globalObject = runString("");
+
             globalObject.console = {log:globalObject.print}
             globalObject.top = {
                 currentResolve,
@@ -292,7 +303,8 @@
             };
             for (let script of scripts)
                 globalObject.loadString(script);
-            return globalObject;
+
+            return isD8 ? realm : globalObject;
         }
 
         var magic = document.getElementById("magic");
@@ -581,6 +593,8 @@
         this.processResults(results);
         if (isInBrowser)
             magicFrame.contentDocument.close();
+        else if (isD8)
+            Realm.dispose(magicFrame);
     }
 
     fetchResources() {
diff --git a/PerformanceTests/JetStream2/cli.js b/PerformanceTests/JetStream2/cli.js
index e3b98db..df7676a 100644
--- a/PerformanceTests/JetStream2/cli.js
+++ b/PerformanceTests/JetStream2/cli.js
@@ -28,6 +28,10 @@
     log: print
 }
 
+const isD8 = typeof Realm !== "undefined";
+if (isD8)
+    readFile = read;
+
 if (typeof testList === "undefined")
     testList = undefined;
 
diff --git a/PerformanceTests/JetStream2/index.html b/PerformanceTests/JetStream2/index.html
index be216c4..5ab9d13 100644
--- a/PerformanceTests/JetStream2/index.html
+++ b/PerformanceTests/JetStream2/index.html
@@ -34,6 +34,7 @@
 
     <script>
     const isInBrowser = true;
+    const isD8 = false;
     var allIsGood = true;
     window.onerror = function() { allIsGood = false; }