A Compartment object abstracts the notion of a distinct global environment, with its own global object, copy of the standard library, but shares the "intrinsics" (standard objects that are not bound to global variables, like the initial value of Object.prototype) with the current execution environment.
class Compartment {
constructor: (
globals : object?, // extra globals added to the global object
modules: object?, // module map, specifier to specifier
) -> object,
// public methods
import(specifier: string) -> promise, // same argument/return value as dynamic import
// inherited accessor properties
get global() -> object, // access this compartment global object
}
- Overrides the native Compartment API. The native one isn't available anymore (except to the shim itself).
- Uses a 3-level module map to go from specifier to specifier withought resorting to symbols. The original Compartment.map isn't available or necessary.
Only bare specifiers are supported (mostly an XS limitation). A clever mapping mechanism need to be developed.
This is an port of the "variation" compartment example from XS: https://github.com/Moddable-OpenSource/moddable/tree/public/examples/js/compartments/variations
The changes are:
- The example files are moved to a
sandbox
directory. - The top level 'main.js
loads the
sandbox/main.js` and passes it a modules map. - The native XS Compartment API is not available anywhere except to the
Compartment
module. - the
sandbox/main
uses the new API.
The API has changed as folllow:
// XS
let { decrement, mod } = Compartment.map;
let mod1 = new Compartment("mod", { name:"mod1" }, { mod, vary:decrement });
mod1.export.test();
// Shim
const cmp1 = new Compartment({ name: 'mod1' }, { '*': { 'mod': 'mod', 'vary': 'decrement' } });
const mod1 = await cmp1.import('mod');
mod1.test();
Place all files in a moddable examples/js/compartments/variation-2
folder.
manifest.json
main.js
Compartment.js
sandbox/
main.js
mod.js
decrement.js
increment.js
Then open XSBug, cd into the directory from terminal, and run mcconfig -m -d
at the command-line.
Same output as the original XS example, plus a few debugging items.
Compartment imported
Compartment.map: Resource,instrumentation,screen,time,timer,Compartment,main,sandbox/main,sandbox/mod,sandbox/increment,sandbox/decrement,mc/config
main imported
Compartment imported
Compartment.map: Compartment,main,mod,increment,decrement
Importing main
sandbox/main imported
Compartment imported
Compartment.map: Compartment,mod,vary
Compartment imported
Compartment.map: Compartment,mod,vary
Importing mod
decrement imported
sandbox/mod imported
Importing mod
increment imported
sandbox/mod imported
mod1 0
mod2 0
mod1 -1
mod2 1
MM says for hosting-completeness, compartment instance should have an
eval()
method that takes options.