README
ShadowRealm API Polyfill
A implementation of the ShadowRealm API Proposal, a JavaScript sandbox, test with TC39 Test262 cases.
įŽäŊä¸æ | English
declare class ShadowRealm {
constructor();
evaluate(sourceText: string): Primitive | Function;
importValue(specifier: string, bindingName: string): Promise<Primitive | Function>;
}
Install
npm i -S shadowrealm-api
Usage
Ponyfill: non-invasive
import ShadowRealm from 'shadowrealm-api'
const realm = new ShadowRealm();
Polyfill: patch up the global object
import 'shadowrealm-api/browser/polyfill.mjs'
const realm = new ShadowRealm();
Debugging
Print internal info for debugging
ShadowRealm.__debug = true;
Limitations
- All code evaluated inside a ShadowRealm runs in strict mode;
- The ESM statement must not contain redundant comments;
// â
import/* */defaultExport from "module-name";
export default/* */'xxx';
// â
import defaultExport from "module-name";
export default 'xxx';
- Exporting variable declarations is not supported;
// â
export const obj = {...}, fn = () => {...};
// â
const obj = {...}, fn = () => {...};
export { obj, fn };
Compatibility
IE | Edge | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|---|
10[1][2][3] | 12[1][2][3] | 4[1][2][3][4] | 13[1][2][3][4] | 6[1][2][3] | 12.1[1][2][3][4] |
14 | 19[1][2][3] | 19[1][2][3] | 8[3] | 15[1][2][3] | |
29[1][3] | 32[1][3] | 10.1 | 19[1][3] | ||
41 | 49 | 36 |
Notes:
- Don't support destructuring assignment in ESM statement;
- Need
Promise
polyfill in ShadowRealm;- Need
fetch
polyfill in top window;- Need
URL
polyfill in top window;
Use fetch
or URL
polyfill:
import "fetch polyfill";
import "URL polyfill";
import "shadowrealm-api/browser/polyfill.mjs";
// Your codes
Use Promise
polyfill:
ShadowRealm.__shims = [
'path/to/promise-polyfill.js',
'other polyfills',
];
Contact
- WeChat: cai_fanwei
- QQ Group: 663286147
- E-mail: ambit_tsai@qq.com