forked from elm/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.js
305 lines (273 loc) · 8.91 KB
/
Utils.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Elm.Native = Elm.Native || {};
Elm.Native.Utils = {};
Elm.Native.Utils.make = function(localRuntime) {
localRuntime.Native = localRuntime.Native || {};
localRuntime.Native.Utils = localRuntime.Native.Utils || {};
if (localRuntime.Native.Utils.values) {
return localRuntime.Native.Utils.values;
}
function eq(l,r) {
var stack = [{'x': l, 'y': r}]
while (stack.length > 0) {
var front = stack.pop();
var x = front.x;
var y = front.y;
if (x === y) continue;
if (typeof x === "object") {
var c = 0;
for (var i in x) {
++c;
if (i in y) {
if (i !== 'ctor') {
stack.push({ 'x': x[i], 'y': y[i] });
}
} else {
return false;
}
}
if ('ctor' in x) {
stack.push({'x': x.ctor, 'y': y.ctor});
}
if (c !== Object.keys(y).length) {
return false;
};
} else if (typeof x === 'function') {
throw new Error('Equality error: general function equality is ' +
'undecidable, and therefore, unsupported');
} else {
return false;
}
}
return true;
}
// code in Generate/JavaScript.hs depends on the particular
// integer values assigned to LT, EQ, and GT
var LT = -1, EQ = 0, GT = 1, ord = ['LT','EQ','GT'];
function compare(x,y) { return { ctor: ord[cmp(x,y)+1] } }
function cmp(x,y) {
var ord;
if (typeof x !== 'object'){
return x === y ? EQ : x < y ? LT : GT;
}
else if (x.isChar){
var a = x.toString();
var b = y.toString();
return a === b ? EQ : a < b ? LT : GT;
}
else if (x.ctor === "::" || x.ctor === "[]") {
while (true) {
if (x.ctor === "[]" && y.ctor === "[]") return EQ;
if (x.ctor !== y.ctor) return x.ctor === '[]' ? LT : GT;
ord = cmp(x._0, y._0);
if (ord !== EQ) return ord;
x = x._1;
y = y._1;
}
}
else if (x.ctor.slice(0,6) === '_Tuple') {
var n = x.ctor.slice(6) - 0;
var err = 'cannot compare tuples with more than 6 elements.';
if (n === 0) return EQ;
if (n >= 1) { ord = cmp(x._0, y._0); if (ord !== EQ) return ord;
if (n >= 2) { ord = cmp(x._1, y._1); if (ord !== EQ) return ord;
if (n >= 3) { ord = cmp(x._2, y._2); if (ord !== EQ) return ord;
if (n >= 4) { ord = cmp(x._3, y._3); if (ord !== EQ) return ord;
if (n >= 5) { ord = cmp(x._4, y._4); if (ord !== EQ) return ord;
if (n >= 6) { ord = cmp(x._5, y._5); if (ord !== EQ) return ord;
if (n >= 7) throw new Error('Comparison error: ' + err); } } } } } }
return EQ;
}
else {
throw new Error('Comparison error: comparison is only defined on ints, ' +
'floats, times, chars, strings, lists of comparable values, ' +
'and tuples of comparable values.');
}
}
var Tuple0 = { ctor: "_Tuple0" };
function Tuple2(x,y) {
return {
ctor: "_Tuple2",
_0: x,
_1: y
};
}
function chr(c) {
var x = new String(c);
x.isChar = true;
return x;
}
function txt(str) {
var t = new String(str);
t.text = true;
return t;
}
function makeText(text) {
var style = '';
var href = '';
while (true) {
if (text.style) {
style += text.style;
text = text.text;
continue;
}
if (text.href) {
href = text.href;
text = text.text;
continue;
}
if (href) {
text = '<a href="' + href + '">' + text + '</a>';
}
if (style) {
text = '<span style="' + style + '">' + text + '</span>';
}
return text;
}
}
var count = 0;
function guid(_) {
return count++
}
function copy(oldRecord) {
var newRecord = {};
for (var key in oldRecord) {
var value = key === '_'
? copy(oldRecord._)
: oldRecord[key]
;
newRecord[key] = value;
}
return newRecord;
}
function remove(key, oldRecord) {
var record = copy(oldRecord);
if (key in record._) {
record[key] = record._[key][0];
record._[key] = record._[key].slice(1);
if (record._[key].length === 0) {
delete record._[key];
}
} else {
delete record[key];
}
return record;
}
function replace(keyValuePairs, oldRecord) {
var record = copy(oldRecord);
for (var i = keyValuePairs.length; i--; ) {
var pair = keyValuePairs[i];
record[pair[0]] = pair[1];
}
return record;
}
function insert(key, value, oldRecord) {
var newRecord = copy(oldRecord);
if (key in newRecord) {
var values = newRecord._[key];
var copiedValues = values ? values.slice(0) : [];
newRecord._[key] = [newRecord[key]].concat(copiedValues);
}
newRecord[key] = value;
return newRecord;
}
function getXY(e) {
var posx = 0;
var posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
} else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
if (localRuntime.isEmbed()) {
var rect = localRuntime.node.getBoundingClientRect();
var relx = rect.left + document.body.scrollLeft + document.documentElement.scrollLeft;
var rely = rect.top + document.body.scrollTop + document.documentElement.scrollTop;
// TODO: figure out if there is a way to avoid rounding here
posx = posx - Math.round(relx) - localRuntime.node.clientLeft;
posy = posy - Math.round(rely) - localRuntime.node.clientTop;
}
return Tuple2(posx, posy);
}
//// LIST STUFF ////
var Nil = { ctor:'[]' };
function Cons(hd,tl) {
return {
ctor: "::",
_0: hd,
_1: tl
};
}
function append(xs,ys) {
// append Text
if (xs.text || ys.text) {
return txt(makeText(xs) + makeText(ys));
}
// append Strings
if (typeof xs === "string") {
return xs + ys;
}
// append Lists
if (xs.ctor === '[]') {
return ys;
}
var root = Cons(xs._0, Nil);
var curr = root;
xs = xs._1;
while (xs.ctor !== '[]') {
curr._1 = Cons(xs._0, Nil);
xs = xs._1;
curr = curr._1;
}
curr._1 = ys;
return root;
}
//// RUNTIME ERRORS ////
function indent(lines) {
return '\n' + lines.join('\n');
}
function badCase(moduleName, span) {
var msg = indent([
'Non-exhaustive pattern match in case-expression.',
'Make sure your patterns cover every case!'
]);
throw new Error('Runtime error in module ' + moduleName + ' (' + span + ')' + msg);
}
function badIf(moduleName, span) {
var msg = indent([
'Non-exhaustive pattern match in multi-way-if expression.',
'It is best to use \'otherwise\' as the last branch of multi-way-if.'
]);
throw new Error('Runtime error in module ' + moduleName + ' (' + span + ')' + msg);
}
function badPort(expected, received) {
var msg = indent([
'Expecting ' + expected + ' but was given ',
JSON.stringify(received)
]);
throw new Error('Runtime error when sending values through a port.' + msg);
}
return localRuntime.Native.Utils.values = {
eq:eq,
cmp:cmp,
compare:F2(compare),
Tuple0:Tuple0,
Tuple2:Tuple2,
chr:chr,
txt:txt,
makeText:makeText,
copy: copy,
remove: remove,
replace: replace,
insert: insert,
guid: guid,
getXY: getXY,
Nil: Nil,
Cons: Cons,
append: F2(append),
badCase: badCase,
badIf: badIf,
badPort: badPort
};
};