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

Instrumenting ES6 code #50

Open
mwaldrich opened this issue Sep 26, 2019 · 1 comment
Open

Instrumenting ES6 code #50

mwaldrich opened this issue Sep 26, 2019 · 1 comment

Comments

@mwaldrich
Copy link
Contributor

Is it currently possible to instrument ES6 code using mx? I know both NodeProf and GraalJS support ES6, but I'm not sure how to actually achieve this with mx.

Here's an example:

one.js

import { add } from "./two";

console.log(add(1, 2));

two.js

function add(x, y) {
    return x + y;
}

export { add };

Execution

❯ mx jalangi --analysis ./src/ch.usi.inf.nodeprof/js/analysis/trivial/emptyTemplate.js one.js
<function>:1
import { add } from "./two";
^

SyntaxError: Expected an operand but found import
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.<anonymous> (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function._load (internal/modules/cjs/loader.js:585:3)
    at Function.runMain (internal/modules/cjs/loader.js:831:12)
    at Object.<anonymous> (/home/mwaldrich/tmp/19-06-27-nodeprof-expr/19-02-21-clean/workspace-nodeprof/nodeprof.js/src/ch.usi.inf.nodeprof/js/jalangi.js:251:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.<anonymous> (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)

GraalJS argument

I also found this page in the GraalJS docs that show the js.ecmascript-version argument. I tried using this with mx, but it didn't help.

❯ mx jalangi --analysis ./src/ch.usi.inf.nodeprof/js/analysis/trivial/emptyTemplate.js one.js --js.ecmascript-version=6
<function>:1
import { add } from "./two";
^

SyntaxError: Expected an operand but found import
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.<anonymous> (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function._load (internal/modules/cjs/loader.js:585:3)
    at Function.runMain (internal/modules/cjs/loader.js:831:12)
    at Object.<anonymous> (/home/mwaldrich/tmp/19-06-27-nodeprof-expr/19-02-21-clean/workspace-nodeprof/nodeprof.js/src/ch.usi.inf.nodeprof/js/jalangi.js:251:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.<anonymous> (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
@alexjordan
Copy link
Collaborator

mx jalangi uses node as the entrypoint for execution (note there is a difference in GraalVM between running pure JS mx js and Node mx node), thus you need to enable experimental support for ES modules in Node.js with an option (also in Node 10.x files must end with .mjs): https://nodejs.org/docs/latest-v10.x/api/esm.html

mx jalangi currently does not pass through node command line options, but you can inject them yourself.

To see what's happening under the hood run mx -v jalangi --analysis foo.js bar.mjs
You'll see the last line starts with env:

env MX_SUBPROCESS_COMMAND_FILE <...> <path>/Release/node --jvm --vm.ea --experimental-options --engine.InstrumentExceptionsAreThrown=true --nodeprof <...> --analysis foo.js bar.mjs

Copy that line and inject --experimental-modules before the jalangi.js script:

env MX_SUBPROCESS_COMMAND_FILE <...> <path>/Release/node --jvm --vm.ea --experimental-options --engine.InstrumentExceptionsAreThrown=true --experimental-modules --nodeprof <...> --analysis foo.js bar.mjs

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