MCPcopy Index your code
hub / github.com/ambit-tsai/shadowrealm-api

github.com/ambit-tsai/shadowrealm-api @v0.8.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.8.2 ↗ · + Follow
78 symbols 178 edges 80 files 1 documented · 1% updated 3y agov0.8.2 · 2022-06-21★ 1364 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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;

限制

  1. 在 ShadowRealm 中运行的所有代码都处于严格模式下;
  2. ESM 语句不能含有冗余的注释;
// ❌
import/* */defaultExport from "module-name";
export default/* */'xxx';

// ✅
import defaultExport from "module-name";
export default 'xxx';
  1. 不支持导出变量声明;
// ❌
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: 1. ESM 语句不支持解构赋值; 1. 顶层作用域需要fetch垫片; 1. 顶层作用域需要URL垫片;

使用垫片:

import "fetch polyfill";
import "URL polyfill";
import "shadowrealm-api/dist/polyfill";
// 你的代码

联系

  1. 微信: cai_fanwei
  2. QQ群: 663286147
  3. 邮箱: ambit_tsai@qq.com

Extension points exported contracts — how you extend this code

ShadowRealmConstructor (Interface)
(no doc)
src/type.ts
Window (Interface)
(no doc)
src/polyfill.ts
ShadowRealm (Interface)
(no doc)
src/type.ts
BuiltinShadowRealm (Interface)
(no doc)
src/type.ts
RealmRecord (Interface)
(no doc)
src/type.ts
Module (Interface)
(no doc)
src/type.ts

Core symbols most depended-on inside this repo

evaluate
called by 142
src/type.ts
apply
called by 11
docs/assets/index.ff870cb1.js
importValue
called by 8
src/type.ts
isObject
called by 6
src/helpers.ts
replace
called by 5
src/helpers.ts
r
called by 4
docs/assets/index.ff870cb1.js
T
called by 4
docs/assets/index.ff870cb1.js
m
called by 3
docs/assets/index.ff870cb1.js

Shape

Function 70
Interface 6
Method 2

Languages

TypeScript100%

Modules by API surface

docs/assets/index.ff870cb1.js29 symbols
src/RealmRecord.ts9 symbols
src/type.ts7 symbols
src/helpers.ts7 symbols
src/ShadowRealm.ts7 symbols
src/es-module/index.ts6 symbols
test/cases/prototype/evaluate/wrapped-function-throws-typeerror-on-non-primitive-arguments.js4 symbols
src/es-module/helpers.ts2 symbols
test/cases/prototype/importValue/throws-if-exportname-not-string.js1 symbols
test/cases/prototype/evaluate/wrapped-function-proxied-observes-boundary.js1 symbols
test/cases/prototype/evaluate/wrapped-function-observing-their-scopes.js1 symbols
test/cases/prototype/evaluate/wrapped-function-arguments-are-wrapped-into-the-inner-realm.js1 symbols

For agents

$ claude mcp add shadowrealm-api \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page