0.8.4 • Published 6 months ago
@haohanyang/shadowrealm-api v0.8.4
ShadowRealm API Polyfill
A implementation of the ShadowRealm API Proposal, a JavaScript sandbox, test with TC39 Test262 cases.
declare class ShadowRealm {
constructor();
evaluate(sourceText: string): Primitive | Function;
importValue(specifier: string, bindingName: string): Promise<Primitive | Function>;
}
Try it now 🎉
Install
npm i -S @haohanyang/shadowrealm-api
Usage
Ponyfill: non-invasive
import ShadowRealm from '@haohanyang/shadowrealm-api'
const realm = new ShadowRealm();
Polyfill: patch up the global object
import '@haohanyang/shadowrealm-api/dist/polyfill'
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 |
---|---|---|---|---|---|
14 | 291 | 321 | 82 | 191 | |
41 | 49 | 10.13 | 36 | ||
14.1 |
Notes: 1. Don't support destructuring assignment in ESM statement; 1. Need
fetch
polyfill in top window; 1. NeedURL
polyfill in top window;
Use polyfills:
import "fetch polyfill";
import "URL polyfill";
import "@haohanyang/shadowrealm-api/dist/polyfill";
// Your codes