Skip to content

Commit 587d8a9

Browse files
Add jco-generated TypeScript interface of the js gem world
1 parent 1a1ee40 commit 587d8a9

File tree

9 files changed

+4511
-257
lines changed

9 files changed

+4511
-257
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ jobs:
103103
runs-on: ubuntu-20.04
104104
steps:
105105
- uses: actions/checkout@v4
106+
with:
107+
submodules: true
106108
- uses: actions/download-artifact@v4
107109
with:
108110
name: build-manifest
@@ -131,6 +133,7 @@ jobs:
131133
chmod +x ./build-exec
132134
# wait for docker exec to be ready
133135
timeout 10 bash -c 'until ./build-exec bundle config set --local without check; do sleep 1; done'
136+
- run: ./build-exec rustup target add wasm32-wasi # For jco build
134137
- run: ./build-exec ./bin/setup
135138
- run: ./build-exec bundle exec rake compile
136139
- name: Pre-release configuration

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "vendor/jco"]
2+
path = vendor/jco
3+
url = [email protected]:kateinoigakukun/jco.git

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ root="$(cd "$(dirname "$0")/.." && pwd)"
77

88
env BUNDLE_GEMFILE="$root/Gemfile" bundle install
99
env BUNDLE_GEMFILE="$root/packages/npm-packages/ruby-wasm-wasi/Gemfile" bundle install
10+
11+
# Build vendored jco if Rust toolchain is available and submodule is checked out
12+
if command -v rustc && [ -f vendor/jco/package.json ]; then
13+
npm -C vendor/jco install
14+
npm -C vendor/jco run build
15+
else
16+
echo "Rust toolchain not found or jco submodule not checked out, skipping jco build"
17+
fi
1018
npm ci

