Skip to content

Commit

Permalink
ConsoleAgent: patch "Test262Error" with "ESHostError" if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed May 11, 2021
1 parent 32ca3aa commit 7605bb1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
49 changes: 45 additions & 4 deletions lib/ConsoleAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ function generateTempFileName() {
return `f-${now}-${process.pid}-${(Math.random() * 0x100000000 + 1).toString(36)}.js`;
}

const ESHostErrorSource = `
function ESHostError(message) {
this.message = message || "";
}
ESHostError.prototype.toString = function () {
return "ESHostError: " + this.message;
};
ESHostError.thrower = (...args) => {
throw new ESHostError(...args);
};
var $ERROR = ESHostError.thrower;
function $DONOTEVALUATE() {
throw "ESHost: This statement should not be evaluated.";
}
`;

const isMissingTest262ErrorDefinition = header => {
if (!header.includes('Test262Error')) {
return false;
}
try {
const f = new Function(`${header}; return typeof Test262Error == 'undefined';`);
return f();
} catch (error) {
return false;
}
};

const useESHostError = (code) => {
return `${ESHostErrorSource}${code}`.replace(/Test262Error/gm, 'ESHostError');
};

class ConsoleAgent extends Agent {
constructor(options) {
super(options);
Expand Down Expand Up @@ -232,12 +264,21 @@ class ConsoleAgent extends Agent {
return code;
} else {
const prologue = code.match(/^("[^\r\n"]*"|'[^\r\n']*'|[\s\r\n;]*|\/\*[\w\W]*?\*\/|\/\/[^\n]*\n)*/);
const header = prologue
? prologue[0] + runtime
: runtime;

const body = prologue
? code.slice(prologue[0].length)
: code;

if (prologue) {
return prologue[0] + runtime + code.slice(prologue[0].length);
} else {
return runtime + code;
const compiledCode = `${header}${body}`;

if (isMissingTest262ErrorDefinition(header)) {
return useESHostError(compiledCode);
}

return compiledCode;
}
}

Expand Down
37 changes: 9 additions & 28 deletions test/consoleagent.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,30 +309,17 @@ describe("ConsoleAgent", () => {
});

it("Does not call agent.compile() when attrs.flags.raw === true", async () => {
const a = new ConsoleAgent();
return Promise.resolve(a).then((agent) => {
return Promise.all([
// Initiate a script evaluation
agent.evalScript(defaultTestRecord, {}),

// Since we control the child process,
// we need to wait a moment and then send
// a close event to end the script evaluation
new Promise((resolve) => {
setTimeout(() => {
child.emit("close");
resolve();
}, 100);
}),
]).then(() => {
expect(compile.callCount).toEqual(0);
});
});
const agent = new ConsoleAgent();

agent.evalScript(defaultTestRecord, {});

child.emit("close");

expect(compile.callCount).toBe(0);
});

it("Does call agent.compile() when attrs.flags.raw !== true", async () => {
// const a = new ConsoleAgent();
const agent = await new ConsoleAgent();
const agent = new ConsoleAgent();

let record = {
...defaultTestRecord,
Expand All @@ -343,17 +330,11 @@ describe("ConsoleAgent", () => {
},
};

const result = agent.evalScript(record, {});

// Since we control the child process,
// we need to wait a moment and then send
// a close event to end the script evaluation
await timeout(100);
agent.evalScript(record, {});

child.emit("close");

expect(compile.callCount).toBe(1);
expect(result).toMatchInlineSnapshot(`Promise {}`);
});
});
});

0 comments on commit 7605bb1

Please sign in to comment.