// test262 API // https://github.com/dckc/test262/blob/master/INTERPRETING.md#host-defined-functions function print(x) { console.log('R0: ', x); // prefix with realmId for clarity } const $ = ( function() { 'use strict'; const vm = require('vm'); const notImplemented = () => { throw("not implemented. not needed for this test."); }; let nextRealmId = 1; function createRealm() { const realmId = nextRealmId++; const global = {}; const context = vm.createContext(global); const $ = Object.freeze({ createRealm: createRealm, detachArrayBuffer: notImplemented, evalScript: script => vm.runInContext( script, context, { filename: `realm${realmId}`, lineOffset: /*see below*/ 47 }), global: global }); // print and $ must be "writable, configurable, non-enumerable properties of the global scope". // Does this code do so? global.print = x => console.log(`R${realmId}: `, x); global.$ = $; return $; } return Object.freeze({ createRealm: createRealm, detachArrayBuffer: notImplemented, evalScript: script => notImplemented, global: this }); })(); // test case starts on line 47 var script = '(' + function () { 'use strict'; $.global.x = 10; Object.defineProperty($.global, 'x', { writable: false, enumerable: false, configurable: false, value: 20 }); try { x = 30; } catch (e) { print(e); } print(x); // should be 20 print(Object.getOwnPropertyDescriptor($.global, 'x')); } + ')()'; var context_$ = $.createRealm(); context_$.evalScript(script);