Skip to content

Commit 6fc1770

Browse files
committed
Fix for #12
1 parent aca6bdd commit 6fc1770

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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)
@@ -102,6 +103,51 @@ const asyncTask = async () => {
102103
asyncTask();
103104
```
104105

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

107153
**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.

0 commit comments

Comments
 (0)