Skip to content

Commit f2eb64c

Browse files
committed
Add conditional BigInt support to loader & minor README fixes
1 parent c6ec5e2 commit f2eb64c

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

lib/loader/README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Instances are automatically populated with useful utility:
4848
* **U32**: `Uint32Array`<br />
4949
A 32-bit unsigned integer view on the memory.
5050

51+
* **I64**: `BigInt64Array`<br />
52+
A 64-bit signed integer view on the memory<sup>1</sup>.
53+
54+
* **U64**: `BigUint64Array`<br />
55+
A 64-bit unsigned integer view on the memory<sup>1</sup>.
56+
5157
* **F32**: `Float32Array`<br />
5258
A 32-bit float view on the memory.
5359

@@ -65,6 +71,8 @@ Instances are automatically populated with useful utility:
6571
* **getString**(ptr: `number`): `string`<br />
6672
Gets a string from the module's memory by its pointer.
6773

74+
<sup>1</sup> This feature has not yet landed in any VM as of this writing.
75+
6876
Examples
6977
--------
7078

@@ -82,13 +90,16 @@ const myModule = await loader.instantiateStreaming(fetch("myModule.wasm"), myImp
8290

8391
```js
8492
var ptrToInt8 = ...;
85-
var value = myModule.U16[ptrToInt8]; // alignment of log2(1)=0
93+
var value = myModule.I8[ptrToInt8]; // alignment of log2(1)=0
8694

8795
var ptrToInt16 = ...;
88-
var value = myModule.U16[ptrToInt16 >>> 1]; // alignment of log2(2)=1
96+
var value = myModule.I16[ptrToInt16 >>> 1]; // alignment of log2(2)=1
8997

9098
var ptrToInt32 = ...;
91-
var value = myModule.U32[ptrToInt32 >>> 2]; // alignment of log2(4)=2
99+
var value = myModule.I32[ptrToInt32 >>> 2]; // alignment of log2(4)=2
100+
101+
var ptrToInt64 = ...;
102+
var value = myModule.I64[ptrToInt64 >>> 3]; // alignment of log2(8)=3
92103

93104
var ptrToFloat32 = ...;
94105
var value = myModule.F32[ptrToFloat32 >>> 2]; // alignment of log2(4)=2
@@ -97,9 +108,10 @@ var ptrToFloat64 = ...;
97108
var value = myModule.F64[ptrToFloat64 >>> 3]; // alignment of log2(8)=3
98109

99110
// Likewise, for writing
100-
myModule.U16[ptrToInt8] = newValue;
101-
myModule.U16[ptrToInt16 >>> 1] = newValue;
102-
myModule.U32[ptrToInt32 >>> 2] = newValue;
111+
myModule.I8[ptrToInt8] = newValue;
112+
myModule.I16[ptrToInt16 >>> 1] = newValue;
113+
myModule.I32[ptrToInt32 >>> 2] = newValue;
114+
myModule.I64[ptrToInt64 >>> 3] = newValue;
103115
myModule.F32[ptrToFloat32 >>> 2] = newValue;
104116
myModule.F64[ptrToFloat64 >>> 3] = newValue;
105117
```
@@ -128,5 +140,3 @@ import MyModule from "myModule"; // pointing at the d.ts
128140

129141
const myModule = loader.instatiateBuffer<typeof MyModule>(fs.readFileSync("myModule.wasm"), myImports);
130142
```
131-
132-
**Hint:** You can produce a `.d.ts` for your module with the `-d` option on the command line.

lib/loader/index.d.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ interface ImportsObject {
1313
/** Utility mixed in by the loader. */
1414
interface ASUtil {
1515
/** An 8-bit signed integer view on the memory. */
16-
readonly I8: Uint8Array,
16+
readonly I8: Uint8Array;
1717
/** An 8-bit unsigned integer view on the memory. */
18-
readonly U8: Uint8Array,
18+
readonly U8: Uint8Array;
1919
/** A 16-bit signed integer view on the memory. */
20-
readonly I16: Uint16Array,
20+
readonly I16: Uint16Array;
2121
/** A 16-bit unsigned integer view on the memory. */
22-
readonly U16: Uint16Array,
22+
readonly U16: Uint16Array;
2323
/** A 32-bit signed integer view on the memory. */
24-
readonly I32: Uint32Array,
24+
readonly I32: Uint32Array;
2525
/** A 32-bit unsigned integer view on the memory. */
26-
readonly U32: Uint32Array,
26+
readonly U32: Uint32Array;
27+
/** A 64-bit signed integer view on the memory. */
28+
readonly I64: any; // BigInt64Array
29+
/** A 64-bit unsigned integer vieww on the memory. */
30+
readonly U64: any; // BigUint64Array
2731
/** A 32-bit float view on the memory. */
28-
readonly F32: Float32Array,
32+
readonly F32: Float32Array;
2933
/** A 64-bit float view on the memory. */
30-
readonly F64: Float64Array,
34+
readonly F64: Float64Array;
3135
/** Allocates a new string in the module's memory and returns its pointer. */
3236
newString(str: string): number;
3337
/** Gets a string from the module's memory by its pointer. */

lib/loader/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
const hasBigInt64 = typeof BigUint64Array !== "undefined";
4+
35
/** Instantiates an AssemblyScript module using the specified imports. */
46
function instantiate(module, imports) {
57

@@ -17,7 +19,7 @@ function instantiate(module, imports) {
1719
var exports = instance.exports;
1820

1921
// Provide views for all sorts of basic values
20-
var mem, I8, U8, I16, U16, I32, U32, F32, F64;
22+
var mem, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
2123

2224
/** Updates memory views if memory has grown meanwhile. */
2325
function checkMem() {
@@ -30,6 +32,10 @@ function instantiate(module, imports) {
3032
U16 = new Uint16Array(mem);
3133
I32 = new Int32Array(mem);
3234
U32 = new Uint32Array(mem);
35+
if (hasBigInt64) {
36+
I64 = new BigInt64Array(mem);
37+
U64 = new BigUint64Array(mem);
38+
}
3339
F32 = new Float32Array(mem);
3440
F64 = new Float64Array(mem);
3541
}
@@ -73,6 +79,8 @@ function instantiate(module, imports) {
7379
get U16() { checkMem(); return U16; },
7480
get I32() { checkMem(); return I32; },
7581
get U32() { checkMem(); return U32; },
82+
get I64() { checkMem(); return I64; },
83+
get U64() { checkMem(); return U64; },
7684
get F32() { checkMem(); return F32; },
7785
get F64() { checkMem(); return F64; },
7886
newString,

0 commit comments

Comments
 (0)