git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 46e04c98fc3dc5aa69dc36fe9176ddc702784118

Files: 46e04c98fc3dc5aa69dc36fe9176ddc702784118 / typeCheckWrapper.js

2957 bytesRaw
1const {LANGUAGE_TYPES_STRG} = require('primea-annotations')
2
3module.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}
158

Built with git-ssb-web