Skip to content

Commit 3dc798e

Browse files
commit wit-bindgen'ed js sources also
1 parent e9b7da1 commit 3dc798e

File tree

8 files changed

+812
-8
lines changed

8 files changed

+812
-8
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/src/bindgen/*
21
!.gitkeep
32
/src/index.js

packages/npm-packages/ruby-wasm-wasi/build-package.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ repo_dir="$package_dir/../../../"
77

88
rm -rf "$dist_dir"
99

10-
wit-bindgen js \
11-
--import "$repo_dir/ext/witapi/bindgen/rb-abi-guest.wit" \
12-
--export "$repo_dir/ext/js/bindgen/rb-js-abi-host.wit" \
13-
--out-dir "$package_dir/src/bindgen"
14-
1510
(
1611
cd "$package_dir" && \
1712
npx rollup -c rollup.config.js && \
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
export function clamp_host(i, min, max) {
3+
if (!Number.isInteger(i)) throw new TypeError(`must be an integer`);
4+
if (i < min || i > max) throw new RangeError(`must be between ${min} and ${max}`);
5+
return i;
6+
}
7+
8+
let DATA_VIEW = new DataView(new ArrayBuffer());
9+
10+
export function data_view(mem) {
11+
if (DATA_VIEW.buffer !== mem.buffer) DATA_VIEW = new DataView(mem.buffer);
12+
return DATA_VIEW;
13+
}
14+
export const UTF8_DECODER = new TextDecoder('utf-8');
15+
16+
const UTF8_ENCODER = new TextEncoder('utf-8');
17+
18+
export function utf8_encode(s, realloc, memory) {
19+
if (typeof s !== 'string') throw new TypeError('expected a string');
20+
21+
if (s.length === 0) {
22+
UTF8_ENCODED_LEN = 0;
23+
return 1;
24+
}
25+
26+
let alloc_len = 0;
27+
let ptr = 0;
28+
let writtenTotal = 0;
29+
while (s.length > 0) {
30+
ptr = realloc(ptr, alloc_len, 1, alloc_len + s.length);
31+
alloc_len += s.length;
32+
const { read, written } = UTF8_ENCODER.encodeInto(
33+
s,
34+
new Uint8Array(memory.buffer, ptr + writtenTotal, alloc_len - writtenTotal),
35+
);
36+
writtenTotal += written;
37+
s = s.slice(read);
38+
}
39+
if (alloc_len > writtenTotal)
40+
ptr = realloc(ptr, alloc_len, 1, writtenTotal);
41+
UTF8_ENCODED_LEN = writtenTotal;
42+
return ptr;
43+
}
44+
export let UTF8_ENCODED_LEN = 0;
45+
46+
export class Slab {
47+
constructor() {
48+
this.list = [];
49+
this.head = 0;
50+
}
51+
52+
insert(val) {
53+
if (this.head >= this.list.length) {
54+
this.list.push({
55+
next: this.list.length + 1,
56+
val: undefined,
57+
});
58+
}
59+
const ret = this.head;
60+
const slot = this.list[ret];
61+
this.head = slot.next;
62+
slot.next = -1;
63+
slot.val = val;
64+
return ret;
65+
}
66+
67+
get(idx) {
68+
if (idx >= this.list.length)
69+
throw new RangeError('handle index not valid');
70+
const slot = this.list[idx];
71+
if (slot.next === -1)
72+
return slot.val;
73+
throw new RangeError('handle index not valid');
74+
}
75+
76+
remove(idx) {
77+
const ret = this.get(idx); // validate the slot
78+
const slot = this.list[idx];
79+
slot.val = undefined;
80+
slot.next = this.head;
81+
this.head = idx;
82+
return ret;
83+
}
84+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export type RbErrno = number;
2+
export type RbId = number;
3+
export class RbAbiGuest {
4+
5+
/**
6+
* The WebAssembly instance that this class is operating with.
7+
* This is only available after the `instantiate` method has
8+
* been called.
9+
*/
10+
instance: WebAssembly.Instance;
11+
12+
/**
13+
* Constructs a new instance with internal state necessary to
14+
* manage a wasm instance.
15+
*
16+
* Note that this does not actually instantiate the WebAssembly
17+
* instance or module, you'll need to call the `instantiate`
18+
* method below to "activate" this class.
19+
*/
20+
constructor();
21+
22+
/**
23+
* This is a low-level method which can be used to add any
24+
* intrinsics necessary for this instance to operate to an
25+
* import object.
26+
*
27+
* The `import` object given here is expected to be used later
28+
* to actually instantiate the module this class corresponds to.
29+
* If the `instantiate` method below actually does the
30+
* instantiation then there's no need to call this method, but
31+
* if you're instantiating manually elsewhere then this can be
32+
* used to prepare the import object for external instantiation.
33+
*/
34+
addToImports(imports: any): void;
35+
36+
/**
37+
* Initializes this object with the provided WebAssembly
38+
* module/instance.
39+
*
40+
* This is intended to be a flexible method of instantiating
41+
* and completion of the initialization of this class. This
42+
* method must be called before interacting with the
43+
* WebAssembly object.
44+
*
45+
* The first argument to this method is where to get the
46+
* wasm from. This can be a whole bunch of different types,
47+
* for example:
48+
*
49+
* * A precompiled `WebAssembly.Module`
50+
* * A typed array buffer containing the wasm bytecode.
51+
* * A `Promise` of a `Response` which is used with
52+
* `instantiateStreaming`
53+
* * A `Response` itself used with `instantiateStreaming`.
54+
* * An already instantiated `WebAssembly.Instance`
55+
*
56+
* If necessary the module is compiled, and if necessary the
57+
* module is instantiated. Whether or not it's necessary
58+
* depends on the type of argument provided to
59+
* instantiation.
60+
*
61+
* If instantiation is performed then the `imports` object
62+
* passed here is the list of imports used to instantiate
63+
* the instance. This method may add its own intrinsics to
64+
* this `imports` object too.
65+
*/
66+
instantiate(
67+
module: WebAssembly.Module | BufferSource | Promise<Response> | Response | WebAssembly.Instance,
68+
imports?: any,
69+
): Promise<void>;
70+
rubyShowVersion(): void;
71+
rubyInit(): void;
72+
rubySysinit(args: string[]): void;
73+
rubyOptions(args: string[]): RbIseq;
74+
rubyScript(name: string): void;
75+
rubyInitLoadpath(): void;
76+
rbEvalStringProtect(str: string): [RbAbiValue, number];
77+
rbFuncallvProtect(recv: RbAbiValue, mid: RbId, args: RbAbiValue[]): [RbAbiValue, number];
78+
rbIntern(name: string): RbId;
79+
rbErrinfo(): RbAbiValue;
80+
rbClearErrinfo(): void;
81+
rstringPtr(value: RbAbiValue): string;
82+
}
83+
84+
export class RbIseq {
85+
// Creates a new strong reference count as a new
86+
// object. This is only required if you're also
87+
// calling `drop` below and want to manually manage
88+
// the reference count from JS.
89+
//
90+
// If you don't call `drop`, you don't need to call
91+
// this and can simply use the object from JS.
92+
clone(): RbIseq;
93+
94+
// Explicitly indicate that this JS object will no
95+
// longer be used. If the internal reference count
96+
// reaches zero then this will deterministically
97+
// destroy the underlying wasm object.
98+
//
99+
// This is not required to be called from JS. Wasm
100+
// destructors will be automatically called for you
101+
// if this is not called using the JS
102+
// `FinalizationRegistry`.
103+
//
104+
// Calling this method does not guarantee that the
105+
// underlying wasm object is deallocated. Something
106+
// else (including wasm) may be holding onto a
107+
// strong reference count.
108+
drop(): void;
109+
}
110+
111+
export class RbAbiValue {
112+
// Creates a new strong reference count as a new
113+
// object. This is only required if you're also
114+
// calling `drop` below and want to manually manage
115+
// the reference count from JS.
116+
//
117+
// If you don't call `drop`, you don't need to call
118+
// this and can simply use the object from JS.
119+
clone(): RbAbiValue;
120+
121+
// Explicitly indicate that this JS object will no
122+
// longer be used. If the internal reference count
123+
// reaches zero then this will deterministically
124+
// destroy the underlying wasm object.
125+
//
126+
// This is not required to be called from JS. Wasm
127+
// destructors will be automatically called for you
128+
// if this is not called using the JS
129+
// `FinalizationRegistry`.
130+
//
131+
// Calling this method does not guarantee that the
132+
// underlying wasm object is deallocated. Something
133+
// else (including wasm) may be holding onto a
134+
// strong reference count.
135+
drop(): void;
136+
}

0 commit comments

Comments
 (0)