Skip to content

Commit 3d02d14

Browse files
committed
fix: classic playground
1 parent bf3bf99 commit 3d02d14

1 file changed

Lines changed: 45 additions & 34 deletions

File tree

src/classic/worker.mjs

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import { createConsole } from '../../lib/inspector.mjs';
23
import {
34
Agent,
@@ -14,6 +15,7 @@ import {
1415
createTest262Intrinsics,
1516
importBundledTest262Harness,
1617
boostTest262Harness,
18+
runJobQueue,
1719
} from '../../lib/engine262.mjs';
1820

1921
postMessage({
@@ -33,31 +35,35 @@ addEventListener('message', ({ data }) => {
3335
if (data.type === 'evaluate') {
3436
const { state, code } = data.value;
3537

38+
const promises = new Set();
3639
const agent = new Agent({
3740
features: [...state.get('features')],
3841
onDebugger() {
3942
// Note: If you're reading this, you should try our new inspector that supports real debugger
4043
// https://engine262.js.org/next.html
4144
debugger;
45+
agent.resumeEvaluate({});
4246
},
47+
hostHooks: {
48+
HostPromiseRejectionTrackers: new Set([
49+
(promise, operation) => {
50+
switch (operation) {
51+
case 'reject':
52+
promises.add(promise);
53+
break;
54+
case 'handle':
55+
promises.delete(promise);
56+
break;
57+
default:
58+
break;
59+
}
60+
},
61+
]),
62+
}
4363
});
4464
setSurroundingAgent(agent);
4565

46-
const promises = new Set();
47-
const realm = new ManagedRealm({
48-
promiseRejectionTracker(promise, operation) {
49-
switch (operation) {
50-
case 'reject':
51-
promises.add(promise);
52-
break;
53-
case 'handle':
54-
promises.delete(promise);
55-
break;
56-
default:
57-
break;
58-
}
59-
},
60-
});
66+
const realm = new ManagedRealm({});
6167

6268
realm.scope(() => {
6369
const print = CreateBuiltinFunction((args) => {
@@ -104,28 +110,33 @@ addEventListener('message', ({ data }) => {
104110
}
105111

106112
let result;
113+
function handleResult(/** @type {import('../../lib/engine262.mjs').ValueCompletion} */ completion) {
114+
result = completion;
115+
if (result instanceof AbruptCompletion) {
116+
postMessage({
117+
type: 'console',
118+
value: {
119+
method: 'error',
120+
values: [inspect(result)],
121+
},
122+
});
123+
}
124+
125+
for (const promise of promises) {
126+
postMessage({
127+
type: 'unhandledRejection',
128+
// eslint-disable-next-line no-use-before-define
129+
value: inspect(promise.PromiseResult),
130+
});
131+
}
132+
}
107133
if (state.get('mode') === 'script') {
108-
result = realm.evaluateScript(code, { specifier: 'code.js' });
134+
result = realm.evaluateScript(code, { specifier: 'code.js' }, handleResult);
109135
} else {
110-
result = realm.evaluateModule(code, 'code.mjs');
111-
}
112-
if (result instanceof AbruptCompletion) {
113-
postMessage({
114-
type: 'console',
115-
value: {
116-
method: 'error',
117-
values: [inspect(result)],
118-
},
119-
});
120-
}
121-
122-
for (const promise of promises) {
123-
postMessage({
124-
type: 'unhandledRejection',
125-
// eslint-disable-next-line no-use-before-define
126-
value: inspect(promise.PromiseResult),
127-
});
136+
result = realm.evaluateModule(code, 'code.mjs', handleResult);
128137
}
138+
if (!result) agent.resumeEvaluate({});
139+
runJobQueue();
129140
});
130141
}
131142
});

0 commit comments

Comments
 (0)