Skip to content

Commit 8da2029

Browse files
Merge pull request #528 from ruby/katei/add-wasip2-pkg
2 parents 71c25d8 + 5b51997 commit 8da2029

File tree

24 files changed

+272
-61
lines changed

24 files changed

+272
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ compile_commands.json
1515
html
1616

1717
build_manifest.json
18+
ci_matrix.json
1819

1920
vendor/bundle
2021
/pkg

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ $ rake build:download_prebuilt
2424
$ rake build:head-wasm32-unknown-wasip1-full
2525

2626
# Build npm package
27-
$ rake npm:ruby-head-wasm-wasi
27+
$ rake npm:ruby-head-wasm-wasip2:build
2828
# Test npm package
29-
$ rake npm:ruby-head-wasm-wasi:check
29+
$ rake npm:ruby-head-wasm-wasip2:check
3030
```
3131

3232
If you need to re-build Ruby, please clean `./rubies` directory, and run `rake npm:ruby-head-wasm-wasi` again.

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ NPM_PACKAGES = [
3030
ruby_version: "head",
3131
gemfile: "packages/npm-packages/ruby-head-wasm-wasi/Gemfile",
3232
target: "wasm32-unknown-wasip1",
33+
},
34+
{
35+
name: "ruby-head-wasm-wasip2",
36+
ruby_version: "head",
37+
gemfile: "packages/npm-packages/ruby-head-wasm-wasip2/Gemfile",
38+
target: "wasm32-unknown-wasip2",
3339
enable_component_model: true,
3440
},
3541
{

builders/wasm32-unknown-wasip1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wasm32-unknown-wasip2

package-lock.json

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/npm-packages/ruby-head-wasm-wasi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
[![npm version](https://badge.fury.io/js/@ruby%2Fhead-wasm-wasi.svg)](https://www.npmjs.com/package/@ruby/head-wasm-wasi)
44

5-
This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting WASI-compatible environments.
5+
This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting environments compatible with WASI Preview1.
66

77
See [`@ruby/wasm-wasi`](https://github.com/ruby/ruby.wasm/blob/main/packages/npm-packages/ruby-wasm-wasi/README.md) for how to use this package.

packages/npm-packages/ruby-head-wasm-wasi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"README.md"
3030
],
3131
"scripts": {
32-
"test": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasi npm -C ../ruby-wasm-wasi run test:run:all",
32+
"test": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasi npm -C ../ruby-wasm-wasi run test:run",
3333
"build:deps": "cd ../ruby-wasm-wasi && npm run build",
3434
"build:static:files": "../ruby-wasm-wasi/tools/pack-static-files.sh ./dist",
3535
"build:static:compat": "../ruby-wasm-wasi/tools/pack-compat-shim.mjs --dist=./dist --pkg=ruby-head-wasm-wasi",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.tgz
2+
/tmp
3+
/bundle
4+
/vendor
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
# We build ./vendor/cache/js-{version}.gem just before evaluating this Gemfile
6+
# so that Bundler builds extensions even from the local gem. (gem extensions
7+
# from "path:" gems are not built by Bundler.)
8+
# Thus even we specify version of "js" gem here, it should always installed
9+
# from the ./vendor/cache/js-{version}.gem, not from rubygems.org. To achieve this,
10+
# we always use non-exist version during development.
11+
require_relative "../../gems/js/lib/js/version.rb"
12+
gem "js", JS::VERSION
13+
gem "ruby_wasm", path: "../../../", group: [:build]
14+
gem "power_assert"
15+
gem "test-unit"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
PATH
2+
remote: ../../..
3+
specs:
4+
ruby_wasm (2.6.2.dev)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
js (2.6.2.dev)
10+
power_assert (2.0.3)
11+
test-unit (3.6.2)
12+
power_assert
13+
14+
PLATFORMS
15+
ruby
16+
x86_64-linux
17+
18+
DEPENDENCIES
19+
js (= 2.6.2.dev)
20+
power_assert
21+
ruby_wasm!
22+
test-unit
23+
24+
BUNDLED WITH
25+
2.6.0.dev
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @ruby/head-wasm-wasip2
2+
3+
[![npm version](https://badge.fury.io/js/@ruby%2Fhead-wasm-wasip2.svg)](https://www.npmjs.com/package/@ruby/head-wasm-wasip2)
4+
5+
This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting environments compatible with WASI Preview2.
6+
7+
See [`@ruby/wasm-wasi`](https://github.com/ruby/ruby.wasm/blob/main/packages/npm-packages/ruby-wasm-wasi/README.md) for how to use this package.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "@ruby/head-wasm-wasip2",
3+
"version": "2.6.2",
4+
"description": "Ruby head built on WASI Preview 2",
5+
"main": "./dist/cjs/index.js",
6+
"module": "./dist/esm/index.js",
7+
"private": true,
8+
"exports": {
9+
".": {
10+
"browser": "./dist/esm/index.js",
11+
"umd": "./dist/umd/index.js",
12+
"import": "./dist/esm/index.js",
13+
"require": "./dist/cjs/index.js"
14+
},
15+
"./dist/*": {
16+
"browser": "./dist/esm/*.js",
17+
"umd": "./dist/umd/*.js",
18+
"import": "./dist/esm/*.js",
19+
"require": "./dist/cjs/*.js"
20+
},
21+
"./*.wasm": {
22+
"browser": "./*.wasm",
23+
"umd": "./*.wasm",
24+
"import": "./*.wasm",
25+
"require": "./*.wasm"
26+
}
27+
},
28+
"files": [
29+
"dist",
30+
"README.md"
31+
],
32+
"scripts": {
33+
"__comment": "FIXME: Enable test:e2e after https://github.com/bytecodealliance/jco/pull/499 is merged",
34+
"test": "npm run run-script -- test:unit && npm run run-script -- test:vitest -- --run",
35+
"run-script": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasip2 ENABLE_COMPONENT_TESTS=1 npm -C ../ruby-wasm-wasi run",
36+
"build:deps": "cd ../ruby-wasm-wasi && npm run build",
37+
"build:static:files": "../ruby-wasm-wasi/tools/pack-static-files.sh ./dist",
38+
"build:static": "npm run build:static:files",
39+
"build:rollup": "rollup -c rollup.config.mjs",
40+
"build": "npm run build:deps && npm run build:static && npm run build:rollup"
41+
},
42+
"repository": "https://github.com/ruby/ruby.wasm",
43+
"homepage": "https://github.com/ruby/ruby.wasm/tree/main/packages/npm-packages/ruby-head-wasm-wasi",
44+
"publishConfig": {
45+
"access": "public"
46+
},
47+
"keywords": [
48+
"wasm",
49+
"webassembly",
50+
"wasi",
51+
"ruby"
52+
],
53+
"license": "MIT",
54+
"dependencies": {
55+
"@ruby/wasm-wasi": "^2.0.0",
56+
"@bytecodealliance/preview2-shim": "^0.16.5"
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json from "@rollup/plugin-json";
2+
import { nodeResolve } from "@rollup/plugin-node-resolve";
3+
import fs from "fs";
4+
import path from "path";
5+
6+
/** @type {import('rollup').RollupOptions[]} */
7+
export default [
8+
{
9+
input: "src/browser.script.js",
10+
output: [
11+
{
12+
file: "dist/browser.script.iife.js",
13+
format: "iife",
14+
banner: "/* " + fs.readFileSync(path.resolve("../../../NOTICE"), "utf8") + "*/",
15+
}
16+
],
17+
plugins: [
18+
json(), nodeResolve()
19+
],
20+
},
21+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { instantiate } from "../dist/component/ruby.component"
2+
import { componentMain } from "@ruby/wasm-wasi/dist/browser.script"
3+
import * as wasip2 from "@bytecodealliance/preview2-shim"
4+
import * as pkg from "../package.json"
5+
6+
componentMain(pkg, { instantiate, wasip2 })
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
!.gitkeep
22
/coverage
33
/playwright-report
4+
/test-results
45
*.tgz

packages/npm-packages/ruby-wasm-wasi/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
],
4040
"license": "MIT",
4141
"scripts": {
42-
"test:run:all": "npm run test:run && ENABLE_COMPONENT_TESTS=1 npm run test:run",
4342
"test:run": "npm run test:unit && npm run test:vitest -- --run && npm run test:e2e",
4443
"test:vitest": "vitest ./test/",
4544
"test:unit": "./tools/run-test-unit.mjs",

packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { DefaultRubyVM } from "./browser.js";
2+
import { RubyInitComponentOptions, RubyComponentInstantiator, RubyVM } from "./vm.js";
23

4+
/**
5+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Core Module.
6+
*/
37
export const main = async (
48
pkg: { name: string; version: string },
59
options?: Parameters<typeof DefaultRubyVM>[1],
@@ -9,7 +13,32 @@ export const main = async (
913
);
1014
const module = await compileWebAssemblyModule(response);
1115
const { vm } = await DefaultRubyVM(module, options);
16+
await mainWithRubyVM(vm);
17+
};
18+
19+
/**
20+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Component.
21+
*/
22+
export const componentMain = async (
23+
pkg: { name: string; version: string },
24+
options: {
25+
instantiate: RubyComponentInstantiator;
26+
wasip2: any;
27+
}
28+
) => {
29+
const componentUrl = `https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/component`;
30+
const fetchComponentFile = (relativePath: string) => fetch(`${componentUrl}/${relativePath}`);
31+
const { vm } = await RubyVM.instantiateComponent({
32+
...options,
33+
getCoreModule: (relativePath: string) => {
34+
const response = fetchComponentFile(relativePath);
35+
return compileWebAssemblyModule(response);
36+
},
37+
});
38+
await mainWithRubyVM(vm);
39+
};
1240

41+
const mainWithRubyVM = async (vm: RubyVM) => {
1342
vm.printVersion();
1443

1544
globalThis.rubyVM = vm;

packages/npm-packages/ruby-wasm-wasi/src/vm.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import {
99
} from "./bindgen/legacy/rb-js-abi-host.js";
1010
import { Binding, ComponentBinding, LegacyBinding, RbAbiValue } from "./binding.js";
1111

12+
/**
13+
* A function type that instantiates a Ruby component
14+
*/
15+
export type RubyComponentInstantiator = (
16+
(
17+
getCoreModule: (path: string) => WebAssembly.Module,
18+
importObject: any,
19+
instantiateCore?: (module: WebAssembly.Module, imports: Record<string, any>) => WebAssembly.Instance | Promise<WebAssembly.Instance>,
20+
) => Promise<({ rubyRuntime: typeof RubyJsRubyRuntime })>
21+
)
22+
1223
export type RubyInitComponentOptions = {
1324
/**
1425
* A lower-level instantiation function that instantiates the Ruby component with the given component
@@ -26,13 +37,7 @@ export type RubyInitComponentOptions = {
2637
/**
2738
* An `instantiate` function generated by `@bytecodealliance/jco` that instantiates the Ruby component.
2839
*/
29-
instantiate: (
30-
(
31-
getCoreModule: (path: string) => WebAssembly.Module,
32-
importObject: any,
33-
instantiateCore?: (module: WebAssembly.Module, imports: Record<string, any>) => WebAssembly.Instance | Promise<WebAssembly.Instance>,
34-
) => Promise<({ rubyRuntime: typeof RubyJsRubyRuntime })>
35-
),
40+
instantiate: RubyComponentInstantiator,
3641

3742
/**
3843
* A function that returns a WebAssembly Core module within the Ruby component transpiled by `@bytecodealliance/jco`.

packages/npm-packages/ruby-wasm-wasi/test/init.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ import { WASI } from "wasi";
44
import { RubyVM } from "../src/index";
55
import * as preview2Shim from "@bytecodealliance/preview2-shim"
66

7-
const rubyModule = (async () => {
7+
const memoize = (fn) => {
8+
let result;
9+
return async () => {
10+
if (!result) {
11+
result = await fn();
12+
}
13+
return result;
14+
};
15+
};
16+
17+
const getRubyModule = memoize(async () => {
818
let binaryPath;
919
if (process.env.RUBY_ROOT) {
1020
binaryPath = path.join(process.env.RUBY_ROOT, "./usr/local/bin/ruby");
@@ -18,7 +28,7 @@ const rubyModule = (async () => {
1828
}
1929
const binary = await fs.readFile(binaryPath);
2030
return await WebAssembly.compile(binary.buffer);
21-
})();
31+
});
2232

2333
const initModuleRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
2434
let preopens = {};
@@ -38,7 +48,7 @@ const initModuleRubyVM = async ({ suppressStderr } = { suppressStderr: false })
3848
preopens,
3949
});
4050

41-
const module = await rubyModule;
51+
const module = await getRubyModule();
4252
const { vm } = await RubyVM.instantiateModule({ module, wasip1: wasi })
4353
return vm;
4454
};

packages/npm-packages/ruby-wasm-wasi/test/package.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const initRubyVM = async (rubyModule, args) => {
3232
};
3333

3434
describe("Packaging validation", () => {
35-
if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
35+
if (
36+
!process.env.RUBY_NPM_PACKAGE_ROOT ||
37+
(process.env.ENABLE_COMPONENT_TESTS && process.env.ENABLE_COMPONENT_TESTS !== 'false')
38+
) {
3639
test.skip("skip", () => {});
3740
return;
3841
}

0 commit comments

Comments
 (0)