git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit 6a5f857a376a1e31ae18a4fd65720456f048c611

added tests

wanderer committed on 3/14/2017, 10:56:03 PM
Parent: d437e946d7e817f50952f0f77abebbce532e671e

Files changed

index.jschanged
package.jsonchanged
README.mdadded
tests/index.jsadded
tests/wasm/asyncTest.wasmadded
tests/wasm/asyncTest.wastadded
tests/wasm/testCode.wasmadded
tests/wasm/testCode.wastadded
tests/wasm/testMem.wasmadded
tests/wasm/testMem.wastadded
index.jsView
@@ -1,50 +1,62 @@
1-module.exports = class Wasm {
1 +module.exports = class WasmContainer {
22 /**
33 * The interface API is the api the exposed to interfaces. All queries about
44 * the enviroment and call to the kernel go through this API
55 */
66 constructor (code) {
77 this._module = WebAssembly.Module(code)
88 }
9 +
10 + static get name () {
11 + return 'wasm'
12 + }
913 /**
1014 * Runs the core VM with a given environment and imports
1115 */
12- async run (message, kernel, imports) {
16 + async run (message, kernel, imports = []) {
1317 const responses = {}
1418 /**
1519 * Builds a import map with an array of given interfaces
1620 */
17- async function buildImports (kernelApi, kernel, imports) {
21 + function buildImports (opts, imports) {
1822 const importMap = {}
1923 for (const Import of imports) {
20- const response = responses[Import.name] = {}
21- const newIterface = new Import(kernelApi, message, response)
22- importMap[Import.name] = newIterface.exports
23- // initailize the import
24- await newIterface.initialize()
24 + const name = Import.name
25 + opts.response = responses[name] = {}
26 + const newInterface = new Import(opts)
27 + const props = Object.getOwnPropertyNames(Import.prototype)
28 +
29 + // bind the methods to the correct 'this'
30 + importMap[name] = {}
31 + for (const prop of props) {
32 + importMap[name][prop] = newInterface[prop].bind(newInterface)
33 + }
2534 }
2635 return importMap
2736 }
2837
2938 let instance
30- const interfaceApi = {
31- /**
32- * adds an aync operation to the operations queue
33- */
34- pushOpsQueue: (promise, callbackIndex, intefaceCallback) => {
35- this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
36- const result = intefaceCallback(values.pop())
37- instance.exports.callback.get(callbackIndex)(result)
38- })
39 +
40 + const opts = {
41 + vm: {
42 + /**
43 + * adds an aync operation to the operations queue
44 + */
45 + pushOpsQueue: (promise, callbackIndex) => {
46 + this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
47 + const result = values.pop()
48 + instance.exports.callback.get(callbackIndex)(result)
49 + })
50 + },
51 + memory: () => {
52 + return instance.exports.memory.buffer
53 + }
3954 },
40- memory: () => {
41- return instance.exports.memory.buffer
42- },
43- kernel: kernel
55 + kernel: kernel,
56 + message: message
4457 }
45-
46- const initializedImports = await buildImports(interfaceApi, kernel, imports)
58 + const initializedImports = buildImports(opts, imports)
4759 instance = WebAssembly.Instance(this._module, initializedImports)
4860
4961 if (instance.exports.main) {
5062 instance.exports.main()
package.jsonView
@@ -1,11 +1,24 @@
11 {
2- "name": "wasm-harness",
3- "version": "1.0.0",
2 + "name": "wasm-container",
3 + "version": "0.0.0",
44 "description": "",
5- "main": "wasmAgent.js",
5 + "main": "index.js",
66 "scripts": {
7- "test": "echo \"Error: no test specified\" && exit 1"
7 + "coverage": "node --harmony ./node_modules/istanbul/lib/cli.js cover ./tests/index.js",
8 + "coveralls": "npm run coverage && coveralls <coverage/lcov.info",
9 + "lint": "standard",
10 + "test": "node --harmony ./tests/index.js"
811 },
912 "author": "",
10- "license": "ISC"
13 + "license": "ISC",
14 + "standard": {
15 + "globals": [
16 + "WebAssembly"
17 + ]
18 + },
19 + "devDependencies": {
20 + "istanbul": "^1.1.0-alpha.1",
21 + "standard": "^9.0.1",
22 + "tape": "^4.6.3"
23 + }
1124 }
README.mdView
tests/index.jsView
@@ -1,0 +1,84 @@
1 +const tape = require('tape')
2 +const fs = require('fs')
3 +const WasmContainer = require('../index.js')
4 +
5 +const dir = `${__dirname}/wasm/`
6 +
7 +tape('interface tests', async t => {
8 + const code = fs.readFileSync(`${dir}testCode.wasm`)
9 + class TestInterface {
10 + constructor (opts) {
11 + opts.response.test = 'response!'
12 + }
13 +
14 + static get name () {
15 + return 'test'
16 + }
17 + }
18 +
19 + t.equals(WasmContainer.name, 'wasm', 'container should have a name')
20 +
21 + const container = new WasmContainer(code)
22 + t.ok(container instanceof WasmContainer, 'should be instance of a container class')
23 +
24 + const promise = container.run()
25 + await promise
26 + t.ok(promise instanceof Promise, 'run should return instance of promise')
27 +
28 + const results = await container.run(null, null, [TestInterface])
29 + t.deepEquals(results, {
30 + 'test': {
31 + 'test': 'response!'
32 + }
33 + })
34 +
35 + t.end()
36 +})
37 +
38 +tape('wasm interface test', async t => {
39 + t.plan(1)
40 + const code = fs.readFileSync(`${dir}testMem.wasm`)
41 + class TestInterfaceMem {
42 + constructor (opts) {
43 + this.opts = opts
44 + }
45 +
46 + static get name () {
47 + return 'test'
48 + }
49 +
50 + memory () {
51 + const memory = this.opts.vm.memory()
52 + t.ok(memory instanceof ArrayBuffer, 'should have access to memory')
53 + }
54 + }
55 + const container = new WasmContainer(code)
56 + await container.run(null, null, [TestInterfaceMem])
57 +})
58 +
59 +tape('async wasm interface test', async t => {
60 + t.plan(1)
61 + const code = fs.readFileSync(`${dir}asyncTest.wasm`)
62 + class TestInterfaceMem {
63 + constructor (opts) {
64 + this.opts = opts
65 + }
66 +
67 + static get name () {
68 + return 'test'
69 + }
70 +
71 + async (cbOffset) {
72 + const opPromise = new Promise((resolve, reject) => {
73 + resolve()
74 + })
75 + this.opts.vm.pushOpsQueue(opPromise, cbOffset)
76 + }
77 +
78 + done () {
79 + t.ok(true)
80 + }
81 + }
82 + const container = new WasmContainer(code)
83 + await container.run(null, null, [TestInterfaceMem])
84 +})
tests/wasm/asyncTest.wasmView
@@ -1,0 +1,2 @@
1 +asm``testasynctestdonepcallbackmain A 
2 +  A
tests/wasm/asyncTest.wastView
@@ -1,0 +1,17 @@
1 +(module
2 + (import "test" "async" (func $async (param i32)))
3 + (import "test" "done" (func $done))
4 + (table
5 + (export "callback")
6 + anyfunc
7 + (elem
8 + $callback
9 + )
10 + )
11 + (func $callback
12 + call $done
13 + )
14 + (func $main
15 + (call $async (i32.const 0))
16 + )
17 + (export "main" (func $main)))
tests/wasm/testCode.wasmView
@@ -1,0 +1,2 @@
1 +asm`~~memoryfac
2 + BS~B B}~
tests/wasm/testCode.wastView
@@ -1,0 +1,13 @@
1 +(module
2 + (func $fac (param i64) (result i64)
3 + (if i64
4 + (i64.lt_s (get_local 0) (i64.const 1))
5 + (then (i64.const 1))
6 + (else
7 + (i64.mul
8 + (get_local 0)
9 + (call $fac
10 + (i64.sub
11 + (get_local 0)
12 + (i64.const 1)))))))
13 + (export "fac" (func $fac)))
tests/wasm/testMem.wasmView
@@ -1,0 +1,2 @@
1 +asm`testmemorymemorymain
2 +
tests/wasm/testMem.wastView
@@ -1,0 +1,8 @@
1 +(module
2 + (import "test" "memory" (func $mem))
3 + (memory (export "memory") 1)
4 + (func $main
5 + (call $mem)
6 + )
7 +
8 + (export "main" (func $main)))

Built with git-ssb-web