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

add simple require() to the SES environment, replace global Nat/def with requires #169

Closed
warner opened this issue Feb 13, 2019 · 3 comments

Comments

@warner
Copy link
Contributor

warner commented Feb 13, 2019

To help with Agoric/SES#13, we're going to add a rudimentary require() to the SES environment. It will initially only accept one of three names: nat, def (which will be renamed harden), and ses. Rather than having Nat/def/SES be injected onto the global namespace, we'll instead inject require into that namespace, and you'll get the other symbols by using require() exactly as you would outside of SES:

const SES = require('ses');
const Nat = require('nat');

The object you get back when using require('ses') inside a SES realm will be different than what you'd get from outside of one. For Nat they'll be the same but SES needs to build a separate version for each nested realm, and def/harden will return a hardener that already knows about all the primordials that are already frozen.

This require() will throw an exception if you give it any other module name: it is not yet a general-purpose module loader (although of course this is a natural place to build one in the future). We'll have to decide later about require/CJS vs import/es6 style.. for now, a basic require() should be enough to let us share code between SES and non-SES environments.

warner referenced this issue in Agoric/SES Feb 13, 2019
There's an esm-related name-mangling problem still pending, so this patch
can't use a simple lazy eval() yet. Instead, we use r.evaluate() from
outside, then pass the new Nat() function into makeRequire(). This forces us
to evaluate the function when creating the SES realm, even if nobody ever
calls require() with 'nat'. Once we figure out that problem, we can revert to
the simpler/lazier version.

refs #51
warner referenced this issue in Agoric/SES Feb 13, 2019
Adds an option named `requireMode: 'allow'`. When enabled, the confined code
has access to a `require()` function that behaves somewhat like the
`require()` in Node.js, except that it is limited to a single name for now,
`nat`. `require('nat')` should yield the same module as it would in Node.js
when you have `@agoric/nat` in your `package.json`.

We'll add more modules to this over time, as well as changing the syntax of
the `requireMode` option, so the use of a binary `allow` is temporary.

refs #51 , but doesn't close it because we have more modules to add
@warner
Copy link
Contributor Author

warner commented Feb 16, 2019

Note to self: per the discussion in agoric-labs/PlaygroundVat#13 (comment) , @jdalton is planning on making a new esm release (something after 3.2.5) that offers a config option to control the renaming (instrumentation) of functions like console and eval. When that is available, we should use it to tell esm to not rename eval, and then rewrite the hack in makeRequire
(https://github.com/Agoric/SES/blob/a33cb8952b243dfd863c1df9c32f20dfadf4715c/src/bundle/make-require.js#L8-L12)

warner referenced this issue in Agoric/SES Feb 18, 2019
warner referenced this issue in Agoric/SES Feb 18, 2019
This yields the same as the previous 'def' global (which has been removed),
which is the one built-in to SES. We are not using the external
harden/makeHardener code yet.

refs #51
@warner
Copy link
Contributor Author

warner commented Feb 19, 2019

As mentioned in https://github.com/Agoric/SES/issues/9#issuecomment-465003139 , instead of adding a requireMode: to the SES.makeSESRootRealm() arguments, we should build a helper function (maybe spelled s.makeRequires() that takes a configuration object and produces an in-Realm require function. The configuration object can start as a table mapping the NAME in require(NAME) to a description of what that require should supply, but can eventually turn into a manifest that describes the full tree of dependencies and their constraints.

@dckc
Copy link
Contributor

dckc commented Feb 28, 2019

This is done, to my satisfaction.

I struggled a bit for lack of docs, but #159 was enough clues for me to write https://github.com/dckc/do-your-worst/blob/master/loader.js

@warner warner closed this as completed Mar 4, 2019
@jfparadis jfparadis transferred this issue from Agoric/SES Feb 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants