git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 225da49428f17d7ef0fc980e3dbd3cda8967ae99

added table ops

wanderer committed on 2/22/2018, 2:55:05 AM
Parent: 124610c716c1ba494bca1d28f39840fff9c2fa6b

Files changed

tests/wasm/memory.wasmchanged
tests/wasm/table.wasmadded
tests/wasmContainer.jschanged
tests/wast/memory.wastchanged
tests/wast/table.wastadded
wasmContainer.jschanged
tests/wasm/memory.wasmView
@@ -1,3 +1,3 @@
1-asm````8testcheckmemory externalizememory internalizememorytest
2-AAAAA
1+asm```+memory externalizememory internalizememorytest
2+AAAAA
33 A test
tests/wasm/table.wasmView
@@ -1,0 +1,3 @@
1+asm```>table externalizememory externalizetable internalizememorytest
2+(&AAAAAA66AAAAA
3+A test
tests/wasmContainer.jsView
@@ -113,12 +113,8 @@
113113
114114 tape('externalize/internalize memory', async t => {
115115 t.plan(1)
116116 tester = t
117- const expectedState = {
118- '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
119- }
120-
121117 const tree = new RadixTree({
122118 db
123119 })
124120
@@ -137,4 +133,28 @@
137133 t.deepEquals(a, b, 'should copy memory correctly')
138134 })
139135 hypervisor.send(message)
140136 })
137+
138+tape('externalize/internalize table', async t => {
139+ t.plan(1)
140+ tester = t
141+ const tree = new RadixTree({
142+ db
143+ })
144+
145+ const wasm = fs.readFileSync('./wasm/table.wasm')
146+
147+ const hypervisor = new Hypervisor(tree)
148+ hypervisor.registerContainer(TestWasmContainer)
149+
150+ const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
151+
152+ const message = new Message({
153+ funcRef: module.getFuncRef('test')
154+ }).on('done', actor => {
155+ const a = actor.container.getMemory(0, 8)
156+ const b = actor.container.getMemory(8, 8)
157+ t.deepEquals(a, b, 'should copy memory correctly')
158+ })
159+ hypervisor.send(message)
160+})
tests/wast/memory.wastView
@@ -1,6 +1,5 @@
11 (module
2- (import "test" "check" (func $check (param i32 i32)))
32 (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
43 (import "memory" "internalize" (func $internalize (param i32 i32 i32 i32)))
54 (memory (export "memory") 1)
65 (data (i32.const 0) "test")
tests/wast/table.wastView
@@ -1,0 +1,25 @@
1+(module
2+ (import "table" "externalize" (func $externalize (param i32 i32) (result i32)))
3+ (import "memory" "externalize" (func $mem_externalize (param i32 i32) (result i32)))
4+ (import "table" "internalize" (func $internalize (param i32 i32 i32 i32)))
5+ (memory (export "memory") 1)
6+ (data (i32.const 0) "test")
7+ (func $test
8+ (i32.const 0)
9+ (call $mem_externalize (i32.const 0) (i32.const 4))
10+ (i32.const 4)
11+ (call $mem_externalize (i32.const 0) (i32.const 4))
12+ (i32.store)
13+ (i32.store)
14+
15+
16+ i32.const 0
17+ i32.const 2
18+ call $externalize
19+
20+ i32.const 0
21+ i32.const 8
22+ i32.const 2
23+ call $internalize
24+ )
25+ (export "test" (func $test)))
wasmContainer.jsView
@@ -34,10 +34,10 @@
3434 class ElementBuffer {
3535 static get type () {
3636 return 'elem'
3737 }
38- constructor (size) {
39- this._array = new Array(size)
38+ constructor (array) {
39+ this._array = array
4040 }
4141
4242 serialize () {
4343 const serialized = this._array.map(ref => ref.serailize())
@@ -238,25 +238,22 @@
238238 }
239239 },
240240 table: {
241241 externalize: (index, length) => {
242- const mem = this.getMemory(index, length * 4)
242+ const mem = Buffer.from(this.getMemory(index, length * 4))
243243 const objects = []
244244 while (length--) {
245- const ref = mem[index + length]
246- if (this.refs.has(ref)) {
247- objects.push(ref)
248- } else {
249- throw new Error('invalid ref')
250- }
245+ const ref = mem.readUInt32LE(length * 4)
246+ const obj = this.refs.get(ref)
247+ objects.unshift(obj)
251248 }
252249 const eleBuf = new ElementBuffer(objects)
253250 return this.refs.add(eleBuf, 'elem')
254251 },
255- internalize: (dataRef, writeOffset, readOffset, length) => {
256- let buf = this.refs.get(dataRef, 'elem')
257- buf = buf.subarray(readOffset, length)
258- const mem = this.getMemory(writeOffset, buf.length)
252+ internalize: (elemRef, srcOffset, sinkOffset, length) => {
253+ let table = this.refs.get(elemRef, 'elem')
254+ const buf = table._array.slice(srcOffset, srcOffset + length).map(obj => this.refs.add(obj))
255+ const mem = new Uint32Array(this.instance.exports.memory.buffer, sinkOffset, length)
259256 mem.set(buf)
260257 }
261258 },
262259 metering: {

Built with git-ssb-web