Skip to content

Commit

Permalink
feat!: Remove js-runner (#1585)
Browse files Browse the repository at this point in the history
* feat!: Remove js-runner

* Implement wasi runner via execSync, call it grainrun

* Also move the v8 flag

* passthrough exit code

* Update workflow step names

* provide wasm file as first argv

* gracefully show errors during grainrun

* Remove remaining js-runner references

* Update issue number

* Fix missed preopens in merge

* Passthrough argv after -- and add to test harness

* Remove MTracepoint

* Fix test arg

* Regenerate lockfile

* add hack for fdPrestatGet

* Remove argv pass-through stuff from this PR

* update timing tests

* Update cli/bin/grain.js

* move comment because formatter is BROKE

---------

Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
ospencer and phated committed Apr 23, 2023
1 parent 686de7e commit e10d612
Show file tree
Hide file tree
Showing 40 changed files with 225 additions and 6,915 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ setup.data
setup.ml
myocamlbuild.ml
.merlin
js-runner/dist
/*.gr
_esy/
**/_esy/
Expand Down
8 changes: 0 additions & 8 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@
/compiler/esy.json @ospencer @phated
/compiler/package.json @ospencer @phated

/js-runner/.prettierignore @ospencer @phated
/js-runner/.prettierrc.json @ospencer @phated
/js-runner/README.md @ospencer @phated
/js-runner/src/ @ospencer @phated @peblair
/js-runner/package.json @ospencer @phated
/js-runner/webpack.*.js @ospencer @phated
/js-runner/LICENSE @ospencer @peblair

/stdlib/ @ospencer @phated @peblair @marcusroberts @jozanza
/stdlib/runtime/ @ospencer @peblair @jozanza @phated
/stdlib/package.json @phated @ospencer
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/build-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ jobs:
run: |
npm run compiler check-format
- name: (js-runner) Check formatting
if: inputs.os != 'windows-latest'
run: |
npm run js-runner check-format
- name: (cli) Check formatting
if: inputs.os != 'windows-latest'
run: |
Expand Down
37 changes: 0 additions & 37 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
releases-created: ${{ steps.release.outputs.releases_created }}
tag-name: ${{ steps.release.outputs.tag_name }}
stdlib-tag-name: ${{ steps.release.outputs.stdlib--tag_name }}
js-runner-tag-name: ${{ steps.release.outputs.js-runner--tag_name }}
steps:
- uses: GoogleCloudPlatform/release-please-action@v3.7.5
id: release
Expand Down Expand Up @@ -87,7 +86,6 @@ jobs:
runs-on: ubuntu-latest
outputs:
stdlib-download-url: ${{ steps.stdlib-upload.outputs.browser_download_url }}
js-runner-download-url: ${{ steps.js-runner-upload.outputs.browser_download_url }}
steps:
- name: Checkout project
uses: actions/checkout@v3
Expand Down Expand Up @@ -118,22 +116,6 @@ jobs:
asset_name: stdlib.tgz
tag: ${{ needs.release-please.outputs.stdlib-tag-name }}

- name: Pack js-runner
working-directory: ./js-runner
# Runs `npm pack` and assigns the filename to an env var we can use later
# `sed` is used to workaround https://github.com/npm/cli/issues/3405
run: |
echo "RUNNER_TAR=$(npm pack --json | jq -r '.[0].filename' | sed -r 's/@//g' | sed -r 's/\//-/g')" >> $GITHUB_ENV
- name: Upload js-runner
id: js-runner-upload
uses: grain-lang/upload-release-action@v3.0.2
with:
token: ${{ secrets.GITHUB_TOKEN }}
file: ./js-runner/${{ env.RUNNER_TAR }}
asset_name: js-runner.tgz
tag: ${{ needs.release-please.outputs.js-runner-tag-name }}

dispatch-website:
needs: [release-please, upload-release]
if: ${{ needs.release-please.outputs.releases-created }}
Expand Down Expand Up @@ -194,22 +176,3 @@ jobs:
npm publish ${{ needs.upload-npm-artifacts.outputs.stdlib-download-url }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_RELEASE }}

npm-release-js-runner:
needs: [release-please, upload-npm-artifacts]
if: ${{ needs.release-please.outputs.releases-created }}
name: Publish js-runner to npm registry
runs-on: ubuntu-latest
steps:
- name: Setup NodeJS
uses: actions/setup-node@v3.6.0
with:
node-version: ">=18.15"
check-latest: true
registry-url: "https://registry.npmjs.org"

