forked from alpertron/calculators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecmfwebw.js
114 lines (106 loc) · 2.75 KB
/
ecmfwebw.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
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
/*
This file is part of Alpertron Calculators.
Copyright 2015 Dario Alejandro Alpern
Alpertron Calculators is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Alpertron Calculators is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
*/
var exports, HEAPU8, wasmLoaded;
var env =
{
"databack": function(data)
{
self.postMessage(PtrToString(data));
},
"tenths": function()
{
return Math.floor(new Date().getTime() / 100);
},
"getCunn": function(data)
{
var req = new XMLHttpRequest();
// Web worker protocol is blob:, so we need to change that to https: as appropriate.
req.open('GET', "https://www.alpertron.com.ar/"+PtrToString(data), false);
req.send(null);
if (req.status == 200)
{
ConvertToString(exports["getFactorsAsciiPtr"](), req.responseText);
}
}
};
var info =
{
"env": env
};
self.onmessage = function (e)
{
var request;
if (wasmLoaded)
{
ConvertToString(exports["getInputStringPtr"](), e.data[0]);
exports["doWork"]();
return;
}
WebAssembly["instantiate"](e.data[1], info).then(function(results)
{
wasmLoaded = 1;
exports = results["instance"]["exports"];
HEAPU8 = new Uint8Array(exports["memory"]["buffer"]);
ConvertToString(exports["getInputStringPtr"](), e.data[0]);
exports["doWork"]();
return;
});
}
function PtrToString(ptr)
{
var t=-1;
var i = 0;
var str="", outString="";
do
{
for (i=0; i<1024; i++)
{
t = HEAPU8[((ptr++)>>0)];
if (t==0)
{
break;
}
if (t>=128)
{
t = ((t-192)<<6) + HEAPU8[((ptr++)>>0)] - 128;
}
str += String.fromCharCode(t);
}
outString += str;
str = "";
} while (t!=0);
outString += str;
return outString;
}
function ConvertToString(ptr, str)
{
var dest = ptr;
var length = str.length;
var i, t;
for (i=0; i<length; i++)
{
t = str.charCodeAt(i);
if (t<128)
{
HEAPU8[dest++] = t;
}
else
{
HEAPU8[dest++] = (t >> 6) + 192;
HEAPU8[dest++] = (t & 63) + 128;
}
}
HEAPU8[dest] = 0;
}