mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 08:51:34 +00:00
more loader work
This commit is contained in:
@ -49,7 +49,7 @@ export function varadd(a: i32 = 1, b: i32 = 2): i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
export const varadd_ptr = varadd;
|
||||
export const varadd_ref = varadd;
|
||||
|
||||
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
|
||||
return fn(a, b);
|
||||
@ -60,3 +60,5 @@ export function dotrace(num: f64): void {
|
||||
}
|
||||
|
||||
export const INT32ARRAY_ID = idof<Int32Array>();
|
||||
export const UINT32ARRAY_ID = idof<Uint32Array>();
|
||||
export const FLOAT32ARRAY_ID = idof<Float32Array>();
|
||||
|
Binary file not shown.
@ -24,43 +24,70 @@ assert(module.memory instanceof WebAssembly.Memory);
|
||||
assert(typeof module.memory.copy === "function");
|
||||
|
||||
// should be able to get an exported string
|
||||
assert.strictEqual(module.getString(module.COLOR), "red");
|
||||
assert.strictEqual(module.__getString(module.COLOR), "red");
|
||||
|
||||
// should be able to allocate and work with a new string
|
||||
var str = "Hello world!𤭢";
|
||||
var ptr = module.newString(str);
|
||||
assert.strictEqual(module.getString(ptr), str);
|
||||
assert.strictEqual(module.strlen(ptr), str.length);
|
||||
{
|
||||
let str = "Hello world!𤭢";
|
||||
let ref = module.__retain(module.__allocString(str));
|
||||
assert.strictEqual(module.__getString(ref), str);
|
||||
assert.strictEqual(module.strlen(ref), str.length);
|
||||
module.__release(ref);
|
||||
}
|
||||
|
||||
// should be able to allocate a typed array and sum up its values
|
||||
var arr = [1, 2, 3, 4, 5, 0x7fffffff];
|
||||
ptr = module.newArray(module.INT32ARRAY_ID, arr);
|
||||
assert.strictEqual(module.sum(ptr), arr.reduce((a, b) => a + b, 0) | 0);
|
||||
// should be able to allocate a typed array
|
||||
{
|
||||
var arr = [1, 2, 3, 4, 5, 0x80000000 | 0];
|
||||
let ref = module.__retain(module.__allocArray(module.INT32ARRAY_ID, arr));
|
||||
assert(module.__instanceof(ref, module.INT32ARRAY_ID));
|
||||
|
||||
// should be able to get a view on an internal typed array
|
||||
assert.deepEqual(Array.from(module.getArray(ptr)), arr);
|
||||
// should be able to get a view on an internal typed array
|
||||
assert.deepEqual(module.__getArray(ref), arr);
|
||||
|
||||
// should be able to release no longer needed references
|
||||
module.__release(ptr);
|
||||
try {
|
||||
module.__release(ptr);
|
||||
assert(false);
|
||||
} catch (e) {};
|
||||
// should be able to sum up its values
|
||||
assert.strictEqual(module.sum(ref), arr.reduce((a, b) => (a + b) | 0, 0) | 0);
|
||||
|
||||
// should be able to just call a function with variable arguments
|
||||
// should be able to release no longer needed references
|
||||
module.__release(ref);
|
||||
try { module.__release(ref); assert(false); } catch (e) {};
|
||||
}
|
||||
|
||||
// should be able to distinguish between signed and unsigned
|
||||
{
|
||||
let arr = [1, -1 >>> 0, 0x80000000];
|
||||
let ref = module.__retain(module.__allocArray(module.UINT32ARRAY_ID, arr));
|
||||
assert(module.__instanceof(ref, module.UINT32ARRAY_ID));
|
||||
assert.deepEqual(module.__getArray(ref), arr);
|
||||
module.__release(ref);
|
||||
try { module.__release(ref); assert(false); } catch (e) {};
|
||||
}
|
||||
|
||||
// should be able to distinguish between integer and float
|
||||
{
|
||||
let arr = [0.0, 1.5, 2.5];
|
||||
let ref = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, arr));
|
||||
assert(module.__instanceof(ref, module.FLOAT32ARRAY_ID));
|
||||
assert.deepEqual(module.__getArray(ref), arr);
|
||||
module.__release(ref);
|
||||
try { module.__release(ref); assert(false); } catch (e) {};
|
||||
}
|
||||
|
||||
// should be able to correctly call a function with variable arguments
|
||||
assert.strictEqual(module.varadd(), 3);
|
||||
assert.strictEqual(module.varadd(2, 3), 5);
|
||||
assert.strictEqual(module.varadd(2), 4);
|
||||
|
||||
// TBD: table is no more exported by default to allow more optimizations
|
||||
|
||||
// should be able to get a function from the table and just call it with variable arguments
|
||||
// var fn = module.getFunction(module.varadd_ptr);
|
||||
// var fn = module.getFunction(module.varadd_ref);
|
||||
// assert.strictEqual(fn(), 3);
|
||||
// assert.strictEqual(fn(2, 3), 5);
|
||||
// assert.strictEqual(fn(2), 4);
|
||||
|
||||
// should be able to create a new function and call it from WASM
|
||||
// ptr = module.newFunction(module.varadd);
|
||||
// assert.strictEqual(module.calladd(ptr, 2, 3), 5);
|
||||
// ref = module.newFunction(module.varadd);
|
||||
// assert.strictEqual(module.calladd(ref, 2, 3), 5);
|
||||
|
||||
// should be able to use a class
|
||||
var car = new module.Car(5);
|
||||
|
Reference in New Issue
Block a user