generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprepare-evaluation.ts
More file actions
387 lines (374 loc) · 11.7 KB
/
prepare-evaluation.ts
File metadata and controls
387 lines (374 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { EvaluatorError } from '@metamask/kernel-errors';
import type { Logger } from '@metamask/logger';
import type { SyntaxNode } from 'tree-sitter';
import { extractNamesFromDeclaration } from './parse/identifiers.ts';
import { ERROR, RETURN } from './symbols.ts';
import type { EvaluatorState, VariableRecord } from './types.ts';
/**
* Creates a wrapper for a function that throws EvaluatorError if the wrapped function throws.
* This is used to wrap $return, $catch, and $capture to differentiate between internal and user errors.
*
* @param func - The function to wrap.
* @returns A hardened function that wraps the original function.
*/
const wrap = <Args extends unknown[]>(
func: (...args: Args) => void,
): ((...args: Args) => void) => {
return harden((...args: Args) => {
try {
func(...args);
} catch (error) {
throw new EvaluatorError(
'REPL evaluation failed',
'',
error instanceof Error ? error : new Error(String(error)),
);
}
});
};
export type Evaluable = {
endowments: {
consts: VariableRecord;
lets: VariableRecord;
$frame: {
$catch: (caught: unknown) => void;
$capture: (...lets: never[]) => void;
$return?: (...values: never[]) => void;
};
};
code: string;
result: EvaluationResult;
commit: () => void;
};
export type EvaluationResult = {
value?: VariableRecord;
[ERROR]?: unknown;
[RETURN]?: unknown;
};
/**
* Wraps an async evaluation in an IIFE to be awaited outside the compartment.
* Assumes a compartment endowed with `{ consts, lets, $catch, $capture }` at
* least.
*
* TODO: Move this functionality to endojs/endo-evaluator
*
* @param args - The arguments to wrap the async evaluation.
* @param args.consts - The consts to destructure.
* @param args.lets - The lets to destructure.
* @param args.code - The code to evaluate.
* @returns Wrapped code ready to evaluate in a compartment endowed with `{ consts, lets, $catch, $capture }`.
*/
const wrapAsyncEvaluation = ({
consts,
lets,
code,
}: {
consts: VariableRecord;
lets: VariableRecord;
code: string;
}): string => {
const constsKeys = Object.keys(consts);
const letsKeys = Object.keys(lets);
const destructureConsts =
constsKeys.length > 0 ? `const { ${constsKeys.join(',')} } = consts;` : '';
const destructureLets =
letsKeys.length > 0 ? `let { ${letsKeys.join(',')} } = lets;` : '';
// The let namespace can be arbitrarily mutated by the statement; the best
// detection is captureion.
const captureLets =
letsKeys.length > 0 ? `$capture(${letsKeys.join(',')});` : '';
// Async IIFE, to be awaited outside the compartment. We are 'vulnerable' to
// return statements, but we only await whatever is returned; we don't read
// the value. We can also prevent top level return via parsing.
return `(async () => {
await null;
const { $capture, $catch, $return } = $frame;
${destructureConsts}
${destructureLets}
try {
${code}
} catch (e) {
$catch(e);
} finally {
${captureLets}
}
})();`;
};
/**
* Make a captor function that captures names from lexical scope into a record.
*
* Building a function factory from source permits captureion of the
* arguments as individual variables while the record to which they are
* assigned is a reference not endowed to the compartment.
*
* The returned function is wrapped with makeSafe to detect internal errors.
*
* @param names - The names to capture.
* @returns A tuple containing the record and a safe-wrapped function that captures the names into the record.
*/
const makeCaptor = (
names: string[],
): [VariableRecord, (...names: string[]) => void] => {
const $value = '$UNIQUE';
if (names.includes($value)) {
throw new Error(`Captor name "${$value}" is reserved`);
}
const value: VariableRecord = {};
const namespace = names.join(',');
// We use eval safely by constructing the function with care and only
// ever evaluating it in a compartment.
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
const captor = Function(
$value,
`return (${namespace}) => void Object.assign(${$value}, { ${namespace} });`,
)(value);
return [value, wrap(captor)];
};
export const prepareEvaluation = (
state: EvaluatorState,
statement: SyntaxNode,
options: { logger?: Logger } = {},
): Evaluable => {
switch (statement.type) {
case 'lexical_declaration':
switch (statement.children[0]?.type) {
case 'const':
return prepareImmutableDeclaration(state, statement, options);
case 'let':
return prepareMutableDeclaration(state, statement, options);
case undefined:
default:
throw new Error(
[
`Unknown lexical_declaration.`,
`statement: ${statement.text}`,
`type: ${statement.toString()}`,
].join('\n'),
);
}
case 'function_declaration':
case 'generator_function_declaration':
return prepareMutableDeclaration(state, statement, options);
case 'variable_declaration':
throw new Error(
`Variable declarations are not allowed: "${statement.text}"`,
);
case 'expression_statement':
return prepareExpression(state, statement, options);
case 'import_statement':
throw new SyntaxError(
'Imports are not allowed. All accessible capabilities are already imported.',
);
case 'if_statement':
case 'for_statement':
case 'for_in_statement':
case 'for_of_statement':
case 'for_await_of_statement':
case 'for_await_in_statement':
case 'for_await_statement':
case 'while_statement':
case 'do_while_statement':
case 'switch_statement':
case 'try_statement':
case 'catch_clause':
case 'finally_clause':
// XXX The above case selector is probably long enough to be the default
return prepareStatement(state, statement, options);
default:
throw new Error(
[
`Unknown statement type.`,
`statement: ${statement.text}`,
`type: ${statement.toString()}`,
].join('\n'),
);
}
};
/**
* Prepare a declaration for evaluation.
*
* @param state - The evaluator state.
* @param statement - The declaration to prepare.
* @returns The prepared declaration.
*/
function prepareDeclaration(
state: EvaluatorState,
statement: SyntaxNode,
): Omit<Evaluable, 'commit'> & { captured: VariableRecord } {
const { consts, lets } = state;
const [captured, $capture] = makeCaptor(Object.keys(lets));
const names = extractNamesFromDeclaration(statement);
const [value, $return] = makeCaptor(names);
const result: EvaluationResult = { value };
const $catch = wrap((caught: unknown) => (result[ERROR] = caught));
return {
endowments: { consts, lets, $frame: { $capture, $catch, $return } },
code: wrapAsyncEvaluation({
consts,
lets,
code: `${statement.text};$return(${names.join(',')});`,
}),
result,
captured,
};
}
/**
* Prepare a mutable declaration (let or function declaration) for evaluation.
*
* @param state - The evaluator state.
* @param statement - The declaration to prepare.
* @param options - The options.
* @param options.logger - The logger.
* @returns The prepared declaration.
*/
function prepareMutableDeclaration(
state: EvaluatorState,
statement: SyntaxNode,
options: { logger?: Logger } = {},
): Evaluable {
const { endowments, code, result, captured } = prepareDeclaration(
state,
statement,
);
const commitLogger = options.logger?.subLogger({ tags: ['commit'] });
return {
endowments,
code,
result,
commit: () => {
commitLogger?.info('captured namespace:', captured);
Object.assign(state.lets, captured);
if (result[ERROR]) {
commitLogger?.info('result error:', result[ERROR]);
return;
}
commitLogger?.info('let declaration:', result.value);
Object.assign(state.lets, result.value);
},
};
}
/**
* Prepare an immutable declaration (const declaration) for evaluation.
*
* @param state - The evaluator state.
* @param statement - The declaration to prepare.
* @param options - The options.
* @param options.logger - The logger.
* @returns The prepared declaration.
*/
function prepareImmutableDeclaration(
state: EvaluatorState,
statement: SyntaxNode,
options: { logger?: Logger } = {},
): Evaluable {
const { endowments, code, result, captured } = prepareDeclaration(
state,
statement,
);
const commitLogger = options.logger?.subLogger({ tags: ['commit'] });
return {
endowments,
code,
result,
commit: () => {
commitLogger?.info('captured namespace:', captured);
Object.assign(state.lets, captured);
if (result[ERROR]) {
commitLogger?.info('result error:', result[ERROR]);
return;
}
commitLogger?.info('const declaration:', result.value);
Object.assign(state.consts, result.value);
},
};
}
/**
* Strips any trailing semicolons from the code.
*
* @param code - The code to strip the trailing semicolons from.
* @returns The code without the trailing semicolons.
*/
const stripTrailingSemicolons = (code: string): string =>
code.trimEnd().endsWith(';')
? stripTrailingSemicolons(code.trimEnd().slice(0, -1))
: code.trimEnd();
/**
* Prepare an expression for evaluation.
*
* @param state - The evaluator state.
* @param statement - The expression to prepare.
* @param options - The options.
* @param options.logger - The logger.
* @returns The prepared expression.
*/
function prepareExpression(
state: EvaluatorState,
statement: SyntaxNode,
options: { logger?: Logger } = {},
): Evaluable {
const { consts, lets } = state;
const [captured, $capture] = makeCaptor(Object.keys(lets));
const result: EvaluationResult = {};
const $return = wrap((value: unknown) => (result[RETURN] = value));
const $catch = wrap((caught: unknown) => (result[ERROR] = caught));
const commitLogger = options.logger?.subLogger({ tags: ['commit'] });
return {
endowments: { consts, lets, $frame: { $capture, $catch, $return } },
code: wrapAsyncEvaluation({
consts,
lets,
code: `$return(${stripTrailingSemicolons(statement.text)});`,
}),
result,
commit: () => {
commitLogger?.info('captured namespace:', captured);
Object.assign(state.lets, captured);
if (result[ERROR]) {
commitLogger?.info('result error:', result[ERROR]);
return;
}
commitLogger?.info('result return:', result[RETURN]);
if (!(RETURN in result)) {
throw new Error(
'Internal: Result is undefined but no error was thrown',
);
}
},
};
}
/**
* Prepare an arbitrary statement for evaluation.
*
* @param state - The evaluator state.
* @param statement - The statement to prepare.
* @param options - The options.
* @param options.logger - The logger.
* @returns The prepared statement.
*/
function prepareStatement(
state: EvaluatorState,
statement: SyntaxNode,
options: { logger?: Logger } = {},
): Evaluable {
const { consts, lets } = state;
const [captured, $capture] = makeCaptor(Object.keys(lets));
const result: EvaluationResult = {};
const $catch = wrap((caught: unknown) => (result[ERROR] = caught));
const commitLogger = options.logger?.subLogger({ tags: ['commit'] });
return {
endowments: { consts, lets, $frame: { $capture, $catch } },
code: wrapAsyncEvaluation({
consts,
lets,
code: statement.text,
}),
result,
commit: () => {
commitLogger?.info('captured namespace:', captured);
Object.assign(state.lets, captured);
if (result[ERROR]) {
commitLogger?.info('result error:', result[ERROR]);
}
},
};
}