git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 7143b85faac4283ee86e42bb48dba742e17af7d6

move wasm container to seprate module

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 3/30/2018, 6:35:34 PM
Parent: a560c11794c6a4a7ea60a385f2aba1532a51d0af

Files changed

tests/typeChecking.jsdeleted
tests/wasm/caller.wasmdeleted
tests/wasm/counter.wasmdeleted
tests/wasm/elem.wasmdeleted
tests/wasm/empty.wasmdeleted
tests/wasm/funcRef_caller.wasmdeleted
tests/wasm/funcRef_reciever.wasmdeleted
tests/wasm/globals.wasmdeleted
tests/wasm/memory.wasmdeleted
tests/wasm/private_caller.wasmdeleted
tests/wasm/reciever.wasmdeleted
tests/wasm/table.wasmdeleted
tests/wasmContainer.jsdeleted
tests/wast/caller.jsondeleted
tests/wast/caller.wastdeleted
tests/wast/elem.wastdeleted
tests/wast/funcRef_caller.jsondeleted
tests/wast/funcRef_caller.wastdeleted
tests/wast/funcRef_reciever.jsondeleted
tests/wast/funcRef_reciever.wastdeleted
tests/wast/globals.jsondeleted
tests/wast/globals.wastdeleted
tests/wast/memory.wastdeleted
tests/wast/private_caller.jsondeleted
tests/wast/private_caller.wasmdeleted
tests/wast/private_caller.wastdeleted
tests/wast/reciever.wastdeleted
tests/wast/table.wastdeleted
tests/wast2wasm.jsdeleted
injectGlobals.jsdeleted
typeCheckWrapper.jsdeleted
tests/typeChecking.jsView
@@ -1,33 +1,0 @@
1-const customTypes = require('../customTypes.js')
2-const WasmContainer = require('../wasmContainer.js')
3-const fs = require('fs')
4-
5-async function main () {
6- let callerJSON = JSON.parse(fs.readFileSync('./wast/caller.json'))
7- let callerWasm = fs.readFileSync('./wasm/caller.wasm')
8- callerWasm = customTypes.inject(callerWasm, callerJSON)
9-
10- let recieverJSON = JSON.parse(fs.readFileSync('./wast/reciever.json'))
11- let recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
12- recieverWasm = customTypes.inject(recieverWasm, recieverJSON)
13-
14- const callerContainer = new WasmContainer()
15- callerContainer.onCreation(callerWasm)
16- callerJSON = callerContainer.json
17-
18- const recieverContainer = new WasmContainer()
19- recieverContainer.onCreation(recieverWasm)
20- recieverJSON = recieverContainer.json
21-
22- const callFuncRef = callerContainer.getFuncRef('call')
23-
24- const funcRef = recieverContainer.getFuncRef('receive')
25- callFuncRef.args.push({
26- type: 'funcRef',
27- arg: funcRef
28- })
29-
30- callerContainer.onMessage(callFuncRef)
31-}
32-
33-main()
tests/wasm/caller.wasmView
@@ -1,3 +1,0 @@
1-asm types`n typeMap
2-``func internalizeptablecall
3-A AA
tests/wasm/counter.wasmView
@@ -1,4 +1,0 @@
1-asm`pmemory increment
2- 
3-A( Aj
4-A 
tests/wasm/elem.wasmView
@@ -1,3 +1,0 @@
1-asm
2-``func internalizeptablereceive A 
3-A AA
tests/wasm/empty.wasmView
@@ -1,1 +1,0 @@
1-asm����p������������memory
tests/wasm/funcRef_caller.wasmView
@@ -1,2 +1,0 @@
1-asm types`n typeMap````Tfunc internalizetestcheckmoduleselfmoduleexportmemory externalizep$memorytablecallcallback
2-"AAA A  A A callback
tests/wasm/funcRef_reciever.wasmView
@@ -1,3 +1,0 @@
1-asm types`n typeMap
2-``*func internalizefuncset_gas_budgetptablereceive
3- A�� AAA
tests/wasm/globals.wasmView
@@ -1,4 +1,0 @@
1-asm persistm```+memory externalizememory internalize A~ A~ memoryloadstore
2-
3-AA$ #AAA
4-A test
tests/wasm/memory.wasmView
@@ -1,3 +1,0 @@
1-asm```+memory externalizememory internalizememorytest
2-AAAAA
3-A test
tests/wasm/private_caller.wasmView
@@ -1,2 +1,0 @@
1-asm types`n typeMap```4func internalizefunc externalizetestcheckpmemorytablecall A 
2-AA A  A
tests/wasm/reciever.wasmView
@@ -1,4 +1,0 @@
1-asm
2-``testcheck receive
3-
4- A
tests/wasm/table.wasmView
@@ -1,3 +1,0 @@
1-asm```>table externalizememory externalizetable internalizememorytest
2-(&AAAAAA66AAAAA
3-A test
tests/wasmContainer.jsView
@@ -1,280 +1,0 @@
1-const tape = require('tape')
2-const fs = require('fs')
3-const path = require('path')
4-const {Message} = require('primea-objects')
5-const Hypervisor = require('../')
6-const WasmContainer = require('../wasmContainer.js')
7-
8-const level = require('level-browserify')
9-const RadixTree = require('dfinity-radix-tree')
10-const db = level('./testdb')
11-
12-const WASM_PATH = path.join(__dirname, 'wasm')
13-
14-let tester
15-
16-class TestWasmContainer extends WasmContainer {
17- constructor (actor) {
18- super(actor)
19- this._storage = new Map()
20- }
21- getInterface (funcRef) {
22- const orginal = super.getInterface(funcRef)
23- return Object.assign(orginal, {
24- test: {
25- check: (a, b) => {
26- tester.equals(a, b)
27- },
28- print: (dataRef) => {
29- let buf = this.refs.get(dataRef, 'buf')
30- console.log(buf.toString())
31- }
32- }
33- })
34- }
35-}
36-
37-tape('basic', async t => {
38- t.plan(1)
39- tester = t
40- const expectedState = Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
41-
42- const tree = new RadixTree({
43- db
44- })
45-
46- const wasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
47-
48- const hypervisor = new Hypervisor(tree)
49- hypervisor.registerContainer(TestWasmContainer)
50-
51- const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
52- const funcRef = module.getFuncRef('receive')
53- funcRef.gas = 300
54-
55- const message = new Message({
56- funcRef,
57- funcArguments: [5]
58- }).on('execution:error', e => {
59- console.log(e)
60- })
61- hypervisor.send(message)
62- const stateRoot = await hypervisor.createStateRoot()
63- // t.deepEquals(stateRoot, expectedState, 'expected root!')
64-})
65-
66-tape('empty', async t => {
67- t.plan(1)
68- tester = t
69- const expectedState = Buffer.from('aeb5418328108a82b7a2a57712bddc989d513f5d', 'hex')
70-
71- const tree = new RadixTree({
72- db
73- })
74-
75- const wasm = fs.readFileSync(WASM_PATH + '/empty.wasm')
76-
77- const hypervisor = new Hypervisor(tree)
78- hypervisor.registerContainer(TestWasmContainer)
79-
80- const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
81- const funcRef = module.getFuncRef('receive')
82- funcRef.gas = 300
83-
84- const message = new Message({
85- funcRef,
86- funcArguments: [5]
87- })
88- hypervisor.send(message)
89- const stateRoot = await hypervisor.createStateRoot()
90- t.deepEquals(stateRoot, expectedState, 'expected root!')
91-})
92-
93-tape('two communicating actors', async t => {
94- t.plan(1)
95- tester = t
96- const expectedState = {
97- '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
98- }
99-
100- const tree = new RadixTree({db})
101-
102- const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
103- const callerWasm = fs.readFileSync(WASM_PATH + '/caller.wasm')
104-
105- const hypervisor = new Hypervisor(tree)
106- hypervisor.registerContainer(TestWasmContainer)
107-
108- const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
109- const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
110- const callFuncRef = callerMod.getFuncRef('call')
111- const recvFuncRef = receiverMod.getFuncRef('receive')
112- callFuncRef.gas = 100000
113- recvFuncRef.gas = 1000
114- const message = new Message({
115- funcRef: callFuncRef,
116- funcArguments: [recvFuncRef]
117- })
118-
119- hypervisor.send(message)
120- const stateRoot = await hypervisor.createStateRoot()
121- // t.deepEquals(stateRoot, expectedState, 'expected root!')
122-})
123-
124-tape('two communicating actors with callback', async t => {
125- t.plan(1)
126- tester = t
127- const expectedState = {
128- '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
129- }
130-
131- const tree = new RadixTree({
132- db
133- })
134-
135- const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
136- const callerWasm = fs.readFileSync(WASM_PATH + '/funcRef_caller.wasm')
137-
138- const hypervisor = new Hypervisor(tree)
139- hypervisor.registerContainer(TestWasmContainer)
140-
141- const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
142- const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
143-
144- const callFuncRef = callerMod.getFuncRef('call')
145- const recvFuncRef = receiverMod.getFuncRef('receive')
146- callFuncRef.gas = 100000
147- recvFuncRef.gas = 100000
148-
149- const message = new Message({
150- funcRef: callFuncRef,
151- funcArguments: [recvFuncRef]
152- }).on('execution:error', e => console.log(e))
153-
154- hypervisor.send(message)
155- const stateRoot = await hypervisor.createStateRoot()
156- // t.deepEquals(stateRoot, expectedState, 'expected root!')
157-})
158-
159-tape('two communicating actors with private callback', async t => {
160- t.plan(1)
161- tester = t
162- const expectedState = {
163- '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
164- }
165-
166- const tree = new RadixTree({
167- db
168- })
169-
170- const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
171- const callerWasm = fs.readFileSync(WASM_PATH + '/private_caller.wasm')
172-
173- const hypervisor = new Hypervisor(tree)
174- hypervisor.registerContainer(TestWasmContainer)
175-
176- const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
177- const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
178-
179- const callFuncRef = callerMod.getFuncRef('call')
180- const recvFuncRef = receiverMod.getFuncRef('receive')
181- callFuncRef.gas = 100000
182- recvFuncRef.gas = 100000
183-
184- const message = new Message({
185- funcRef: callFuncRef,
186- funcArguments: [recvFuncRef]
187- })
188-
189- hypervisor.send(message)
190- const stateRoot = await hypervisor.createStateRoot()
191- // t.deepEquals(stateRoot, expectedState, 'expected root!')
192-})
193-
194-tape('externalize/internalize memory', async t => {
195- t.plan(1)
196- tester = t
197- const tree = new RadixTree({
198- db
199- })
200-
201- const wasm = fs.readFileSync(WASM_PATH + '/memory.wasm')
202-
203- const hypervisor = new Hypervisor(tree)
204- hypervisor.registerContainer(TestWasmContainer)
205-
206- const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
207- const funcRef = module.getFuncRef('test')
208- funcRef.gas = 10000
209-
210- const message = new Message({funcRef}).on('done', actor => {
211- const a = actor.container.get8Memory(0, 5)
212- const b = actor.container.get8Memory(5, 5)
213- t.deepEquals(a, b, 'should copy memory correctly')
214- })
215- hypervisor.send(message)
216-})
217-
218-tape('externalize/internalize table', async t => {
219- t.plan(1)
220- tester = t
221- const tree = new RadixTree({
222- db
223- })
224-
225- const wasm = fs.readFileSync(WASM_PATH + '/table.wasm')
226- const hypervisor = new Hypervisor(tree)
227- hypervisor.registerContainer(TestWasmContainer)
228-
229- const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
230-
231- const funcRef = module.getFuncRef('test')
232- funcRef.gas = 10000
233-
234- const message = new Message({funcRef}).on('done', actor => {
235- const a = actor.container.get8Memory(0, 8)
236- const b = actor.container.get8Memory(8, 8)
237- t.deepEquals(a, b, 'should copy memory correctly')
238- })
239- hypervisor.send(message)
240-})
241-
242-tape('load / store globals', async t => {
243- t.plan(1)
244- tester = t
245- const tree = new RadixTree({
246- db
247- })
248-
249- const wasm = fs.readFileSync(WASM_PATH + '/globals.wasm')
250-
251- const hypervisor = new Hypervisor(tree)
252- hypervisor.registerContainer(TestWasmContainer)
253-
254- const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
255-
256- await new Promise((resolve, reject) => {
257- const funcRef = module.getFuncRef('store')
258- funcRef.gas = 400
259- const message = new Message({
260- funcRef
261- }).on('done', actor => {
262- resolve()
263- })
264- hypervisor.send(message)
265- })
266-
267- await new Promise((resolve, reject) => {
268- const funcRef = module.getFuncRef('load')
269- funcRef.gas = 400
270- const message = new Message({
271- funcRef
272- }).on('done', actor => {
273- const b = actor.container.get8Memory(5, 4)
274- const result = Buffer.from(b).toString()
275- t.deepEquals(result, 'test', 'should copy memory correctly')
276- resolve()
277- })
278- hypervisor.send(message)
279- })
280-})
tests/wast/caller.jsonView
@@ -1,12 +1,0 @@
1-{
2- "types": [{
3- "form": "func",
4- "params": [
5- "func"
6- ]
7- }],
8- "typeMap": [{
9- "func": 0,
10- "type": 0
11- }]
12-}
tests/wast/caller.wastView
@@ -1,12 +1,0 @@
1-(module
2- (import "func" "internalize" (func $internalize (param i32 i32)))
3- (table (export "table") 1 1 anyfunc)
4- (func $call (param i32)
5- i32.const 5
6- get_local 0
7- i32.const 0
8- call $internalize
9- i32.const 0
10- call_indirect (param i32)
11- )
12- (export "call" (func $call)))
tests/wast/elem.wastView
@@ -1,13 +1,0 @@
1-(module
2- (import "func" "internalize" (func $internalize (param i32 i32)))
3- (table (export "table") 1 1 anyfunc)
4- (elem (i32.const 0) $receive)
5- (func $receive (param i32)
6- i32.const 5
7- get_local 0
8- i32.const 0
9- call $internalize
10- i32.const 0
11- call_indirect (param i32)
12- )
13- (export "receive" (func $receive)))
tests/wast/funcRef_caller.jsonView
@@ -1,12 +1,0 @@
1-{
2- "types": [{
3- "form": "func",
4- "params": [
5- "func"
6- ]
7- }],
8- "typeMap": [{
9- "func": 0,
10- "type": 0
11- }]
12-}
tests/wast/funcRef_caller.wastView
@@ -1,29 +1,0 @@
1-(module
2- (import "func" "internalize" (func $internalize (param i32 i32)))
3- (import "test" "check" (func $check (param i32 i32)))
4- (import "module" "self" (func $self (result i32)))
5- (import "module" "export" (func $exports (param i32 i32) (result i32)))
6- (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
7- (memory (export "memory") 1)
8- (data (i32.const 0) "callback")
9- (table (export "table") 1 1 anyfunc)
10- (func $call (param i32)
11- call $self
12- i32.const 0
13- i32.const 8
14- call $externalize
15- call $exports
16-
17- i32.const 0
18- get_local 0
19- call $internalize
20- i32.const 0
21- call_indirect (param i32)
22- )
23- (func $callback (param i32)
24- get_local 0
25- i32.const 5
26- call $check
27- )
28- (export "call" (func $call))
29- (export "callback" (func $callback)))
tests/wast/funcRef_reciever.jsonView
@@ -1,12 +1,0 @@
1-{
2- "types": [{
3- "form": "func",
4- "params": [
5- "func"
6- ]
7- }],
8- "typeMap": [{
9- "func": 0,
10- "type": 0
11- }]
12-}
tests/wast/funcRef_reciever.wastView
@@ -1,17 +1,0 @@
1-(module
2- (import "func" "internalize" (func $internalize (param i32 i32)))
3- (import "func" "set_gas_budget" (func $set_gas_budget (param i32 i32)))
4- (table (export "table") 1 1 anyfunc)
5- (func $receive (param i32)
6- get_local 0
7- i32.const 10500
8- call $set_gas_budget
9-
10- get_local 0
11- i32.const 0
12- call $internalize
13- i32.const 5
14- i32.const 0
15- call_indirect (param i32)
16- )
17- (export "receive" (func $receive)))
tests/wast/globals.jsonView
@@ -1,7 +1,0 @@
1-{
2- "persist": [{
3- "form": "global",
4- "index": 0,
5- "type": "data"
6- }]
7-}
tests/wast/globals.wastView
@@ -1,23 +1,0 @@
1-(module
2- (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
3- (import "memory" "internalize" (func $internalize (param i32 i32 i32 i32)))
4- (global (mut i32) (i32.const -2))
5- (global (mut i32) (i32.const -2))
6- (memory (export "memory") 1)
7- (data (i32.const 0) "test")
8- (func $store
9- i32.const 0
10- i32.const 4
11- call $externalize
12- set_global 0
13- )
14-
15- (func $load
16- get_global 0
17- i32.const 0
18- i32.const 5
19- i32.const 4
20- call $internalize
21- )
22- (export "load" (func $load))
23- (export "store" (func $store)))
tests/wast/memory.wastView
@@ -1,15 +1,0 @@
1-(module
2- (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
3- (import "memory" "internalize" (func $internalize (param i32 i32 i32 i32)))
4- (memory (export "memory") 1)
5- (data (i32.const 0) "test")
6- (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
14- )
15- (export "test" (func $test)))
tests/wast/private_caller.jsonView
@@ -1,12 +1,0 @@
1-{
2- "types": [{
3- "form": "func",
4- "params": [
5- "func"
6- ]
7- }],
8- "typeMap": [{
9- "func": 0,
10- "type": 0
11- }]
12-}
tests/wast/private_caller.wasmView
@@ -1,2 +1,0 @@
1-asm```4func internalizefunc externalizetestcheckpmemorytablecall A 
2-AA A  A
tests/wast/private_caller.wastView
@@ -1,22 +1,0 @@
1-(module
2- (import "func" "internalize" (func $internalize (param i32 i32)))
3- (import "func" "externalize" (func $externalize (param i32) (result i32)))
4- (import "test" "check" (func $check (param i32 i32)))
5- (memory (export "memory") 1)
6- (table (export "table") 1 anyfunc)
7- (elem (i32.const 0) $callback)
8- (func $call (param i32)
9- i32.const 0
10- call $externalize
11- i32.const 0
12- get_local 0
13- call $internalize
14- i32.const 0
15- call_indirect (param i32)
16- )
17- (func $callback (param i32)
18- get_local 0
19- i32.const 5
20- call $check
21- )
22- (export "call" (func $call)))
tests/wast/reciever.wastView
@@ -1,8 +1,0 @@
1-(module
2- (import "test" "check" (func $check (param i32 i32)))
3- (func $receive (param i32)
4- get_local 0
5- i32.const 5
6- call $check
7- )
8- (export "receive" (func $receive)))
tests/wast/table.wastView
@@ -1,25 +1,0 @@
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)))
tests/wast2wasm.jsView
@@ -1,35 +1,0 @@
1-const wabt = require('wabt')
2-const fs = require('fs')
3-const annotations = require('primea-annotations')
4-
5-function filesWast2wasm () {
6- const srcFiles = fs.readdirSync(`${__dirname}/wast`)
7- const wastFiles = srcFiles.filter(name => name.split('.').pop() === 'wast')
8- for (let file of wastFiles) {
9- const wat = fs.readFileSync(`${__dirname}/wast/${file}`).toString()
10- file = file.split('.')[0]
11- let json
12- try {
13- json = fs.readFileSync(`${__dirname}/wast/${file}.json`)
14- json = JSON.parse(json)
15- } catch (e) {
16- console.log(`no json for ${file}`)
17- }
18-
19- try {
20- const mod = wabt.parseWat('module.wast', wat)
21- const r = mod.toBinary({log: true})
22- let binary = Buffer.from(r.buffer)
23- if (json) {
24- console.log(json)
25- binary = annotations.encodeAndInject(json, binary)
26- }
27- fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary)
28- } catch (e) {
29- console.log(`failed at ${file}`)
30- console.log(e)
31- }
32- }
33-}
34-
35-filesWast2wasm()
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-}
typeCheckWrapper.jsView
@@ -1,157 +1,0 @@
1-const {LANGUAGE_TYPES_STRG} = require('primea-annotations')
2-
3-module.exports = function (params) {
4- const module = [{
5- 'name': 'preramble',
6- 'magic': [
7- 0,
8- 97,
9- 115,
10- 109
11- ],
12- 'version': [
13- 1,
14- 0,
15- 0,
16- 0
17- ]
18- }, {
19- 'name': 'type',
20- 'entries': [{
21- 'form': 'func',
22- 'params': [
23- ]
24- }, {
25- 'form': 'func',
26- 'params': [
27- // imported check
28- ]
29- }, {
30- 'form': 'func',
31- 'params': [
32- // exported check
33- ]
34- }, {
35- 'form': 'func',
36- 'params': [
37- // invoke
38- ]
39- }]
40- }, {
41- 'name': 'import',
42- 'entries': [{
43- 'moduleStr': 'env',
44- 'fieldStr': 'checkTypes',
45- 'kind': 'function',
46- 'type': 0
47- }]
48- }, {
49- 'name': 'function',
50- 'entries': [
51- 1,
52- 2
53- ]
54- }, {
55- 'name': 'table',
56- 'entries': [{
57- 'elementType': 'anyFunc',
58- 'limits': {
59- 'flags': 1,
60- 'intial': 1,
61- 'maximum': 1
62- }
63- }]
64- }, {
65- 'name': 'global',
66- 'entries': []
67- }, {
68- 'name': 'export',
69- 'entries': [{
70- 'field_str': 'table',
71- 'kind': 'table',
72- 'index': 0
73- }, {
74- 'field_str': 'invoke',
75- 'kind': 'function',
76- 'index': 2
77- }, {
78- 'field_str': 'check',
79- 'kind': 'function',
80- 'index': 1
81- }]
82- }, {
83- 'name': 'code',
84- 'entries': [{
85- 'locals': [],
86- 'code': []
87- }, {
88- 'locals': [],
89- 'code': []
90- }]
91- }]
92-
93- const definedTypes = new Set(['actor', 'func', 'buf'])
94- const setGlobals = []
95- const importType = module[1].entries[0].params
96- const checkType = module[1].entries[1].params
97- const invokerType = module[1].entries[2].params
98- const invokeType = module[1].entries[3].params
99- const checkCode = module[7].entries[0].code
100- const invokeCode = module[7].entries[1].code
101-
102- params.forEach((param, index) => {
103- let baseType = param
104- const typeCode = LANGUAGE_TYPES_STRG[param]
105- // import type
106- if (definedTypes.has(param)) {
107- baseType = 'i32'
108- } else {
109- baseType = param
110- }
111-
112- // check import
113- importType.push('i32')
114- importType.push('i32')
115- checkCode.push({
116- 'return_type': 'i32',
117- 'name': 'const',
118- 'immediates': typeCode
119- })
120- checkCode.push({
121- 'name': 'get_local',
122- 'immediates': index
123- })
124- invokeCode.push({
125- 'name': 'get_local',
126- 'immediates': index
127- })
128- // check export
129- checkType.push(baseType)
130- // invoke
131- invokeType.push(baseType)
132- invokerType.push(baseType)
133- })
134-
135- module[7].entries[0].code = checkCode.concat(setGlobals, [{
136- 'name': 'call',
137- 'immediates': '0'
138- }, {
139- 'name': 'end'
140- }])
141- invokeCode.push({
142- 'return_type': 'i32',
143- 'name': 'const',
144- 'immediates': '0'
145- })
146- invokeCode.push({
147- 'name': 'call_indirect',
148- 'immediates': {
149- 'index': 3,
150- 'reserved': 0
151- }
152- })
153- invokeCode.push({
154- 'name': 'end'
155- })
156- return module
157-}

Built with git-ssb-web