Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Objects #129

Closed
wants to merge 2 commits into from
Closed

Export Objects #129

wants to merge 2 commits into from

Conversation

caridy
Copy link
Contributor

@caridy caridy commented Feb 21, 2016

Tagline: Formalizing a reflective API to create export bindings for Reflective Module Records.

Details

  • Removing the confusing mutator in favor of re-usable Export Objects
  • Reflect.Module.Export.let()
  • Reflect.Module.Export.var()
  • Reflect.Module.Export.const()
  • Reflect.Module.Export.from()

Usage

import * as fs from "fs";

var Export = Reflect.Module.Export;

var y, TAU;

// reflective API takes Export objects to represent each export slot
var mod = new Reflect.Module({
  // initialized mutable (includes "uninitialized" var)
  x: Export.var(),
  y: (y = Export.var(42)),

  // uninitialized mutable (let, class)
  z: Export.let(),

  // initialized immutable (const)
  PI: Export.const(Math.PI),

  // uninitialized immutable (const)
  TAU: (TAU = Export.const()),

  // indirect binding from arbitrary object
  bar: Export.from(delegate, "bar"), // optimizable special case; helps interop with existing module systems

  // re-export (heavily optimizable since `fs` is known to be a module namespace object)
  readFile: Export.from(fs, "readFile")
});

console.log(mod.y); // 42
y.set(43);          // mutates the y slot
console.log(mod.y); // 43
TAU.get();          // throws TDZ error

Re-exporing all from Module

function exportAllFrom(ns) {
  let o = Object.create(null);
  Object.keys(ns).forEach(key => {
    o[key] = Reflect.Module.Export.from(ns, key);
  });
  return Reflect.Module(o);
} // this return a new namespace object for the new reflective module that was created from the namespace argument.

Node Compat-Mode

function exportAllFromCJS(cjs) {
  let o = Object.create(null);
  Object.keys(cjs).forEach(key => {
    o[key] = Reflect.Module.Export.let(cjs[key]);
  });
  return Reflect.Module(o);
} // this return a new namespace object for the new reflective module that was created from the cjs module argument.

Rendered HTML

https://rawgit.com/caridy/6430042924e0436918ea/raw/0f9e2224f1c29ac4449000f5350fdcb2ae439157/loader-pr129.html

@caridy caridy self-assigned this Feb 21, 2016
@caridy
Copy link
Contributor Author

caridy commented Feb 21, 2016

/cc @dherman @wycats

@@ -1131,7 +1131,7 @@ A <dfn>reflective module record</dfn> is a kind of module record. In addition to
<tr>
<td>\[[LocalExports]]</td>
<td>A List of Strings</td>
<td>The set of exported names stored in this module's environment.</td>
<td>The set of exported names accessible in this module's environment.</td>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not longer stored directly in the env record, but it is accessible from there.

@rektide
Copy link

rektide commented Apr 28, 2016

I'm curious what happened to the descriptors? Why is the API all now exporting objects, no descriptors? It seems like it'd be handy to be handy to be able to getter/setters.

@caridy
Copy link
Contributor Author

caridy commented Apr 28, 2016

@rektide the descriptors we had before were not object property descriptors, they were export descriptors.

As for getter/setter, we have been drilling on the requirements, eventually we will provide that as a new feature, but that will require spec changes in 262 and discussions with implementers (which we have done some already), which will take longer.

@caridy
Copy link
Contributor Author

caridy commented May 3, 2016

We have decided to maintain the old API (export descriptors). Few thoughts:

  • it reflects better the construction of a source text module record
  • the extra machinery for the special "export module record" is a no-go
  • Export api has probably better ergonomics, but this is not intended for citizen developers, but low level (libraries and fw) code.
  • Export doesn't give us the edge we thought in terms of functionalities, everything could be replicated with the export descriptors in the same way you would get it done using declarative syntax.

note: there are few editorial things from this PR that we will cherry-pick

@caridy caridy closed this May 3, 2016
@caridy
Copy link
Contributor Author

caridy commented May 3, 2016

@dherman did I missed anything else here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants