-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #525 from ruby/katei/npm-jco
Add `RubyVM.instantiateComponent` and `RubyVM.instantiateModule`
- Loading branch information
Showing
26 changed files
with
452 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import * as preview2Shim from "@bytecodealliance/preview2-shim" | ||
import * as nodeWasi from "wasi"; | ||
import fs from "fs/promises"; | ||
import path from "path"; | ||
import { RubyVM } from "@ruby/wasm-wasi"; | ||
import * as readline from 'node:readline/promises'; | ||
import { stdin as input, stdout as output } from 'node:process'; | ||
import { parseArgs } from "node:util" | ||
|
||
async function instantiateComponent(pkgPath) { | ||
const componentJsPath = path.resolve(pkgPath, "dist/component/ruby.component.js"); | ||
const { instantiate } = await import(componentJsPath); | ||
const getCoreModule = async (relativePath) => { | ||
const coreModulePath = path.resolve(pkgPath, "dist/component", relativePath); | ||
const buffer = await fs.readFile(coreModulePath); | ||
return WebAssembly.compile(buffer); | ||
} | ||
const { cli, filesystem } = preview2Shim; | ||
cli._setArgs(["ruby.wasm"].concat(process.argv.slice(2))); | ||
cli._setCwd("/") | ||
filesystem._setPreopens({}) | ||
|
||
const { vm } = await RubyVM.instantiateComponent({ | ||
instantiate, getCoreModule, wasip2: preview2Shim, | ||
}); | ||
return vm; | ||
} | ||
|
||
async function instantiateModule(pkgPath) { | ||
const binaryPath = path.resolve(pkgPath, "dist/ruby.debug+stdlib.wasm"); | ||
const binary = await fs.readFile(binaryPath); | ||
const rubyModule = await WebAssembly.compile(binary); | ||
const wasi = new nodeWasi.WASI({ | ||
stdio: "inherit", | ||
args: ["ruby.wasm"].concat(process.argv.slice(2)), | ||
env: process.env, | ||
version: "preview1", | ||
}); | ||
|
||
const { vm } = await RubyVM.instantiateModule({ | ||
module: rubyModule, wasip1: wasi | ||
}) | ||
return vm; | ||
}; | ||
|
||
function parseOptions(args) { | ||
/** @type {import("util").ParseArgsConfig["options"]} */ | ||
const options = { | ||
pkg: { | ||
type: "string", | ||
short: "p", | ||
default: (() => { | ||
const dirname = path.dirname(new URL(import.meta.url).pathname); | ||
return path.resolve(dirname, "../packages/npm-packages/ruby-head-wasm-wasi"); | ||
})() | ||
}, | ||
type: { | ||
type: "string", | ||
short: "t", | ||
default: "component", | ||
}, | ||
help: { | ||
type: "boolean", | ||
short: "h", | ||
}, | ||
} | ||
const { values } = parseArgs({ args, options }); | ||
return values; | ||
} | ||
|
||
function printUsage() { | ||
console.log("Usage: repl.mjs [--pkg <npm-package-path>] [--type <component|module>]"); | ||
} | ||
|
||
async function main() { | ||
const args = parseOptions(process.argv.slice(2)); | ||
if (args["help"]) { | ||
printUsage(); | ||
return; | ||
} | ||
const pkgPath = args["pkg"]; | ||
const vm = await (async () => { | ||
switch (args["type"]) { | ||
case "component": { | ||
console.log(`Loading component from ${pkgPath}`); | ||
return await instantiateComponent(pkgPath); | ||
} | ||
case "module": { | ||
console.log(`Loading core module from ${pkgPath}`); | ||
return await instantiateModule(pkgPath); | ||
} | ||
default: | ||
throw new Error(`Unknown type: ${args["type"]}`); | ||
} | ||
})(); | ||
const rl = readline.createInterface({ input, output }); | ||
|
||
vm.eval(`puts RUBY_DESCRIPTION`); | ||
|
||
const printer = vm.eval(` | ||
class ReplPrinter | ||
def puts(*args) | ||
args.each do |arg| | ||
Kernel.puts(arg) | ||
end | ||
end | ||
end | ||
ReplPrinter.new | ||
`); | ||
while (true) { | ||
const line = await rl.question(`>> `); | ||
try { | ||
const result = vm.eval(line); | ||
printer.call("puts", result); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.