forked from pixiebrix/jq-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre.js
68 lines (62 loc) · 1.44 KB
/
pre.js
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
/** @format */
var initialized = false
var initListeners = []
var stdin = ''
var inBuffer = []
var outBuffer = []
var errBuffer = []
function toByteArray(str) {
var byteArray = []
var encodedStr = unescape(encodeURIComponent(str))
for (var i = 0; i < encodedStr.length; i++) {
byteArray.push(encodedStr.charCodeAt(i))
}
return byteArray
}
function fromByteArray(data) {
var array = new Uint8Array(data)
var str = ''
for (var i = 0; i < array.length; ++i) {
str += String.fromCharCode(array[i])
}
return decodeURIComponent(escape(str))
}
// Note about Emscripten, even though the module is now named 'jq', pre.js still uses Module, but post.js uses 'jq'
Module = Object.assign(
{
noInitialRun: true,
noExitRuntime: true,
onRuntimeInitialized: function() {
initialized = true
initListeners.forEach(function(cb) {
cb()
})
},
preRun: function() {
FS.init(
function input() {
if (inBuffer.length) {
return inBuffer.pop()
}
if (!stdin) return null
inBuffer = toByteArray(stdin)
stdin = ''
inBuffer.push(null)
inBuffer.reverse()
return inBuffer.pop()
},
function output(c) {
if (c) {
outBuffer.push(c)
}
},
function error(c) {
if (c) {
errBuffer.push(c)
}
}
)
}
},
Module
)