Commit 0c6302f3b6cf83783870ae49f570c87c38ddb414
added callback test
wanderer committed on 6/30/2017, 7:20:24 AMParent: 2696b0ddd05bbef598f8aec13320e0c97f0d4176
Files changed
index.js | changed |
package.json | changed |
tests/c/callback.c | changed |
tests/c/readMem.c | changed |
tests/c/run.c | added |
tests/index.js | changed |
tests/testInterface.js | changed |
tests/wasm/callback.wasm | changed |
tests/wasm/readMem.wasm | changed |
tests/wasm/main.wasm | added |
tests/wast/callback.wast | changed |
tests/wast/readMem.wast | changed |
tests/wast/main.wast | added |
index.js | ||
---|---|---|
@@ -6,23 +6,32 @@ | ||
6 | 6 | * interfaces for interacting with the exoInterface |
7 | 7 | * @param {object} exoInterface - the exoInterface instance |
8 | 8 | * @param {object} imports - a map of imports to expose to the wasm binary |
9 | 9 | */ |
10 | - constructor (exoInterface, imports) { | |
11 | - this.exoInterface = exoInterface | |
10 | + constructor (kernel, imports) { | |
11 | + this.kernel = kernel | |
12 | 12 | this.imports = imports |
13 | 13 | this.referanceMap = new ReferanceMap() |
14 | 14 | } |
15 | 15 | |
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 | + | |
16 | 23 | /** |
17 | 24 | * Runs the wasm VM given a message |
18 | 25 | * @param {object} message |
19 | 26 | * @returns {Promise} a promise that resolves once the compuation is finished |
20 | 27 | */ |
21 | 28 | 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 | |
25 | 34 | const importMap = {} |
26 | 35 | for (const name in this.imports) { |
27 | 36 | importMap[name] = {} |
28 | 37 | const Import = this.imports[name] |
@@ -36,12 +45,12 @@ | ||
36 | 45 | } |
37 | 46 | } |
38 | 47 | } |
39 | 48 | |
40 | - const result = await WebAssembly.instantiate(this.exoInterface.state['/'].code, importMap) | |
49 | + const result = await WebAssembly.instantiate(this.kernel.state.code, importMap) | |
41 | 50 | this.instance = result.instance |
42 | 51 | // runs the wasm code |
43 | - this.instance.exports.main() | |
52 | + this.instance.exports[method]() | |
44 | 53 | return this.onDone() |
45 | 54 | } |
46 | 55 | |
47 | 56 | /** |
@@ -84,22 +93,5 @@ | ||
84 | 93 | */ |
85 | 94 | getMemory (offset, length) { |
86 | 95 | return new Uint8Array(this.instance.exports.memory.buffer, offset, length) |
87 | 96 | } |
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 | - } | |
105 | 97 | } |
package.json | ||
---|---|---|
@@ -25,7 +25,8 @@ | ||
25 | 25 | "ipfs": "^0.24.1", |
26 | 26 | "istanbul": "^1.1.0-alpha.1", |
27 | 27 | "primea-hypervisor": "0.0.1", |
28 | 28 | "standard": "^10.0.0", |
29 | - "tape": "^4.6.3" | |
29 | + "tape": "^4.6.3", | |
30 | + "wast2wasm": "0.0.1" | |
30 | 31 | } |
31 | 32 | } |
tests/c/callback.c | ||
---|---|---|
@@ -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.c | ||
---|---|---|
@@ -1,8 +1,8 @@ | ||
1 | 1 | extern int readMem(const char*); |
2 | 2 | extern void equals(int, int); |
3 | 3 | |
4 | -int main() | |
4 | +int init() | |
5 | 5 | { |
6 | 6 | const char * s = "\x61\x73\x6d\x01"; |
7 | 7 | int val = readMem(s); |
8 | 8 | equals(val, 0x61); |
tests/c/run.c | ||
---|---|---|
@@ -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.js | ||
---|---|---|
@@ -59,51 +59,48 @@ | ||
59 | 59 | t.true(true, 'should clear refances') |
60 | 60 | } |
61 | 61 | }) |
62 | 62 | |
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 | + | |
63 | 74 | tape('wasm container - mem', async t => { |
64 | - t.plan(2) | |
75 | + t.plan(1) | |
65 | 76 | const hypervisor = new Hypervisor(node.dag) |
66 | 77 | const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) |
67 | 78 | hypervisor.registerContainer('wasm', WasmContainer, { |
68 | 79 | env: ContainerTestInterface, |
69 | 80 | test: testInterface(t) |
70 | 81 | }) |
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) | |
76 | 83 | }) |
77 | 84 | |
78 | 85 | tape('wasm container - callbacks', async t => { |
79 | - t.plan(2) | |
86 | + t.plan(1) | |
80 | 87 | const hypervisor = new Hypervisor(node.dag) |
81 | - const readMem = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) | |
88 | + const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) | |
82 | 89 | hypervisor.registerContainer('wasm', WasmContainer, { |
83 | 90 | env: ContainerTestInterface, |
84 | 91 | test: testInterface(t) |
85 | 92 | }) |
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) | |
91 | 94 | }) |
92 | 95 | |
93 | - tape('wasm container - invalid', async t => { | |
96 | + tape.only('wasm container - invalid', async t => { | |
94 | 97 | t.plan(1) |
95 | 98 | const hypervisor = new Hypervisor(node.dag) |
96 | 99 | hypervisor.registerContainer('wasm', WasmContainer, { |
97 | 100 | env: ContainerTestInterface, |
98 | 101 | test: testInterface(t) |
99 | 102 | }) |
100 | 103 | |
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])) | |
108 | 105 | }) |
109 | 106 | }) |
tests/testInterface.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | module.exports = function (t) { |
2 | - return class TeInterface { | |
2 | + return class TestInterface { | |
3 | 3 | constructor () { |
4 | 4 | this.t = t |
5 | 5 | } |
6 | 6 |
tests/wasm/callback.wasm | ||
---|---|---|
@@ -1,2 +1,2 @@ | ||
1 | - asm ` ` ` ` envcallback testequals p (table memory callbackFunc main A | |
2 | - AA A A | |
1 | + asm ` ` ` ` envcallback testequals p 'table memory theCallback init A | |
2 | + A A A A |
tests/wasm/readMem.wasm | ||
---|---|---|
@@ -1,2 +1,2 @@ | ||
1 | - asm `` ` testequals envreadMem p memory main | |
1 | + asm `` ` testequals envreadMem p memory init | |
2 | 2 | AA� A Aasm |
tests/wast/callback.wast | ||
---|---|---|
@@ -4,20 +4,20 @@ | ||
4 | 4 | (type $FUNCSIG$v (func)) |
5 | 5 | (import "env" "callback" (func $callback (param i32))) |
6 | 6 | (import "test" "equals" (func $equals (param i32 i32))) |
7 | 7 | (table (export "table") 2 2 anyfunc ) |
8 | - (elem (i32.const 0) $__wasm_nullptr $callbackFunc) | |
8 | + (elem (i32.const 0) $__wasm_nullptr $theCallback) | |
9 | 9 | (memory $0 1) |
10 | 10 | (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) | |
14 | 14 | (call $equals |
15 | - (i32.const 1) | |
16 | - (i32.const 1) | |
15 | + (i32.const 0) | |
16 | + (i32.const 0) | |
17 | 17 | ) |
18 | 18 | ) |
19 | - (func $main (result i32) | |
19 | + (func $init (result i32) | |
20 | 20 | (call $callback |
21 | 21 | (i32.const 1) |
22 | 22 | ) |
23 | 23 | (i32.const 1) |
tests/wast/readMem.wast | ||
---|---|---|
@@ -6,10 +6,10 @@ | ||
6 | 6 | (table 0 anyfunc) |
7 | 7 | (memory $0 1) |
8 | 8 | (data (i32.const 16) "asm\01\00") |
9 | 9 | (export "memory" (memory $0)) |
10 | - (export "main" (func $main)) | |
11 | - (func $main (result i32) | |
10 | + (export "init" (func $init)) | |
11 | + (func $init (result i32) | |
12 | 12 | (call $equals |
13 | 13 | (call $readMem |
14 | 14 | (i32.const 16) |
15 | 15 | ) |
tests/wast/main.wast | ||
---|---|---|
@@ -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