From 7660c132f632e6599946465e2867aa7efbe8eebf Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 8 Sep 2024 12:19:10 +0000 Subject: [PATCH 01/11] Split out Component binaries into `@ruby/wasm-wasip2` package The `@ruby/wasm-wasi` package included both the Core Module and the Component binaries but it has two problems: 1. npm package size was too large and it was very close to the limit of the jsdelivr CDN. 2. The Component model build (in other words, dynamic linking build) well supports incremental build. But the Core Module build does not because it requires to re-link C-extensions every time statically. And it led to clean build every time. Splitting out the Component build makes the p2 package itself incremental buildable. --- Rakefile | 6 ++ .../ruby-head-wasm-wasi/README.md | 2 +- .../ruby-head-wasm-wasi/package.json | 2 +- .../ruby-head-wasm-wasip2/.gitignore | 4 + .../ruby-head-wasm-wasip2/Gemfile | 15 ++++ .../ruby-head-wasm-wasip2/Gemfile.lock | 25 ++++++ .../ruby-head-wasm-wasip2/README.md | 7 ++ .../ruby-head-wasm-wasip2/package.json | 56 +++++++++++++ .../ruby-head-wasm-wasip2/rollup.config.mjs | 21 +++++ .../src/browser.script.js | 6 ++ .../npm-packages/ruby-wasm-wasi/package.json | 1 - .../ruby-wasm-wasi/src/browser.script.ts | 29 +++++++ .../npm-packages/ruby-wasm-wasi/src/vm.ts | 19 +++-- rakelib/packaging.rake | 80 ++++++++++--------- 14 files changed, 225 insertions(+), 48 deletions(-) create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/.gitignore create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/Gemfile create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/Gemfile.lock create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/README.md create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/package.json create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/rollup.config.mjs create mode 100644 packages/npm-packages/ruby-head-wasm-wasip2/src/browser.script.js diff --git a/Rakefile b/Rakefile index 05ff059282..4a0784e8bb 100644 --- a/Rakefile +++ b/Rakefile @@ -30,6 +30,12 @@ NPM_PACKAGES = [ ruby_version: "head", gemfile: "packages/npm-packages/ruby-head-wasm-wasi/Gemfile", target: "wasm32-unknown-wasip1", + }, + { + name: "ruby-head-wasm-wasip2", + ruby_version: "head", + gemfile: "packages/npm-packages/ruby-head-wasm-wasip2/Gemfile", + target: "wasm32-unknown-wasip2", enable_component_model: true, }, { diff --git a/packages/npm-packages/ruby-head-wasm-wasi/README.md b/packages/npm-packages/ruby-head-wasm-wasi/README.md index d25df14174..61d7c23167 100644 --- a/packages/npm-packages/ruby-head-wasm-wasi/README.md +++ b/packages/npm-packages/ruby-head-wasm-wasi/README.md @@ -2,6 +2,6 @@ [![npm version](https://badge.fury.io/js/@ruby%2Fhead-wasm-wasi.svg)](https://www.npmjs.com/package/@ruby/head-wasm-wasi) -This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting WASI-compatible environments. +This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting environments compatible with WASI Preview1. 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. diff --git a/packages/npm-packages/ruby-head-wasm-wasi/package.json b/packages/npm-packages/ruby-head-wasm-wasi/package.json index 6df914973c..b0f5ff508a 100644 --- a/packages/npm-packages/ruby-head-wasm-wasi/package.json +++ b/packages/npm-packages/ruby-head-wasm-wasi/package.json @@ -29,7 +29,7 @@ "README.md" ], "scripts": { - "test": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasi npm -C ../ruby-wasm-wasi run test:run:all", + "test": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasi npm -C ../ruby-wasm-wasi run test:run", "build:deps": "cd ../ruby-wasm-wasi && npm run build", "build:static:files": "../ruby-wasm-wasi/tools/pack-static-files.sh ./dist", "build:static:compat": "../ruby-wasm-wasi/tools/pack-compat-shim.mjs --dist=./dist --pkg=ruby-head-wasm-wasi", diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/.gitignore b/packages/npm-packages/ruby-head-wasm-wasip2/.gitignore new file mode 100644 index 0000000000..cbb80a5cd4 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/.gitignore @@ -0,0 +1,4 @@ +*.tgz +/tmp +/bundle +/vendor diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile b/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile new file mode 100644 index 0000000000..35a14732c1 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# We build ./vendor/cache/js-{version}.gem just before evaluating this Gemfile +# so that Bundler builds extensions even from the local gem. (gem extensions +# from "path:" gems are not built by Bundler.) +# Thus even we specify version of "js" gem here, it should always installed +# from the ./vendor/cache/js-{version}.gem, not from rubygems.org. To achieve this, +# we always use non-exist version during development. +require_relative "../../gems/js/lib/js/version.rb" +gem "js", JS::VERSION +gem "ruby_wasm", path: "../../../", group: [:build] +gem "power_assert" +gem "test-unit" diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile.lock b/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile.lock new file mode 100644 index 0000000000..f5bf0d9958 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/Gemfile.lock @@ -0,0 +1,25 @@ +PATH + remote: ../../.. + specs: + ruby_wasm (2.6.2.dev) + +GEM + remote: https://rubygems.org/ + specs: + js (2.6.2.dev) + power_assert (2.0.3) + test-unit (3.6.2) + power_assert + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + js (= 2.6.2.dev) + power_assert + ruby_wasm! + test-unit + +BUNDLED WITH + 2.6.0.dev diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/README.md b/packages/npm-packages/ruby-head-wasm-wasip2/README.md new file mode 100644 index 0000000000..e5b87edac6 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/README.md @@ -0,0 +1,7 @@ +# @ruby/head-wasm-wasip2 + +[![npm version](https://badge.fury.io/js/@ruby%2Fhead-wasm-wasip2.svg)](https://www.npmjs.com/package/@ruby/head-wasm-wasip2) + +This package provides WebAssembly binaries of CRuby built from the latest `HEAD` source code targeting environments compatible with WASI Preview2. + +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. diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/package.json b/packages/npm-packages/ruby-head-wasm-wasip2/package.json new file mode 100644 index 0000000000..92b3c9e1af --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/package.json @@ -0,0 +1,56 @@ +{ + "name": "@ruby/head-wasm-wasip2", + "version": "2.6.2", + "description": "Ruby head built on WASI Preview 2", + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.js", + "exports": { + ".": { + "browser": "./dist/esm/index.js", + "umd": "./dist/umd/index.js", + "import": "./dist/esm/index.js", + "require": "./dist/cjs/index.js" + }, + "./dist/*": { + "browser": "./dist/esm/*.js", + "umd": "./dist/umd/*.js", + "import": "./dist/esm/*.js", + "require": "./dist/cjs/*.js" + }, + "./*.wasm": { + "browser": "./*.wasm", + "umd": "./*.wasm", + "import": "./*.wasm", + "require": "./*.wasm" + } + }, + "files": [ + "dist", + "README.md" + ], + "scripts": { + "test": "RUBY_NPM_PACKAGE_ROOT=../ruby-head-wasm-wasi ENABLE_COMPONENT_TESTS=1 npm -C ../ruby-wasm-wasi run test:run", + "build:deps": "cd ../ruby-wasm-wasi && npm run build", + "build:static:files": "../ruby-wasm-wasi/tools/pack-static-files.sh ./dist", + "build:static:compat": "../ruby-wasm-wasi/tools/pack-compat-shim.mjs --dist=./dist --pkg=ruby-head-wasm-wasi", + "build:static": "npm run build:static:files && npm run build:static:compat", + "build:rollup": "rollup -c rollup.config.mjs", + "build": "npm run build:deps && npm run build:static && npm run build:rollup && ../ruby-wasm-wasi/tools/post-build.sh ./dist" + }, + "repository": "https://github.com/ruby/ruby.wasm", + "homepage": "https://github.com/ruby/ruby.wasm/tree/main/packages/npm-packages/ruby-head-wasm-wasi", + "publishConfig": { + "access": "public" + }, + "keywords": [ + "wasm", + "webassembly", + "wasi", + "ruby" + ], + "license": "MIT", + "dependencies": { + "@ruby/wasm-wasi": "^2.0.0", + "@bytecodealliance/preview2-shim": "^0.16.5" + } +} diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/rollup.config.mjs b/packages/npm-packages/ruby-head-wasm-wasip2/rollup.config.mjs new file mode 100644 index 0000000000..302c2c6720 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/rollup.config.mjs @@ -0,0 +1,21 @@ +import json from "@rollup/plugin-json"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import fs from "fs"; +import path from "path"; + +/** @type {import('rollup').RollupOptions[]} */ +export default [ + { + input: "src/browser.script.js", + output: [ + { + file: "dist/browser.script.iife.js", + format: "iife", + banner: "/* " + fs.readFileSync(path.resolve("../../../NOTICE"), "utf8") + "*/", + } + ], + plugins: [ + json(), nodeResolve() + ], + }, +]; diff --git a/packages/npm-packages/ruby-head-wasm-wasip2/src/browser.script.js b/packages/npm-packages/ruby-head-wasm-wasip2/src/browser.script.js new file mode 100644 index 0000000000..aff548be89 --- /dev/null +++ b/packages/npm-packages/ruby-head-wasm-wasip2/src/browser.script.js @@ -0,0 +1,6 @@ +import { instantiate } from "../dist/component/ruby.component" +import { componentMain } from "@ruby/wasm-wasi/dist/browser.script" +import * as wasip2 from "@bytecodealliance/preview2-shim" +import * as pkg from "../package.json" + +componentMain(pkg, { instantiate, wasip2 }) diff --git a/packages/npm-packages/ruby-wasm-wasi/package.json b/packages/npm-packages/ruby-wasm-wasi/package.json index fdd86b4413..a6c09454b0 100644 --- a/packages/npm-packages/ruby-wasm-wasi/package.json +++ b/packages/npm-packages/ruby-wasm-wasi/package.json @@ -39,7 +39,6 @@ ], "license": "MIT", "scripts": { - "test:run:all": "npm run test:run && ENABLE_COMPONENT_TESTS=1 npm run test:run", "test:run": "npm run test:unit && npm run test:vitest -- --run && npm run test:e2e", "test:vitest": "vitest ./test/", "test:unit": "./tools/run-test-unit.mjs", diff --git a/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts b/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts index 65c1b8af90..2c5b2bd32a 100644 --- a/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts +++ b/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts @@ -1,5 +1,9 @@ import { DefaultRubyVM } from "./browser.js"; +import { RubyInitComponentOptions, RubyComponentInstantiator, RubyVM } from "./vm.js"; +/** + * The main entry point of `