package-lock.json

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@
5353
"build": "npm run build:rollup && npm run build:tsc && npm run build:static && ./tools/post-build.sh ./dist"
5454
},
5555
"devDependencies": {
56+
"@bjorn3/browser_wasi_shim": "^0.2.19",
57+
"@bytecodealliance/jco": "../../../vendor/jco",
5658
"@rollup/plugin-node-resolve": "^15.2.3",
5759
"@rollup/plugin-typescript": "^11.1.6",
5860
"@types/jest": "^29.5.11",
5961
"@types/node": "20.12.2",
6062
"jest": "^29.7.0",
6163
"prettier": "^3.2.5",
62-
"@bjorn3/browser_wasi_shim": "^0.2.19",
6364
"typescript": "^5.4.3"
6465
},
6566
"dependencies": {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export namespace RubyJsJsRuntime {
2+
export function evalJs(code: string): JsAbiResult;
3+
export function isJs(value: JsAbiValue): boolean;
4+
export function instanceOf(value: JsAbiValue, klass: JsAbiValue): boolean;
5+
export function globalThis(): JsAbiValue;
6+
export function intToJsNumber(value: number): JsAbiValue;
7+
export function floatToJsNumber(value: number): JsAbiValue;
8+
export function stringToJsString(value: string): JsAbiValue;
9+
export function boolToJsBool(value: boolean): JsAbiValue;
10+
export function procToJsFunction(value: number): JsAbiValue;
11+
export function rbObjectToJsRbValue(rawRbAbiValue: number): JsAbiValue;
12+
export function jsValueToString(value: JsAbiValue): string;
13+
export function jsValueToInteger(value: JsAbiValue): RawInteger;
14+
export function exportJsValueToHost(value: JsAbiValue): void;
15+
export function importJsValueFromHost(): JsAbiValue;
16+
export function jsValueTypeof(value: JsAbiValue): string;
17+
export function jsValueEqual(lhs: JsAbiValue, rhs: JsAbiValue): boolean;
18+
export function jsValueStrictlyEqual(lhs: JsAbiValue, rhs: JsAbiValue): boolean;
19+
export function reflectApply(target: JsAbiValue, thisArgument: JsAbiValue, arguments: JsAbiValue[]): JsAbiResult;
20+
export function reflectGet(target: JsAbiValue, propertyKey: string): JsAbiResult;
21+
export function reflectSet(target: JsAbiValue, propertyKey: string, value: JsAbiValue): JsAbiResult;
22+
export function throwProhibitRewindException(message: string): void;
23+
export { JsAbiValue };
24+
}
25+
export type JsAbiResult = JsAbiResultSuccess | JsAbiResultFailure;
26+
export interface JsAbiResultSuccess {
27+
tag: 'success',
28+
val: JsAbiValue,
29+
}
30+
export interface JsAbiResultFailure {
31+
tag: 'failure',
32+
val: JsAbiValue,
33+
}
34+
export type RawInteger = RawIntegerAsFloat | RawIntegerBignum;
35+
export interface RawIntegerAsFloat {
36+
tag: 'as-float',
37+
val: number,
38+
}
39+
export interface RawIntegerBignum {
40+
tag: 'bignum',
41+
val: string,
42+
}
43+
44+
export class JsAbiValue {
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export namespace RubyJsRubyRuntime {
2+
export function rubyShowVersion(): void;
3+
export function rubyInit(): void;
4+
export function rubySysinit(args: string[]): void;
5+
export function rubyOptions(args: string[]): RbIseq;
6+
export function rubyScript(name: string): void;
7+
export function rubyInitLoadpath(): void;
8+
export function rbEvalStringProtect(str: string): [RbAbiValue, number];
9+
export function rbFuncallvProtect(recv: RbAbiValue, mid: RbId, args: RbAbiValue[]): [RbAbiValue, number];
10+
export function rbIntern(name: string): RbId;
11+
export function rbErrinfo(): RbAbiValue;
12+
export function rbClearErrinfo(): void;
13+
export function rstringPtr(value: RbAbiValue): string;
14+
export function rbVmBugreport(): void;
15+
export function rbGcEnable(): boolean;
16+
export function rbGcDisable(): boolean;
17+
export function rbSetShouldProhibitRewind(newValue: boolean): boolean;
18+
export { RbIseq };
19+
export { RbAbiValue };
20+
}
21+
import type { JsAbiValue } from './ruby-js-js-runtime.js';
22+
export { JsAbiValue };
23+
export type RbErrno = number;
24+
export type RbId = number;
25+
26+
export class RbIseq {
27+
}
28+
29+
export class RbAbiValue {
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This script generates TypeScript bindings from the WIT definitions
3+
* in the js gem.
4+
*/
5+
import { fileURLToPath } from "url";
6+
import { generateTypes, $init } from "../../../../node_modules/@bytecodealliance/jco/obj/js-component-bindgen-component.js";
7+
import path from "path"
8+
import fs from "fs/promises"
9+
10+
async function main() {
11+
const selfPath = fileURLToPath(import.meta.url)
12+
const witDir = path.resolve(path.join(
13+
selfPath,
14+
"../../../../gems/js/wit"
15+
))
16+
await $init
17+
console.log(`Generating TypeScript bindings from ${witDir}`)
18+
const generated = generateTypes("ruby", {
19+
wit: {
20+
tag: "dir",
21+
val: witDir
22+
},
23+
world: "ext",
24+
tlaCompat: true
25+
})
26+
const bindgenDir = path.join(selfPath, "../../src/bindgen")
27+
for (const [name, content] of generated) {
28+
if (name == "ruby.d.ts") {
29+
console.log(`Skipping ${name}`)
30+
continue;
31+
}
32+
33+
const filePath = path.join(bindgenDir, name)
34+
console.log(`Writing ${filePath}`)
35+
await fs.mkdir(path.dirname(filePath), { recursive: true })
36+
await fs.writeFile(filePath, content)
37+
}
38+
}
39+
40+
await main()

vendor/jco

Submodule jco added at 7f1241d

0 commit comments

Comments
 (0)