ShadowRealms are a distinct global environment, with its own global object containing its own intrinsics and built-ins (standard objects that are not bound to global variables, like the initial value of Object.prototype).
class ShadowRealm | globals module | Bun BuildDocsReferenceGuidesBlogDiscord/Globals/ShadowRealmMevaluateMimportValue BuildDocsReferenceGuidesBlogDiscord /Globals/ShadowRealmMevaluateMimportValue evaluate(sourceText: string): any;importValue(specifier: string,bindingName: string): Promiseany>;
Search the reference...
/
class
ShadowRealmclass ShadowRealmconst red = new ShadowRealm();
// realms can import modules that will execute within it's own environment.
// When the module is resolved, it captured the binding value, or creates a new
// wrapped function that is connected to the callable binding.
const redAdd = await red.importValue('./inside-code.js', 'add');
// redAdd is a wrapped function exotic object that chains it's call to the
// respective imported binding.
let result = redAdd(2, 3);
console.assert(result === 5); // yields true
// The evaluate method can provide quick code evaluation within the constructed
// shadowRealm without requiring any module loading, while it still requires CSP
// relaxing.
globalThis.someValue = 1;
red.evaluate('globalThis.someValue = 2'); // Affects only the ShadowRealm's global
console.assert(globalThis.someValue === 1);
// The wrapped functions can also wrap other functions the other way around.
const setUniqueValue =
await red.importValue('./inside-code.js', 'setUniqueValue');
// setUniqueValue = (cb) => (cb(globalThis.someValue) * 2);
result = setUniqueValue((x) => x ** 3);
console.assert(result === 16); // yields true
Creates a new ShadowRealm
const red = new ShadowRealm();
// realms can import modules that will execute within it's own environment.
// When the module is resolved, it captured the binding value, or creates a new
// wrapped function that is connected to the callable binding.
const redAdd = await red.importValue('./inside-code.js', 'add');
// redAdd is a wrapped function exotic object that chains it's call to the
// respective imported binding.
let result = redAdd(2, 3);
console.assert(result === 5); // yields true
// The evaluate method can provide quick code evaluation within the constructed
// shadowRealm without requiring any module loading, while it still requires CSP
// relaxing.
globalThis.someValue = 1;
red.evaluate('globalThis.someValue = 2'); // Affects only the ShadowRealm's global
console.assert(globalThis.someValue === 1);
// The wrapped functions can also wrap other functions the other way around.
const setUniqueValue =
await red.importValue('./inside-code.js', 'setUniqueValue');
// setUniqueValue = (cb) => (cb(globalThis.someValue) * 2);
result = setUniqueValue((x) => x ** 3);
console.assert(result === 16); // yields true
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlogToolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →class ShadowRealm | globals module | Bun,AI智能索引,全网链接索引,智能导航,网页索引
- ShadowRealms are a distinct global environment, with its own global object
containing its own intrinsics and built-ins (standard objects that are not
bound to global variables, like the initial value of Object.prototype).