primea-wasm-container
Advanced tools
Comparing version 0.5.0 to 0.6.0
80
index.js
const {Message, FunctionRef, ModuleRef, getType} = require('primea-objects') | ||
const {wasm2json, json2wasm} = require('wasm-json-toolkit') | ||
const annotations = require('primea-annotations') | ||
const persist = require('wasm-persist') | ||
const wasmMetering = require('wasm-metering') | ||
@@ -25,2 +26,5 @@ const ReferenceMap = require('reference-map') | ||
} | ||
if (funcRef.params == null) { | ||
throw new Error('invalid funcRef') | ||
} | ||
let wrapper = typeCheckWrapper(funcRef.params) | ||
@@ -39,3 +43,3 @@ const wasm = json2wasm(wrapper) | ||
if (!nativeTypes.has(type)) { | ||
arg = container.refs.get(arg, type) | ||
arg = container.refs.get(arg) | ||
checkedArgs.push(arg) | ||
@@ -72,2 +76,6 @@ } else if (type === 'i64') { | ||
externalize: index => { | ||
if (!self.instance.exports.table) { | ||
throw new Error('no table exported') | ||
} | ||
const func = self.instance.exports.table.get(index) | ||
@@ -173,11 +181,2 @@ const object = func.object | ||
}, | ||
storage: { | ||
get: () => { | ||
return this.refs.add(this.actor.storage, getType(this.actor.storage)) | ||
}, | ||
set: ref => { | ||
const object = this.refs.get(ref) | ||
this.actor.storage = object | ||
} | ||
}, | ||
metering: { | ||
@@ -204,2 +203,12 @@ usegas: amount => { | ||
wasm = json2wasm(moduleJSON) | ||
const globals = [] | ||
json.persist.map(global => global.index).forEach(index => { | ||
globals[index] = true | ||
}) | ||
wasm = persist.prepare(wasm, { | ||
memory: false, | ||
table: false, | ||
globals | ||
}) | ||
const modRef = fromMetaJSON(json, id) | ||
@@ -209,3 +218,8 @@ return { | ||
json, | ||
modRef | ||
modRef, | ||
state: json.persist.map(entry => { | ||
if (entry.type === 'anyref') { | ||
return [] | ||
} | ||
}) | ||
} | ||
@@ -215,4 +229,3 @@ } | ||
static onCreation (unverifiedWasm, id) { | ||
const {modRef} = this.createModule(unverifiedWasm, id) | ||
return modRef | ||
return this.createModule(unverifiedWasm, id) | ||
} | ||
@@ -223,2 +236,11 @@ | ||
this.instance = WebAssembly.Instance(this.mod, this.interface) | ||
const state = this.json.persist.map((entry, index) => { | ||
const obj = this.actor.storage[index] | ||
if (entry.type === 'anyref') { | ||
return this.refs.add(obj, getType(obj)) | ||
} else { | ||
return obj | ||
} | ||
}) | ||
persist.resume(this.instance, {globals: state, symbol: '_'}) | ||
// map table indexes | ||
@@ -238,2 +260,3 @@ const table = this.instance.exports.table | ||
const args = [] | ||
this.funcRef.params.forEach(type => { | ||
@@ -247,3 +270,3 @@ const arg = message.funcArguments[index] | ||
} else { | ||
args.push(this.refs.add(arg, type)) | ||
args.push(this.refs.add(arg, getType(arg))) | ||
} | ||
@@ -265,2 +288,13 @@ index++ | ||
await this.onDone() | ||
// hibernate the wasm instance | ||
let postState = persist.hibernate(this.instance) | ||
// map the ints to objects | ||
this.actor.storage = this.json.persist.map((entry, index) => { | ||
const i = postState.globals[index] | ||
if (entry.type === 'anyref') { | ||
return this.refs.get(i) | ||
} else { | ||
return i | ||
} | ||
}) | ||
this.refs.clear() | ||
@@ -300,7 +334,21 @@ } | ||
get8Memory (offset, length) { | ||
return new Uint8Array(this.instance.exports.memory.buffer, offset, length) | ||
if (!this.instance.exports.memory) { | ||
throw new Error('no memory exported') | ||
} | ||
try { | ||
return new Uint8Array(this.instance.exports.memory.buffer, offset, length) | ||
} catch (e) { | ||
throw new Error(`invalid memory range (offset=${offset}, length=${length})`) | ||
} | ||
} | ||
get32Memory (offset, length) { | ||
return new Uint32Array(this.instance.exports.memory.buffer, offset, length) | ||
if (!this.instance.exports.memory) { | ||
throw new Error('no memory exported') | ||
} | ||
try { | ||
return new Uint32Array(this.instance.exports.memory.buffer, offset, length) | ||
} catch (e) { | ||
throw new Error(`invalid memory range (offset=${offset}, length=${length})`) | ||
} | ||
} | ||
@@ -307,0 +355,0 @@ |
{ | ||
"name": "primea-wasm-container", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "", | ||
@@ -26,6 +26,6 @@ "main": "index.js", | ||
"coveralls": "^3.0.0", | ||
"dfinity-radix-tree": "0.2.1", | ||
"dfinity-radix-tree": "0.2.2", | ||
"level-browserify": "^1.1.2", | ||
"nyc": "^11.6.0", | ||
"primea-hypervisor": "^0.7.0", | ||
"nyc": "^11.7.1", | ||
"primea-hypervisor": "^0.7.1", | ||
"standard": "^11.0.1", | ||
@@ -36,8 +36,9 @@ "tape": "^4.9.0", | ||
"dependencies": { | ||
"primea-annotations": "^0.0.3", | ||
"primea-objects": "^0.0.3", | ||
"primea-annotations": "^0.0.5", | ||
"primea-objects": "^0.0.4", | ||
"reference-map": "1.2.6", | ||
"wasm-json-toolkit": "^0.2.3", | ||
"wasm-metering": "^0.2.1" | ||
"wasm-json-toolkit": "^0.3.0", | ||
"wasm-metering": "^0.2.1", | ||
"wasm-persist": "0.0.1" | ||
} | ||
} |
@@ -121,29 +121,2 @@ const tape = require('tape') | ||
tape('empty', async t => { | ||
t.plan(1) | ||
tester = t | ||
const expectedState = Buffer.from('bb15867b26293aa36c774b8c82541dd64212ba9a', 'hex') | ||
const tree = new RadixTree({ | ||
db | ||
}) | ||
const wasm = fs.readFileSync(WASM_PATH + '/empty.wasm') | ||
const hypervisor = new Hypervisor({tree}) | ||
hypervisor.registerContainer(TestWasmContainer) | ||
const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) | ||
const funcRef = module.getFuncRef('receive') | ||
funcRef.gas = 3000 | ||
const message = new Message({ | ||
funcRef, | ||
funcArguments: [5] | ||
}) | ||
hypervisor.send(message) | ||
const stateRoot = await hypervisor.createStateRoot() | ||
t.deepEquals(stateRoot, expectedState, 'expected root!') | ||
}) | ||
tape('two communicating actors', async t => { | ||
@@ -194,3 +167,3 @@ t.plan(1) | ||
const recvFuncRef = receiverMod.getFuncRef('receive') | ||
callFuncRef.gas = 100000 | ||
callFuncRef.gas = 1000000 | ||
recvFuncRef.gas = 100000 | ||
@@ -225,3 +198,3 @@ | ||
const recvFuncRef = receiverMod.getFuncRef('receive') | ||
callFuncRef.gas = 100000 | ||
callFuncRef.gas = 1000000 | ||
recvFuncRef.gas = 100000 | ||
@@ -306,43 +279,2 @@ | ||
tape('storage', async t => { | ||
tester = t | ||
const tree = new RadixTree({db}) | ||
let wasm = fs.readFileSync(WASM_PATH + '/storage.wasm') | ||
const egress = new EgressDriver() | ||
egress.on('message', msg => { | ||
t.equals(msg.funcArguments[0].toString(), 'hello world') | ||
t.end() | ||
}) | ||
const hypervisor = new Hypervisor({ | ||
tree, | ||
containers: [TestWasmContainer], | ||
drivers: [egress] | ||
}) | ||
const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) | ||
const funcRef = module.getFuncRef('main') | ||
funcRef.gas = 322000 | ||
const message = new Message({ | ||
funcRef | ||
}) | ||
hypervisor.send(message) | ||
const funcRef2 = module.getFuncRef('load') | ||
funcRef2.gas = 322000 | ||
await hypervisor.createStateRoot() | ||
const message2 = new Message({ | ||
funcRef: funcRef2, | ||
funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})] | ||
}) | ||
hypervisor.send(message2) | ||
}) | ||
tape('link', async t => { | ||
@@ -438,1 +370,35 @@ tester = t | ||
}) | ||
tape('persitant globals', async t => { | ||
tester = t | ||
const tree = new RadixTree({db}) | ||
let wasm = fs.readFileSync(WASM_PATH + '/persistantGlobals.wasm') | ||
const hypervisor = new Hypervisor({ | ||
tree, | ||
containers: [TestWasmContainer] | ||
}) | ||
const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) | ||
const funcRef = module.getFuncRef('e') | ||
funcRef.gas = 322000 | ||
const message = new Message({ | ||
funcRef | ||
}) | ||
hypervisor.send(message) | ||
hypervisor.scheduler.on('idle', async () => { | ||
const actor = await hypervisor.loadActor(module.id) | ||
t.deepEquals(actor.storage, [[], 5, [0, 3]]) | ||
}) | ||
await hypervisor.createStateRoot() | ||
hypervisor.send(message) | ||
hypervisor.scheduler.on('idle', async () => { | ||
const actor = await hypervisor.loadActor(module.id) | ||
t.deepEquals(actor.storage, [[], 5, [0, 3]]) | ||
}) | ||
t.end() | ||
}) |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"func" | ||
"anyref" | ||
] | ||
@@ -8,0 +8,0 @@ }], |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"data" | ||
"anyref" | ||
] | ||
@@ -8,0 +8,0 @@ }], |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"func" | ||
"anyref" | ||
] | ||
@@ -8,0 +8,0 @@ }], |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"func" | ||
"anyref" | ||
] | ||
@@ -8,0 +8,0 @@ }], |
@@ -6,3 +6,3 @@ { | ||
"i64", | ||
"data" | ||
"anyref" | ||
] | ||
@@ -9,0 +9,0 @@ }], |
@@ -6,3 +6,3 @@ { | ||
"i64", | ||
"data" | ||
"anyref" | ||
] | ||
@@ -9,0 +9,0 @@ }], |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"func" | ||
"anyref" | ||
] | ||
@@ -12,3 +12,8 @@ }], | ||
"type": 0 | ||
}], | ||
"persist": [{ | ||
"form": "global", | ||
"index": 0, | ||
"type": "anyref" | ||
}] | ||
} |
@@ -5,3 +5,3 @@ { | ||
"params": [ | ||
"func" | ||
"anyref" | ||
] | ||
@@ -8,0 +8,0 @@ }], |
@@ -6,3 +6,3 @@ { | ||
"i64", | ||
"data" | ||
"anyref" | ||
] | ||
@@ -9,0 +9,0 @@ }], |
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
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
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
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
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 7 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 5 instances in 1 package
999177
108
1904
6
29
+ Addedwasm-persist@0.0.1
+ Addedprimea-annotations@0.0.5(transitive)
+ Addedprimea-objects@0.0.4(transitive)
+ Addedwasm-json-toolkit@0.3.0(transitive)
+ Addedwasm-persist@0.0.1(transitive)
- Removedbuffer-pipe@0.0.3(transitive)
- Removedprimea-annotations@0.0.3(transitive)
- Removedprimea-objects@0.0.3(transitive)
- Removedwasm-json-toolkit@0.2.6(transitive)
Updatedprimea-annotations@^0.0.5
Updatedprimea-objects@^0.0.4
Updatedwasm-json-toolkit@^0.3.0