Skip to content

Commit cc9a19c

Browse files
Implement the new component binding skeleton
1 parent 984aa36 commit cc9a19c

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export interface Binding {
1212
rubyOptions(args: string[]): void;
1313
rubyScript(name: string): void;
1414
rubyInitLoadpath(): void;
15-
rbEvalStringProtect(str: string): [RbAbi.RbAbiValue, number];
16-
rbFuncallvProtect(recv: RbAbi.RbAbiValue, mid: RbAbi.RbId, args: RbAbi.RbAbiValue[]): [RbAbi.RbAbiValue, number];
15+
rbEvalStringProtect(str: string): [RbAbiValue, number];
16+
rbFuncallvProtect(recv: RbAbiValue, mid: RbAbi.RbId, args: RbAbiValue[]): [RbAbiValue, number];
1717
rbIntern(name: string): RbAbi.RbId;
18-
rbErrinfo(): RbAbi.RbAbiValue;
18+
rbErrinfo(): RbAbiValue;
1919
rbClearErrinfo(): void;
20-
rstringPtr(value: RbAbi.RbAbiValue): string;
20+
rstringPtr(value: RbAbiValue): string;
2121
rbVmBugreport(): void;
2222
rbGcEnable(): boolean;
2323
rbGcDisable(): boolean;
@@ -27,6 +27,9 @@ export interface Binding {
2727
addToImports(imports: WebAssembly.Imports): void;
2828
}
2929

30+
// Low-level opaque representation of a Ruby value.
31+
export interface RbAbiValue {}
32+
3033
export class LegacyBinding extends RbAbi.RbAbiGuest implements Binding {
3134
async setInstance(instance: WebAssembly.Instance): Promise<void> {
3235
await this.instantiate(instance);
@@ -53,40 +56,40 @@ export class ComponentBinding implements Binding {
5356
this.underlying.rubyOptions(args);
5457
}
5558
rubyScript(name: string): void {
56-
throw new Error("Method not implemented.");
59+
this.underlying.rubyScript(name);
5760
}
5861
rubyInitLoadpath(): void {
59-
throw new Error("Method not implemented.");
62+
this.underlying.rubyInitLoadpath();
6063
}
61-
rbEvalStringProtect(str: string): [RbAbi.RbAbiValue, number] {
62-
throw new Error("Method not implemented.");
64+
rbEvalStringProtect(str: string): [RbAbiValue, number] {
65+
return this.underlying.rbEvalStringProtect(str);
6366
}
64-
rbFuncallvProtect(recv: RbAbi.RbAbiValue, mid: number, args: RbAbi.RbAbiValue[]): [RbAbi.RbAbiValue, number] {
65-
throw new Error("Method not implemented.");
67+
rbFuncallvProtect(recv: RbAbiValue, mid: number, args: RbAbiValue[]): [RbAbiValue, number] {
68+
return this.rbFuncallvProtect(recv, mid, args);
6669
}
6770
rbIntern(name: string): number {
68-
throw new Error("Method not implemented.");
71+
return this.rbIntern(name);
6972
}
7073
rbErrinfo(): RbAbi.RbAbiValue {
71-
throw new Error("Method not implemented.");
74+
return this.rbErrinfo();
7275
}
7376
rbClearErrinfo(): void {
74-
throw new Error("Method not implemented.");
77+
return this.rbClearErrinfo();
7578
}
7679
rstringPtr(value: RbAbi.RbAbiValue): string {
77-
throw new Error("Method not implemented.");
80+
return this.rstringPtr(value);
7881
}
7982
rbVmBugreport(): void {
80-
throw new Error("Method not implemented.");
83+
this.rbVmBugreport();
8184
}
8285
rbGcEnable(): boolean {
83-
throw new Error("Method not implemented.");
86+
return this.rbGcEnable();
8487
}
8588
rbGcDisable(): boolean {
86-
throw new Error("Method not implemented.");
89+
return this.rbGcDisable();
8790
}
8891
rbSetShouldProhibitRewind(newValue: boolean): boolean {
89-
throw new Error("Method not implemented.");
92+
return this.rbSetShouldProhibitRewind(newValue);
9093
}
9194

9295
async setInstance(instance: WebAssembly.Instance): Promise<void> {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { RubyJsRubyRuntime } from "./bindgen/interfaces/ruby-js-ruby-runtime.js";
12
import * as RbAbi from "./bindgen/legacy/rb-abi-guest.js";
23
import {
34
RbJsAbiHost,
45
addRbJsAbiHostToImports,
56
JsAbiResult,
67
JsAbiValue,
78
} from "./bindgen/legacy/rb-js-abi-host.js";
8-
import { Binding, LegacyBinding } from "./binding.js";
9+
import { Binding, ComponentBinding, LegacyBinding, RbAbiValue } from "./binding.js";
910

1011
/**
1112
* A Ruby VM instance
@@ -35,7 +36,7 @@ export class RubyVM {
3536
hasJSFrameAfterRbFrame: false,
3637
};
3738

38-
constructor() {
39+
constructor(binding?: Binding) {
3940
// Wrap exported functions from Ruby VM to prohibit nested VM operation
4041
// if the call stack has sandwitched JS frames like JS -> Ruby -> JS -> Ruby.
4142
const proxyExports = (exports: Binding) => {
@@ -77,11 +78,16 @@ export class RubyVM {
7778
}
7879
return exports;
7980
};
80-
this.guest = proxyExports(new LegacyBinding());
81+
this.guest = proxyExports(binding ?? new LegacyBinding());
8182
this.transport = new JsValueTransport();
8283
this.exceptionFormatter = new RbExceptionFormatter();
8384
}
8485

86+
static _instantiate(component: typeof RubyJsRubyRuntime): RubyVM {
87+
const binding = new ComponentBinding(component)
88+
return new RubyVM(binding);
89+
}
90+
8591
/**
8692
* Initialize the Ruby VM with the given command line arguments
8793
* @param args The command line arguments to pass to Ruby. Must be
@@ -433,7 +439,7 @@ export class RbValue {
433439
* @hideconstructor
434440
*/
435441
constructor(
436-
private inner: RbAbi.RbAbiValue,
442+
private inner: RbAbiValue,
437443
private vm: RubyVM,
438444
private privateObject: RubyVMPrivate,
439445
) {}
@@ -704,9 +710,9 @@ function wrapRbOperation<R>(vm: RubyVM, body: () => R): R {
704710
const callRbMethod = (
705711
vm: RubyVM,
706712
privateObject: RubyVMPrivate,
707-
recv: RbAbi.RbAbiValue,
713+
recv: RbAbiValue,
708714
callee: string,
709-
args: RbAbi.RbAbiValue[],
715+
args: RbAbiValue[],
710716
) => {
711717
const mid = vm.guest.rbIntern(callee + "\0");
712718
return wrapRbOperation(vm, () => {

0 commit comments

Comments
 (0)