Skip to content

Commit 59f5562

Browse files
committed
use new agent api
1 parent fa51801 commit 59f5562

File tree

1 file changed

+92
-94
lines changed

1 file changed

+92
-94
lines changed

src/worker.js

Lines changed: 92 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ importScripts('https://unpkg.com/[email protected]/lib/nearley.js');
88
importScripts('/engine262/engine262.js');
99

1010
const {
11-
initializeAgent,
11+
Agent,
1212
Realm,
1313
Abstract,
1414
AbruptCompletion,
@@ -27,112 +27,110 @@ addEventListener('message', ({ data }) => {
2727
if (data.type === 'evaluate') {
2828
const { state, code } = data.value;
2929

30-
try {
31-
initializeAgent({
32-
features: [...state.get('features')],
33-
});
34-
} catch (e) {
35-
// o.o
36-
}
37-
38-
const promises = new Set();
39-
const realm = new Realm({
40-
promiseRejectionTracker(promise, operation) {
41-
switch (operation) {
42-
case 'reject':
43-
promises.add(promise);
44-
break;
45-
case 'handle':
46-
promises.delete(promise);
47-
break;
48-
default:
49-
break;
50-
}
51-
},
30+
const agent = new Agent({
31+
features: [...state.get('features')],
5232
});
53-
const print = new Value(realm, (args) => {
54-
postMessage({
55-
type: 'console',
56-
value: {
57-
method: 'log',
58-
values: args.map((a) => inspect(a)),
33+
34+
agent.scope(() => {
35+
const promises = new Set();
36+
const realm = new Realm({
37+
promiseRejectionTracker(promise, operation) {
38+
switch (operation) {
39+
case 'reject':
40+
promises.add(promise);
41+
break;
42+
case 'handle':
43+
promises.delete(promise);
44+
break;
45+
default:
46+
break;
47+
}
5948
},
6049
});
61-
return Value.undefined;
62-
}, [], realm);
63-
Abstract.CreateDataProperty(realm.global, new Value(realm, 'print'), print);
50+
const print = new Value(realm, (args) => {
51+
postMessage({
52+
type: 'console',
53+
value: {
54+
method: 'log',
55+
values: args.map((a) => inspect(a)),
56+
},
57+
});
58+
return Value.undefined;
59+
}, [], realm);
60+
Abstract.CreateDataProperty(realm.global, new Value(realm, 'print'), print);
6461

65-
{
66-
const console = new APIObject(realm);
67-
Abstract.CreateDataProperty(realm.global, new Value(realm, 'console'), console);
62+
{
63+
const console = new APIObject(realm);
64+
Abstract.CreateDataProperty(realm.global, new Value(realm, 'console'), console);
6865

69-
[
70-
'log',
71-
'warn',
72-
'debug',
73-
'error',
74-
'clear',
75-
].forEach((method) => {
76-
const fn = new Value(realm, (args) => {
77-
postMessage({
78-
type: 'console',
79-
value: {
80-
method,
81-
values: args.map((a, i) => {
82-
if (i === 0 && Abstract.Type(a) === 'String') {
83-
return a.stringValue();
84-
}
85-
return inspect(a);
86-
}),
87-
},
66+
[
67+
'log',
68+
'warn',
69+
'debug',
70+
'error',
71+
'clear',
72+
].forEach((method) => {
73+
const fn = new Value(realm, (args) => {
74+
postMessage({
75+
type: 'console',
76+
value: {
77+
method,
78+
values: args.map((a, i) => {
79+
if (i === 0 && Abstract.Type(a) === 'String') {
80+
return a.stringValue();
81+
}
82+
return inspect(a);
83+
}),
84+
},
85+
});
86+
return Value.undefined;
8887
});
89-
return Value.undefined;
88+
Abstract.CreateDataProperty(console, new Value(realm, method), fn);
9089
});
91-
Abstract.CreateDataProperty(console, new Value(realm, method), fn);
92-
});
93-
}
94-
95-
postMessage({
96-
type: 'console',
97-
value: {
98-
method: 'clear',
99-
values: [],
100-
},
101-
});
102-
103-
let result;
104-
if (state.get('mode') === 'script') {
105-
result = realm.evaluateScript(code, { specifier: 'code.js' });
106-
} else {
107-
result = realm.createSourceTextModule('code.mjs', code);
108-
if (!(result instanceof AbruptCompletion)) {
109-
const module = result;
110-
realm.moduleEntry = module;
111-
result = module.Link();
112-
if (!(result instanceof AbruptCompletion)) {
113-
result = module.Evaluate();
114-
if (result.PromiseState === 'rejected') {
115-
result = Throw(realm, result.PromiseResult);
116-
}
117-
}
11890
}
119-
}
120-
if (result instanceof AbruptCompletion) {
91+
12192
postMessage({
12293
type: 'console',
12394
value: {
124-
method: 'error',
125-
values: [inspect(result, realm)],
95+
method: 'clear',
96+
values: [],
12697
},
12798
});
128-
}
12999

130-
for (const promise of promises) {
131-
postMessage({
132-
type: 'unhandledRejection',
133-
// eslint-disable-next-line no-use-before-define
134-
value: inspect(promise.PromiseResult, realm),
135-
});
136-
}
100+
let result;
101+
if (state.get('mode') === 'script') {
102+
result = realm.evaluateScript(code, { specifier: 'code.js' });
103+
} else {
104+
result = realm.createSourceTextModule('code.mjs', code);
105+
if (!(result instanceof AbruptCompletion)) {
106+
const module = result;
107+
realm.moduleEntry = module;
108+
result = module.Link();
109+
if (!(result instanceof AbruptCompletion)) {
110+
result = module.Evaluate();
111+
if (result.PromiseState === 'rejected') {
112+
result = Throw(realm, result.PromiseResult);
113+
}
114+
}
115+
}
116+
}
117+
if (result instanceof AbruptCompletion) {
118+
postMessage({
119+
type: 'console',
120+
value: {
121+
method: 'error',
122+
values: [inspect(result, realm)],
123+
},
124+
});
125+
}
126+
127+
for (const promise of promises) {
128+
postMessage({
129+
type: 'unhandledRejection',
130+
// eslint-disable-next-line no-use-before-define
131+
value: inspect(promise.PromiseResult, realm),
132+
});
133+
}
134+
});
137135
}
138136
});

0 commit comments

Comments
 (0)