Skip to content

Instantly share code, notes, and snippets.

@jfparadis
Created January 24, 2020 04:14
Show Gist options
  • Save jfparadis/2eb91005635240be42a052e37fcc0296 to your computer and use it in GitHub Desktop.
Save jfparadis/2eb91005635240be42a052e37fcc0296 to your computer and use it in GitHub Desktop.
Testing global powers in XS

Testing global powers in XS

This test checks the status of global powers, in top level main and in a compartment.

Output

*** Testing from main
new Date(): Thu Jan 23 2020 20:13:36 GMT-0800
Date.now(): 1579839216141
Math.random(): 0.6539189622988547
Error.captureStackTrace: undefined
Error.stackTraceLimit: undefined
Error.prototype.stack: undefined
RegExp static methods: prototype,Symbol(Symbol.species)
*** Testing from compartment
new Date(): Thu Jan 23 2020 20:13:36 GMT-0800
Date.now(): 1579839216141
Math.random(): 0.41599935685098144
Error.captureStackTrace: undefined
Error.stackTraceLimit: undefined
Error.prototype.stack: undefined
RegExp static methods: prototype,Symbol(Symbol.species)
import { test } from "test";
trace('from main\n');
test();
trace('from compartment\n');
new Compartment("test").export.test();
{
"include": "$(MODDABLE)/examples/manifest_base.json",
"modules": {
"*": [
"./main",
"./test",
],
},
"preload": [
],
}
function measure(msg, fn) {
trace(`${msg}: ${fn()}\n`);
}
function desc(obj, prop) {
if (!obj) return undefined;
const desc = Object.getOwnPropertyDescriptor(obj, prop);
if (!desc) return undefined;
if (desc.value) return `value ${desc.value}`;
if (desc.get || desc.set) return `accessor`;
throw new TypeError();
}
export function test() {
measure("new Date()", () => new Date);
measure("Date.now()", () => Date.now());
measure("Math.random()", () => Math.random());
measure("Error.captureStackTrace", () => desc(Error, 'captureStackTrace'));
measure("Error.stackTraceLimit", () => desc(Error, 'stackTraceLimit'));
measure("Error.prototype.stack", () => desc(Error.prototype, 'stack'));
measure("RegExp static methods", () => [
...Object.getOwnPropertyNames(RegExp),
...Object.getOwnPropertySymbols(RegExp).map(symbol => symbol.toString()),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment