Skip to content

Commit 732b4e9

Browse files
authored
Merge pull request #23 from torch2424/instantiate-sync
Added sync instantiate and some other folder cleanup
2 parents 9f273d1 + d4c97fd commit 732b4e9

File tree

9 files changed

+74
-7
lines changed

9 files changed

+74
-7
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const asyncTask = async () => {
104104
asyncTask();
105105
```
106106

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)*
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)_
108108

109109
## Additional Examples
110110

@@ -146,7 +146,7 @@ const asyncTask = async () => {
146146
});
147147
148148
// Should call consoleLog, and log: "Hello from AS!"
149-
asBindInstance.exports.myExportedFunctionThatWillCallConsoleLog();
149+
asBindInstance.exports.myExportedFunctionThatWillCallConsoleLog();
150150
};
151151
asyncTask();
152152
```
@@ -208,6 +208,20 @@ This function is the equivalent to the [AssemblyScript Loader instantiate](https
208208

209209
- A [WebAssembly importObject](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/WebAssembly/instantiateStreaming), which would have all of your imported functions that can be called from within your AssemblyScript module.
210210

211+
##### instantiateSync
212+
213+
```typescript
214+
AsBind.instantiate: (
215+
moduleOrBuffer: (
216+
WebAssembly.Module |
217+
BufferSource
218+
),
219+
imports?: WasmImports
220+
) => AsBindInstance`
221+
```
222+
223+
This is a synchronous version of `AsBind.instantiate`. This does not accept a promise-like as it's module, and returns an AsBindInstance instead of a Promise that resolves an AsBindInstance. **This is only reccomended for use in testing or development**. Please see the Documentation sections for `AsBind.instantiate` for more information.
224+
211225
#### Instance Properties
212226
213227
An AsBindInstance is vaugley similar to a [WebAssembly instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance).
@@ -280,7 +294,7 @@ _If you're project is using as-bind, and you would like to be featured here. Ple
280294
281295
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.
282296
283-
*Didn't find a solution to your problem? Feel free to open an issue!*
297+
_Didn't find a solution to your problem? Feel free to open an issue!_
284298
285299
## Contributing
286300

lib/asbind-instance.js renamed to lib/asbind-instance/asbind-instance.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Class for asbind instances
22

3-
import { asbindInstantiate } from "./instantiate";
3+
import { asbindInstantiate, asbindInstantiateSync } from "./instantiate";
44
import { bindImportFunction, bindExportFunction } from "./bind-function";
55
import { isReservedExportKey } from "./reserved-export-keys";
66

@@ -36,6 +36,28 @@ export default class AsbindInstance {
3636
}
3737

3838
async _instantiate(source, importObject) {
39+
// Bind our import function
40+
this._instantiateBindImportFunctions(importObject);
41+
42+
// Instantiate the module through the loader
43+
const unboundExports = await asbindInstantiate(source, this.importObject);
44+
45+
// Bind our unbound exports
46+
this._instantiateBindUnboundExports(unboundExports);
47+
}
48+
49+
_instantiateSync(source, importObject) {
50+
// Bind our import function
51+
this._instantiateBindImportFunctions(importObject);
52+
53+
// Instantiate the module through the loader
54+
const unboundExports = asbindInstantiateSync(source, this.importObject);
55+
56+
// Bind our unbound exports
57+
this._instantiateBindUnboundExports(unboundExports);
58+
}
59+
60+
_instantiateBindImportFunctions(importObject) {
3961
// Set our import object, as we will need it to store type caching
4062
this.importObject = importObject;
4163

@@ -54,9 +76,11 @@ export default class AsbindInstance {
5476
);
5577
}
5678
);
79+
}
5780

58-
// Instantiate the module through the loader
59-
this.unboundExports = await asbindInstantiate(source, this.importObject);
81+
_instantiateBindUnboundExports(unboundExports) {
82+
// Set our unbound exports
83+
this.unboundExports = unboundExports;
6084

6185
// Wrap appropriate the appropriate export functions
6286
this.exports = {};
File renamed without changes.

lib/instantiate.js renamed to lib/asbind-instance/instantiate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ export async function asbindInstantiate(source, importObject) {
2323

2424
return wasmInstanceExports;
2525
}
26+
27+
export function asbindInstantiateSync(source, importObject) {
28+
return loader.instantiateSync(source, importObject);
29+
}
File renamed without changes.

lib/lib.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import packageJson from "../package.json";
2-
import AsbindInstance from "./asbind-instance";
2+
import AsbindInstance from "./asbind-instance/asbind-instance";
33

44
export const AsBind = {
55
// General asbind versionn
@@ -10,6 +10,12 @@ export const AsBind = {
1010
let asbindInstance = new AsbindInstance();
1111
await asbindInstance._instantiate(source, importObject);
1212
return asbindInstance;
13+
},
14+
15+
instantiateSync: (source, importObject) => {
16+
let asbindInstance = new AsbindInstance();
17+
asbindInstance._instantiateSync(source, importObject);
18+
return asbindInstance;
1319
}
1420
};
1521

test/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ describe("asbind", () => {
3535
assert(asbindInstance.exports.__release !== undefined, true);
3636
});
3737

38+
it("should instantiateSync WebAssembly.Module", async () => {
39+
const wasmModule = await WebAssembly.compile(wasmBytes);
40+
asbindInstance = AsBind.instantiateSync(wasmModule, baseImportObject);
41+
42+
assert(asbindInstance.exports !== undefined, true);
43+
assert(asbindInstance.exports.helloWorld !== undefined, true);
44+
assert(asbindInstance.exports.__alloc !== undefined, true);
45+
assert(asbindInstance.exports.__release !== undefined, true);
46+
});
47+
3848
it("should instantiate Uint8Array", async () => {
3949
asbindInstance = await AsBind.instantiate(wasmBytes, baseImportObject);
4050

@@ -44,6 +54,15 @@ describe("asbind", () => {
4454
assert(asbindInstance.exports.__release !== undefined, true);
4555
});
4656

57+
it("should instantiateSync Uint8Array", async () => {
58+
asbindInstance = AsBind.instantiateSync(wasmBytes, baseImportObject);
59+
60+
assert(asbindInstance.exports !== undefined, true);
61+
assert(asbindInstance.exports.helloWorld !== undefined, true);
62+
assert(asbindInstance.exports.__alloc !== undefined, true);
63+
assert(asbindInstance.exports.__release !== undefined, true);
64+
});
65+
4766
it("should instantiate Promise/Response", async () => {
4867
// Mock our Browser Response, and Wasm Instantiate streaming for response
4968
global.WebAssembly.instantiateStreaming = async (response, imports) => {

0 commit comments

Comments
 (0)