Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit df98df7

Browse files
authored
Merge pull request #6 from ruby-syntax-tree/update
Update
2 parents 50bdb3e + dff2180 commit df98df7

17 files changed

+665
-532
lines changed

.github/workflows/main.yml

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1-
name: Github Pages
2-
on: [push]
1+
# Simple workflow for deploying static content to GitHub Pages
2+
name: Deploy static content to Pages
3+
4+
on:
5+
# Runs on pushes targeting the default branch
6+
push:
7+
branches: ["main"]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow one concurrent deployment
19+
concurrency:
20+
group: "pages"
21+
cancel-in-progress: true
22+
323
jobs:
4-
build-and-deploy:
24+
# Single deploy job since we're just deploying
25+
deploy:
26+
environment:
27+
name: github-pages
28+
url: ${{ steps.deployment.outputs.page_url }}
529
runs-on: ubuntu-latest
630
steps:
731
- name: Checkout
8-
uses: actions/checkout@master
32+
uses: actions/checkout@v3
933

1034
- name: Setup Ruby
1135
uses: ruby/setup-ruby@v1
@@ -16,17 +40,23 @@ jobs:
1640
- name: Setup node
1741
uses: actions/setup-node@v2
1842
with:
19-
node-version: 14.x
43+
node-version: 18.x
2044
cache: yarn
2145

46+
- name: Setup Pages
47+
uses: actions/configure-pages@v3
48+
2249
- name: Package application
2350
run: |
2451
bundle exec rake
2552
yarn install --frozen-lockfile
2653
yarn build
2754
28-
- name: Deploy
29-
uses: peaceiris/actions-gh-pages@v3
55+
- name: Upload artifact
56+
uses: actions/upload-pages-artifact@v1
3057
with:
31-
github_token: ${{ secrets.GITHUB_TOKEN }}
32-
publish_dir: ./docs
58+
path: './docs'
59+
60+
- name: Deploy to GitHub Pages
61+
id: deployment
62+
uses: actions/deploy-pages@v1

bin/build

Lines changed: 0 additions & 21 deletions
This file was deleted.

bin/build.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./wasmPlugin.js";
4+
5+
const entryPoint = url.fileURLToPath(new URL("../src/index", import.meta.url));
6+
const outdir = url.fileURLToPath(new URL("../docs", import.meta.url));
7+
8+
const { metafile } = await esbuild.build({
9+
bundle: true,
10+
entryPoints: [entryPoint],
11+
format: "esm",
12+
metafile: true,
13+
minify: true,
14+
outdir,
15+
plugins: [wasmPlugin],
16+
sourcemap: true,
17+
splitting: true,
18+
target: "es6"
19+
});
20+
21+
const analysis = await esbuild.analyzeMetafile(metafile);
22+
console.log(analysis);

bin/serve

Lines changed: 0 additions & 25 deletions
This file was deleted.

bin/serve.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./wasmPlugin.js";
4+
5+
const entryPoint = url.fileURLToPath(new URL("../src/index", import.meta.url));
6+
const outdir = url.fileURLToPath(new URL("../docs", import.meta.url));
7+
8+
const ctx = await esbuild.context({
9+
bundle: true,
10+
entryPoints: [entryPoint],
11+
format: "esm",
12+
outdir,
13+
plugins: [wasmPlugin],
14+
sourcemap: true,
15+
splitting: true,
16+
target: "esnext"
17+
});
18+
19+
const { host, port } = await ctx.serve({ servedir: outdir });
20+
console.log(`Listening at ${host}:${port}`);

