mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-26 07:22:21 +00:00
Add conditional BigInt support to loader & minor README fixes
This commit is contained in:
parent
c6ec5e2832
commit
f2eb64c0fd
@ -48,6 +48,12 @@ Instances are automatically populated with useful utility:
|
|||||||
* **U32**: `Uint32Array`<br />
|
* **U32**: `Uint32Array`<br />
|
||||||
A 32-bit unsigned integer view on the memory.
|
A 32-bit unsigned integer view on the memory.
|
||||||
|
|
||||||
|
* **I64**: `BigInt64Array`<br />
|
||||||
|
A 64-bit signed integer view on the memory<sup>1</sup>.
|
||||||
|
|
||||||
|
* **U64**: `BigUint64Array`<br />
|
||||||
|
A 64-bit unsigned integer view on the memory<sup>1</sup>.
|
||||||
|
|
||||||
* **F32**: `Float32Array`<br />
|
* **F32**: `Float32Array`<br />
|
||||||
A 32-bit float view on the memory.
|
A 32-bit float view on the memory.
|
||||||
|
|
||||||
@ -65,6 +71,8 @@ Instances are automatically populated with useful utility:
|
|||||||
* **getString**(ptr: `number`): `string`<br />
|
* **getString**(ptr: `number`): `string`<br />
|
||||||
Gets a string from the module's memory by its pointer.
|
Gets a string from the module's memory by its pointer.
|
||||||
|
|
||||||
|
<sup>1</sup> This feature has not yet landed in any VM as of this writing.
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
|
|
||||||
@ -82,13 +90,16 @@ const myModule = await loader.instantiateStreaming(fetch("myModule.wasm"), myImp
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
var ptrToInt8 = ...;
|
var ptrToInt8 = ...;
|
||||||
var value = myModule.U16[ptrToInt8]; // alignment of log2(1)=0
|
var value = myModule.I8[ptrToInt8]; // alignment of log2(1)=0
|
||||||
|
|
||||||
var ptrToInt16 = ...;
|
var ptrToInt16 = ...;
|
||||||
var value = myModule.U16[ptrToInt16 >>> 1]; // alignment of log2(2)=1
|
var value = myModule.I16[ptrToInt16 >>> 1]; // alignment of log2(2)=1
|
||||||
|
|
||||||
var ptrToInt32 = ...;
|
var ptrToInt32 = ...;
|
||||||
var value = myModule.U32[ptrToInt32 >>> 2]; // alignment of log2(4)=2
|
var value = myModule.I32[ptrToInt32 >>> 2]; // alignment of log2(4)=2
|
||||||
|
|
||||||
|
var ptrToInt64 = ...;
|
||||||
|
var value = myModule.I64[ptrToInt64 >>> 3]; // alignment of log2(8)=3
|
||||||
|
|
||||||
var ptrToFloat32 = ...;
|
var ptrToFloat32 = ...;
|
||||||
var value = myModule.F32[ptrToFloat32 >>> 2]; // alignment of log2(4)=2
|
var value = myModule.F32[ptrToFloat32 >>> 2]; // alignment of log2(4)=2
|
||||||
@ -97,9 +108,10 @@ var ptrToFloat64 = ...;
|
|||||||
var value = myModule.F64[ptrToFloat64 >>> 3]; // alignment of log2(8)=3
|
var value = myModule.F64[ptrToFloat64 >>> 3]; // alignment of log2(8)=3
|
||||||
|
|
||||||
// Likewise, for writing
|
// Likewise, for writing
|
||||||
myModule.U16[ptrToInt8] = newValue;
|
myModule.I8[ptrToInt8] = newValue;
|
||||||
myModule.U16[ptrToInt16 >>> 1] = newValue;
|
myModule.I16[ptrToInt16 >>> 1] = newValue;
|
||||||
myModule.U32[ptrToInt32 >>> 2] = newValue;
|
myModule.I32[ptrToInt32 >>> 2] = newValue;
|
||||||
|
myModule.I64[ptrToInt64 >>> 3] = newValue;
|
||||||
myModule.F32[ptrToFloat32 >>> 2] = newValue;
|
myModule.F32[ptrToFloat32 >>> 2] = newValue;
|
||||||
myModule.F64[ptrToFloat64 >>> 3] = newValue;
|
myModule.F64[ptrToFloat64 >>> 3] = newValue;
|
||||||
```
|
```
|
||||||
@ -128,5 +140,3 @@ import MyModule from "myModule"; // pointing at the d.ts
|
|||||||
|
|
||||||
const myModule = loader.instatiateBuffer<typeof MyModule>(fs.readFileSync("myModule.wasm"), myImports);
|
const myModule = loader.instatiateBuffer<typeof MyModule>(fs.readFileSync("myModule.wasm"), myImports);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Hint:** You can produce a `.d.ts` for your module with the `-d` option on the command line.
|
|
||||||
|
20
lib/loader/index.d.ts
vendored
20
lib/loader/index.d.ts
vendored
@ -13,21 +13,25 @@ interface ImportsObject {
|
|||||||
/** Utility mixed in by the loader. */
|
/** Utility mixed in by the loader. */
|
||||||
interface ASUtil {
|
interface ASUtil {
|
||||||
/** An 8-bit signed integer view on the memory. */
|
/** An 8-bit signed integer view on the memory. */
|
||||||
readonly I8: Uint8Array,
|
readonly I8: Uint8Array;
|
||||||
/** An 8-bit unsigned integer view on the memory. */
|
/** An 8-bit unsigned integer view on the memory. */
|
||||||
readonly U8: Uint8Array,
|
readonly U8: Uint8Array;
|
||||||
/** A 16-bit signed integer view on the memory. */
|
/** A 16-bit signed integer view on the memory. */
|
||||||
readonly I16: Uint16Array,
|
readonly I16: Uint16Array;
|
||||||
/** A 16-bit unsigned integer view on the memory. */
|
/** A 16-bit unsigned integer view on the memory. */
|
||||||
readonly U16: Uint16Array,
|
readonly U16: Uint16Array;
|
||||||
/** A 32-bit signed integer view on the memory. */
|
/** A 32-bit signed integer view on the memory. */
|
||||||
readonly I32: Uint32Array,
|
readonly I32: Uint32Array;
|
||||||
/** A 32-bit unsigned integer view on the memory. */
|
/** A 32-bit unsigned integer view on the memory. */
|
||||||
readonly U32: Uint32Array,
|
readonly U32: Uint32Array;
|
||||||
|
/** A 64-bit signed integer view on the memory. */
|
||||||
|
readonly I64: any; // BigInt64Array
|
||||||
|
/** A 64-bit unsigned integer vieww on the memory. */
|
||||||
|
readonly U64: any; // BigUint64Array
|
||||||
/** A 32-bit float view on the memory. */
|
/** A 32-bit float view on the memory. */
|
||||||
readonly F32: Float32Array,
|
readonly F32: Float32Array;
|
||||||
/** A 64-bit float view on the memory. */
|
/** A 64-bit float view on the memory. */
|
||||||
readonly F64: Float64Array,
|
readonly F64: Float64Array;
|
||||||
/** Allocates a new string in the module's memory and returns its pointer. */
|
/** Allocates a new string in the module's memory and returns its pointer. */
|
||||||
newString(str: string): number;
|
newString(str: string): number;
|
||||||
/** Gets a string from the module's memory by its pointer. */
|
/** Gets a string from the module's memory by its pointer. */
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
const hasBigInt64 = typeof BigUint64Array !== "undefined";
|
||||||
|
|
||||||
/** Instantiates an AssemblyScript module using the specified imports. */
|
/** Instantiates an AssemblyScript module using the specified imports. */
|
||||||
function instantiate(module, imports) {
|
function instantiate(module, imports) {
|
||||||
|
|
||||||
@ -17,7 +19,7 @@ function instantiate(module, imports) {
|
|||||||
var exports = instance.exports;
|
var exports = instance.exports;
|
||||||
|
|
||||||
// Provide views for all sorts of basic values
|
// Provide views for all sorts of basic values
|
||||||
var mem, I8, U8, I16, U16, I32, U32, F32, F64;
|
var mem, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
|
||||||
|
|
||||||
/** Updates memory views if memory has grown meanwhile. */
|
/** Updates memory views if memory has grown meanwhile. */
|
||||||
function checkMem() {
|
function checkMem() {
|
||||||
@ -30,6 +32,10 @@ function instantiate(module, imports) {
|
|||||||
U16 = new Uint16Array(mem);
|
U16 = new Uint16Array(mem);
|
||||||
I32 = new Int32Array(mem);
|
I32 = new Int32Array(mem);
|
||||||
U32 = new Uint32Array(mem);
|
U32 = new Uint32Array(mem);
|
||||||
|
if (hasBigInt64) {
|
||||||
|
I64 = new BigInt64Array(mem);
|
||||||
|
U64 = new BigUint64Array(mem);
|
||||||
|
}
|
||||||
F32 = new Float32Array(mem);
|
F32 = new Float32Array(mem);
|
||||||
F64 = new Float64Array(mem);
|
F64 = new Float64Array(mem);
|
||||||
}
|
}
|
||||||
@ -73,6 +79,8 @@ function instantiate(module, imports) {
|
|||||||
get U16() { checkMem(); return U16; },
|
get U16() { checkMem(); return U16; },
|
||||||
get I32() { checkMem(); return I32; },
|
get I32() { checkMem(); return I32; },
|
||||||
get U32() { checkMem(); return U32; },
|
get U32() { checkMem(); return U32; },
|
||||||
|
get I64() { checkMem(); return I64; },
|
||||||
|
get U64() { checkMem(); return U64; },
|
||||||
get F32() { checkMem(); return F32; },
|
get F32() { checkMem(); return F32; },
|
||||||
get F64() { checkMem(); return F64; },
|
get F64() { checkMem(); return F64; },
|
||||||
newString,
|
newString,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user