git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit 172b9d65cd4d2fbc210b0cee09b451dfdccabf34

Merge branch 'master' into greenkeeper/reference-map-1.2.4

wanderer authored on 4/3/2018, 7:33:44 PM
GitHub committed on 4/3/2018, 7:33:44 PM
Parent: 92fb09c87c224982aa17e3fa025810620e571b26
Parent: d29552f49ecfa1893262e16349cb6b9878377cef

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
tests/index.jschanged
tests/wasm/memory.wasmchanged
tests/wasm/link.wasmadded
tests/wasm/storage.wasmadded
tests/wast/funcRef_reciever.wastchanged
tests/wast/memory.wastchanged
tests/wast/link.jsonadded
tests/wast/link.wastadded
tests/wast/storage.jsonadded
tests/wast/storage.wastadded
typeCheckWrapper.jschanged
injectGlobals.jsdeleted
index.jsView
@@ -1,10 +1,9 @@
1-const {Message, FunctionRef, ModuleRef, DEFAULTS} = require('primea-objects')
1+const {Message, FunctionRef, ModuleRef, getType} = require('primea-objects')
22 const {wasm2json, json2wasm} = require('wasm-json-toolkit')
33 const annotations = require('primea-annotations')
44 const wasmMetering = require('wasm-metering')
55 const ReferanceMap = require('reference-map')
6-const injectGlobals = require('./injectGlobals.js')
76 const typeCheckWrapper = require('./typeCheckWrapper.js')
87
98 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
109 const FUNC_INDEX_OFFSET = 1
@@ -62,8 +61,9 @@
6261 module.exports = class WasmContainer {
6362 constructor (actor) {
6463 this.actor = actor
6564 this.refs = new ReferanceMap()
65+ this._opsQueue = Promise.resolve()
6666 }
6767
6868 static createModule (wasm, id) {
6969 if (!WebAssembly.validate(wasm)) {
@@ -75,12 +75,8 @@
7575 moduleJSON = wasmMetering.meterJSON(moduleJSON, {
7676 meterType: 'i32'
7777 })
7878
79- // initialize the globals
80- if (json.persist.length) {
81- moduleJSON = injectGlobals(moduleJSON, json.persist)
82- }
8379 // recompile the wasm
8480 wasm = json2wasm(moduleJSON)
8581 const modRef = fromMetaJSON(json, id)
8682 return {
@@ -119,9 +115,9 @@
119115 const funcRef = self.refs.get(ref, 'func')
120116 const wrapper = generateWrapper(funcRef, self)
121117 self.instance.exports.table.set(index, wrapper.exports.check)
122118 },
123- get_gas_budget: (funcRef) => {
119+ get_gas_budget: funcRef => {
124120 const func = self.refs.get(funcRef, 'func')
125121 return func.gas
126122 },
127123 set_gas_budget: (funcRef, amount) => {
@@ -136,11 +132,14 @@
136132 const link = {'/': obj}
137133 return self.refs.add(link, 'link')
138134 },
139135 unwrap: async (ref, cb) => {
140- const obj = self.refs.get(ref, 'link')
141- const promise = self.actor.tree.dataStore.get(obj)
142- await self._opsQueue.push(promise)
136+ const link = self.refs.get(ref, 'link')
137+ const promise = self.actor.tree.graph.tree(link)
138+ await self.pushOpsQueue(promise)
139+ const obj = link['/']
140+ const linkRef = self.refs.add(obj, getType(obj))
141+ self.instance.exports.table.get(cb)(linkRef)
143142 }
144143 },
145144 module: {
146145 new: dataRef => {
@@ -187,17 +186,26 @@
187186 return this.refs.add(objects, 'elem')
188187 },
189188 internalize: (elemRef, srcOffset, sinkOffset, length) => {
190189 let table = self.refs.get(elemRef, 'elem')
191- const buf = table.slice(srcOffset, srcOffset + length).map(obj => self.refs.add(obj))
190+ const buf = table.slice(srcOffset, srcOffset + length).map(obj => self.refs.add(obj, getType(obj)))
192191 const mem = self.get32Memory(sinkOffset, length)
193192 mem.set(buf)
194193 },
195194 length (elemRef) {
196195 let elem = self.refs.get(elemRef, 'elem')
197196 return elem.length
198197 }
199198 },
199+ storage: {
200+ get: () => {
201+ return this.refs.add(this.actor.storage, getType(this.actor.storage))
202+ },
203+ set: ref => {
204+ const object = this.refs.get(ref)
205+ this.actor.storage = object
206+ }
207+ },
200208 metering: {
201209 usegas: amount => {
202210 self.actor.incrementTicks(amount)
203211 funcRef.gas -= amount
@@ -239,49 +247,20 @@
239247 }
240248 index++
241249 })
242250
243- // setup globals
244- let numOfGlobals = this.json.persist.length
245- if (numOfGlobals) {
246- const refs = []
247- while (numOfGlobals--) {
248- const obj = this.actor.storage[numOfGlobals] || DEFAULTS[this.json.persist[numOfGlobals].type]
249- refs.push(this.refs.add(obj, this.json.persist[numOfGlobals].type))
250- }
251- this.instance.exports.setter_globals(...refs)
251+ // call entrypoint function
252+ let wasmFunc
253+ if (funcRef.identifier[0]) {
254+ wasmFunc = this.instance.exports.table.get(funcRef.identifier[1])
255+ } else {
256+ wasmFunc = this.instance.exports[funcRef.identifier[1]]
252257 }
253258
254- try {
255- // call entrypoint function
256- let wasmFunc
257- if (funcRef.identifier[0]) {
258- wasmFunc = this.instance.exports.table.get(funcRef.identifier[1])
259- } else {
260- wasmFunc = this.instance.exports[funcRef.identifier[1]]
261- }
262-
263- const wrapper = generateWrapper(funcRef)
264- wrapper.exports.table.set(0, wasmFunc)
265- wrapper.exports.invoke(...args)
266- await this.onDone()
267- } catch (e) {
268- console.log(e)
269- }
270-
271- // store globals
272- numOfGlobals = this.json.persist.length
273- if (numOfGlobals) {
274- const storage = []
275- this.instance.exports.getter_globals()
276- const mem = this.get32Memory(0, numOfGlobals)
277- while (numOfGlobals--) {
278- const ref = mem[numOfGlobals]
279- storage.push(this.refs.get(ref, this.json.persist[numOfGlobals].type))
280- }
281- this.actor.storage = storage
282- }
283-
259+ const wrapper = generateWrapper(funcRef)
260+ wrapper.exports.table.set(0, wasmFunc)
261+ wrapper.exports.invoke(...args)
262+ await this.onDone()
284263 this.refs.clear()
285264 }
286265
287266 /**
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 198276 bytes
New file size: 198276 bytes
package.jsonView
@@ -25,17 +25,17 @@
2525 "coveralls": "^3.0.0",
2626 "dfinity-radix-tree": "0.2.1",
2727 "level-browserify": "^1.1.2",
2828 "nyc": "^11.6.0",
29- "primea-hypervisor": "^0.4.5",
29+ "primea-hypervisor": "^0.5.0",
3030 "standard": "^11.0.1",
3131 "tape": "^4.9.0",
3232 "wabt": "^1.0.0"
3333 },
3434 "dependencies": {
3535 "borc": "^2.0.2",
3636 "primea-annotations": "^0.0.3",
37- "primea-objects": "^0.0.2",
37+ "primea-objects": "^0.0.3",
3838 "reference-map": "1.2.4",
3939 "wasm-json-toolkit": "^0.2.3",
4040 "wasm-metering": "^0.1.1"
4141 }
tests/index.jsView
@@ -1,15 +1,17 @@
11 const tape = require('tape')
22 const fs = require('fs')
33 const path = require('path')
4-const {Message} = require('primea-objects')
4+const Buffer = require('safe-buffer').Buffer
5+const {Message, FunctionRef} = require('primea-objects')
56 const Hypervisor = require('primea-hypervisor')
7+const EgressDriver = require('primea-hypervisor/egressDriver')
68 const WasmContainer = require('../')
79
810 const level = require('level-browserify')
911 const RadixTree = require('dfinity-radix-tree')
1012
11-const db = level(__dirname + '/testdb')
13+const db = level(`${__dirname}/testdb`)
1214 const WASM_PATH = path.join(__dirname, 'wasm')
1315
1416 let tester
1517
@@ -49,10 +51,8 @@
4951 funcRef.gas = 322000
5052
5153 const message = new Message({
5254 funcRef
53- }).on('execution:error', e => {
54- console.log(e)
5555 })
5656 hypervisor.send(message)
5757 })
5858
@@ -70,10 +70,8 @@
7070 funcRef.gas = 322000
7171
7272 const message = new Message({
7373 funcRef
74- }).on('execution:error', e => {
75- console.log(e)
7674 })
7775 hypervisor.send(message)
7876 })
7977
@@ -91,10 +89,8 @@
9189 funcRef.gas = 322000
9290
9391 const message = new Message({
9492 funcRef
95- }).on('execution:error', e => {
96- console.log(e)
9793 })
9894 hypervisor.send(message)
9995 })
10096
@@ -117,10 +113,8 @@
117113
118114 const message = new Message({
119115 funcRef,
120116 funcArguments: [5]
121- }).on('execution:error', e => {
122- console.log(e)
123117 })
124118 hypervisor.send(message)
125119 // const stateRoot = await hypervisor.createStateRoot()
126120 // t.deepEquals(stateRoot, expectedState, 'expected root!')
@@ -155,11 +149,8 @@
155149
156150 tape('two communicating actors', async t => {
157151 t.plan(1)
158152 tester = t
159- const expectedState = {
160- '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
161- }
162153
163154 const tree = new RadixTree({db})
164155
165156 const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
@@ -179,18 +170,13 @@
179170 funcArguments: [recvFuncRef]
180171 })
181172
182173 hypervisor.send(message)
183- const stateRoot = await hypervisor.createStateRoot()
184- // t.deepEquals(stateRoot, expectedState, 'expected root!')
185174 })
186175
187176 tape('two communicating actors with callback', async t => {
188177 t.plan(1)
189178 tester = t
190- const expectedState = {
191- '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
192- }
193179
194180 const tree = new RadixTree({
195181 db
196182 })
@@ -211,21 +197,16 @@
211197
212198 const message = new Message({
213199 funcRef: callFuncRef,
214200 funcArguments: [recvFuncRef]
215- }).on('execution:error', e => console.log(e))
201+ })
216202
217203 hypervisor.send(message)
218- const stateRoot = await hypervisor.createStateRoot()
219- // t.deepEquals(stateRoot, expectedState, 'expected root!')
220204 })
221205
222206 tape('two communicating actors with private callback', async t => {
223207 t.plan(1)
224208 tester = t
225- const expectedState = {
226- '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
227- }
228209
229210 const tree = new RadixTree({
230211 db
231212 })
@@ -249,10 +230,8 @@
249230 funcArguments: [recvFuncRef]
250231 })
251232
252233 hypervisor.send(message)
253- const stateRoot = await hypervisor.createStateRoot()
254- // t.deepEquals(stateRoot, expectedState, 'expected root!')
255234 })
256235
257236 tape('externalize/internalize memory', async t => {
258237 t.plan(1)
@@ -301,66 +280,134 @@
301280 })
302281 hypervisor.send(message)
303282 })
304283
305-tape('load / store globals', async t => {
284+tape('creation', async t => {
306285 t.plan(1)
307286 tester = t
308- const tree = new RadixTree({
309- db
310- })
287+ const tree = new RadixTree({db})
288+ let wasm = fs.readFileSync(WASM_PATH + '/creation.wasm')
289+ let receiver = fs.readFileSync(WASM_PATH + '/reciever.wasm')
311290
312- const wasm = fs.readFileSync(WASM_PATH + '/globals.wasm')
313-
314291 const hypervisor = new Hypervisor(tree)
315292 hypervisor.registerContainer(TestWasmContainer)
316293
317294 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
295+ const funcRef = module.getFuncRef('main')
296+ funcRef.gas = 322000
318297
319- await new Promise((resolve, reject) => {
320- const funcRef = module.getFuncRef('store')
321- funcRef.gas = 400
322- const message = new Message({
323- funcRef
324- }).on('done', actor => {
325- resolve()
326- })
327- hypervisor.send(message)
298+ const message = new Message({
299+ funcRef,
300+ funcArguments: [receiver]
328301 })
302+ hypervisor.send(message)
303+})
329304
330- await new Promise((resolve, reject) => {
331- const funcRef = module.getFuncRef('load')
332- funcRef.gas = 400
333- const message = new Message({
334- funcRef
335- }).on('done', actor => {
336- const b = actor.container.get8Memory(5, 4)
337- const result = Buffer.from(b).toString()
338- t.deepEquals(result, 'test', 'should copy memory correctly')
339- resolve()
340- })
341- hypervisor.send(message)
305+tape('storage', async t => {
306+ tester = t
307+ const tree = new RadixTree({db})
308+ let wasm = fs.readFileSync(WASM_PATH + '/storage.wasm')
309+
310+ const egress = new EgressDriver()
311+
312+ egress.on('message', msg => {
313+ t.equals(msg.funcArguments[0].toString(), 'hello world')
314+ t.end()
342315 })
316+
317+ const hypervisor = new Hypervisor(tree, [TestWasmContainer], [egress])
318+
319+ const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
320+ const funcRef = module.getFuncRef('main')
321+ funcRef.gas = 322000
322+
323+ const message = new Message({
324+ funcRef
325+ })
326+
327+ hypervisor.send(message)
328+
329+ const funcRef2 = module.getFuncRef('load')
330+ funcRef2.gas = 322000
331+
332+ await hypervisor.createStateRoot()
333+
334+ const message2 = new Message({
335+ funcRef: funcRef2,
336+ funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})]
337+ })
338+
339+ hypervisor.send(message2)
343340 })
344341
345-tape('creation', async t => {
342+tape('link', async t => {
343+ tester = t
344+ const tree = new RadixTree({db})
345+ let wasm = fs.readFileSync(WASM_PATH + '/link.wasm')
346+
347+ const egress = new EgressDriver()
348+
349+ egress.on('message', msg => {
350+ t.equals(msg.funcArguments[0].toString(), 'hello world')
351+ t.end()
352+ })
353+
354+ const hypervisor = new Hypervisor(tree, [TestWasmContainer], [egress])
355+
356+ const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
357+ const funcRef = module.getFuncRef('main')
358+ funcRef.gas = 322000
359+
360+ const message = new Message({
361+ funcRef
362+ })
363+
364+ hypervisor.send(message)
365+
366+ const funcRef2 = module.getFuncRef('load')
367+ funcRef2.gas = 322000
368+
369+ await hypervisor.createStateRoot()
370+
371+ const message2 = new Message({
372+ funcRef: funcRef2,
373+ funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})]
374+ })
375+
376+ hypervisor.send(message2)
377+})
378+
379+tape('invalid binary', async t => {
346380 t.plan(1)
347381 tester = t
348382 const tree = new RadixTree({db})
349- let wasm = fs.readFileSync(WASM_PATH + '/creation.wasm')
350- let receiver = fs.readFileSync(WASM_PATH + '/reciever.wasm')
383+ const wasm = Buffer.from([0])
351384
352385 const hypervisor = new Hypervisor(tree)
353386 hypervisor.registerContainer(TestWasmContainer)
354387
388+ try {
389+ await hypervisor.createActor(TestWasmContainer.typeId, wasm)
390+ } catch (e) {
391+ t.pass()
392+ }
393+})
394+
395+tape('out of gas', async t => {
396+ t.plan(1)
397+ tester = t
398+ const tree = new RadixTree({db})
399+ let wasm = fs.readFileSync(WASM_PATH + '/i64.wasm')
400+
401+ const hypervisor = new Hypervisor(tree)
402+ hypervisor.registerContainer(TestWasmContainer)
403+
355404 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
356405 const funcRef = module.getFuncRef('main')
357- funcRef.gas = 322000
358406
359407 const message = new Message({
360- funcRef,
361- funcArguments: [receiver]
408+ funcRef
362409 }).on('execution:error', e => {
363- console.log(e)
410+ t.pass()
364411 })
365412 hypervisor.send(message)
366413 })
tests/wasm/memory.wasmView
@@ -1,3 +1,3 @@
1-asm```+memory externalizememory internalizememorytest
2-AAAAA
1+asm````;memory externalizememory internalizememorylengthmemorytest
2+AA"AA 
33 A test
tests/wasm/link.wasmView
@@ -1,0 +1,3 @@
1+asm types`n typeMap#```````� testchecktestprintmemory externalizestoragegetstoragesetelem externalizeelem internalizeelemlengthfunc internalizelinkwraplinkunwrappA  memorytablemain load A 
2+EAAA  6AA  $AAA(A
3+ A# A A hello world
tests/wasm/storage.wasmView
@@ -1,0 +1,2 @@
1+asm types`n typeMap``````�testchecktestprintmemory externalizestoragegetstoragesetelem externalizeelem internalizefunc internalizep memorytablemainload
2+4AAA 6AA AAAA A(A A hello world
tests/wast/funcRef_reciever.wastView
@@ -1,14 +1,16 @@
11 (module
22 (import "func" "internalize" (func $internalize (param i32 i32)))
33 (import "func" "set_gas_budget" (func $set_gas_budget (param i32 i32) (result i32)))
44 (table (export "table") 1 1 anyfunc)
5- (func $receive (param i32)
5+ (func $receive (param $funcRef i32)
66 (call $internalize
77 (i32.const 0)
8- (call $set_gas_budget (get_local 0) (i32.const 10500)))
8+ (call $set_gas_budget
9+ (get_local $funcRef)
10+ (i32.const 10500)))
911
10- i32.const 5
11- i32.const 0
12- call_indirect (param i32)
12+ (call_indirect (param i32)
13+ (i32.const 5)
14+ (i32.const 0))
1315 )
1416 (export "receive" (func $receive)))
tests/wast/memory.wastView
@@ -1,15 +1,18 @@
11 (module
22 (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
33 (import "memory" "internalize" (func $internalize (param i32 i32 i32 i32)))
4+ (import "memory" "length" (func $length (param i32) (result i32)))
45 (memory (export "memory") 1)
56 (data (i32.const 0) "test")
67 (func $test
7- i32.const 0
8- i32.const 4
9- call $externalize
10- i32.const 0
11- i32.const 5
12- i32.const 4
13- call $internalize
8+ (local $dataRef i32)
9+ (call $internalize
10+ (tee_local $dataRef
11+ (call $externalize
12+ (i32.const 0)
13+ (i32.const 4)))
14+ (i32.const 0)
15+ (i32.const 5)
16+ (call $length (get_local $dataRef)))
1417 )
1518 (export "test" (func $test)))
tests/wast/link.jsonView
@@ -1,0 +1,12 @@
1+{
2+ "types": [{
3+ "form": "func",
4+ "params": [
5+ "func"
6+ ]
7+ }],
8+ "typeMap": [{
9+ "func": 1,
10+ "type": 0
11+ }]
12+}
tests/wast/link.wastView
@@ -1,0 +1,53 @@
1+(module
2+ (import "test" "check" (func $check (param i32 i32)))
3+ (import "test" "print" (func $print (param i32)))
4+ (import "memory" "externalize" (func $mem.externalize (param i32 i32) (result i32)))
5+ (import "storage" "get" (func $storage.get (result i32)))
6+ (import "storage" "set" (func $storage.set (param i32)))
7+ (import "elem" "externalize" (func $elem.externalize (param i32 i32) (result i32)))
8+ (import "elem" "internalize" (func $elem.internalize (param i32 i32 i32 i32)))
9+ (import "elem" "length" (func $elem.length (param i32) (result i32)))
10+ (import "func" "internalize" (func $func.internalize (param i32 i32)))
11+ (import "link" "wrap" (func $link.wrap (param i32) (result i32)))
12+ (import "link" "unwrap" (func $link.unwrap (param i32 i32)))
13+
14+ (memory (export "memory") 1)
15+ (data (i32.const 0) "hello world")
16+ (table (export "table") 1 1 anyfunc)
17+ (elem (i32.const 0) $callback)
18+ (global $egressRef (mut i32) (i32.const 0))
19+
20+ (func $main
21+ (i32.store
22+ (i32.const 0)
23+ (call $link.wrap
24+ (call $mem.externalize (i32.const 0) (i32.const 11))))
25+
26+ (call $storage.set
27+ (call $elem.externalize (i32.const 0) (i32.const 1))))
28+
29+ (func $load (param $egress i32)
30+ (set_global $egressRef (get_local $egress))
31+
32+ (call $elem.internalize
33+ (call $storage.get)
34+ (i32.const 0)
35+ (i32.const 0)
36+ (call $elem.length (call $storage.get)))
37+
38+ (call $link.unwrap
39+ (i32.load (i32.const 0))
40+ (i32.const 0)))
41+
42+ (func $callback (param $ref i32)
43+ (call $func.internalize
44+ (i32.const 0)
45+ (get_global $egressRef))
46+
47+ (call_indirect (param i32)
48+ (get_local $ref)
49+ (i32.const 0))
50+ )
51+
52+ (export "main" (func $main))
53+ (export "load" (func $load)))
tests/wast/storage.jsonView
@@ -1,0 +1,12 @@
1+{
2+ "types": [{
3+ "form": "func",
4+ "params": [
5+ "func"
6+ ]
7+ }],
8+ "typeMap": [{
9+ "func": 1,
10+ "type": 0
11+ }]
12+}
tests/wast/storage.wastView
@@ -1,0 +1,39 @@
1+(module
2+ (import "test" "check" (func $check (param i32 i32)))
3+ (import "test" "print" (func $print (param i32)))
4+ (import "memory" "externalize" (func $mem.externalize (param i32 i32) (result i32)))
5+ (import "storage" "get" (func $storage.get (result i32)))
6+ (import "storage" "set" (func $storage.set (param i32)))
7+ (import "elem" "externalize" (func $elem.externalize (param i32 i32) (result i32)))
8+ (import "elem" "internalize" (func $elem.internalize (param i32 i32 i32 i32)))
9+ (import "func" "internalize" (func $func.internalize (param i32 i32)))
10+
11+ (memory (export "memory") 1)
12+ (data (i32.const 0) "hello world")
13+ (table (export "table") 1 1 anyfunc)
14+ (func $main
15+ (i32.store
16+ (i32.const 0)
17+ (call $mem.externalize (i32.const 0) (i32.const 11)))
18+
19+ (call $storage.set
20+ (call $elem.externalize (i32.const 0) (i32.const 1))))
21+
22+ (func $load (param $egress i32)
23+ (call $elem.internalize
24+ (call $storage.get)
25+ (i32.const 0)
26+ (i32.const 0)
27+ (i32.const 1)
28+ )
29+
30+ (call $func.internalize
31+ (i32.const 0)
32+ (get_local $egress))
33+
34+ (call_indirect (param i32)
35+ (i32.load (i32.const 0))
36+ (i32.const 0)))
37+
38+ (export "main" (func $main))
39+ (export "load" (func $load)))
typeCheckWrapper.jsView
@@ -112,25 +112,25 @@
112112
113113 // check import
114114 importType.push('i32')
115115 importType.push('i32')
116- checkCode.push(i32_const(typeCode))
116+ checkCode.push(i32Const(typeCode))
117117 if (baseType === 'i64') {
118118 importType.push('i32')
119119
120120 // splits an i64 into 2 i32
121121 const spliti64 = [
122- get_local(index),
123- i64_const(32),
124- shr_u(),
125- wrap_i64(),
126- get_local(index),
127- wrap_i64()]
122+ getLocal(index),
123+ i64Const(32),
124+ shrU(),
125+ wrapI64(),
126+ getLocal(index),
127+ wrapI64()]
128128
129129 checkCode = checkCode.concat(spliti64)
130130
131131 const i32wrapCode = [
132- get_local(invokeIndex), {
132+ getLocal(invokeIndex), {
133133 'return_type': 'i64',
134134 'name': 'extend_u/i32'
135135 }, {
136136 'return_type': 'i64',
@@ -139,9 +139,9 @@
139139 }, {
140140 'return_type': 'i64',
141141 'name': 'shl'
142142 },
143- get_local(++invokeIndex), {
143+ getLocal(++invokeIndex), {
144144 'return_type': 'i64',
145145 'name': 'extend_u/i32'
146146 }, {
147147 'return_type': 'i64',
@@ -151,10 +151,10 @@
151151
152152 invokeCode = invokeCode.concat(i32wrapCode)
153153 invokerType.push('i32')
154154 } else {
155- checkCode.push(get_local(index))
156- invokeCode.push(get_local(invokeIndex))
155+ checkCode.push(getLocal(index))
156+ invokeCode.push(getLocal(invokeIndex))
157157 }
158158 invokerType.push('i32')
159159 // check export
160160 checkType.push(baseType)
@@ -163,10 +163,10 @@
163163 invokeIndex++
164164 })
165165
166166 module[7].entries[0].code = checkCode.concat(setGlobals, [call(0), end()])
167- invokeCode.push(i32_const(0))
168- invokeCode.push(call_indirect(3))
167+ invokeCode.push(i32Const(0))
168+ invokeCode.push(callIndirect(3))
169169 invokeCode.push(end())
170170 module[7].entries[1].code = invokeCode
171171 return module
172172 }
@@ -177,9 +177,9 @@
177177 'immediates': index
178178 }
179179 }
180180
181-function call_indirect (index) {
181+function callIndirect (index) {
182182 return {
183183 'name': 'call_indirect',
184184 'immediates': {
185185 'index': index,
@@ -193,39 +193,39 @@
193193 name: 'end'
194194 }
195195 }
196196
197-function get_local (index) {
197+function getLocal (index) {
198198 return {
199199 name: 'get_local',
200200 immediates: index
201201 }
202202 }
203203
204-function i32_const (num) {
204+function i32Const (num) {
205205 return {
206206 'return_type': 'i32',
207207 'name': 'const',
208208 'immediates': num
209209 }
210210 }
211211
212-function i64_const (num) {
212+function i64Const (num) {
213213 return {
214214 'return_type': 'i64',
215215 'name': 'const',
216216 'immediates': num
217217 }
218218 }
219219
220-function shr_u() {
220+function shrU () {
221221 return {
222222 'return_type': 'i64',
223223 'name': 'shr_u'
224224 }
225225 }
226226
227-function wrap_i64() {
227+function wrapI64 () {
228228 return {
229229 'return_type': 'i32',
230230 'name': 'wrap/i64'
231231 }
injectGlobals.jsView
@@ -1,104 +1,0 @@
1-const {findSections} = require('wasm-json-toolkit')
2-
3-module.exports = function injectGlobals (json, globals) {
4- const wantedSections = ['type', 'import', 'function', 'export', 'code']
5- const iter = findSections(json, wantedSections)
6- const {value: type = {entries: []}} = iter.next()
7- const getterType = type.entries.push(typeEntry()) - 1
8- const setterType = type.entries.push(typeEntry(Array(globals.length).fill('i32'))) - 1
9-
10- const {value: imports = {entries: []}} = iter.next()
11- const {value: func = {entries: []}} = iter.next()
12- const getterIndex = func.entries.push(getterType) - 1 + imports.entries.length
13- const setterIndex = func.entries.push(setterType) - 1 + imports.entries.length
14- const {value: exports = {entries: []}} = iter.next()
15- exports.entries.push(exportEntry('getter_globals', getterIndex))
16- exports.entries.push(exportEntry('setter_globals', setterIndex))
17- const {value: code = {entries: []}} = iter.next()
18- const getterCode = []
19- const setterCode = []
20- globals.forEach((global, index) => {
21- const globalIndex = global.index
22- // getter
23- getterCode.push(i32_const(index * 4))
24- getterCode.push(get_global(globalIndex))
25- getterCode.push(i32_store())
26- // setter
27- setterCode.push(get_local(index))
28- setterCode.push(set_global(globalIndex))
29- })
30-
31- getterCode.push(end())
32- setterCode.push(end())
33- code.entries.push(section_code([], getterCode))
34- code.entries.push(section_code([], setterCode))
35- return json
36-}
37-
38-function exportEntry (field_str, index) {
39- return {
40- field_str,
41- kind: 'function',
42- index
43- }
44-}
45-
46-function typeEntry (params = []) {
47- return {
48- form: 'func',
49- params: params
50- }
51-}
52-
53-function end () {
54- return {
55- name: 'end'
56- }
57-}
58-
59-function get_local (index) {
60- return {
61- name: 'get_local',
62- immediates: index
63- }
64-}
65-
66-function get_global (index) {
67- return {
68- name: 'get_global',
69- immediates: index
70- }
71-}
72-
73-function set_global (index) {
74- return {
75- name: 'set_global',
76- immediates: index
77- }
78-}
79-
80-function i32_const (num) {
81- return {
82- 'return_type': 'i32',
83- 'name': 'const',
84- 'immediates': num
85- }
86-}
87-
88-function i32_store () {
89- return {
90- 'return_type': 'i32',
91- 'name': 'store',
92- 'immediates': {
93- 'flags': 2,
94- 'offset': 0
95- }
96- }
97-}
98-
99-function section_code (locals, code) {
100- return {
101- locals: locals,
102- code: code
103- }
104-}

Built with git-ssb-web