bin/wasmPlugin.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module.exports = {
1+
import path from 'node:path';
2+
import fs from 'node:fs';
3+
4+
export default {
25
name: 'wasm',
36
setup(build) {
4-
let path = require('path')
5-
let fs = require('fs')
6-
77
// Resolve ".wasm" files to a path with a namespace
88
build.onResolve({ filter: /\.wasm$/ }, args => {
99
// If this is the import inside the stub module, import the
@@ -37,7 +37,7 @@ module.exports = {
3737
// binary itself is imported from a second virtual module.
3838
build.onLoad({ filter: /.*/, namespace: 'wasm-stub' }, async (args) => ({
3939
contents: `import wasm from ${JSON.stringify(args.path)}
40-
export default (imports) => WebAssembly.instantiate(wasm, imports).then((result) => result.instance)`,
40+
export default () => WebAssembly.compile(wasm)`,
4141
}))
4242

4343
// Virtual modules in the "wasm-binary" namespace contain the
@@ -50,3 +50,4 @@ module.exports = {
5050
}))
5151
},
5252
}
53+

docs/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ <h1>Syntax Tree</h1>
2323
</select>
2424
</div>
2525
</nav>
26-
<textarea id="editor">1 + 2 * 3</textarea>
26+
<textarea id="editor">def fibonacci(n)
27+
(n == 0 || n == 1) ? n : fibonacci(n - 1) + fibonacci(n - 2)
28+
end
29+
</textarea>
2730
<textarea id="output" disabled readonly>Loading...</textarea>
28-
<div id="graph-container" class="graph-container"></div>
31+
<div id="graph" class="graph">Loading...</div>
2932
</main>
3033
<script type="module" src="index.js"></script>
3134
</body>

package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
{
22
"license": "MIT",
3+
"type": "module",
34
"scripts": {
4-
"build": "bin/build",
5-
"serve": "bin/serve"
5+
"build": "node bin/build.js",
6+
"serve": "node bin/serve.js"
67
},
78
"dependencies": {
89
"@monaco-editor/loader": "^1.3.2",
9-
"@wasmer/wasi": "^0.12.0",
10-
"@wasmer/wasmfs": "^0.12.0",
11-
"mermaid": "^9.4.0",
12-
"path-browserify": "^1.0.1",
13-
"ruby-head-wasm-wasi": "^0.3.0"
10+
"mermaid": "^10.0.0",
11+
"monaco-editor": "^0.36.0",
12+
"ruby-head-wasm-wasi": "^0.6.0"
1413
},
1514
"devDependencies": {
16-
"@types/node": "^17.0.33",
17-
"@types/path-browserify": "^1.0.0",
18-
"esbuild": "^0.14.39",
19-
"monaco-editor": "^0.33.0"
15+
"esbuild": "^0.17.10"
2016
}
2117
}

src/createRuby.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { DefaultRubyVM } from "ruby-head-wasm-wasi/dist/browser.esm";
2+
import app from "./app.wasm";
3+
4+
export default async function createRuby() {
5+
const { vm } = await DefaultRubyVM(await app());
6+
7+
// Once our virtual machine is booted, we're going to require the necessary
8+
// files to make it work. I'm not sure why I need to explicitly require
9+
// did_you_mean here, but it doesn't work without it.
10+
vm.eval(`
11+
require "rubygems"
12+
require "did_you_mean"
13+
require "json"
14+
require "pp"
15+
$:.unshift("/lib")
16+
require_relative "/lib/syntax_tree"
17+
require_relative "/lib/prettier_print"
18+
`);
19+
20+
return {
21+
// A function that disassembles the YARV instructions for the given source.
22+
disasm(source) {
23+
const jsonSource = JSON.stringify(JSON.stringify(source));
24+
const rubySource = `RubyVM::InstructionSequence.compile(JSON.parse(${jsonSource})).disasm`;
25+
26+
return vm.eval(rubySource).toString();
27+
},
28+
mermaid(source) {
29+
const jsonSource = JSON.stringify(JSON.stringify(source));
30+
const rubySource = `SyntaxTree.parse(JSON.parse(${jsonSource})).to_mermaid`;
31+
32+
return vm.eval(rubySource).toString();
33+
},
34+
// A function that calls through to the SyntaxTree.format function to get
35+
// the pretty-printed version of the source.
36+
format(source) {
37+
const jsonSource = JSON.stringify(JSON.stringify(source));
38+
const rubySource = `SyntaxTree.format(JSON.parse(${jsonSource}))`;
39+
40+
return vm.eval(rubySource).toString();
41+
},
42+
// A function that calls through to PP to get the pretty-printed version of
43+
// the syntax tree.
44+
prettyPrint(source) {
45+
const jsonSource = JSON.stringify(JSON.stringify(source));
46+
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${jsonSource})), +"", 80)`;
47+
48+
return vm.eval(rubySource).toString();
49+
}
50+
};
51+
};

src/createRuby.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)