|
| 1 | +import { RubyJsRubyRuntime } from "./bindgen/interfaces/ruby-js-ruby-runtime.js"; |
| 2 | +import * as RbAbi from "./bindgen/legacy/rb-abi-guest.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * This interface bridges between the Ruby runtime and the JavaScript runtime |
| 6 | + * and defines how to interact with underlying import/export functions. |
| 7 | + */ |
| 8 | +export interface Binding { |
| 9 | + rubyShowVersion(): void; |
| 10 | + rubyInit(): void; |
| 11 | + rubySysinit(args: string[]): void; |
| 12 | + rubyOptions(args: string[]): void; |
| 13 | + rubyScript(name: string): void; |
| 14 | + rubyInitLoadpath(): void; |
| 15 | + rbEvalStringProtect(str: string): [RbAbiValue, number]; |
| 16 | + rbFuncallvProtect(recv: RbAbiValue, mid: RbAbi.RbId, args: RbAbiValue[]): [RbAbiValue, number]; |
| 17 | + rbIntern(name: string): RbAbi.RbId; |
| 18 | + rbErrinfo(): RbAbiValue; |
| 19 | + rbClearErrinfo(): void; |
| 20 | + rstringPtr(value: RbAbiValue): string; |
| 21 | + rbVmBugreport(): void; |
| 22 | + rbGcEnable(): boolean; |
| 23 | + rbGcDisable(): boolean; |
| 24 | + rbSetShouldProhibitRewind(newValue: boolean): boolean; |
| 25 | + |
| 26 | + setInstance(instance: WebAssembly.Instance): Promise<void>; |
| 27 | + addToImports(imports: WebAssembly.Imports): void; |
| 28 | +} |
| 29 | + |
| 30 | +// Low-level opaque representation of a Ruby value. |
| 31 | +export interface RbAbiValue {} |
| 32 | + |
| 33 | +export class LegacyBinding extends RbAbi.RbAbiGuest implements Binding { |
| 34 | + async setInstance(instance: WebAssembly.Instance): Promise<void> { |
| 35 | + await this.instantiate(instance); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export class ComponentBinding implements Binding { |
| 40 | + underlying: typeof RubyJsRubyRuntime; |
| 41 | + |
| 42 | + constructor(underlying: typeof RubyJsRubyRuntime) { |
| 43 | + this.underlying = underlying; |
| 44 | + } |
| 45 | + |
| 46 | + rubyShowVersion(): void { |
| 47 | + this.underlying.rubyShowVersion(); |
| 48 | + } |
| 49 | + rubyInit(): void { |
| 50 | + this.underlying.rubyInit(); |
| 51 | + } |
| 52 | + rubySysinit(args: string[]): void { |
| 53 | + this.underlying.rubySysinit(args); |
| 54 | + } |
| 55 | + rubyOptions(args: string[]) { |
| 56 | + this.underlying.rubyOptions(args); |
| 57 | + } |
| 58 | + rubyScript(name: string): void { |
| 59 | + this.underlying.rubyScript(name); |
| 60 | + } |
| 61 | + rubyInitLoadpath(): void { |
| 62 | + this.underlying.rubyInitLoadpath(); |
| 63 | + } |
| 64 | + rbEvalStringProtect(str: string): [RbAbiValue, number] { |
| 65 | + return this.underlying.rbEvalStringProtect(str); |
| 66 | + } |
| 67 | + rbFuncallvProtect(recv: RbAbiValue, mid: number, args: RbAbiValue[]): [RbAbiValue, number] { |
| 68 | + return this.rbFuncallvProtect(recv, mid, args); |
| 69 | + } |
| 70 | + rbIntern(name: string): number { |
| 71 | + return this.rbIntern(name); |
| 72 | + } |
| 73 | + rbErrinfo(): RbAbi.RbAbiValue { |
| 74 | + return this.rbErrinfo(); |
| 75 | + } |
| 76 | + rbClearErrinfo(): void { |
| 77 | + return this.rbClearErrinfo(); |
| 78 | + } |
| 79 | + rstringPtr(value: RbAbi.RbAbiValue): string { |
| 80 | + return this.rstringPtr(value); |
| 81 | + } |
| 82 | + rbVmBugreport(): void { |
| 83 | + this.rbVmBugreport(); |
| 84 | + } |
| 85 | + rbGcEnable(): boolean { |
| 86 | + return this.rbGcEnable(); |
| 87 | + } |
| 88 | + rbGcDisable(): boolean { |
| 89 | + return this.rbGcDisable(); |
| 90 | + } |
| 91 | + rbSetShouldProhibitRewind(newValue: boolean): boolean { |
| 92 | + return this.rbSetShouldProhibitRewind(newValue); |
| 93 | + } |
| 94 | + |
| 95 | + async setInstance(instance: WebAssembly.Instance): Promise<void> { |
| 96 | + // No-op |
| 97 | + } |
| 98 | + addToImports(imports: WebAssembly.Imports): void { |
| 99 | + // No-op |
| 100 | + } |
| 101 | +} |
0 commit comments