forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnimwasm.cfg
229 lines (224 loc) · 7.36 KB
/
nimwasm.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#$1 -> wasm module name
#$2 -> jshelpers
# TODO: move nimWasmImport to configurable, or at least allow to extend
glue.jsHelpers = """
function toJSStr(ptr){
// takes a ptr into memory, reads the first 4 bytes to determine
// the length of the string, then return the string
var initialOffset = ptr+8 // len+cap is 8bytes
var lenarr = $$$1.memory.slice(ptr,ptr+4)
var len = (new Uint32Array(lenarr.buffer))[0]
var valarr = $$$1.memory.slice(initialOffset,initialOffset+len)
//if (len == 8){
// console.log((new Float64Array(valarr.buffer))[0])
//}
//else if (len == 4){
// console.log((new Int32Array(valarr.buffer))[0])
//}
var asciiPart = new Array(len);
var fcc = String.fromCharCode;
var nonAsciiPart = null;
var nonAsciiOffset = 0;
for (var i = initialOffset; i < initialOffset+len; ++i) {
if (nonAsciiPart !== null) {
var offset = (i - nonAsciiOffset) * 2;
var code = $$$1.memory[i].toString(16);
if (code.length == 1) {
code = "0"+code;
}
nonAsciiPart[offset] = "%";
nonAsciiPart[offset + 1] = code;
}
else if ($$$1.memory[i] < 128)
asciiPart[i] = fcc($$$1.memory[i]);
else {
asciiPart.length = i;
nonAsciiOffset = i;
nonAsciiPart = new Array((len - i) * 2);
--i;
}
}
asciiPart = asciiPart.join("");
return (nonAsciiPart === null) ?
asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
}
function newtoJSStr(ptr){
// takes a ptr into memory, reads the first 4 bytes to determine
// the length of the string, then return the string
const initialOffset = ptr+8 // len+cap is 8bytes
const len = $$$1.dt.getUint32(ptr, true)
const valarr = $$$1.memory.slice(initialOffset,initialOffset+len); //-1 CHECK: why? null term?
return (new TextDecoder()).decode(valarr);
}
function rawEcho(){
//var buf = "";
for (var i = 0; i < arguments.length; ++i) {
//buf +=
toJSStr(arguments[i]);
}
//console.log(buf);
}
function nimFloatToJSString(num) {
if (Number.isInteger(num)) {
return num + ".0";
} else {
return num.toString();
}
}
function nimIntToJSString(num) {return num.toString();}
function encodeAndPut(toencode){
// get the stack ptr
// TODO: maybe just export the stackptr as a global?
// then it'd be just $$$1.exports.stackptr
let stackptr = $$$1.dt.getUint32(4, true); // first 4 bytes are empty
let strptr = stackptr;
// encode the string to bytes
const encoded = (new TextEncoder()).encode(toencode);
// save len, cap
$$$1.dt.setUint32(stackptr, encoded.length, true);
$$$1.dt.setUint32(stackptr+4, encoded.length, true);
stackptr += 8;
// save the string bytes
encoded.forEach((b)=>{
$$$1.dt.setUint8(stackptr, b);
stackptr += 1;
});
// put back the stackptr
$$$1.dt.setUint32(4, stackptr, true);
// return the ptr to the string to wasm
return strptr;
}
function nimIntToStr(num){
return encodeAndPut(nimIntToJSString(num));
}
function nimFloatToStr(num){
return encodeAndPut(nimFloatToJSString(num));
}
function nimRaise(msgobj){
// msgobj is an object with a string as first elem
//const strptr = $$$1.dt.getUint32(msgobj, true)
throw new Error(newtoJSStr(msgobj));
}
"""
glue.loader = """
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
Look in the console and check out the <code>$$$1</code> variable.
<br>
eg. <code>$$$1.exports._memory</code> or <code>$$$1.exports.myfunc</code>
</div>
<script>
var nimWasmImports = {
glue: {
log: function(arg){
console.log(arg)
},
assert: function(arg){
console.assert(arg)
},
rawEcho: function(strptr) {
rawEcho(strptr)
},
floatToString: function(num){
return nimFloatToStr(num)
},
intToString: function(num){
return nimIntToStr(num)
},
echoFloat: function(num){
console.log(nimFloatToJSString(num))
},
echoInt: function(num){
console.log(nimIntToJSString(num))
},
echoBool: function(num){
console.log(num ? true : false)
},
echoString: function(ptr){
console.log(newtoJSStr(ptr))
},
raise: function(msg){
nimRaise(msg);
}
}
};
var $$$1 = {};
fetch('$1.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes,nimWasmImports)
).then(results => {
$$$1.module = results.module;
$$$1.memory = new Uint8Array(results.instance.exports.$$memory.buffer);
$$$1.dt = new DataView(results.instance.exports.$$memory.buffer);
$$$1.exports = results.instance.exports;
try {
$$$1.exports.nimInit() // call the init proc, like calling main in C;
} catch (msg) {
console.error(msg)
return; // cleaner errors
}
});
</script>
<script>$2</script>
</body>
</html>
"""
glue.loaderNode = """
var fs = require('fs')
var path = require('path')
var root = path.dirname(require.main.filename);
var nimWasmImports = {
glue: {
log: function(arg){
console.log(arg)
},
assert: function(arg){
console.assert(arg)
},
rawEcho: function(strptr) {
rawEcho(strptr)
},
floatToString: function(num){
return nimFloatToStr(num)
},
intToString: function(num){
return nimIntToStr(num)
},
echoFloat: function(num){
console.log(nimFloatToJSString(num))
},
echoInt: function(num){
console.log(nimIntToJSString(num))
},
echoBool: function(num){
console.log(num ? true : false)
},
echoString: function(ptr){
console.log(newtoJSStr(ptr))
},
raise: function(msg){
nimRaise(msg);
}
}
};
var $$$1 = {};
var buf = fs.readFileSync(root+'/$1.wasm')
WebAssembly.instantiate(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength), nimWasmImports).then(results => {
$$$1.module = results.module
$$$1.memory = new Uint8Array(results.instance.exports.$$memory.buffer)
$$$1.dt = new DataView(results.instance.exports.$$memory.buffer);
$$$1.exports = results.instance.exports
try{
$$$1.exports.nimInit() // call the init proc, like calling main in C
} catch (msg) {
console.error(msg)
process.exitCode = 1; // cleaner node errors
}
});
$2
"""