ShadowRealm API Polyfill
依照 ShadowRealm API 提案实现的 JavaScript 沙箱,使用 TC39 Test262 的用例进行测试。
简体中文 | English
declare class ShadowRealm {
constructor();
evaluate(sourceText: string): Primitive | Function;
importValue(specifier: string, bindingName: string): Promise<Primitive | Function>;
}
安装
npm i -S shadowrealm-api
使用
Ponyfill: 无侵入性
import ShadowRealm from 'shadowrealm-api'
const realm = new ShadowRealm();
Polyfill: 修补全局对象
import 'shadowrealm-api/dist/polyfill'
const realm = new ShadowRealm();
调试
打印内部调试信息
ShadowRealm.__debug = true;
限制
- 在 ShadowRealm 中运行的所有代码都处于严格模式下;
- ESM 语句不能含有冗余的注释;
`
js // ❌ import/* /defaultExport from "module-name"; export default/ */'xxx';
// ✅ import defaultExport from "module-name"; export default 'xxx';
3. 不支持导出变量声明;
```js
// ❌
export const obj = {...}, fn = () => {...};
// ✅
const obj = {...}, fn = () => {...};
export { obj, fn };
兼容性
IE | Edge | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|---|
|14 | 29[1][2] | 32[1][2] | 8[2][3] | 19[1][2] | |
| | 41 | 49 | 10.1[3] | 36 | |
| | | | 14.1 |
Notes:
- ESM 语句不支持解构赋值;
- 顶层作用域需要
fetch
垫片;- 顶层作用域需要
URL
垫片;
使用垫片:
import "fetch polyfill";
import "URL polyfill";
import "shadowrealm-api/dist/polyfill";
// 你的代码
联系
- 微信: cai_fanwei
- QQ群: 663286147
- 邮箱: ambit_tsai@qq.com