Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dherman
Last active January 25, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherman/66627aac764a9795dc3875e270f55918 to your computer and use it in GitHub Desktop.
Save dherman/66627aac764a9795dc3875e270f55918 to your computer and use it in GitHub Desktop.

History

  • worked on this during ES2015 time frame, so never went through stages process
  • got punted to later (rightly so!)
  • goal for today: resume work on this, reassert committee interest via advancing to stage 1

Intuitions

  • sandbox
  • iframe without DOM
  • principled version of Node's 'vm' module
  • sync Worker

Use cases

  • security isolation (with synchronous but coarse-grained communication channel)
  • plugins (e.g., spreadsheet functions)
  • in-browser code editors
  • server-side rendering
  • testing/mocking (e.g., jsdom)
  • in-browser transpilation

Example: simple realm

let realm = new Realm();

let outerGlobal = window;
let innerGlobal = realm.global;

let f = realm.evalScript("(function() { return 17 })");

f() === 17 // true

Reflect.getPrototypeOf(f) === outerFlobal.Function.prototype // false
Reflect.getPrototypeOf(f) === innerGlobal.Function.prototype // true

Example: simple subclass

class EmptyRealm extends Realm {
  constructor(...args) { super(...args); }
  init() { /* do nothing */ }
}

Example: DOM mocking

class FakeWindow extends Realm {
  init() {
    super.init(); // install the standard primordials
    let global = this.global;

    global.document = new FakeDocument(...);
    global.alert = new Proxy(fakeAlert, { ... });
    ...
  }
}

Example: language hooks

class ESNextRealm extends Realm {
  [Realm.directEval](source) { return compile(source); }
  [Realm.indirectEval](source) { ... }
  ...
}

Further considerations

  • realm.evalModule()
  • module registry
  • hooks for manipulating module requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment