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

Resolve issue #60 with mangled variable. #65

Merged
merged 3 commits into from
Oct 1, 2019

Conversation

jfparadis
Copy link
Contributor

@jfparadis jfparadis commented Oct 1, 2019

Prevent mangling of variables.
#60
Changes:

  • Keep cleanupSource for builds.
  • Use minimized UMD in demo.
  • Integration tests.

@michaelfig
Copy link
Member

globalEval was needed to prevent ESM from mangling the (1,eval) form. Please see #54 (which incorrectly cites rollup as the cause) and test that importing realms-shim under ESM using your PR with SES can create a root realm.

We may need to find a different solution for rollup only.

@Jack-Works
Copy link
Contributor

Jack-Works commented Oct 1, 2019 via email

@jfparadis
Copy link
Contributor Author

@michaelfig Thanks for the info!

Would you know of a test that fails?

@jfparadis
Copy link
Contributor Author

@Jack-Works, good trick, and it works.

Normally, cleanupSource removes ESM. An easy way to check that is to remove the call to cleanupSource() in unsafeRec and run the tests. You get something like:

ReferenceError: _d39‍ is not defined

But instead I'm passing the unsafe global as an argument. It's a little more obvious. I've check that this approach also defeats ESM detection by removing the call to cleanupSource() in a manual test.

@michaelfig
Copy link
Member

Here is a test that fails:

npm run build
node -r esm
import Realm from '.';
r = Realm.makeRootRealm();

Expected result: Realm {}

Actual result:

Thrown:
ReferenceError: _e85‍ is not defined
    at repairFunction (eval at createNewUnsafeRec (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:704:16), <anonymous>:17:7)
    at repairFunctions (eval at createNewUnsafeRec (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:704:16), <anonymous>:75:3)
    at eval (eval at createNewUnsafeRec (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:704:16), <anonymous>:79:3)
    at eval (<anonymous>)
    at createNewUnsafeRec (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:704:16)
    at initRootRealm (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:1442:21)
    at callAndWrapError (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:69:14)
    at Proxy.makeRootRealm (/Users/michael/agoric/cosmic-workspace/realms-shim/dist/realms-shim.cjs.js:124:7)
    at repl:1
    at Script.runInThisContext (vm.js:123:20)

This error happened before #54 was merged, in commit hash 46d17c1404f4e4d3883c5cf3a70eb6a89ce7ef2e.

Agoric#60
Changes:
- Keep cleanupSource for builds.
- Use minimized UMD in demo.
@jfparadis jfparadis force-pushed the jfparadis/master/fix-mangling branch from be25d06 to 6c899e0 Compare October 1, 2019 15:35
Copy link
Member

@michaelfig michaelfig left a comment

Choose a reason for hiding this comment

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

Need less magic in cleanupSources.

src/utilities.js Outdated
@@ -29,14 +29,11 @@ export function assert(condition, message) {

// Remove code modifications.
export function cleanupSource(src) {
/* START_TESTS_ONLY */

// Restore eval which is modified by esm module.
src = src.replace(/\(0,[^)]+\)/g, '(0, eval)');
Copy link
Member

Choose a reason for hiding this comment

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

This is getting really close!

Can you please focus the regexp a little more rather than just relying on (0,any(thing), after a zero comma) to mean (0, eval)? I'd suggest changing the calls to ('indirectEval', eval) and then matching on that pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about we implement the fix from the "other" branch?

export function cleanupSource(src) {

  // Restore eval which is modified by esm module.
  // (0, eval) => (0, _<something>.e)
  src = src.replace(/\(0, _[^.]+\.e\)/g, '(0, eval)');

  // Restore Reflect which is modified by esm module.
  // Reflect => _<something>.e.Reflect
  src = src.replace(/_[^.]+\.g\.Reflect/g, 'Reflect');

  // Remove code coverage which is injected by nyc module.
  src = src.replace(/cov_[^+]+\+\+[;,]/g, '');

  return src;
}

Copy link
Member

@michaelfig michaelfig left a comment

Choose a reason for hiding this comment

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

LGTM!

@jfparadis
Copy link
Contributor Author

@michaelfig Many thanks.

@jfparadis jfparadis merged commit c3d104d into Agoric:master Oct 1, 2019
@jfparadis jfparadis deleted the jfparadis/master/fix-mangling branch October 7, 2019 01:24
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

Successfully merging this pull request may close these issues.

3 participants