-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm32-test.js
71 lines (64 loc) · 2.86 KB
/
wasm32-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
window.addEventListener("load",function(){
var moduleWriter=new Wasm32ModuleWriter();
// addTwo: (a,b) => a+b
var codeWriter=new Wasm32CodeWriter();
codeWriter.get_local(0);
codeWriter.get_local(1);
codeWriter.i32_add();
codeWriter.end();
var type=new Wasm32TypeWriter([Wasm32VarType.i32,Wasm32VarType.i32],[Wasm32VarType.i32]).toUint8Array();
moduleWriter.addFunction("addTwo",type,codeWriter);
moduleWriter.exportFunction("addTwo","nativeAdd");
// triangle: (x) => (x==0)?0:(x+triangle(x-1))
var codeWriter=new Wasm32CodeWriter();
codeWriter.get_local(0);
codeWriter.get_local(0);
codeWriter.if(Wasm32VarType.i32);
codeWriter.get_local(0);
codeWriter.i32_const(1);
codeWriter.i32_sub();
codeWriter.call("triangle");
codeWriter.else();
codeWriter.i32_const(0);
codeWriter.end();
codeWriter.i32_add();
codeWriter.end();
var type=new Wasm32TypeWriter([Wasm32VarType.i32],[Wasm32VarType.i32]).toUint8Array();
moduleWriter.addFunction("triangle",type,codeWriter);
moduleWriter.exportFunction("triangle","triangle");
// addTwoCallback: (a,b) => a+b
var codeWriter=new Wasm32CodeWriter();
codeWriter.get_local(0);
codeWriter.get_local(1);
codeWriter.i32_add();
codeWriter.call("log");
codeWriter.get_local(0);
codeWriter.get_local(1);
codeWriter.call("log2");
codeWriter.end();
var type=new Wasm32TypeWriter([Wasm32VarType.i32,Wasm32VarType.i32],[]).toUint8Array();
moduleWriter.addFunction("addTwoCallback",type,codeWriter);
moduleWriter.exportFunction("addTwoCallback","nativeAddCallback");
moduleWriter.importFunction("log",new Wasm32TypeWriter([Wasm32VarType.i32],[]).toUint8Array(),"console","log");
moduleWriter.importFunction("log2",new Wasm32TypeWriter([Wasm32VarType.i32,Wasm32VarType.i32],[]).toUint8Array(),"console","log2");
var byteCode=moduleWriter.generateModule();
var arr=[];
for(var i=0;i<byteCode.length;++i){
arr[i]=byteCode[i].toString(16);
}
console.log(arr);
WebAssembly.instantiate(byteCode,{console:{log:function(x){console.log("Callback:" +x);},log2:function(x,y){console.log("Callback:" +x+","+y);}}}).then(function(result){
var instance=result.instance;
console.log(instance.exports.nativeAdd(5,5));
console.log(instance.exports.nativeAdd(1,2));
console.log(instance.exports.nativeAdd(-2,-5));
console.log(instance.exports.nativeAdd(-2,5));
console.log(instance.exports.nativeAdd(1024,1024));
console.log(instance.exports.triangle(10));
console.log(instance.exports.triangle(0));
console.log(instance.exports.triangle(1));
console.log(instance.exports.triangle(10000));
instance.exports.nativeAddCallback(1024,1024);
instance.exports.nativeAddCallback(10,55);
});
});