- name: Publish to npm
run: |
npm publish ${{ needs.upload-npm-artifacts.outputs.js-runner-download-url }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_RELEASE }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ setup.data
setup.ml
myocamlbuild.ml
.merlin
js-runner/dist
/*.gr
_esy/
pkg/
Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{".":"0.5.13","cli":"0.5.13","compiler":"0.5.13","js-runner":"0.5.13","stdlib":"0.5.13"}
{".":"0.5.13","cli":"0.5.13","compiler":"0.5.13","stdlib":"0.5.13"}
11 changes: 0 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ npm run compiler test

It can sometimes be helpful to run small Grain programs directly to test some functionality without running the full test suite.

### JS runner

After making changes in the `js-runner` directory, run:

```bash
npm run js-runner build
npm run js-runner test
```

Once the JS runner has been built, it's the only one active. Grain programs that you run from the command line or the tests will use that version.

### Standard library

It's usually easiest to create a small Grain program that imports your library to try it out, like so:
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ For instructions on how to build Grain from source, please consult the [official

### Other Commands

To build the JS runner:

```bash
npm run js-runner build
```

To link the CLI:

```bash
Expand Down
28 changes: 28 additions & 0 deletions cli/bin/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,37 @@ function execGrainlsp(options, program, execOpts = { stdio: "inherit" }) {
return execSync(`${grainlsp} ${flags.join(" ")}`, execOpts);
}

function getGrainrun() {
const node = process.execPath;
const grainlsp_js = path.join(__dirname, "grainrun.js");
return `"${node}" ${grainlsp_js}`;
}

const grainrun = getGrainrun();

function execGrainrun(file, options, program, execOpts = { stdio: "inherit" }) {
const preopens = {};
options.dir?.forEach((preopen) => {
const [guestDir, hostDir = guestDir] = preopen.split("=");
preopens[guestDir] = hostDir;
});

const env = {
PREOPENS: JSON.stringify(preopens),
NODE_OPTIONS: `--experimental-wasi-unstable-preview1 --no-warnings`,
};

try {
execSync(`${grainrun} ${file}`, { ...execOpts, env });
} catch (e) {
process.exit(e.status);
}
}

module.exports = {
grainc: execGrainc,
graindoc: execGraindoc,
grainformat: execGrainformat,
grainlsp: execGrainlsp,
grainrun: execGrainrun,
};
25 changes: 5 additions & 20 deletions cli/bin/grain.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
#!/usr/bin/env node

// https://github.com/grain-lang/grain/issues/114
const v8 = require("v8");
/* From the Node.js docs:
*
* The v8.setFlagsFromString() method can be used to programmatically set V8 command-line flags.
* This method should be used with care. Changing settings after the VM has started may result
* in unpredictable behavior, including crashes and data loss; or it may simply do nothing.
*
* This seems to work for our needs with Node 18, but we should be cautious when updating.
*/
v8.setFlagsFromString("--experimental-wasm-return-call");

const commander = require("commander");
const exec = require("./exec.js");
const run = require("./run.js");
const pkgJson = require("../package.json");

