git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit 0c6302f3b6cf83783870ae49f570c87c38ddb414

added callback test

wanderer committed on 6/30/2017, 7:20:24 AM
Parent: 2696b0ddd05bbef598f8aec13320e0c97f0d4176

Files changed

index.jschanged
package.jsonchanged
tests/c/callback.cchanged
tests/c/readMem.cchanged
tests/c/run.cadded
tests/index.jschanged
tests/testInterface.jschanged
tests/wasm/callback.wasmchanged
tests/wasm/readMem.wasmchanged
tests/wasm/main.wasmadded
tests/wast/callback.wastchanged
tests/wast/readMem.wastchanged
tests/wast/main.wastadded
index.jsView
@@ -6,23 +6,32 @@
66 * interfaces for interacting with the exoInterface
77 * @param {object} exoInterface - the exoInterface instance
88 * @param {object} imports - a map of imports to expose to the wasm binary
99 */
10- constructor (exoInterface, imports) {
11- this.exoInterface = exoInterface
10+ constructor (kernel, imports) {
11+ this.kernel = kernel
1212 this.imports = imports
1313 this.referanceMap = new ReferanceMap()
1414 }
1515
16+ async initailize (message) {
17+ if (!WebAssembly.validate(this.kernel.state.code)) {
18+ throw new Error('invalid wasm binary')
19+ }
20+ return this._run(message, 'init')
21+ }
22+
1623 /**
1724 * Runs the wasm VM given a message
1825 * @param {object} message
1926 * @returns {Promise} a promise that resolves once the compuation is finished
2027 */
2128 async run (message) {
22- /**
23- * Builds a import map with an array of given interfaces
24- */
29+ return this._run(message, 'main')
30+ }
31+
32+ async _run (message, method) {
33+ // Builds a import map with an array of given interfaces
2534 const importMap = {}
2635 for (const name in this.imports) {
2736 importMap[name] = {}
2837 const Import = this.imports[name]
@@ -36,12 +45,12 @@
3645 }
3746 }
3847 }
3948
40- const result = await WebAssembly.instantiate(this.exoInterface.state['/'].code, importMap)
49+ const result = await WebAssembly.instantiate(this.kernel.state.code, importMap)
4150 this.instance = result.instance
4251 // runs the wasm code
43- this.instance.exports.main()
52+ this.instance.exports[method]()
4453 return this.onDone()
4554 }
4655
4756 /**
@@ -84,22 +93,5 @@
8493 */
8594 getMemory (offset, length) {
8695 return new Uint8Array(this.instance.exports.memory.buffer, offset, length)
8796 }
88-
89- /**
90- * creates the intail state for a wasm contract
91- * @param {ArrayBuffer} wasm - the wasm code
92- * @returns {Object}
93- */
94- static createState (wasm) {
95- if (!WebAssembly.validate(wasm)) {
96- throw new Error('invalid wasm binary')
97- }
98-
99- return {
100- nonce: [0],
101- ports: {},
102- code: wasm
103- }
104- }
10597 }
package.jsonView
@@ -25,7 +25,8 @@
2525 "ipfs": "^0.24.1",
2626 "istanbul": "^1.1.0-alpha.1",
2727 "primea-hypervisor": "0.0.1",
2828 "standard": "^10.0.0",
29- "tape": "^4.6.3"
29+ "tape": "^4.6.3",
30+ "wast2wasm": "0.0.1"
3031 }
3132 }
tests/c/callback.cView
@@ -1,0 +1,12 @@
1+extern void callback(void (*callback)(void));
2+extern void equals(int, int);
3+
4+void theCallback(void) {
5+ equals(0, 0);
6+}
7+
8+int init(void)
9+{
10+ callback(&theCallback);
11+ return 1;
12+}
tests/c/readMem.cView
@@ -1,8 +1,8 @@
11 extern int readMem(const char*);
22 extern void equals(int, int);
33
4-int main()
4+int init()
55 {
66 const char * s = "\x61\x73\x6d\x01";
77 int val = readMem(s);
88 equals(val, 0x61);
tests/c/run.cView
@@ -1,0 +1,12 @@
1+extern void equals(int, int);
2+
3+int init()
4+{
5+ return 0;
6+}
7+
8+int main()
9+{
10+ equals(0, 0);
11+ return 0;
12+}
tests/index.jsView
@@ -59,51 +59,48 @@
5959 t.true(true, 'should clear refances')
6060 }
6161 })
6262
63+ tape('wasm container - main', async t => {
64+ t.plan(1)
65+ const hypervisor = new Hypervisor(node.dag)
66+ const main = fs.readFileSync(`${__dirname}/wasm/main.wasm`)
67+ hypervisor.registerContainer('wasm', WasmContainer, {
68+ test: testInterface(t)
69+ })
70+ const instance = await hypervisor.createInstance('wasm', main)
71+ instance.run(instance.createMessage())
72+ })
73+
6374 tape('wasm container - mem', async t => {
64- t.plan(2)
75+ t.plan(1)
6576 const hypervisor = new Hypervisor(node.dag)
6677 const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`)
6778 hypervisor.registerContainer('wasm', WasmContainer, {
6879 env: ContainerTestInterface,
6980 test: testInterface(t)
7081 })
71- const root = await hypervisor.createInstance('wasm', {
72- '/': WasmContainer.createState(readMem)
73- })
74- const r = await root.run()
75- t.deepEquals(r, {}, 'should have no return value')
82+ await hypervisor.createInstance('wasm', readMem)
7683 })
7784
7885 tape('wasm container - callbacks', async t => {
79- t.plan(2)
86+ t.plan(1)
8087 const hypervisor = new Hypervisor(node.dag)
81- const readMem = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
88+ const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
8289 hypervisor.registerContainer('wasm', WasmContainer, {
8390 env: ContainerTestInterface,
8491 test: testInterface(t)
8592 })
86- const root = await hypervisor.createInstance('wasm', {
87- '/': WasmContainer.createState(readMem)
88- })
89- const r = await root.run()
90- t.deepEquals(r, {}, 'should have no return value')
93+ hypervisor.createInstance('wasm', callBackWasm)
9194 })
9295
93- tape('wasm container - invalid', async t => {
96+ tape.only('wasm container - invalid', async t => {
9497 t.plan(1)
9598 const hypervisor = new Hypervisor(node.dag)
9699 hypervisor.registerContainer('wasm', WasmContainer, {
97100 env: ContainerTestInterface,
98101 test: testInterface(t)
99102 })
100103
101- try {
102- await hypervisor.createInstance('wasm', {
103- '/': WasmContainer.createState(Buffer.from([0x00]))
104- })
105- } catch (e) {
106- t.true(true, 'should trap on invalid wasm')
107- }
104+ await hypervisor.createInstance('wasm', Buffer.from([0x00]))
108105 })
109106 })
tests/testInterface.jsView
@@ -1,6 +1,6 @@
11 module.exports = function (t) {
2- return class TeInterface {
2+ return class TestInterface {
33 constructor () {
44 this.t = t
55 }
66
tests/wasm/callback.wasmView
@@ -1,2 +1,2 @@
1-asm````envcallbacktestequalsp(tablememory callbackFuncmain A 
2-AA AA 
1+asm````envcallbacktestequalsp'tablememory theCallbackinit A 
2+AA AA 
tests/wasm/readMem.wasmView
@@ -1,2 +1,2 @@
1-asm```testequalsenvreadMempmemorymain
1+asm```testequalsenvreadMempmemoryinit
22  AA�A A asm
tests/wasm/main.wasmView
@@ -1,0 +1,4 @@
1+asm
2+``testequalspmemoryinitmain
3+A
4+AAA
tests/wast/callback.wastView
@@ -4,20 +4,20 @@
44 (type $FUNCSIG$v (func))
55 (import "env" "callback" (func $callback (param i32)))
66 (import "test" "equals" (func $equals (param i32 i32)))
77 (table (export "table") 2 2 anyfunc )
8- (elem (i32.const 0) $__wasm_nullptr $callbackFunc)
8+ (elem (i32.const 0) $__wasm_nullptr $theCallback)
99 (memory $0 1)
1010 (export "memory" (memory $0))
11- (export "callbackFunc" (func $callbackFunc))
12- (export "main" (func $main))
13- (func $callbackFunc (type $FUNCSIG$v)
11+ (export "theCallback" (func $theCallback))
12+ (export "init" (func $init))
13+ (func $theCallback (type $FUNCSIG$v)
1414 (call $equals
15- (i32.const 1)
16- (i32.const 1)
15+ (i32.const 0)
16+ (i32.const 0)
1717 )
1818 )
19- (func $main (result i32)
19+ (func $init (result i32)
2020 (call $callback
2121 (i32.const 1)
2222 )
2323 (i32.const 1)
tests/wast/readMem.wastView
@@ -6,10 +6,10 @@
66 (table 0 anyfunc)
77 (memory $0 1)
88 (data (i32.const 16) "asm\01\00")
99 (export "memory" (memory $0))
10- (export "main" (func $main))
11- (func $main (result i32)
10+ (export "init" (func $init))
11+ (func $init (result i32)
1212 (call $equals
1313 (call $readMem
1414 (i32.const 16)
1515 )
tests/wast/main.wastView
@@ -1,0 +1,19 @@
1+(module
2+ (type $FUNCSIG$vii (func (param i32 i32)))
3+ (import "test" "equals" (func $equals (param i32 i32)))
4+ (table 0 anyfunc)
5+ (memory $0 1)
6+ (export "memory" (memory $0))
7+ (export "init" (func $init))
8+ (export "main" (func $main))
9+ (func $init (result i32)
10+ (i32.const 0)
11+ )
12+ (func $main (result i32)
13+ (call $equals
14+ (i32.const 0)
15+ (i32.const 0)
16+ )
17+ (i32.const 0)
18+ )
19+)

Built with git-ssb-web