-
Notifications
You must be signed in to change notification settings - Fork 130
/
core.js
720 lines (584 loc) · 15.5 KB
/
core.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
var op = {};
exports.op = op;
var Hash = require('./hash.js');
var CodeRef = require('./code-ref.js');
var LexPadHack = require('./lexpad-hack.js');
var NQPInt = require('./nqp-int.js');
var reprs = require('./reprs.js');
exports.CodeRef = CodeRef;
op.atpos = function(array, index) {
if (array instanceof Array) {
if (index < 0) {
index = array.length + index;
}
return array[index];
} else {
return array.$$atpos(index);
}
};
op.bindpos = function(array, index, value) {
if (array instanceof Array) {
if (index < 0) {
index = array.length + index;
}
return (array[index] = value);
} else {
return array.$$bindpos(index, value);
}
};
op.getcomp = function(lang) {
};
op.isinvokable = function(obj) {
return (obj instanceof CodeRef || (obj._STable && obj._STable.invocationSpec) ? 1 : 0);
};
op.escape = function(str) {
return str
.replace(/\\/g, '\\\\')
.replace(/\x1B/g, '\\e')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
.replace(/\f/g, '\\f')
.replace(/[\b]/g, '\\b')
.replace(/"/g, '\\"');
};
op.x = function(str, times) {
if (!times) return '';
var ret = str;
while (--times) ret += str;
return ret;
};
op.radix = function(radix, str, zpos, flags) {
if (flags != 2) {throw 'flags != 2 not implemented yet: ' + flags}
var lowercase = 'a-' + String.fromCharCode('a'.charCodeAt(0) + radix - 11);
var uppercase = 'A-' + String.fromCharCode('A'.charCodeAt(0) + radix - 11);
var letters = radix >= 11 ? lowercase + uppercase : '';
var digitclass = '[0-' + Math.min(radix - 1, 9) + letters + ']';
var minus = flags & 0x02 ? '-?' : '';
var regex = new RegExp(
'^' + minus + digitclass + '(?:_' +
digitclass + '|' + digitclass + ')*');
var str = str.slice(zpos);
var search = str.match(regex);
if (search == null) {
return [0, 1, -1];
}
var number = search[0].replace(/_/g, '');
var power = number[0] == '-' ? number.length - 1 : number.length;
var pow = Math.pow(radix, power);
var ret = [parseInt(number, radix), pow, zpos + search[0].length];
return ret;
};
function Iter(array) {
this.array = array;
this.target = array.length;
this.idx = 0;
}
Iter.prototype.shift = function() {
return this.array[this.idx++];
};
Iter.prototype.$$to_bool = function(ctx) {
return this.idx < this.target;
};
function HashIter(hash) {
this.hash = hash;
this.keys = Object.keys(hash.content);
this.target = this.keys.length;
this.idx = 0;
}
HashIter.prototype.shift = function() {
return new IterPair(this.hash, this.keys[this.idx++]);
};
HashIter.prototype.$$to_bool = function(ctx) {
return this.idx < this.target;
};
function IterPair(hash, key) {
this._key = key;
this._hash = hash.content;
}
IterPair.prototype.iterval = function() {
return this._hash[this._key];
};
IterPair.prototype.iterkey_s = function() {
return this._key;
};
IterPair.prototype.Str = function(ctx) {
return this._key;
};
IterPair.prototype.key = function(ctx, named) {
return this._key;
};
IterPair.prototype.value = function(ctx, named) {
return this._hash[this._key];
};
op.iterator = function(obj) {
if (obj instanceof Array) {
return new Iter(obj);
} else if (obj instanceof Hash) {
return new HashIter(obj);
} else if (obj instanceof LexPadHack) {
return new Iter(Object.keys(obj.content));
} else {
throw 'unsupported thing to iterate over';
}
};
exports.hash = function() {
return new Hash();
};
exports.slurpy_named = function(named) {
var hash = new Hash();
for (key in named) {
hash.content[key] = named[key];
}
return hash;
};
exports.unwrap_named = function(named) {
if (!named instanceof Hash) console.log('expecting a hash here');
return named.content;
};
exports.named = function(parts) {
var all = {};
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
for (var key in part) {
all[key] = part[key];
}
}
return all;
};
exports.op.ishash = function(obj) {
return obj instanceof Hash ? 1 : 0;
};
op.existspos = function(array, idx) {
if (idx < 0) idx += array.length;
return array.hasOwnProperty(idx) ? 1 : 0;
};
op.create = function(obj) {
return obj._STable.REPR.allocate(obj._STable);
};
// HACK - till the array refactor we hack this
op.bootarray = function(obj) {
return {_STable: {REPR: {allocate: function(STable) {
return [];
}}}};
};
op.defined = function(obj) {
// TODO - handle more things that aren't defined
if (obj === undefined || obj === null || obj.type_object_) {
return 0;
}
return 1;
};
op.setinvokespec = function(obj, classHandle, attrName, invocationHandler) {
if (invocationHandler !== null) {
throw 'invocationHandler argument to setinvokespec not supported';
}
obj._STable.setinvokespec(classHandle, attrName, invocationHandler);
return obj;
};
// Stub
op.setboolspec = function(obj, mode, method) {
obj._STable.setboolspec(mode, method);
return obj;
};
function Capture(named, pos) {
this.pos = pos;
this.named = named;
}
op.savecapture = function(args) {
return new Capture(args[1], Array.prototype.slice.call(args, 2));
};
op.captureposelems = function(capture) {
return capture.pos.length;
};
op.captureposarg = function(capture, i) {
return capture.pos[i];
};
op.setcodeobj = function(codeRef, codeObj) {
codeRef.codeObj = codeObj;
return codeRef;
};
op.getcodeobj = function(codeRef) {
return codeRef.codeObj;
};
op.curcode = function() {
var current = arguments.callee.caller;
return current.codeRef;
};
op.callercode = function() {
var current = arguments.callee.caller;
/* Skip all fake first _ functions so we can skip a real one*/
while (current.name == '_') {
current = current.caller;
}
/* Skip a real function */
current = current.caller;
/* Skip all fake _ functions to get to a real one*/
while (current.name == '_') {
current = current.caller;
}
return current.codeRef;
};
// TODO benchmark and pick a fast way of doing this
op.splice = function(target, source, offset, length) {
var args = [offset, length];
for (var i = 0; i < source.length; i++) {
args.push(source[i]);
}
target.splice.apply(target, args);
return target;
};
op.findmethod = function(obj, method) {
return obj._STable.method_cache[method];
};
op.istype = function(obj, type) {
/* Null always type checks false. */
/* HACK - undefined */
if (obj === null || obj === undefined) {
return 0;
}
// HACK
if (typeof obj === 'number' || typeof obj === 'string' || obj instanceof Array || obj instanceof Hash || obj instanceof NQPInt) {
return 0;
}
// TODO cases where the type_check_cache isn't authoritative
var cache = obj._STable.type_check_cache;
for (var i = 0; i < cache.length; i++) {
if (cache[i] === type) {
return 1;
}
}
return 0;
};
op.settypecache = function(obj, cache) {
obj._STable.type_check_cache = cache;
return obj;
};
op.setmethcache = function(obj, cache) {
if (!cache instanceof Hash) {
console.log('we expect a hash here');
}
obj._STable.setMethodCache(cache.content);
return obj;
};
op.setmethcacheauth = function(obj, isAuth) {
/* TODO we currently assume method caches are always authorative
sadly that's not always the case*/
return obj;
};
op.reprname = function(obj) {
return obj._STable.REPR.name;
};
op.newtype = function(how, repr) {
if (!reprs[repr]) {
throw 'Unknown REPR: ' + repr;
}
var REPR = new reprs[repr]();
REPR.name = repr;
return REPR.type_object_for(how);
};
op.can = function(obj, method) {
return obj._STable.method_cache.hasOwnProperty(method) ? 1 : 0;
};
op.getcodename = function(code) {
return code.name;
};
op.setcodename = function(code, name) {
code.name = name;
};
op.rebless = function(obj, new_type) {
obj._STable.REPR.change_type(obj, new_type);
return obj;
};
op.composetype = function(obj, reprinfo) {
obj._STable.REPR.compose(obj._STable, reprinfo);
};
op.clone = function(obj) {
if (obj.$$clone) {
return obj.$$clone();
} else if (obj instanceof Array) {
return obj.slice();
} else {
// STUB
console.log('NYI cloning', obj);
return obj;
}
};
var where_counter = 0;
op.where = function(obj) {
if (obj._STable) { // HACK
if (!obj._WHERE) {
obj._WHERE = ++where_counter;
}
return obj._WHERE;
} else {
throw 'WHERE on this type of thing unimplemented';
}
};
/* HACK - take the current HLL settings into regard */
var hllsyms = {}
op.bindcurhllsym = function(name, value) {
hllsyms[name] = value;
return value;
};
op.getcurhllsym = function(name) {
return hllsyms.hasOwnProperty(name) ? hllsyms[name] : null;
};
op.settypehllrole = function(type, role) {
/* STUB */
return role;
};
op.sethllconfig = function(language, configHash) {
/* STUB */
return configHash;
};
var sha1 = require('sha1');
op.sha1 = function(text) {
return sha1(text).toUpperCase();
};
op.curlexpad = function(get, set) {
return new CurLexpad(get, set);
};
op.setcontspec = function(type, cont_spec_type, hash) {
var fetch = hash.content.fetch;
var store = hash.content.store;
if (cont_spec_type === 'code_pair') {
type._STable.addInternalMethod('$$assignunchecked', function(ctx, value) {
store.$call(ctx, {}, this, value);
return value;
});
type._STable.addInternalMethod('$$assign', function(ctx, value) {
store.$call(ctx, {}, this, value);
return value;
});
type._STable.addInternalMethod('$$decont', function(ctx) {
return fetch.$call(ctx, {}, this);
});
} else {
throw 'NYI cont spec: ' + cont_spec_type;
}
};
op.iscont = function(cont) {
return cont.$$decont ? 1 : 0;
};
op.decont = function(ctx, cont) {
return cont.$$decont ? cont.$$decont(ctx) : cont;
};
op.box_n = function(n, type) {
var repr = type._STable.REPR;
var obj = repr.allocate(type._STable);
obj.$$set_num(n);
return obj;
};
op.unbox_n = function(obj) {
return obj.$$get_num();
};
op.box_s = function(value, type) {
var repr = type._STable.REPR;
var obj = repr.allocate(type._STable);
obj.$$set_str(value);
return obj;
};
op.unbox_s = function(obj) {
return obj.$$get_str();
};
op.elems = function(obj) {
if (obj instanceof Array) {
return obj.length;
} else if (obj.$$elems) {
return obj.$$elems();
}
};
op.markcodestatic = function(code) {
code.isStatic = true;
return code;
};
op.markcodestub = function(code) {
code.isCompilerStub = true;
return code;
};
op.freshcoderef = function(code) {
// TODO - think about static code info
return code.$$clone();
};
/* TODO - make serialization take this into account */
var compilingSCs = [];
op.pushcompsc = function(sc) {
compilingSCs.push(sc);
return sc;
};
op.popcompsc = function(sc) {
return compilingSCs.pop();
};
var compilerRegistry = {};
op.bindcomp = function(language, compiler) {
return (compilerRegistry[language] = compiler);
};
compilerRegistry['JavaScript'] = {
eval: function(ctx, named, code) {
return eval(code);
}
};
compilerRegistry['nqp'] = {
backend: function(ctx, named) {
return {
name: function(ctx, named) {
return 'js';
}
};
}
};
op.getcomp = function(language) {
return compilerRegistry[language];
};
op.backendconfig = function() {
var config = new Hash();
config.content.intvalsize = 4;
return config;
};
op.ordbaseat = function(str, index) {
throw 'ordbaseat NYI';
};
op.getpid = function() {
return process.pid;
};
op.getmessage = function(exception) {
return exception.msg;
};
op.unshift = function(target, value) {
if (target.$$unshift) return target.$$unshift(value);
return target.unshift(value);
};
op.isnum = function(value) {
return (typeof value == "number") ? 1 : 0;
};
op.isint = function(value) {
return (value instanceof NQPInt) ? 1 : 0;
};
function renameEncoding(encoding) {
return encoding == 'utf16' ? 'utf16le' : encoding;
}
function byteSize(buf) {
if (buf.bytes) return buf.bytes;
var bits = buf._STable.REPR.type._STable.REPR.bits;
if (bits % 8) {
throw "only buffer sizes that are a multiple of 8 are supported";
}
return bits / 8;
}
// HACK should be using buf instead of creating a new one
// TODO needs to be fixed after an Array handling refactor
op.encode = function(str, encoding_, buf) {
var encoding = renameEncoding(encoding_);
var elementSize = byteSize(buf);
var ret = [];
ret.bytes = elementSize;
var buffer = new Buffer(str, encoding);
var offset = 0;
for (var i=0; i < buffer.length / elementSize; i++) {
ret[i] = buffer.readIntLE(offset, elementSize);
offset += elementSize;
}
return ret;
};
op.decode = function(buf, encoding_) {
var encoding = renameEncoding(encoding_);
var elementSize = byteSize(buf);
var buffer = new Buffer(buf.length * elementSize);
var offset = 0;
for (var i=0; i < buf.length; i++) {
buffer.writeIntLE(buf[i], offset, elementSize);
offset += elementSize;
}
return buffer.toString(encoding);
};
op.objprimspec = function(obj) {
return (obj._STable && obj._STable.REPR.boxed_primitive ? obj._STable.REPR.boxed_primitive : 0);
};
/* Parametricity operations. */
op.setparameterizer = function(ctx, type, parameterizer) {
var st = type._STable;
/* Ensure that the type is not already parametric or parameterized. */
if (st.parameterizer) {
ctx.die("This type is already parametric");
return null;
} else if (st.parametricType) {
ctx.die("Cannot make a parameterized type also be parametric");
return null;
}
/* Set up the type as parameterized. */
st.parameterizer = parameterizer;
st.parameterizerCache = [];
return type;
};
op.parameterizetype = function(ctx, type, params) {
/* Ensure we have a parametric type. */
var st = type._STable;
if (!st.parameterizer) {
ctx.die("This type is not parametric");
}
var lookup = st.parameterizerCache;
for (var i = 0; i < lookup.length; i++) {
if (params.length == lookup[i].params.length) {
var match = true;
for (var j=0; j < params.length; j++) {
/* XXX More cases to consider here. - copied over from the jvm backend, need to consider what they are*/
if (params[j] != lookup[i].params[j]) {
match = false;
break;
}
}
if (match) {
return lookup[i].type;
}
}
}
var result = st.parameterizer.$call(ctx, {}, st.WHAT, params);
var newSTable = result._STable;
newSTable.parametricType = type;
newSTable.parameters = params;
lookup.push({type: result, params: params});
return result;
};
function typeparameters(ctx, type) {
var st = type._STable;
if (!st.parametricType) {
ctx.die("This type is not parameterized");
}
return st.parameters;
}
op.typeparameters = typeparameters;
op.typeparameterat = function(ctx, type, idx) {
return typeparameters(ctx, type)[idx];
};
op.typeparameterized = function(type) {
var st = type._STable;
var nqp = require('nqp-runtime');
return st.parametricType ? st.parametricType : null;
};
function startTrampoline(thunk_) {
var thunk = thunk_;
while (thunk) {
thunk = thunk();
}
console.log("ended trampoline");
};
var continuationValue;
op.continuationreset = function(ctx, tag, continuation) {
startTrampoline(function() {
continuation.$callCPS(ctx, {}, function(value) {
continuationValue = value;
});
});
};
op.continuationcontrol = function(ctx, protect, tag, run, cont) {
run.$callCPS(ctx, {}, cont, function() {
console.log("hmm what should we do");
});
return null;
};
op.continuationinvoke = function(ctx, cont, inject) {
// TODO inject
startTrampoline(cont);
return continuationValue;
};