* tameFunctionConstructors() * This block replaces the original Function constructor, and the original * %GeneratorFunction% %AsyncFunction% and %AsyncGeneratorFunction%, with * safe replacements that throw if invoked.
()
| 4242 | * safe replacements that throw if invoked. |
| 4243 | */ |
| 4244 | function tameFunctionConstructors() { |
| 4245 | try { |
| 4246 | // Verify that the method is not callable. |
| 4247 | // eslint-disable-next-line @endo/no-polymorphic-call |
| 4248 | FERAL_FUNCTION.prototype.constructor('return 1'); |
| 4249 | }catch( ignore) { |
| 4250 | // Throws, no need to patch. |
| 4251 | return freeze({}); |
| 4252 | } |
| 4253 | |
| 4254 | const newIntrinsics= {}; |
| 4255 | |
| 4256 | /* |
| 4257 | * The process to repair constructors: |
| 4258 | * 1. Create an instance of the function by evaluating syntax |
| 4259 | * 2. Obtain the prototype from the instance |
| 4260 | * 3. Create a substitute tamed constructor |
| 4261 | * 4. Replace the original constructor with the tamed constructor |
| 4262 | * 5. Replace tamed constructor prototype property with the original one |
| 4263 | * 6. Replace its [[Prototype]] slot with the tamed constructor of Function |
| 4264 | */ |
| 4265 | function repairFunction(name, intrinsicName, declaration) { |
| 4266 | let FunctionInstance; |
| 4267 | try { |
| 4268 | // eslint-disable-next-line no-eval, no-restricted-globals |
| 4269 | FunctionInstance= (0, eval)(declaration); |
| 4270 | }catch( e) { |
| 4271 | if( e instanceof SyntaxError) { |
| 4272 | // Prevent failure on platforms where async and/or generators |
| 4273 | // are not supported. |
| 4274 | return; |
| 4275 | } |
| 4276 | // Re-throw |
| 4277 | throw e; |
| 4278 | } |
| 4279 | const FunctionPrototype= getPrototypeOf(FunctionInstance); |
| 4280 | |
| 4281 | // Prevents the evaluation of source when calling constructor on the |
| 4282 | // prototype of functions. |
| 4283 | // eslint-disable-next-line func-names |
| 4284 | const InertConstructor= function() { |
| 4285 | throw TypeError( |
| 4286 | 'Function.prototype.constructor is not a valid constructor.'); |
| 4287 | |
| 4288 | }; |
| 4289 | defineProperties(InertConstructor, { |
| 4290 | prototype: { value: FunctionPrototype}, |
| 4291 | name: { |
| 4292 | value: name, |
| 4293 | writable: false, |
| 4294 | enumerable: false, |
| 4295 | configurable: true}}); |
| 4296 | |
| 4297 | |
| 4298 | |
| 4299 | defineProperties(FunctionPrototype, { |
| 4300 | constructor: { value: InertConstructor}}); |
| 4301 |
no test coverage detected