# shadowrealm-api **Repository Path**: ambit/shadowrealm-api ## Basic Information - **Project Name**: shadowrealm-api - **Description**: 🗳️ 安全的 JavaScript 沙箱,在浏览器中提供独立于全局的隔离运行时环境。依照 ShadowRealm API 提案实现,使用 TC39 Test262 的用例进行测试。 - **Primary Language**: TypeScript - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: https://ambit-tsai.github.io/shadowrealm-api/ - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 2 - **Created**: 2021-11-09 - **Last Updated**: 2024-10-14 ## Categories & Tags **Categories**: javascript-toolkits **Tags**: 沙箱, sandbox, Realm, 沙盒 ## README # ShadowRealm API Polyfill A implementation of the ShadowRealm API Proposal, a JavaScript sandbox, test with TC39 Test262 cases. [简体中文](https://gitee.com/ambit/shadowrealm-api) | English ```ts declare class ShadowRealm { constructor(); evaluate(sourceText: string): Primitive | Function; importValue(specifier: string, bindingName: string): Promise; } ``` Try it now 🎉 ## Install ``` npm i -S shadowrealm-api ``` ## Usage ### Po**n**yfill: non-invasive ```javascript import ShadowRealm from 'shadowrealm-api' const realm = new ShadowRealm(); ``` ### Po**l**yfill: patch up the global object ```javascript import 'shadowrealm-api/dist/polyfill' const realm = new ShadowRealm(); ``` ## Debugging Print internal info for debugging ```js ShadowRealm.__debug = true; ``` ## Limitations 1. All code evaluated inside a ShadowRealm runs in **strict mode**; 2. The ESM statement must not contain redundant comments; ```js // ❌ import/* */defaultExport from "module-name"; export default/* */'xxx'; // ✅ import defaultExport from "module-name"; export default 'xxx'; ``` 3. Exporting variable declarations is not supported; ```js // ❌ export const obj = {...}, fn = () => {...}; // ✅ const obj = {...}, fn = () => {...}; export { obj, fn }; ``` ## Compatibility |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: > 1. Don't support destructuring assignment in ESM statement; > 1. Need `fetch` polyfill in top window; > 1. Need `URL` polyfill in top window; Use polyfills: ```js import "fetch polyfill"; import "URL polyfill"; import "shadowrealm-api/dist/polyfill"; // Your codes ``` ## Contact 1. WeChat: cai_fanwei 1. QQ Group: 663286147 1. E-mail: ambit_tsai@qq.com