Skip to content

Commit f2c5d5c

Browse files
authored
Merge pull request #29 from torch2424/fix-28
Implented a fix for #28, and did some type clarification
2 parents 28487fb + 4a30fb0 commit f2c5d5c

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,18 @@ asyncTask();
153153

154154
## Supported Data Types
155155

156-
**TL;DR:** Currently Numbers, Strings, and Typed Arrays are supported. Returning a high-level data type from an imported JavaScript function, and passing AssemblyScript Classes will be coming later.
156+
**TL;DR:** Currently Numbers, Strings, and Typed Arrays are supported. Returning a high-level data type from an imported JavaScript function, [BigInt](https://github.com/WebAssembly/JS-BigInt-integration), and passing AssemblyScript Classes will be coming later.
157+
158+
**Note:** As discovered in #28, [`Array<NumberType>`](https://docs.assemblyscript.org/standard-library/array#api) is **NOT** the same as [`TypedArray`](https://docs.assemblyscript.org/standard-library/typedarray#api), and may not work for your use case. You want to use [`TypedArray`](https://docs.assemblyscript.org/standard-library/typedarray#api) where possible.
157159

158160
<!-- Generated from: https://www.tablesgenerator.com/markdown_tables# -->
159161

160-
| Function & Direction | Number (Integers and Floats) | Strings | Int8Array | Uint8Array | Int16Array | UInt16Array | Int32Array | Uint32Array | Float32Array | Float64Array | AssemblyScript Classes |
161-
| ------------------------------------------- | ---------------------------- | ------- | --------- | ---------- | ---------- | ----------- | ---------- | ----------- | ------------ | ------------ | ---------------------- |
162-
| Exported AssemblyScript Function Parameters | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
163-
| Exported AssemblyScript Function Return | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
164-
| Imported JavaScript Function Paramters | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
165-
| Imported JavaScript Function Return | ✔️ |||||||||||
162+
| Function & Direction | Number (32-bit Integers and 32-bit / 64-bit Floats) | Strings | Int8Array | Uint8Array | Int16Array | UInt16Array | Int32Array | Uint32Array | Float32Array | Float64Array | AssemblyScript Classes |
163+
| ------------------------------------------- | --------------------------------------------------- | ------- | --------- | ---------- | ---------- | ----------- | ---------- | ----------- | ------------ | ------------ | ---------------------- |
164+
| Exported AssemblyScript Function Parameters | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
165+
| Exported AssemblyScript Function Return | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
166+
| Imported JavaScript Function Paramters | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
167+
| Imported JavaScript Function Return | ✔️ |||||||||||
166168

167169
## Supported AssemblyScript Runtime Variants
168170

lib/asbind-instance/bind-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
176176
});
177177

178178
// Call the Export
179-
// NOTE: Super random, but we don't hav to worry about ref conflicts here.
179+
// NOTE: Super random, but we don't have to worry about ref conflicts here.
180180
// Since we can only return a single value, there should be no matching reference
181181
// if the returned value is not a reference.
182182
const exportFunctionResponse = originalExport.apply(

test/assembly/test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// The entry file of your WebAssembly module.
22

3+
// User submitted issues
4+
5+
// Learned from this, you must use a TypedArray, and not something like: Array<f64>
6+
export function issue28(arr: Float64Array): f64 {
7+
let sum: f64 = 2.0;
8+
for (let i = 0; i < 3; ++i) {
9+
sum += arr[i];
10+
}
11+
return arr.length;
12+
}
13+
14+
// Basic value Passing
15+
316
export function helloWorld(world: string): string {
417
return "Hello " + world + "!";
518
}
@@ -20,6 +33,18 @@ export function numberAndRefArgsReturnsNumber(num: i32, ref: string): i32 {
2033
return ref.length + num;
2134
}
2235

36+
export function Int32Support(value: i32): i32 {
37+
return value + 1;
38+
}
39+
40+
export function Float32Support(value: f32): f32 {
41+
return value + 1.0;
42+
}
43+
44+
export function Float64Support(value: f64): f64 {
45+
return value + 1.0;
46+
}
47+
2348
export function mapInt8Array(array: Int8Array): Int8Array {
2449
return array.map((value: i8) => value * 2);
2550
}

test/assembly/test.wasm

-711 Bytes
Binary file not shown.

test/test.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe("asbind", () => {
2424
}
2525
};
2626

27+
describe("Issues", () => {
28+
it("#28", async () => {
29+
const wasmModule = await WebAssembly.compile(wasmBytes);
30+
asbindInstance = await AsBind.instantiate(wasmModule, baseImportObject);
31+
32+
const arr = Float64Array.from([1, 2, 3]);
33+
const response = asbindInstance.exports.issue28(arr);
34+
assert(response, 4);
35+
});
36+
});
37+
2738
describe("instantiation", () => {
2839
it("should instantiate WebAssembly.Module", async () => {
2940
const wasmModule = await WebAssembly.compile(wasmBytes);
@@ -93,7 +104,7 @@ describe("asbind", () => {
93104
describe("exported functions", () => {
94105
let asbindInstance;
95106

96-
before(async () => {
107+
beforeEach(async () => {
97108
asbindInstance = await AsBind.instantiate(wasmBytes, baseImportObject);
98109
});
99110

@@ -125,18 +136,37 @@ describe("asbind", () => {
125136
});
126137

127138
it("should allow passing in a number and a ref, and return a number", () => {
139+
// TODO: The response here can be detected as a string?
140+
// If the first param is 24, this will give an expected response of 30.
141+
// In this case, the response is 30, but the original export returns 30?
128142
const response = asbindInstance.exports.numberAndRefArgsReturnsNumber(
129-
24,
143+
30,
130144
"asbind"
131145
);
132-
assert.equal(response, 30);
146+
assert.equal(response, 36);
133147
});
134148

135149
it("should handle Strings", () => {
136150
const response = asbindInstance.exports.helloWorld("asbind");
137151
assert.equal(response, "Hello asbind!");
138152
});
139153

154+
// Number Types
155+
[
156+
"Int32",
157+
// "Int64" - BigInt not supported in Wasm Currently: WebAssembly/JS-BigInt-integration
158+
"Float32",
159+
"Float64"
160+
].forEach(numberKey => {
161+
it(`should support passing and returning ${numberKey}`, () => {
162+
const randomValue = Math.floor(Math.random() * 10);
163+
const response = asbindInstance.exports[numberKey + "Support"](
164+
randomValue
165+
);
166+
assert.equal(response, randomValue + 1);
167+
});
168+
});
169+
140170
// TypedArrays
141171
[
142172
"Int8Array",
@@ -150,7 +180,11 @@ describe("asbind", () => {
150180
].forEach(typedArrayKey => {
151181
it(`should handle ${typedArrayKey}`, () => {
152182
const randomValue = Math.floor(Math.random() * 10);
153-
const array = global[typedArrayKey].from([randomValue]);
183+
const array = global[typedArrayKey].from([
184+
randomValue,
185+
randomValue,
186+
randomValue
187+
]);
154188
const arrayMapResponse = asbindInstance.exports["map" + typedArrayKey](
155189
array
156190
);

0 commit comments

Comments
 (0)