Skip to content

Commit a120993

Browse files
authored
Merge pull request #18 from torch2424/post-launch-improvements
Post launch improvements
2 parents aca6bdd + 458b65a commit a120993

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ Isomorphic library to handle passing high-level data structures between Assembly
1919
- [Features](#features)
2020
- [Installation](#installation)
2121
- [Quick Start](#quick-start)
22+
- [Additional Examples](#additional-examples)
2223
- [Supported Data Types](#supported-data-types)
2324
- [Supported AssemblyScript Runtime Variants](#supported-assemblyscript-runtime-variants)
2425
- [Reference API](#reference-api)
2526
- [Motivation](#motivation)
2627
- [Performance](#performance)
2728
- [Projects using as-bind](#projects-using-as-bind)
29+
- [FAQ and Common Issues](#faq-and-common-issues)
2830
- [Contributing](#contributing)
2931
- [License](#license)
3032

@@ -102,6 +104,53 @@ const asyncTask = async () => {
102104
asyncTask();
103105
```
104106

107+
*Did the quick start not work for you, or you are noticing some weird behavior? Please see the [FAQ and Common Issues](#faq-and-common-issues)*
108+
109+
## Additional Examples
110+
111+
## Passing a high-level type to a an exported function, and returning a high-level type
112+
113+
[See the Quick Start](#quick-start)
114+
115+
## Passing a high-level type to an imported function
116+
117+
In this example, we will implement a `console.log` that we can call from AssemblyScript!
118+
119+
**AssemblyScript**
120+
121+
Inside of `myWasmFileName.ts`:
122+
123+
```
124+
declare function consoleLog(message: string): void;
125+
126+
export function myExportedFunctionThatWillCallConsoleLog(): void {
127+
consoleLog("Hello from AS!");
128+
}
129+
```
130+
131+
**JavaScript**
132+
133+
```
134+
import { AsBind } from "as-bind";
135+
136+
const wasm = fetch("./path-to-my-wasm.wasm");
137+
138+
const asyncTask = async () => {
139+
// Instantiate the wasm file, and pass in our importObject
140+
const asBindInstance = await AsBind.instantiate(wasm, {
141+
myWasmFileName: {
142+
consoleLog: message => {
143+
console.log(message);
144+
}
145+
}
146+
});
147+
148+
// Should call consoleLog, and log: "Hello from AS!"
149+
asBindInstance.exports.myExportedFunctionThatWillCallConsoleLog();
150+
};
151+
asyncTask();
152+
```
153+
105154
## Supported Data Types
106155

107156
**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.
@@ -225,6 +274,14 @@ In the future, these types of high-level data passing tools will not be needed f
225274

226275
_If you're project is using as-bind, and you would like to be featured here. Please open a README with links to your project, and if appropriate, explaining how as-bind is being used._ 😊
227276

277+
## FAQ and Common Issues
278+
279+
> I am calling my exports, but it is not returning the types that I am returning? It seems to be returning pointers?
280+
281+
This is probably because you are not adding the as-bind entry file. Please see the [Quick Start](#quick-start) on how to compile your AssemblyScript module with this entry file. If this still does not work, please take a look at the [Supported Types](#supported-types) to ensure what type you are trying to pass will work.
282+
283+
*Didn't find a solution to your problem? Feel free to open an issue!*
284+
228285
## Contributing
229286

230287
Contributions are definitely welcome! Feel free to open a PR for small fixes such as typos and things. Larger fixes, or new features should start out as an issue for discussion, in which then a PR should be made. 🥳

lib/bind-function.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ export function bindImportFunction(
8585
});
8686

8787
// Call the import function
88-
originalImport.apply(null, argumentsWithReplacedRefs);
88+
const response = originalImport.apply(null, argumentsWithReplacedRefs);
8989

90-
// TODO: Returning from Import functions is not supported by asbind :(
90+
// TODO: Returning High-level types from Import functions is not supported by asbind :(
91+
return response;
9192
};
9293

9394
// Initialize the state of our function

test/assembly/test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export function mapFloat64Array(array: Float64Array): Float64Array {
5252
return array.map((value: f64) => value * 2);
5353
}
5454

55-
// NOTE: Asbind does not support return types on import object functions
5655
declare function testImportString(value: string): void;
5756
export function callTestImportString(value: string): void {
5857
testImportString(value);
@@ -66,6 +65,12 @@ export function callTestImportTwoStrings(
6665
testImportTwoStrings(valueOne, valueTwo);
6766
}
6867

68+
declare function testImportReturnNumber(): i32;
69+
export function callTestImportReturnNumber(): i32 {
70+
let response: i32 = testImportReturnNumber();
71+
return response;
72+
}
73+
6974
declare function testImportInt8Array(value: Int8Array): void;
7075
export function callTestImportInt8Array(value: Int8Array): void {
7176
testImportInt8Array(value);

test/assembly/test.wasm

165 Bytes
Binary file not shown.

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("asbind", () => {
99
test: {
1010
testImportString: () => {},
1111
testImportTwoStrings: () => {},
12+
testImportReturnNumber: () => -1,
1213
testImportInt8Array: () => {},
1314
testImportUint8Array: () => {},
1415
testImportInt16Array: () => {},
@@ -160,6 +161,7 @@ describe("asbind", () => {
160161
testImportTwoStrings: (value1, value2) => {
161162
testImportCalledWith = [value1, value2];
162163
},
164+
testImportReturnNumber: () => -1,
163165
testImportInt8Array: importObjectFunction,
164166
testImportUint8Array: importObjectFunction,
165167
testImportInt16Array: importObjectFunction,
@@ -192,6 +194,11 @@ describe("asbind", () => {
192194
assert.equal(testImportCalledWith[1], "asbind2");
193195
});
194196

197+
it("should allow numbers to be returned from the importObject", () => {
198+
const response = asbindInstance.exports.callTestImportReturnNumber();
199+
assert.equal(response, -1);
200+
});
201+
195202
// TypedArrays
196203
[
197204
"Int8Array",
@@ -234,6 +241,7 @@ describe("asbind", () => {
234241
testImportTwoStrings: (value1, value2) => {
235242
testImportCalledWith = [value1, value2];
236243
},
244+
testImportReturnNumber: () => -1,
237245
testImportInt8Array: importObjectFunction,
238246
testImportUint8Array: importObjectFunction,
239247
testImportInt16Array: importObjectFunction,

0 commit comments

Comments
 (0)