-
Notifications
You must be signed in to change notification settings - Fork 63
/
runtime.js
3318 lines (2746 loc) · 72.9 KB
/
runtime.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* Higgs JavaScript Virtual Machine
*
* This file is part of the Higgs project. The project is distributed at:
* https://github.com/maximecb/Higgs
*
* Copyright (c) 2012-2015, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
/***
Undefined value
*/
$ir_obj_def_const(this, 'undefined', $undef, false);
/**
Not-a-number value
*/
$ir_obj_def_const(this, 'NaN', $ir_div_f64(0.0, 0.0), false);
/**
Infinity value
*/
$ir_obj_def_const(this, 'Infinity', $ir_div_f64(1.0, 0.0), false);
/**
Test if a value is NaN
*/
function isNaN(v)
{
if ($ir_is_float64(v))
return $ir_ne_f64(v,v);
var n = $rt_toNumber(v);
return ($ir_is_float64(n) && $ir_ne_f64(n, n));
}
/**
Load and execute a source file
*/
function load(fileName)
{
if (!$ir_is_string(fileName) && !$ir_is_rope(fileName))
throw TypeError("expected string for file name argument");
return $ir_load_file($rt_toString(fileName));
}
/**
Evaluate a source string in the global scope
*/
function eval(input)
{
if ($ir_is_string(input) || $ir_is_rope(input))
return $ir_eval_str($rt_toString(input));
return input;
}
/**
Print a value to the console
*/
function print()
{
// For each argument
for (var i = 0; i < $argc; ++i)
{
var arg = $ir_get_arg(i);
// Convert the value to a string if it isn't one
if (!$ir_is_string(arg))
arg = $rt_toString(arg);
// Print the string
$ir_print_str(arg);
// If this is not the last argument, print a space
if ($ir_lt_i32($ir_add_i32(i, 1), $argc))
$ir_print_str(' ');
}
// Print a final newline
$ir_print_str('\n');
}
/**
Perform an assertion test
*/
function assert(testVal, errorMsg)
{
// If testVal === true, do nothing
if ($ir_is_bool(testVal) && $ir_eq_bool(testVal, true))
return;
// If no error message is specified
if ($argc < 2)
errorMsg = 'assertion failed';
// If the global Error object exists
if (this.Error !== $undef)
throw Error(errorMsg);
// Throw the error message as-is
$ir_throw(errorMsg);
}
/**
Throw an exception value
Note: this primitive makes exception handling simpler as the
throw instruction will always unwind at least one stack frame.
*/
function $rt_throwExc(excVal)
{
$ir_throw(excVal);
}
/**
Test if a value is an object
*/
function $rt_valIsObj(val)
{
return ($ir_is_object(val) || $ir_is_array(val) || $ir_is_closure(val));
}
/**
Test if a value is the global object
*/
function $rt_isGlobalObj(val)
{
return $ir_is_object(val) && $ir_eq_refptr(val, $global);
}
/**
Allocate and initialize a closure cell
*/
function $rt_makeClosCell()
{
var cell = $rt_cell_alloc();
return cell;
}
/**
Set the value stored in a closure cell
*/
function $rt_setCellVal(cell, val)
{
var word = $ir_get_word(val);
var type = $ir_get_tag(val);
$rt_cell_set_word(cell, word);
$rt_cell_set_tag(cell, type);
}
/**
Get the value stored in a closure cell
*/
function $rt_getCellVal(cell)
{
var word = $rt_cell_get_word(cell);
var type = $rt_cell_get_tag(cell);
//print('getCellVal: ' + $ir_make_value(word, 0));
return $ir_make_value(word, type);
}
/**
Concatenate the strings from two string objects
*/
function $rt_strcat(strA, strB)
{
// Get the length of both strings
var lenA = $rt_str_get_len(strA);
var lenB = $rt_str_get_len(strB);
// Allocate a string object
var lenO = $ir_add_i32(lenA, lenB);
var strO = $rt_str_alloc(lenO);
// Output pointer
var dataO = $ir_add_ptr_i32(strO, $rt_str_ofs_data(strO, 0));
// A string pointers
var dataA = $ir_add_ptr_i32(strA, $rt_str_ofs_data(strA, 0));
var endA = $ir_add_ptr_i32(dataA, $ir_lsft_i32($ir_rsft_i32(lenA, 2), 3));
// 8 by 8 copy
while ($ir_ne_rawptr(dataA, endA))
{
$ir_store_u64(dataO, 0, $ir_load_u64(dataA, 0));
dataA = $ir_add_ptr_i32(dataA, 8);
dataO = $ir_add_ptr_i32(dataO, 8);
}
var remA = $ir_and_i32(lenA, 3);
// Tail remainder copy
switch (remA)
{
case 3: $ir_store_u16(dataO, 4, $ir_load_u64(dataA, 4));
case 2: $ir_store_u16(dataO, 2, $ir_load_u64(dataA, 2));
case 1: $ir_store_u16(dataO, 0, $ir_load_u64(dataA, 0));
};
dataO = $ir_add_ptr_i32(dataO, $ir_lsft_i32(remA, 1));
// B string pointers
var dataB = $ir_add_ptr_i32(strB, $rt_str_ofs_data(strB, 0));
var endB = $ir_add_ptr_i32(dataB, $ir_lsft_i32($ir_rsft_i32(lenB, 2), 3));
// 8 by 8 copy
while ($ir_ne_rawptr(dataB, endB))
{
$ir_store_u64(dataO, 0, $ir_load_u64(dataB, 0));
dataB = $ir_add_ptr_i32(dataB, 8);
dataO = $ir_add_ptr_i32(dataO, 8);
}
var remB = $ir_and_i32(lenB, 3);
// Tail remainder copy
switch (remB)
{
case 3: $ir_store_u16(dataO, 4, $ir_load_u64(dataB, 4));
case 2: $ir_store_u16(dataO, 2, $ir_load_u64(dataB, 2));
case 1: $ir_store_u16(dataO, 0, $ir_load_u64(dataB, 0));
};
// Find/add the concatenated string in the string table
return $ir_get_str(strO);
}
/**
Compare two string objects lexicographically by iterating over UTF-16
code units. This conforms to section 11.8.5 of the ECMAScript 262
specification.
*/
function $rt_strcmp(strA, strB)
{
// Get the length of both strings
var lenA = $rt_str_get_len(strA);
var lenB = $rt_str_get_len(strB);
// Compute the minimum of both string lengths
var minLen = $ir_lt_i32(lenA, lenB)? lenA:lenB;
// For each character to be compared
for (var i = 0; $ir_lt_i32(i, minLen); i = $ir_add_i32(i, 1))
{
var ch1 = $rt_str_get_data(strA, i);
var ch2 = $rt_str_get_data(strB, i);
if ($ir_lt_i32(ch1, ch2))
return -1;
if ($ir_gt_i32(ch1, ch2))
return 1;
}
if ($ir_lt_i32(lenA, lenB))
return -1;
if ($ir_gt_i32(lenB, lenA))
return 1;
return 0;
}
/**
Compute the integer value of a string
*/
function $rt_strToInt(strVal)
{
// TODO: add radix support
var strLen = $rt_str_get_len(strVal);
var intVal = 0;
var neg = false;
var state = 'PREWS';
// For each string character
for (var i = 0; $ir_lt_i32(i, strLen);)
{
var ch = $rt_str_get_data(strVal, i);
if ($ir_eq_refptr(state, 'PREWS'))
{
// Space or tab
if ($ir_eq_i32(ch, 32) || $ir_eq_i32(ch, 9))
{
i = $ir_add_i32(i, 1);
}
// + or -
else if ($ir_eq_i32(ch, 43) || $ir_eq_i32(ch, 45))
{
state = 'SIGN';
}
// Any other character
else
{
state = 'DIGITS';
}
}
else if ($ir_eq_refptr(state, 'SIGN'))
{
// Plus sign
if ($ir_eq_i32(ch, 43))
{
i = $ir_add_i32(i, 1);
}
// Minus sign
else if ($ir_eq_i32(ch, 45))
{
neg = true;
i = $ir_add_i32(i, 1);
}
state = 'DIGITS';
}
else if ($ir_eq_refptr(state, 'DIGITS'))
{
// If this is not a digit
if ($ir_lt_i32(ch, 48) || $ir_gt_i32(ch, 57))
{
state = 'POSTWS';
continue;
}
var digit = ch - 48;
intVal = 10 * intVal + digit;
i = $ir_add_i32(i, 1);
}
else if ($ir_eq_refptr(state, 'POSTWS'))
{
// If this is not a space or tab
if ($ir_ne_i32(ch, 32) && $ir_ne_i32(ch, 9))
{
// Invalid number
return NaN;
}
i = $ir_add_i32(i, 1);
}
else
{
throw "invalid state";
}
}
if ($ir_eq_bool(neg, true))
intVal *= -1;
return intVal;
}
/**
Create a string representing an integer value
*/
function $rt_intToStr(intVal, radix)
{
assert (
$ir_is_int32(radix) &&
$ir_gt_i32(radix, 0) &&
$ir_le_i32(radix, 36),
'invalid radix'
);
var strLen;
var neg;
// If the integer is negative, adjust the string length for the minus sign
if (intVal < 0)
{
strLen = 1;
intVal *= -1;
neg = true;
}
else
{
strLen = 0;
neg = false;
}
// Compute the number of digits to add to the string length
var intVal2 = intVal;
do
{
strLen = $ir_add_i32(strLen, 1);
intVal2 = $ir_div_i32(intVal2, radix);
} while ($ir_ne_i32(intVal2, 0));
// Allocate a string object
var strObj = $rt_str_alloc(strLen);
// If the string is negative, write the minus sign
if ($ir_eq_bool(neg, true))
{
$rt_str_set_data(strObj, 0, 45);
}
var digits = '0123456789abcdefghijklmnopqrstuvwxyz';
// Write the digits in the string
var i = $ir_sub_i32(strLen, 1);
do
{
var digit = $ir_mod_i32(intVal, radix);
var ch = $rt_str_get_data(digits, digit);
$rt_str_set_data(strObj, i, ch);
intVal = $ir_div_i32(intVal, radix);
i = $ir_sub_i32(i, 1);
} while ($ir_ne_i32(intVal, 0));
// Get the corresponding string from the string table
return $ir_get_str(strObj);
}
/**
Convert number to string
*/
function $rt_numToStr(v, radix)
{
if (!$ir_is_int32(radix))
{
radix = 10;
}
if ($ir_lt_i32(radix, 2) || $ir_gt_i32(radix, 36))
{
throw RangeError("radix is not between 2 and 36");
}
if ($ir_is_int32(v))
{
return $rt_intToStr(v, radix);
}
// NaN
if ($ir_ne_f64(v, v))
return "NaN";
if ($ir_eq_f64(v, Infinity))
return "Infinity";
if ($ir_eq_f64(v, -Infinity))
return "-Infinity";
return $ir_f64_to_str(v);
}
/**
Inlined rope to string conversion with cache check
*/
function $rt_ropeToStr(rope)
{
// Get the right-hand string
var rightStr = $rt_rope_get_right(rope);
// If this rope was already converted to a string
if ($ir_eq_refptr(rightStr, null))
{
return $ir_load_string(rope, $rt_rope_ofs_left(rope));
}
return $rt_concatRope(rope, rightStr);
}
/**
Convert a rope to a string by concatenation
*/
function $rt_concatRope(rope, rightStr)
{
var ropeLen = $rt_rope_get_len(rope);
// Allocate a string object for the output
var dstStr = $rt_str_alloc(ropeLen);
// Output string data pointer
var dataO = $ir_add_ptr_i32(dstStr, $rt_str_ofs_data(null, 0));
var idxO = $ir_lsft_i32(ropeLen, 1);
// Until we are done traversing the ropes
for (var curRope = rope;;)
{
// The right-hand node must be a string
var rightLen = $rt_str_get_len(rightStr);
// Right string data pointers
var dataI = $ir_add_ptr_i32(rightStr, $rt_str_ofs_data(null, 0));
var idxI = $ir_lsft_i32(rightLen, 1);
// Copy the string characters
while ($ir_ne_i32(idxI, 0))
{
idxI = $ir_sub_i32(idxI, 2);
idxO = $ir_sub_i32(idxO, 2);
$ir_store_u16(dataO, idxO, $ir_load_u16(dataI, idxI));
}
// Move to the next rope
curRope = $rt_rope_get_left(curRope);
// If this is the last string in the chain, stop
if ($ir_eq_i32($rt_rope_get_header(curRope), $rt_LAYOUT_STR))
{
var leftStr = curRope;
break;
}
// Get the right-hand string for the current rope
rightStr = $rt_rope_get_right(curRope);
// If the rope was already converted to a string
if ($ir_eq_refptr(rightStr, null))
{
var leftStr = $rt_rope_get_left(curRope);
break;
}
}
// Copy the last string
var leftLen = $rt_str_get_len(leftStr);
// Left string data pointers
var dataI = $ir_add_ptr_i32(leftStr, $rt_str_ofs_data(null, 0));
var idxI = $ir_lsft_i32(leftLen, 1);
// Copy the string characters
while ($ir_ne_i32(idxI, 0))
{
idxI = $ir_sub_i32(idxI, 2);
idxO = $ir_sub_i32(idxO, 2);
$ir_store_u16(dataO, idxO, $ir_load_u16(dataI, idxI));
}
// Get the corresponding string from the string table
dstStr = $ir_get_str(dstStr);
// Cache the concatenated string in the original rope
$rt_rope_set_left(rope, dstStr);
$rt_rope_set_right(rope, null);
return dstStr;
}
/**
Get the string representation of a value
Note: this function returns plain strings only, no ropes
*/
function $rt_toString(v)
{
if ($rt_valIsObj(v))
{
var str = v.toString();
if ($ir_is_string(str))
return str;
if ($rt_valIsObj(str))
throw TypeError('toString produced non-primitive value');
return $rt_toString(str);
}
if ($ir_is_int32(v))
{
return $rt_intToStr(v, 10);
}
if ($ir_is_float64(v))
{
return $rt_numToStr(v, 10);
}
if ($ir_is_string(v))
{
return v;
}
if ($ir_is_rope(v))
{
return $rt_ropeToStr(v);
}
if ($ir_is_undef(v))
{
return "undefined";
}
if ($ir_is_null(v))
{
return "null";
}
if ($ir_is_bool(v))
{
return $ir_eq_bool(v, true)? "true":"false";
}
assert (false, "unhandled type in toString");
}
/**
http://www.ecma-international.org/ecma-262/5.1/#sec-9.9
*/
function $rt_toObject(arg)
{
if (arg === null || arg === undefined)
throw new TypeError("Cannot be null or undefined");
switch (typeof(arg))
{
case 'boolean': return new Boolean(arg);
case 'number' : return new Number(arg);
case 'string' : return new String(arg);
}
return arg;
}
/**
Convert any value to a primitive value
*/
function $rt_toPrim(v)
{
if ($ir_is_int32(v) || $ir_is_float64(v))
return v
if ($ir_is_undef(v))
return v;
if ($ir_is_null(v))
return v;
if ($ir_is_bool(v))
return v;
if ($ir_is_string(v))
return v;
if ($ir_is_rope(v))
return $rt_ropeToStr(v);
if ($rt_valIsObj(v))
{
var str = v.toString();
if ($rt_valIsObj(str))
throw TypeError('toString produced non-primitive value');
if ($ir_is_rope(str))
return $rt_ropeToStr(str);
return str;
}
throw TypeError('unexpected type in toPrimitive');
}
/**
Evaluate a value as a boolean
*/
function $rt_toBool(v)
{
if ($ir_is_bool(v))
return $ir_eq_bool(v, true);
if ($ir_is_int32(v))
return $ir_ne_i32(v, 0);
if ($ir_is_float64(v))
return $ir_ne_f64(v, 0.0) && $ir_eq_f64(v, v);
if ($ir_is_string(v))
return $ir_gt_i32($rt_str_get_len(v), 0);
if ($ir_is_object(v) || $ir_is_array(v) || $ir_is_closure(v))
return true;
if ($ir_is_rawptr(v))
return $ir_ne_rawptr(v, $nullptr);
return false;
}
/**
Attempt to convert a value to a number. If this fails, return NaN
Note: toNumber(undefined) is NaN according to the ES5 specification
*/
function $rt_toNumber(v)
{
if ($ir_is_int32(v) || $ir_is_float64(v))
return v;
if ($ir_is_null(v))
return 0;
if ($ir_is_bool(v))
return $ir_eq_bool(v, true)? 1:0;
if ($ir_is_string(v))
return $rt_strToInt(v);
if ($rt_valIsObj(v))
return $rt_toNumber($rt_toString(v));
return NaN;
}
/**
http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
*/
function $rt_toInteger(val)
{
var number = $rt_toNumber(val);
// NaN
if (number !== number) return +0;
// -0, +0, -Infinity, Infinity
if (number === 0 ||
number === Infinity ||
number === -Infinity)
{
return number;
}
var sign = number < 0 ? -1 : 1;
return sign * Math.floor(Math.abs(number));
}
/**
Convert any value to a signed 32-bit integer
*/
function $rt_toInt32(x)
{
x = $rt_toNumber(x);
if ($ir_is_int32(x))
return x;
// NaN or infinity
if ($ir_ne_f64(x, x) ||
$ir_eq_f64(x, Infinity) ||
$ir_eq_f64(x, -Infinity))
return 0;
return $ir_f64_to_i32(x);
}
/**
Convert any value to an unsigned 32-bit integer
*/
function $rt_toUint32(x)
{
x = $rt_toNumber(x);
if ($ir_is_int32(x))
return x;
// NaN or infinity
if ($ir_ne_f64(x, x) ||
$ir_eq_f64(x, Infinity) ||
$ir_eq_f64(x, -Infinity))
return 0;
if ($ir_ge_i32(x, 0.0))
return $ir_f64_to_i32(x);
else
return $ir_f64_to_i32($ir_sub_f64(0.0, x));
}
/**
JS typeof operator
*/
function $rt_typeof(v)
{
if ($ir_is_int32(v) || $ir_is_float64(v))
return "number";
if ($ir_is_undef(v))
return "undefined";
if ($ir_is_null(v))
return "object";
if ($ir_is_bool(v))
return "boolean";
if ($ir_is_object(v) || $ir_is_array(v))
return "object";
if ($ir_is_closure(v))
return "function";
if ($ir_is_string(v) || $ir_is_rope(v))
return "string";
if ($ir_is_rawptr(v))
return "rawptr";
throw TypeError("unhandled type in typeof");
}
//=============================================================================
// Arithmetic operators
//=============================================================================
/**
JS unary plus (+) operator
*/
function $rt_plus(x)
{
// If x is integer
if ($ir_is_int32(x))
{
return x;
}
// If x is floating-point
else if ($ir_is_float64(x))
{
return x;
}
return $rt_toNumber(x);
}
/**
JS unary minus (-) operator
*/
function $rt_minus(x)
{
// If x is integer
if ($ir_is_int32(x))
{
if ($ir_eq_i32(x, 0))
return -0;
return $ir_sub_i32(0, x);
}
// If x is floating-point
else if ($ir_is_float64(x))
{
if ($ir_eq_f64(x, 0.0))
return -0;
return $ir_sub_f64(0.0, x);
}
return -1 * $rt_toNumber(x);
}
/**
JS addition operator
*/
function $rt_add(x, y)
{
// If x is integer
if ($ir_is_int32(x))
{
if ($ir_is_int32(y))
{
var r;
if (r = $ir_add_i32_ovf(x, y))
{
return r;
}
else
{
// Handle the overflow case
var fx = $ir_i32_to_f64(x);
var fy = $ir_i32_to_f64(y);
return $ir_add_f64(fx, fy);
}
}
if ($ir_is_float64(y))
return $ir_add_f64($ir_i32_to_f64(x), y);
}
// If x is floating-point
else if ($ir_is_float64(x))
{
if ($ir_is_int32(y))
return $ir_add_f64(x, $ir_i32_to_f64(y));
if ($ir_is_float64(y))
return $ir_add_f64(x, y);
}
// If x is a string
else if ($ir_is_string(x))
{
if ($ir_is_string(y))
{
var rope = $rt_rope_alloc();
var len = $ir_add_i32($rt_str_get_len(x), $rt_str_get_len(y));
$rt_rope_set_left(rope, x);
$rt_rope_set_right(rope, y);
$rt_rope_set_len(rope, len);
return rope;
}
if ($ir_is_int32(y) || $ir_is_float64(y))
{
y = $rt_numToStr(y, 10);
var rope = $rt_rope_alloc();
var len = $ir_add_i32($rt_str_get_len(x), $rt_str_get_len(y));
$rt_rope_set_left(rope, x);
$rt_rope_set_right(rope, y);
$rt_rope_set_len(rope, len);
return rope;
}
}
// If x is a rope
else if ($ir_is_rope(x))
{
var sy = $ir_is_string(y)? y:$rt_toString(y);
var rope = $rt_rope_alloc();
var len = $ir_add_i32($rt_rope_get_len(x), $rt_str_get_len(sy));
$rt_rope_set_left(rope, x);
$rt_rope_set_right(rope, sy);
$rt_rope_set_len(rope, len);
return rope;
}
// Convert x and y to primitives
var px = $rt_toPrim(x);
var py = $rt_toPrim(y);
// If x is a string
if ($ir_is_string(px))
{
if ($ir_is_string(py))
return $rt_strcat(px, py);
return $rt_strcat(px, $rt_toString(py));
}
// If y is a string
if ($ir_is_string(py))
{
return $rt_strcat($rt_toString(x), py);
}
// Convert both values to numbers and add them
return $rt_add($rt_toNumber(x), $rt_toNumber(y));
}
/**
Specialized add for the (int,int) and (float,float) cases
*/
function $rt_addIntFloat(x, y)
{
// If x is integer
if ($ir_is_int32(x))
{
if ($ir_is_int32(y))
{
var r;
if (r = $ir_add_i32_ovf(x, y))