primea-wasm-container
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -1,1 +0,1 @@ | ||
{"/home/null/code/wasm-container/index.js":{"path":"/home/null/code/wasm-container/index.js","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":78,"column":1}},"1":{"start":{"line":7,"column":4},"end":{"line":7,"column":43}},"2":{"start":{"line":11,"column":4},"end":{"line":11,"column":17}},"3":{"start":{"line":17,"column":22},"end":{"line":17,"column":24}},"4":{"start":{"line":22,"column":24},"end":{"line":22,"column":26}},"5":{"start":{"line":23,"column":6},"end":{"line":34,"column":7}},"6":{"start":{"line":24,"column":21},"end":{"line":24,"column":32}},"7":{"start":{"line":25,"column":8},"end":{"line":25,"column":44}},"8":{"start":{"line":26,"column":29},"end":{"line":26,"column":45}},"9":{"start":{"line":27,"column":22},"end":{"line":27,"column":66}},"10":{"start":{"line":30,"column":8},"end":{"line":32,"column":9}},"11":{"start":{"line":31,"column":10},"end":{"line":31,"column":68}},"12":{"start":{"line":33,"column":8},"end":{"line":33,"column":38}},"13":{"start":{"line":35,"column":6},"end":{"line":35,"column":22}},"14":{"start":{"line":40,"column":17},"end":{"line":57,"column":5}},"15":{"start":{"line":46,"column":10},"end":{"line":49,"column":12}},"16":{"start":{"line":47,"column":27},"end":{"line":47,"column":57}},"17":{"start":{"line":48,"column":12},"end":{"line":48,"column":64}},"18":{"start":{"line":52,"column":10},"end":{"line":52,"column":47}},"19":{"start":{"line":58,"column":31},"end":{"line":58,"column":58}},"20":{"start":{"line":59,"column":4},"end":{"line":59,"column":69}},"21":{"start":{"line":61,"column":4},"end":{"line":63,"column":5}},"22":{"start":{"line":62,"column":6},"end":{"line":62,"column":29}},"23":{"start":{"line":64,"column":4},"end":{"line":64,"column":23}},"24":{"start":{"line":65,"column":4},"end":{"line":65,"column":20}},"25":{"start":{"line":73,"column":4},"end":{"line":76,"column":5}},"26":{"start":{"line":74,"column":6},"end":{"line":74,"column":30}},"27":{"start":{"line":75,"column":6},"end":{"line":75,"column":26}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":6,"column":2},"end":{"line":6,"column":3}},"loc":{"start":{"line":6,"column":21},"end":{"line":8,"column":3}}},"1":{"name":"(anonymous_1)","decl":{"start":{"line":10,"column":2},"end":{"line":10,"column":3}},"loc":{"start":{"line":10,"column":21},"end":{"line":12,"column":3}}},"2":{"name":"(anonymous_2)","decl":{"start":{"line":16,"column":2},"end":{"line":16,"column":3}},"loc":{"start":{"line":16,"column":44},"end":{"line":66,"column":3}}},"3":{"name":"buildImports","decl":{"start":{"line":21,"column":13},"end":{"line":21,"column":25}},"loc":{"start":{"line":21,"column":42},"end":{"line":36,"column":5}}},"4":{"name":"(anonymous_4)","decl":{"start":{"line":45,"column":22},"end":{"line":45,"column":23}},"loc":{"start":{"line":45,"column":68},"end":{"line":50,"column":9}}},"5":{"name":"(anonymous_5)","decl":{"start":{"line":46,"column":71},"end":{"line":46,"column":72}},"loc":{"start":{"line":46,"column":81},"end":{"line":49,"column":11}}},"6":{"name":"(anonymous_6)","decl":{"start":{"line":51,"column":16},"end":{"line":51,"column":17}},"loc":{"start":{"line":51,"column":22},"end":{"line":53,"column":9}}},"7":{"name":"(anonymous_7)","decl":{"start":{"line":71,"column":2},"end":{"line":71,"column":3}},"loc":{"start":{"line":71,"column":18},"end":{"line":77,"column":3}}}},"branchMap":{"0":{"loc":{"start":{"line":16,"column":30},"end":{"line":16,"column":42}},"type":"default-arg","locations":[{"start":{"line":16,"column":40},"end":{"line":16,"column":42}}]},"1":{"loc":{"start":{"line":61,"column":4},"end":{"line":63,"column":5}},"type":"if","locations":[{"start":{"line":61,"column":4},"end":{"line":63,"column":5}},{"start":{"line":61,"column":4},"end":{"line":63,"column":5}}]}},"s":{"0":1,"1":3,"2":1,"3":4,"4":4,"5":4,"6":3,"7":3,"8":3,"9":3,"10":3,"11":6,"12":3,"13":4,"14":4,"15":1,"16":1,"17":1,"18":1,"19":4,"20":4,"21":4,"22":2,"23":4,"24":4,"25":4,"26":1,"27":1},"f":{"0":3,"1":1,"2":4,"3":4,"4":1,"5":1,"6":1,"7":4},"b":{"0":[1],"1":[2,2]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"6501776cf653e030faf07eaaa5aab70519c05199"}} | ||
{"/home/null/code/js-primea-wasm-container/index.js":{"path":"/home/null/code/js-primea-wasm-container/index.js","statementMap":{"0":{"start":{"line":1,"column":21},"end":{"line":1,"column":45}},"1":{"start":{"line":2,"column":26},"end":{"line":2,"column":62}},"2":{"start":{"line":3,"column":23},"end":{"line":3,"column":56}},"3":{"start":{"line":5,"column":0},"end":{"line":122,"column":1}},"4":{"start":{"line":13,"column":4},"end":{"line":13,"column":11}},"5":{"start":{"line":14,"column":4},"end":{"line":14,"column":24}},"6":{"start":{"line":15,"column":4},"end":{"line":15,"column":29}},"7":{"start":{"line":16,"column":4},"end":{"line":16,"column":42}},"8":{"start":{"line":20,"column":15},"end":{"line":20,"column":27}},"9":{"start":{"line":21,"column":4},"end":{"line":31,"column":5}},"10":{"start":{"line":22,"column":6},"end":{"line":22,"column":44}},"11":{"start":{"line":24,"column":6},"end":{"line":29,"column":7}},"12":{"start":{"line":25,"column":23},"end":{"line":25,"column":41}},"13":{"start":{"line":26,"column":8},"end":{"line":28,"column":9}},"14":{"start":{"line":27,"column":10},"end":{"line":27,"column":46}},"15":{"start":{"line":30,"column":6},"end":{"line":30,"column":35}},"16":{"start":{"line":32,"column":4},"end":{"line":32,"column":43}},"17":{"start":{"line":41,"column":4},"end":{"line":41,"column":42}},"18":{"start":{"line":46,"column":22},"end":{"line":46,"column":24}},"19":{"start":{"line":47,"column":4},"end":{"line":59,"column":5}},"20":{"start":{"line":48,"column":6},"end":{"line":48,"column":26}},"21":{"start":{"line":49,"column":21},"end":{"line":49,"column":39}},"22":{"start":{"line":50,"column":27},"end":{"line":50,"column":43}},"23":{"start":{"line":51,"column":20},"end":{"line":51,"column":64}},"24":{"start":{"line":54,"column":6},"end":{"line":58,"column":7}},"25":{"start":{"line":55,"column":8},"end":{"line":57,"column":9}},"26":{"start":{"line":56,"column":10},"end":{"line":56,"column":71}},"27":{"start":{"line":61,"column":19},"end":{"line":61,"column":83}},"28":{"start":{"line":62,"column":4},"end":{"line":62,"column":35}},"29":{"start":{"line":65,"column":23},"end":{"line":65,"column":53}},"30":{"start":{"line":68,"column":4},"end":{"line":68,"column":45}},"31":{"start":{"line":69,"column":4},"end":{"line":69,"column":24}},"32":{"start":{"line":78,"column":4},"end":{"line":81,"column":5}},"33":{"start":{"line":79,"column":6},"end":{"line":79,"column":30}},"34":{"start":{"line":80,"column":6},"end":{"line":80,"column":19}},"35":{"start":{"line":82,"column":4},"end":{"line":82,"column":29}},"36":{"start":{"line":91,"column":4},"end":{"line":91,"column":59}},"37":{"start":{"line":92,"column":4},"end":{"line":92,"column":25}},"38":{"start":{"line":101,"column":4},"end":{"line":101,"column":48}},"39":{"start":{"line":111,"column":4},"end":{"line":111,"column":78}},"40":{"start":{"line":115,"column":16},"end":{"line":115,"column":50}},"41":{"start":{"line":116,"column":4},"end":{"line":116,"column":16}},"42":{"start":{"line":120,"column":4},"end":{"line":120,"column":37}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":12,"column":2},"end":{"line":12,"column":3}},"loc":{"start":{"line":12,"column":35},"end":{"line":17,"column":3}},"line":12},"1":{"name":"(anonymous_1)","decl":{"start":{"line":19,"column":2},"end":{"line":19,"column":3}},"loc":{"start":{"line":19,"column":29},"end":{"line":33,"column":3}},"line":19},"2":{"name":"(anonymous_2)","decl":{"start":{"line":40,"column":2},"end":{"line":40,"column":3}},"loc":{"start":{"line":40,"column":22},"end":{"line":42,"column":3}},"line":40},"3":{"name":"(anonymous_3)","decl":{"start":{"line":44,"column":2},"end":{"line":44,"column":3}},"loc":{"start":{"line":44,"column":31},"end":{"line":70,"column":3}},"line":44},"4":{"name":"(anonymous_4)","decl":{"start":{"line":76,"column":2},"end":{"line":76,"column":3}},"loc":{"start":{"line":76,"column":18},"end":{"line":83,"column":3}},"line":76},"5":{"name":"(anonymous_5)","decl":{"start":{"line":90,"column":2},"end":{"line":90,"column":3}},"loc":{"start":{"line":90,"column":25},"end":{"line":93,"column":3}},"line":90},"6":{"name":"(anonymous_6)","decl":{"start":{"line":100,"column":2},"end":{"line":100,"column":3}},"loc":{"start":{"line":100,"column":20},"end":{"line":102,"column":3}},"line":100},"7":{"name":"(anonymous_7)","decl":{"start":{"line":110,"column":2},"end":{"line":110,"column":3}},"loc":{"start":{"line":110,"column":29},"end":{"line":112,"column":3}},"line":110},"8":{"name":"(anonymous_8)","decl":{"start":{"line":114,"column":2},"end":{"line":114,"column":3}},"loc":{"start":{"line":114,"column":26},"end":{"line":117,"column":3}},"line":114},"9":{"name":"(anonymous_9)","decl":{"start":{"line":119,"column":2},"end":{"line":119,"column":3}},"loc":{"start":{"line":119,"column":23},"end":{"line":121,"column":3}},"line":119}},"branchMap":{"0":{"loc":{"start":{"line":21,"column":4},"end":{"line":31,"column":5}},"type":"if","locations":[{"start":{"line":21,"column":4},"end":{"line":31,"column":5}},{"start":{"line":21,"column":4},"end":{"line":31,"column":5}}],"line":21},"1":{"loc":{"start":{"line":26,"column":8},"end":{"line":28,"column":9}},"type":"if","locations":[{"start":{"line":26,"column":8},"end":{"line":28,"column":9}},{"start":{"line":26,"column":8},"end":{"line":28,"column":9}}],"line":26},"2":{"loc":{"start":{"line":55,"column":8},"end":{"line":57,"column":9}},"type":"if","locations":[{"start":{"line":55,"column":8},"end":{"line":57,"column":9}},{"start":{"line":55,"column":8},"end":{"line":57,"column":9}}],"line":55}},"s":{"0":1,"1":1,"2":1,"3":1,"4":7,"5":7,"6":7,"7":7,"8":7,"9":7,"10":1,"11":6,"12":11,"13":11,"14":1,"15":6,"16":6,"17":1,"18":7,"19":7,"20":12,"21":12,"22":12,"23":12,"24":12,"25":37,"26":25,"27":7,"28":7,"29":7,"30":7,"31":7,"32":7,"33":2,"34":2,"35":7,"36":2,"37":2,"38":2,"39":3,"40":1,"41":1,"42":14},"f":{"0":7,"1":7,"2":1,"3":7,"4":7,"5":2,"6":2,"7":3,"8":1,"9":14},"b":{"0":[1,6],"1":[1,10],"2":[25,12]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"f0c91166a81de2666fc22952b5609ffe0b76191f"}} |
148
index.js
@@ -1,66 +0,70 @@ | ||
module.exports = class WasmContainer { | ||
const ReferanceMap = require('reference-map') | ||
const AbstractContainer = require('primea-abstract-container') | ||
const ContainerTable = require('primea-container-table') | ||
module.exports = class WasmContainer extends AbstractContainer { | ||
/** | ||
* The interface API is the api the exposed to interfaces. All queries about | ||
* the enviroment and call to the kernel go through this API | ||
* The wasm container runs wasm code and provides a basic API for wasm | ||
* interfaces for interacting with the kernel | ||
* @param {object} kernel - the kernel instance | ||
* @param {object} interfaces - a map of interfaces to expose to the wasm binary | ||
*/ | ||
constructor (code) { | ||
this._module = WebAssembly.Module(code) | ||
constructor (kernel, interfaces) { | ||
super() | ||
this.kernel = kernel | ||
this.imports = interfaces | ||
this.referanceMap = new ReferanceMap() | ||
} | ||
static get name () { | ||
return 'wasm' | ||
async onCreation (message) { | ||
let code = message.data | ||
if (!WebAssembly.validate(code)) { | ||
throw new Error('invalid wasm binary') | ||
} else { | ||
for (const name in this.imports) { | ||
const interf = this.imports[name] | ||
if (interf.initialize) { | ||
code = await interf.initialize(code) | ||
} | ||
} | ||
this.kernel.state.code = code | ||
} | ||
return this._run(message, 'onCreation') | ||
} | ||
/** | ||
* Runs the core VM with a given environment and imports | ||
* Runs the wasm VM given a message | ||
* @param {object} message | ||
* @returns {Promise} a promise that resolves once the compuation is finished | ||
*/ | ||
async run (message, kernel, imports = []) { | ||
const responses = {} | ||
/** | ||
* Builds a import map with an array of given interfaces | ||
*/ | ||
function buildImports (opts, imports) { | ||
const importMap = {} | ||
for (const Import of imports) { | ||
const name = Import.name | ||
opts.response = responses[name] = {} | ||
const newInterface = new Import(opts) | ||
const props = Object.getOwnPropertyNames(Import.prototype) | ||
onMessage (message) { | ||
return this._run(message, 'onMessage') | ||
} | ||
// bind the methods to the correct 'this' | ||
for (const prop of props) { | ||
newInterface[prop] = newInterface[prop].bind(newInterface) | ||
async _run (message, method) { | ||
// Builds a import map with an array of given interfaces | ||
const importMap = {} | ||
for (const name in this.imports) { | ||
importMap[name] = {} | ||
const Import = this.imports[name] | ||
const newInterface = new Import(this) | ||
const props = Object.getOwnPropertyNames(Import.prototype) | ||
// bind the methods to the correct 'this' | ||
for (const prop of props) { | ||
if (prop !== 'constructor') { | ||
importMap[name][prop] = newInterface[prop].bind(newInterface) | ||
} | ||
importMap[name] = newInterface | ||
} | ||
return importMap | ||
} | ||
let instance | ||
const result = await WebAssembly.instantiate(this.kernel.state.code, importMap) | ||
this.instance = result.instance | ||
const opts = { | ||
vm: { | ||
/** | ||
* adds an aync operation to the operations queue | ||
*/ | ||
pushOpsQueue: (promise, callbackIndex, intefaceCallback) => { | ||
this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => { | ||
const result = intefaceCallback(values.pop()) | ||
instance.exports.callback.get(callbackIndex)(result) | ||
}) | ||
}, | ||
memory: () => { | ||
return instance.exports.memory.buffer | ||
} | ||
}, | ||
kernel: kernel, | ||
message: message | ||
} | ||
const initializedImports = buildImports(opts, imports) | ||
instance = WebAssembly.Instance(this._module, initializedImports) | ||
// add the message and ports to the refereance map | ||
const messageRef = this.referanceMap.add(message) | ||
if (instance.exports.main) { | ||
instance.exports.main() | ||
} | ||
await this.onDone() | ||
return responses | ||
// runs the wasm code | ||
this.instance.exports[method](messageRef) | ||
return this.onDone() | ||
} | ||
@@ -70,2 +74,3 @@ | ||
* returns a promise that resolves when the wasm instance is done running | ||
* @returns {Promise} | ||
*/ | ||
@@ -76,5 +81,44 @@ async onDone () { | ||
prevOps = this._opsQueue | ||
await this._opsQueue | ||
await prevOps | ||
} | ||
this.referanceMap.clear() | ||
} | ||
/** | ||
* Pushed an async operation to the a promise queue that | ||
* @returns {Promise} the returned promise resolves in the order the intail | ||
* operation was pushed to the queue | ||
*/ | ||
pushOpsQueue (promise) { | ||
this._opsQueue = Promise.all([this._opsQueue, promise]) | ||
return this._opsQueue | ||
} | ||
/** | ||
* executes a callback given an index in the exported callback container | ||
* @param {integer} cb | ||
* @param {*} val - a value to return to the callback function | ||
*/ | ||
execute (cb, val) { | ||
this.instance.exports.callbacks.get(cb)(val) | ||
} | ||
/** | ||
* returns a section of memory from the wasm instance | ||
* @param {integer} offset | ||
* @param {integer} length | ||
* @returns {Uint8Array} | ||
*/ | ||
getMemory (offset, length) { | ||
return new Uint8Array(this.instance.exports.memory.buffer, offset, length) | ||
} | ||
setMemory (offset, val) { | ||
const mem = this.getMemory(offset, val.length) | ||
mem.set(val) | ||
} | ||
static get typeId () { | ||
return ContainerTable.WebAssembly | ||
} | ||
} |
{ | ||
"name": "primea-wasm-container", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "", | ||
@@ -25,6 +25,15 @@ "main": "index.js", | ||
"devDependencies": { | ||
"coveralls": "^2.13.1", | ||
"ipfs": "^0.25.0", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"standard": "^9.0.1", | ||
"tape": "^4.6.3" | ||
"primea-hypervisor": "0.1.0", | ||
"standard": "^10.0.0", | ||
"tape": "^4.6.3", | ||
"wast2wasm": "0.0.1" | ||
}, | ||
"dependencies": { | ||
"primea-abstract-container": "0.0.4", | ||
"primea-container-table": "0.0.0", | ||
"reference-map": "0.1.0" | ||
} | ||
} |
[![NPM Package](https://img.shields.io/npm/v/primea-wasm-container.svg?style=flat-square)](https://www.npmjs.org/package/primea-wasm-container) | ||
[![Build Status](https://img.shields.io/travis/ewasm/primea-wasm-container.svg?branch=master&style=flat-square)](https://travrs-ci.org/ewasm/primea-wasm-container) | ||
[![Coverage Status](https://img.shields.io/coveralls/ewasm/primea-wasm-container.svg?style=flat-square)](https://coveralls.io/r/ewasm/primea-wasm-container) | ||
[![Build Status](https://img.shields.io/travis/primea/js-primea-wasm-container.svg?branch=master&style=flat-square)](https://travis-ci.org/primea/js-primea-wasm-container) | ||
[![Coverage Status](https://img.shields.io/coveralls/primea/js-primea-wasm-container.svg?style=flat-square)](https://coveralls.io/primea/js-primea-wasm-container) | ||
@@ -5,0 +5,0 @@ [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) |
const tape = require('tape') | ||
const fs = require('fs') | ||
const Hypervisor = require('primea-hypervisor') | ||
const Message = require('primea-message') | ||
const WasmContainer = require('../index.js') | ||
const testInterface = require('./testInterface.js') | ||
const IPFS = require('ipfs') | ||
const dir = `${__dirname}/wasm/` | ||
const node = new IPFS({ | ||
start: false | ||
}) | ||
tape('interface tests', async t => { | ||
const code = fs.readFileSync(`${dir}testCode.wasm`) | ||
class TestInterface { | ||
constructor (opts) { | ||
opts.response.test = 'response!' | ||
} | ||
class ContainerTestInterface { | ||
constructor (wasmContainer) { | ||
this.wasmContainer = wasmContainer | ||
} | ||
static get name () { | ||
return 'test' | ||
} | ||
readMem (offset) { | ||
return this.wasmContainer.getMemory(offset, 1) | ||
} | ||
t.equals(WasmContainer.name, 'wasm', 'container should have a name') | ||
writeMem (offset, val) { | ||
return this.wasmContainer.setMemory(offset, [val]) | ||
} | ||
const container = new WasmContainer(code) | ||
t.ok(container instanceof WasmContainer, 'should be instance of a container class') | ||
numOfReferances () { | ||
return this.wasmContainer.referanceMap.size | ||
} | ||
const promise = container.run() | ||
await promise | ||
t.ok(promise instanceof Promise, 'run should return instance of promise') | ||
async callback (cb) { | ||
const promise = new Promise((resolve, reject) => { | ||
resolve() | ||
}) | ||
await this.wasmContainer.pushOpsQueue(promise) | ||
this.wasmContainer.execute(cb) | ||
} | ||
} | ||
const results = await container.run(null, null, [TestInterface]) | ||
t.deepEquals(results, { | ||
'test': { | ||
'test': 'response!' | ||
} | ||
node.on('ready', () => { | ||
tape('wasm container - main', async t => { | ||
t.plan(1) | ||
const hypervisor = new Hypervisor(node.dag) | ||
const main = fs.readFileSync(`${__dirname}/wasm/run.wasm`) | ||
hypervisor.registerContainer(WasmContainer, { | ||
test: testInterface(t) | ||
}) | ||
const instance = await hypervisor.createInstance(WasmContainer.typeId, new Message({ | ||
data: main | ||
})) | ||
instance.message(instance.createMessage()) | ||
}) | ||
t.end() | ||
}) | ||
tape('referances', async t => { | ||
t.plan(1) | ||
const hypervisor = new Hypervisor(node.dag) | ||
const main = fs.readFileSync(`${__dirname}/wasm/referances.wasm`) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
const ports = hypervisor.createChannel() | ||
await hypervisor.createInstance(WasmContainer.typeId, new Message({ | ||
data: main, | ||
ports: ports | ||
})) | ||
}) | ||
tape('wasm interface test', async t => { | ||
t.plan(1) | ||
const code = fs.readFileSync(`${dir}testMem.wasm`) | ||
class TestInterfaceMem { | ||
constructor (opts) { | ||
this.opts = opts | ||
tape('wasm container - mem', async t => { | ||
t.plan(1) | ||
try { | ||
const hypervisor = new Hypervisor(node.dag) | ||
const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
await hypervisor.createInstance(WasmContainer.typeId, new Message({ | ||
data: readMem | ||
})) | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
}) | ||
static get name () { | ||
return 'test' | ||
tape('write mem', async t => { | ||
try { | ||
const hypervisor = new Hypervisor(node.dag) | ||
const readMem = fs.readFileSync(`${__dirname}/wasm/writeMem.wasm`) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
const root = await hypervisor.createInstance(WasmContainer.typeId, new Message({ | ||
data: readMem | ||
})) | ||
const mem = root.container.getMemory(0, 1) | ||
t.equals(mem[0], 9) | ||
t.end() | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
}) | ||
memory () { | ||
const memory = this.opts.vm.memory() | ||
t.ok(memory instanceof ArrayBuffer, 'should have access to memory') | ||
} | ||
} | ||
const container = new WasmContainer(code) | ||
await container.run(null, null, [TestInterfaceMem]) | ||
}) | ||
tape('wasm container - callbacks', async t => { | ||
t.plan(1) | ||
const hypervisor = new Hypervisor(node.dag) | ||
const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
hypervisor.createInstance(WasmContainer.typeId, new Message({ | ||
data: callBackWasm | ||
})) | ||
}) | ||
tape('async wasm interface test', async t => { | ||
t.plan(1) | ||
const code = fs.readFileSync(`${dir}asyncTest.wasm`) | ||
class TestInterfaceMem { | ||
constructor (opts) { | ||
this.opts = opts | ||
} | ||
tape('wasm container - invalid', async t => { | ||
t.plan(1) | ||
const hypervisor = new Hypervisor(node.dag) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
static get name () { | ||
return 'test' | ||
} | ||
const message = new Message({ | ||
data: Buffer.from([0x00]) | ||
}) | ||
async (cbOffset) { | ||
const opPromise = new Promise((resolve, reject) => { | ||
resolve() | ||
}) | ||
this.opts.vm.pushOpsQueue(opPromise, cbOffset, () => {}) | ||
const rp = message.responsePort = {destPort: {messages: []}} | ||
await hypervisor.createInstance(WasmContainer.typeId, message) | ||
t.equals(rp.destPort.messages[0].data.exception, true) | ||
}) | ||
tape('initailize', async t => { | ||
t.plan(2) | ||
const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) | ||
class ContainerTestInterface { | ||
constructor (wasmContainer) { | ||
this.wasmContainer = wasmContainer | ||
} | ||
readMem (offset) { | ||
return this.wasmContainer.getMemory(offset, 1) | ||
} | ||
async callback (cb) { | ||
const promise = new Promise((resolve, reject) => { | ||
resolve() | ||
}) | ||
await this.wasmContainer.pushOpsQueue(promise) | ||
this.wasmContainer.execute(cb) | ||
} | ||
static initialize (code) { | ||
t.equals(code, callBackWasm) | ||
return code | ||
} | ||
} | ||
done () { | ||
t.ok(true) | ||
} | ||
} | ||
const container = new WasmContainer(code) | ||
await container.run(null, null, [TestInterfaceMem]) | ||
const hypervisor = new Hypervisor(node.dag) | ||
hypervisor.registerContainer(WasmContainer, { | ||
env: ContainerTestInterface, | ||
test: testInterface(t) | ||
}) | ||
const message = new Message({ | ||
data: callBackWasm | ||
}) | ||
hypervisor.createInstance(WasmContainer.typeId, message) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 3 instances in 1 package
88646
34
694
3
7
3
4
+ Addedprimea-container-table@0.0.0
+ Addedreference-map@0.1.0
+ Addedprimea-abstract-container@0.0.4(transitive)
+ Addedprimea-container-table@0.0.0(transitive)
+ Addedreference-map@0.1.0(transitive)