• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From ed6584a8304882dc14a25cbd1f27f70961658147 Mon Sep 17 00:00:00 2001
2Date: Thu, 15 Jul 2021 09:34:47 +0800
3Subject: [PATCH] fix test
4
5---
6 lib/Agent.js        |  1 +
7 lib/ConsoleAgent.js | 29 ++++++++++-----
8 lib/agents/panda.js | 88 +++++++++++++++++++++++++++++++++++++++++++++
9 runtimes/panda.js   | 44 +++++++++++++++++++++++
10 4 files changed, 154 insertions(+), 8 deletions(-)
11 create mode 100644 lib/agents/panda.js
12 create mode 100644 runtimes/panda.js
13
14diff --git a/lib/Agent.js b/lib/Agent.js
15index edcdf0e..7e655c5 100644
16--- a/lib/Agent.js
17+++ b/lib/Agent.js
18@@ -7,6 +7,7 @@ class Agent {
19     this.args = options.hostArguments || [];
20     this.transform = options.transform || (x => x);
21     this.out = options.out || '';
22+    this.test262Dir = options.test262Dir;
23
24     if (typeof this.args === 'string') {
25       this.args = this.args.includes(' ') ?
26diff --git a/lib/ConsoleAgent.js b/lib/ConsoleAgent.js
27index 947c1db..dc14ded 100644
28--- a/lib/ConsoleAgent.js
29+++ b/lib/ConsoleAgent.js
30@@ -19,7 +19,7 @@ const {
31 const cpSym = Symbol.for('cp');
32 const tpSym = Symbol.for('tp');
33
34-function generateTempFileName() {
35+function generateTempFileName(file) {
36   const now = Date.now();
37   return `f-${now}-${process.pid}-${(Math.random() * 0x100000000 + 1).toString(36)}.js`;
38 }
39@@ -47,9 +47,23 @@ class ConsoleAgent extends Agent {
40     }
41   }
42
43+  genTempFileName(code){
44+    let file = code.file;
45+    let scenario = code.scenario === 'strict mode' ? 'strict' : code.scenario;
46+    let tmps = file.split(this.test262Dir);
47+    let tempfile = path.join(this.out,tmps[1]);
48+    tempfile = tempfile.substring(0,tempfile.indexOf('.js'));
49+    tempfile = path.normalize(
50+      `${tempfile}-${scenario}.js`
51+    );
52+    return tempfile;
53+  }
54+
55   evalScript(code, options = {}) {
56-    let tempfile = path.join(this[tpSym], generateTempFileName());
57-    let temppath = this[tpSym];
58+
59+    let tempfile = this.genTempFileName(code);
60+    //let tempfile = path.join(this.out, generateTempFileName(code.file));
61+    let temppath = this.out;
62
63     let isExpectingRawSource = false;
64     let hasDependencies = false;
65@@ -57,10 +71,10 @@ class ConsoleAgent extends Agent {
66     const sources = [];
67     const dependencies = [];
68
69-    if (this.out) {
70-      tempfile = tempfile.replace(temppath, this.out);
71-      temppath = this.out;
72-    }
73+    // if (this.out) {
74+    //   tempfile = tempfile.replace(temppath, this.out);
75+    //   temppath = this.out;
76+    // }
77
78     // When evalScript is called with a test262-stream test record:
79     if (typeof code === 'object' && code.contents) {
80@@ -161,7 +175,6 @@ class ConsoleAgent extends Agent {
81       sources.forEach(({0: file}) => fs.unlink(file, () => { /* ignore */ }));
82
83       const result = this.normalizeResult({ stderr, stdout });
84-
85       result.error = this.parseError(result.stderr);
86
87       return result;
88diff --git a/lib/agents/panda.js b/lib/agents/panda.js
89new file mode 100644
90index 0000000..ab22b47
91--- /dev/null
92+++ b/lib/agents/panda.js
93@@ -0,0 +1,88 @@
94+'use strict';
95+
96+const fs = require('fs');
97+const runtimePath = require('../runtime-path');
98+const ConsoleAgent = require('../ConsoleAgent');
99+
100+const errorRe = /[(](\d+),(\d+)[)]: (.*)/;
101+const errorRe1 = /^(\w+): (.*)$/m;
102+// const errorRe2 = /^(?:(\w+): (.*))|(?:(\w+))$/m;
103+const errorRe2 = /(\w+): (\w+): (.*)$/m;
104+
105+function parseSyntaxError(syntaxErrorMessage) {
106+  const matches = syntaxErrorMessage.match();
107+  if (matches && matches.length) {
108+    return {
109+      message: matches[3],
110+      lineNumber: Number(matches[1]),
111+      columnNumber: Number(matches[2])
112+    };
113+  }
114+  return null;
115+}
116+
117+class PandaAgent extends ConsoleAgent{
118+    constructor(options) {
119+        super(options);
120+    }
121+
122+    createChildProcess(args) {
123+      let js_file = args[0]
124+      args = []
125+      args.unshift(`--js-file=${js_file}`)
126+      return super.createChildProcess(args);
127+    }
128+
129+    evalScript(code, options = {}) {
130+        return super.evalScript(code, options);
131+    }
132+
133+    parseError(str) {
134+        let match = str.match(errorRe1);
135+        if (match) {
136+          return {
137+            name: match[1],
138+            message: match[2],
139+            stack: [],
140+          };
141+        } else {
142+          // Syntax errors don't have nice error messages...
143+          let error = null;
144+          let errors = str.match(/[(](\d+),(\d+)[)]: (.*)/gm);
145+
146+          if (errors && errors.length) {
147+            error = {
148+              name: 'SyntaxError',
149+              message: errors[0],
150+              stack: []
151+            };
152+
153+            const stack = parseSyntaxError(errors[0]);
154+
155+            if (stack) {
156+              error.stack.push(stack);
157+              error.message = stack.message;
158+            }
159+          }
160+
161+          if (error) {
162+            return error;
163+          }
164+
165+          // Last chance...
166+          errors = str.match(errorRe2);
167+          if (errors && errors.length >3) {
168+            return {
169+              name: errors[2],
170+              message: errors[0],
171+              stack: [],
172+            };
173+          }
174+        }
175+
176+        return null;
177+      }
178+}
179+
180+PandaAgent.runtime = fs.readFileSync(runtimePath.for('panda'), 'utf8');
181+module.exports = PandaAgent;
182\ No newline at end of file
183diff --git a/runtimes/panda.js b/runtimes/panda.js
184new file mode 100644
185index 0000000..0acbd09
186--- /dev/null
187+++ b/runtimes/panda.js
188@@ -0,0 +1,44 @@
189+if (!globalThis.$262) {
190+  globalThis.$262 = {
191+    global: globalThis,
192+    evalScript(code) {
193+      try {
194+        global.evalScript(code);
195+        return { type: 'normal', value: undefined };
196+      } catch (e) {
197+        return { type: 'throw', value: e };
198+      }
199+    },
200+    gc() {
201+      throw new Test262Error('gc() not yet supported.');
202+    },
203+    getGlobal(name) {
204+      return global[name];
205+    },
206+    setGlobal(name, value) {
207+      global[name] = value;
208+    },
209+    agent: (function() {
210+      function thrower() {
211+        throw new Test262Error('agent.* not yet supported.');
212+      };
213+      return {
214+        start: thrower,
215+        broadcast: thrower,
216+        getReport: thrower,
217+        sleep: thrower,
218+        monotonicNow: thrower,
219+      };
220+    })(),
221+  };
222+}
223+
224+$262.IsHTMLDDA = function() {};
225+$262.destroy = function() {};
226+$262.getGlobal = function(name) {
227+  return this.global[name];
228+};
229+$262.setGlobal = function(name, value) {
230+  this.global[name] = value;
231+};
232+$262.source = $SOURCE;
233--