const stdlibPath = require("@grain/stdlib");
Expand Down Expand Up @@ -159,7 +146,6 @@ class GrainCommand extends commander.Command {
"--use-start-section",
"replaces the _start export with a start section during linking"
);
cmd.forwardOption("--no-link", "disable static linking");
cmd.forwardOption(
"--no-pervasives",
"don't automatically import the Grain Pervasives module"
Expand Down Expand Up @@ -189,28 +175,27 @@ program
.command("compile-and-run <file>", { isDefault: true, hidden: true })
// `--version` should only be available on the default command
.version(pkgJson.version, "-v, --version", "output the current version")
.addOption(new commander.Option("-p, --print-output").hideHelp())
.forwardOption("-o <filename>", "output filename")
.action(function (file, options, program) {
exec.grainc(file, options, program);
if (options.o) {
run(options.o, options);
exec.grainrun(options.o, options, program);
} else {
run(file.replace(/\.gr$/, ".gr.wasm"), options);
exec.grainrun(file.replace(/\.gr$/, ".gr.wasm"), options, program);
}
});

program
.command("compile <file>")
.description("compile a grain program into wasm")
.forwardOption("-o <filename>", "output filename")
.forwardOption("--no-link", "disable static linking")
.action(exec.grainc);

program
.command("run <file>")
.description("run a wasm file with grain's javascript runner")
.addOption(new commander.Option("-p, --print-output").hideHelp())
.action(run);
.description("run a wasm file via grain's WASI runner")
.action(exec.grainrun);

program
.command("lsp")
Expand Down
44 changes: 44 additions & 0 deletions cli/bin/grainrun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require("./pkg");

// https://github.com/grain-lang/grain/issues/1816
const v8 = require("v8");
/* From the Node.js docs:
*
* The v8.setFlagsFromString() method can be used to programmatically set V8 command-line flags.
* This method should be used with care. Changing settings after the VM has started may result
* in unpredictable behavior, including crashes and data loss; or it may simply do nothing.
*
* This seems to work for our needs with Node 18, but we should be cautious when updating.
*/
v8.setFlagsFromString("--experimental-wasm-return-call");

const { readFile } = require("fs/promises");
const { WASI } = require("wasi");
const { argv, env } = require("process");

const preopens = JSON.parse(env.PREOPENS);

delete env.PREOPENS;
delete env.NODE_OPTIONS;

const wasi = new WASI({
args: argv.slice(2),
env,
preopens,
});

const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

async function run(filename) {
try {
const wasm = await WebAssembly.compile(await readFile(filename));
const instance = await WebAssembly.instantiate(wasm, importObject);

wasi.start(instance);
} catch (err) {
console.error(err.stack);
process.exit(1);
}
}

run(argv[2]);
29 changes: 0 additions & 29 deletions cli/bin/run.js

This file was deleted.

3 changes: 1 addition & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"link": "npm link",
"format": "prettier --write .",
"check-format": "prettier --check .",
"build-pkg": "pkg ./package.json --no-bytecode --compress Brotli --output ../pkg/grain"
"build-pkg": "pkg ./package.json --no-bytecode --compress Brotli --options=experimental-wasi-unstable-preview1,no-warnings --output ../pkg/grain"
},
"pkg": {
"assets": "bin/*.js"
Expand All @@ -34,7 +34,6 @@
},
"homepage": "https://github.com/grain-lang/grain#readme",
"dependencies": {
"@grain/js-runner": "0.5.13",
"@grain/stdlib": "0.5.13",
"commander": "^8.1.0"
},
Expand Down
24 changes: 0 additions & 24 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ let equal_mod = "GRAIN$MODULE$runtime/equal";
let equal_ident = Ident.create_persistent("equal");
let equal_closure_ident = Ident.create_persistent("GRAIN$EXPORT$equal");

/* JS-runner support */
let console_mod = "console";
let tracepoint_ident = Ident.create_persistent("tracepoint");

let required_global_imports = [
{
mimp_id: reloc_base,
Expand Down Expand Up @@ -219,15 +215,6 @@ let grain_function_imports = [
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: tracepoint_ident,
mimp_mod: console_mod,
mimp_name: Ident.name(tracepoint_ident),
mimp_type: MFuncImport([Types.Unmanaged(WasmI32)], []),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: equal_ident,
mimp_mod: equal_mod,
Expand Down Expand Up @@ -406,16 +393,6 @@ let call_equal = (wasm_mod, env, args) =>
Type.int32,
);

/** Will print "tracepoint <n> reached" to the console when executed (for debugging WASM output) */

let tracepoint = (wasm_mod, env, n) =>
Expression.Call.make(
wasm_mod,
get_wasm_imported_name(console_mod, tracepoint_ident),
[Expression.Const.make(wasm_mod, const_int32(n))],
Type.none,
);

/** Untags the number */

let untag_number = (wasm_mod, value) =>
Expand Down Expand Up @@ -2923,7 +2900,6 @@ and compile_instr = (wasm_mod, env, instr) =>
switch (instr.instr_desc) {
| MDrop(arg) =>
Expression.Drop.make(wasm_mod, compile_instr(wasm_mod, env, arg))
| MTracepoint(x) => tracepoint(wasm_mod, env, x)
| MImmediate(imm) => compile_imm(wasm_mod, env, imm)
| MAllocate(alloc) => compile_allocation(wasm_mod, env, alloc)
| MTupleOp(tuple_op, tup) => compile_tuple_op(wasm_mod, env, tup, tuple_op)
Expand Down
Loading

0 comments on commit e10d612

Please sign in to comment.