|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | // eslint-disable-next-line no-invalid-this, no-shadow
|
4 |
| -const {GeneratorFunction, AsyncFunction, AsyncGeneratorFunction, global, Contextify, host} = this; |
| 4 | +const {GeneratorFunction, AsyncFunction, AsyncGeneratorFunction, global, internal, host, hook} = this; |
| 5 | +const {Contextify, Decontextify} = internal; |
5 | 6 | // eslint-disable-next-line no-shadow
|
6 |
| -const {Function, eval: eval_, Promise, Object, Reflect, RegExp, VMError} = global; |
7 |
| -const {setPrototypeOf, getOwnPropertyDescriptor, defineProperty} = Object; |
| 7 | +const {Function, eval: eval_, Promise, Object, Reflect} = global; |
| 8 | +const {getOwnPropertyDescriptor, defineProperty, assign} = Object; |
8 | 9 | const {apply: rApply, construct: rConstruct} = Reflect;
|
9 |
| -const {test} = RegExp.prototype; |
10 | 10 |
|
11 |
| -function rejectAsync() { |
12 |
| - throw new VMError('Async not available'); |
13 |
| -} |
14 |
| - |
15 |
| -const testAsync = setPrototypeOf(/\basync\b/, null); |
16 |
| - |
17 |
| -function checkAsync(source) { |
18 |
| - // Filter async functions, await can only be in them. |
19 |
| - if (rApply(test, testAsync, [source])) { |
20 |
| - throw rejectAsync(); |
21 |
| - } |
22 |
| - return source; |
23 |
| -} |
24 |
| - |
25 |
| -const AsyncCheckHandler = { |
| 11 | +const FunctionHandler = { |
26 | 12 | __proto__: null,
|
27 | 13 | apply(target, thiz, args) {
|
28 |
| - let i; |
29 |
| - for (i=0; i<args.length; i++) { |
30 |
| - // We want a exception here if args[i] is a Symbol |
31 |
| - // since Function does the same thing |
32 |
| - args[i] = checkAsync('' + args[i]); |
| 14 | + const type = this.type; |
| 15 | + args = Decontextify.arguments(args); |
| 16 | + try { |
| 17 | + args = Contextify.value(hook(type, args)); |
| 18 | + } catch (e) { |
| 19 | + throw Contextify.value(e); |
33 | 20 | }
|
34 |
| - return rApply(target, undefined, args); |
| 21 | + return rApply(target, thiz, args); |
35 | 22 | },
|
36 | 23 | construct(target, args, newTarget) {
|
37 |
| - let i; |
38 |
| - for (i=0; i<args.length; i++) { |
39 |
| - // We want a exception here if args[i] is a Symbol |
40 |
| - // since Function does the same thing |
41 |
| - args[i] = checkAsync('' + args[i]); |
| 24 | + const type = this.type; |
| 25 | + args = Decontextify.arguments(args); |
| 26 | + try { |
| 27 | + args = Contextify.value(hook(type, args)); |
| 28 | + } catch (e) { |
| 29 | + throw Contextify.value(e); |
42 | 30 | }
|
43 |
| - return rConstruct(target, args); |
| 31 | + return rConstruct(target, args, newTarget); |
44 | 32 | }
|
45 | 33 | };
|
46 | 34 |
|
47 |
| -const AsyncEvalHandler = { |
48 |
| - __proto__: null, |
49 |
| - apply(target, thiz, args) { |
50 |
| - if (args.length === 0) return undefined; |
51 |
| - const script = args[0]; |
52 |
| - if (typeof script !== 'string') { |
53 |
| - // Eval does the same thing |
54 |
| - return script; |
55 |
| - } |
56 |
| - checkAsync(script); |
57 |
| - return eval_(script); |
58 |
| - } |
59 |
| -}; |
| 35 | +function makeCheckFunction(type) { |
| 36 | + return assign({ |
| 37 | + __proto__: null, |
| 38 | + type |
| 39 | + }, FunctionHandler); |
| 40 | +} |
60 | 41 |
|
61 | 42 | function override(obj, prop, value) {
|
62 | 43 | const desc = getOwnPropertyDescriptor(obj, prop);
|
63 | 44 | desc.value = value;
|
64 | 45 | defineProperty(obj, prop, desc);
|
65 | 46 | }
|
66 | 47 |
|
67 |
| -const proxiedFunction = new host.Proxy(Function, AsyncCheckHandler); |
| 48 | +const proxiedFunction = new host.Proxy(Function, makeCheckFunction('function')); |
68 | 49 | override(Function.prototype, 'constructor', proxiedFunction);
|
69 | 50 | if (GeneratorFunction) {
|
70 | 51 | Object.setPrototypeOf(GeneratorFunction, proxiedFunction);
|
71 |
| - override(GeneratorFunction.prototype, 'constructor', new host.Proxy(GeneratorFunction, AsyncCheckHandler)); |
| 52 | + override(GeneratorFunction.prototype, 'constructor', new host.Proxy(GeneratorFunction, makeCheckFunction('generator_function'))); |
72 | 53 | }
|
73 |
| -if (AsyncFunction || AsyncGeneratorFunction) { |
74 |
| - const AsyncFunctionRejectHandler = { |
75 |
| - __proto__: null, |
76 |
| - apply: rejectAsync, |
77 |
| - construct: rejectAsync |
78 |
| - }; |
79 |
| - if (AsyncFunction) { |
80 |
| - Object.setPrototypeOf(AsyncFunction, proxiedFunction); |
81 |
| - override(AsyncFunction.prototype, 'constructor', new host.Proxy(AsyncFunction, AsyncFunctionRejectHandler)); |
82 |
| - } |
83 |
| - if (AsyncGeneratorFunction) { |
84 |
| - Object.setPrototypeOf(AsyncGeneratorFunction, proxiedFunction); |
85 |
| - override(AsyncGeneratorFunction.prototype, 'constructor', new host.Proxy(AsyncGeneratorFunction, AsyncFunctionRejectHandler)); |
86 |
| - } |
| 54 | +if (AsyncFunction) { |
| 55 | + Object.setPrototypeOf(AsyncFunction, proxiedFunction); |
| 56 | + override(AsyncFunction.prototype, 'constructor', new host.Proxy(AsyncFunction, makeCheckFunction('async_function'))); |
| 57 | +} |
| 58 | +if (AsyncGeneratorFunction) { |
| 59 | + Object.setPrototypeOf(AsyncGeneratorFunction, proxiedFunction); |
| 60 | + override(AsyncGeneratorFunction.prototype, 'constructor', new host.Proxy(AsyncGeneratorFunction, makeCheckFunction('async_generator_function'))); |
87 | 61 | }
|
88 | 62 |
|
89 |
| -global.Function = Function.prototype.constructor; |
90 |
| -global.eval = new host.Proxy(eval_, AsyncEvalHandler); |
| 63 | +global.Function = proxiedFunction; |
| 64 | +global.eval = new host.Proxy(eval_, makeCheckFunction('eval')); |
91 | 65 |
|
92 | 66 | if (Promise) {
|
93 |
| - const AsyncRejectHandler = { |
94 |
| - __proto__: null, |
95 |
| - apply: rejectAsync |
96 |
| - }; |
97 | 67 |
|
98 |
| - Promise.prototype.then = new host.Proxy(Promise.prototype.then, AsyncRejectHandler); |
| 68 | + Promise.prototype.then = new host.Proxy(Promise.prototype.then, makeCheckFunction('promise_then')); |
99 | 69 | Contextify.connect(host.Promise.prototype.then, Promise.prototype.then);
|
100 | 70 |
|
101 | 71 | if (Promise.prototype.finally) {
|
102 |
| - Promise.prototype.finally = new host.Proxy(Promise.prototype.finally, AsyncRejectHandler); |
| 72 | + Promise.prototype.finally = new host.Proxy(Promise.prototype.finally, makeCheckFunction('promise_finally')); |
103 | 73 | Contextify.connect(host.Promise.prototype.finally, Promise.prototype.finally);
|
104 | 74 | }
|
105 | 75 | if (Promise.prototype.catch) {
|
106 |
| - Promise.prototype.catch = new host.Proxy(Promise.prototype.catch, AsyncRejectHandler); |
| 76 | + Promise.prototype.catch = new host.Proxy(Promise.prototype.catch, makeCheckFunction('promise_catch')); |
107 | 77 | Contextify.connect(host.Promise.prototype.catch, Promise.prototype.catch);
|
108 | 78 | }
|
109 | 79 |
|
|
0 commit comments