forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.js
1895 lines (1507 loc) · 43.7 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
opal = {};
(function() {
// So we can minimize
var Op = opal;
/**
All methods and properties available to ruby/js sources at runtime. These
are kept in their own namespace to keep the opal namespace clean.
*/
Op.runtime = {};
// for minimizng
var Rt = Op.runtime;
Rt.opal = Op;
/**
Opal platform - this is overriden in gem context and nodejs context. These
are the default values used in the browser, `opal-browser'.
*/
Op.platform = {
platform: "opal",
engine: "opal-browser",
version: "1.9.2",
argv: []
};
/**
Core runtime classes, objects and literals.
*/
var cBasicObject, cObject, cModule, cClass,
mKernel, cNilClass, cTrueClass, cFalseClass,
cArray,
cRegexp, cMatch, top_self, Qnil,
Qfalse, Qtrue,
cDir;
/**
Core object type flags. Added as local variables, and onto runtime.
*/
var T_CLASS = Rt.T_CLASS = 1,
T_MODULE = Rt.T_MODULE = 2,
T_OBJECT = Rt.T_OBJECT = 4,
T_BOOLEAN = Rt.T_BOOLEAN = 8,
T_STRING = Rt.T_STRING = 16,
T_ARRAY = Rt.T_ARRAY = 32,
T_NUMBER = Rt.T_NUMBER = 64,
T_PROC = Rt.T_PROC = 128,
T_SYMBOL = Rt.T_SYMBOL = 256,
T_HASH = Rt.T_HASH = 512,
T_RANGE = Rt.T_RANGE = 1024,
T_ICLASS = Rt.T_ICLASS = 2056,
FL_SINGLETON = Rt.FL_SINGLETON = 4112;
/**
Define methods. Public method for defining a method on the given base.
@param {RubyObject} base The base to define method on
@param {String} method_id Ruby mid
@param {Function} body The method implementation
@param {Boolean} singleton Singleton or Normal method. true for singleton
*/
Rt.dm = function(base, method_id, body, singleton) {
if (singleton) {
define_singleton_method(base, method_id, body);
} else {
// should this instead do a rb_singleton_method?? probably..
if (base.$flags & T_OBJECT) {
base = base.$klass;
}
define_method(base, method_id, body);
}
return Qnil;
};
/**
Define classes. This is the public API for defining classes, shift classes
and modules.
@param {RubyObject} base
@param {RClass} super_class
@param {String} id
@param {Function} body
@param {Number} flag
*/
Rt.dc = function(base, super_class, id, body, flag) {
var klass;
switch (flag) {
case 0:
if (base.$flags & T_OBJECT) {
base = class_real(base.$klass);
}
if (super_class == Qnil) {
super_class = cObject;
}
klass = define_class_under(base, id, super_class);
break;
case 1:
klass = singleton_class(base);
break;
case 2:
if (base.$flags & T_OBJECT) {
base = class_real(base.$klass);
}
klass = define_module_under(base, id);
break;
default:
raise(eException, "define_class got a unknown flag " + flag);
}
var res = body(klass);
return res;
};
/**
Regexp object. This holds the results of last regexp match.
X for regeXp.
*/
Rt.X = null;
/**
Undefine methods
*/
Rt.um = function(kls) {
var args = [].slice.call(arguments, 1);
for (var i = 0, ii = args.length; i < ii; i++) {
(function(mid) {
var func = function() {
raise(eNoMethodError, "undefined method `" + mid + "' for " + this.m$inspect());
};
kls.allocator.prototype['m$' + mid] = func;
if (kls.$bridge_prototype) {
kls.$bridge_prototype['m$' + mid] = func;
}
})(args[i].m$to_s());
}
return Qnil;
};
/**
Method missing support - used in debug mode (opt in).
*/
Rt.mm = function(method_ids) {
var prototype = boot_base_class.prototype;
for (var i = 0, ii = method_ids.length; i < ii; i++) {
var mid = 'm$' + method_ids[i];
if (!prototype[mid]) {
var imp = (function(mid, method_id) {
return function() {
var args = [].slice.call(arguments, 0);
args.unshift(Rt.Y(method_id));
return this.m$method_missing.apply(this, args);
};
})(mid, method_ids[i]);
imp.$rbMM = true;
prototype[mid] = imp;
}
}
};
/**
Debug support for checking argument counts. This is called when a method
did not receive the right number of args as expected.
*/
Rt.ac = function(expected, actual) {
throw new Error("ArgumentError - wrong number of arguments(" + actual + " for " + expected + ")");
};
/**
Sets the constant value `val` on the given `klass` as `id`.
@param {RClass} klass
@param {String} id
@param {Object} val
@return {Object} returns the set value
*/
function const_set(klass, id, val) {
// klass.$c_prototype[id] = val;
// klass.$const_table[id] = val;
klass.$c[id] = val;
return val;
}
/**
Lookup a constant named `id` on the `klass`. This will throw an error if
the constant cannot be found.
@param {RClass} klass
@param {String} id
*/
function const_get(klass, id) {
if (klass.$c[id]) {
return (klass.$c[id]);
}
var parent = klass.$parent;
while (parent && parent != cObject) {
// while (parent) {
if (parent.$c[id] !== undefined) {
return parent.$c[id];
}
parent = parent.$parent;
}
raise(eNameError, 'uninitialized constant ' + id);
};
Rt.const_get = const_get;
/**
Returns true or false depending whether a constant named `id` is defined
on the receiver `klass`.
@param {RClass} klass
@param {String} id
@return {true, false}
*/
function const_defined(klass, id) {
if (klass.$c[id]) {
return true;
}
return false;
};
/**
Set an instance variable on the receiver.
*/
function ivar_set(obj, id, val) {
obj[id] = val;
return val;
};
/**
Return an instance variable set on the receiver, or nil if one does not
exist.
*/
function ivar_get(obj, id) {
return obj.hasOwnProperty(id) ? obj[id] : Qnil;
};
/**
Determines whether and instance variable has been set on the receiver.
*/
function ivar_defined(obj, id) {
return obj.hasOwnProperty(id) ? true : false;
};
/**
This table holds all the global variables accessible from ruby.
Entries are mapped by their global id => an object that contains the
given keys:
- name
- value
- getter
- setter
*/
var global_tbl = {};
/**
Defines a hooked/global variable.
@param {String} name The global name (e.g. '$:')
@param {Function} getter The getter function to return the variable
@param {Function} setter The setter function used for setting the var
@return {null}
*/
function define_hooked_variable(name, getter, setter) {
var entry = {
"name": name,
"value": Qnil,
"getter": getter,
"setter": setter
};
global_tbl[name] = entry;
};
/**
A default read only getter for a global variable. This will simply throw a
name error with the given id. This can be used for variables that should
not be altered.
*/
function gvar_readonly_setter(id, value) {
raise(eNameError, id + " is a read-only variable");
};
/**
Retrieve a global variable. This will use the assigned getter.
*/
function gvar_get(id) {
var entry = global_tbl[id];
if (!entry) { return Qnil; }
return entry.getter(id);
};
/**
Set a global. If not already set, then we assign basic getters and setters.
*/
function gvar_set(id, value) {
var entry = global_tbl[id];
if (entry) { return entry.setter(id, value); }
define_hooked_variable(id,
function(id) {
return global_tbl[id].value;
},
function(id, value) {
return (global_tbl[id].value = value);
}
);
return gvar_set(id, value);
};
/**
Every object has a unique id. This count is used as the next id for the
next created object. Therefore, first ruby object has id 0, next has 1 etc.
*/
var hash_yield = 0;
/**
Yield the next object id, updating the count, and returning it.
*/
function yield_hash() {
return hash_yield++;
};
var cHash;
/**
Returns a new hash with values passed from the runtime.
*/
Rt.H = function() {
var hash = new cHash.allocator(), k, v, args = [].slice.call(arguments);
hash.$keys = [];
hash.$assocs = {};
hash.$default = Qnil;
for (var i = 0, ii = args.length; i < ii; i++) {
k = args[i];
v = args[i + 1];
i++;
hash.$keys.push(k);
hash.$assocs[k.$hash()] = v;
}
return hash;
};
/**
Root of all classes and objects (except for bridged).
*/
var boot_base_class = function() {};
boot_base_class.$hash = function() {
return this.$id;
};
boot_base_class.prototype.$r = true;
/**
Internal method for defining a method.
@param {RClass} klass The klass to define the method on
@param {String} name The method id
@param {Function} body Method implementation
@return {Qnil}
*/
function define_method(klass, name, body) {
if (!body.$rbName) {
body.$rbName = name;
}
klass.$methods.push(intern(name));
define_raw_method(klass, 'm$' + name, body);
return Qnil;
};
Rt.define_method = define_method;
var alias_method = Rt.alias_method = function(klass, new_name, old_name) {
var body = klass.allocator.prototype['m$' + old_name];
if (!body) {
throw new Error("NameError: undefined method `" + old_name + "' for class `" + klass.__classid__ + "'");
}
define_raw_method(klass, 'm$' + new_name, body);
return Qnil;
};
/**
This does the main work, but does not call runtime methods like
singleton_method_added etc. define_method does that.
*/
function define_raw_method(klass, name, body) {
klass.allocator.prototype[name] = body;
klass.$method_table[name] = body;
var included_in = klass.$included_in, includee;
if (included_in) {
for (var i = 0, ii = included_in.length; i < ii; i++) {
includee = included_in[i];
define_raw_method(includee, name, body);
}
}
// this class is actually bridged, so add method to bridge native
// prototype as well
if (klass.$bridge_prototype) {
klass.$bridge_prototype[name] = body;
}
// if we are dealing with Object or BasicObject, we need to donate
// to bridged prototypes as well
if (klass == cObject || klass == cBasicObject) {
var bridged = bridged_classes;
for (var i = 0, ii = bridged.length; i < ii; i++) {
// do not overwrite bridged's own implementation
if (!bridged[i][name] || bridged[i][name].$rbMM) {
bridged[i][name] = body;
}
}
}
};
function define_singleton_method(klass, name, body) {
define_method(singleton_class(klass), name, body);
};
function define_alias(base, new_name, old_name) {
define_method(base, new_name, base.$m_tbl[old_name]);
return Qnil;
};
function obj_alloc(klass) {
var result = new klass.allocator();
return result;
};
/**
Raise the exception class with the given string message.
*/
function raise(exc, str) {
if (str === undefined) {
str = exc;
exc = eException;
}
var exception = exc.m$new(str);
raise_exc(exception);
};
Rt.raise = raise;
/**
Raise an exception instance (DO NOT pass strings to this)
*/
function raise_exc(exc) {
throw exc;
};
Rt.raise_exc = raise_exc;
var cString, cSymbol;
/**
Returns a new ruby symbol with the given intern value. Symbols are made
using the new String() constructor, and just have its klass and method
table reassigned. This makes dealing with strings/symbols internally
easier as both can be used as a string within opal.
@param {String} intern Symbol value
@return {RSymbol} symbol
*/
var intern = Rt.Y = function(intern) {
if (symbol_table.hasOwnProperty(intern)) {
return symbol_table[intern];
}
var res = new cSymbol.allocator();
res.$value = intern;
symbol_table[intern] = res;
return res;
};
/**
Call a super method.
callee is the function that actually called super(). We use this to find
the right place in the tree to find the method that actually called super.
This is actually done in super_find.
*/
Rt.S = function(callee, self, args) {
var mid = 'm$' + callee.$rbName;
var func = super_find(self.$klass, callee, mid);
if (!func) {
raise(eNoMethodError, "super: no super class method `" + mid + "`" +
" for " + self.m$inspect());
}
// var args_to_send = [self].concat(args);
var args_to_send = [].concat(args);
return func.apply(self, args_to_send);
};
/**
Actually find super impl to call. Returns null if cannot find it.
*/
function super_find(klass, callee, mid) {
var cur_method;
while (klass) {
if (klass.$method_table[mid]) {
if (klass.$method_table[mid] == callee) {
break;
}
}
klass = klass.$super;
}
if (!klass) { return null; }
klass = klass.$super;
while (klass) {
if (klass.$method_table[mid]) {
return klass.$method_table[mid];
}
klass = klass.$super;
}
return null;
};
/**
Exception classes. Some of these are used by runtime so they are here for
convenience.
*/
var eException, eStandardError, eLocalJumpError, eNameError,
eNoMethodError, eArgError, eScriptError, eLoadError,
eRuntimeError, eTypeError, eIndexError, eKeyError,
eRangeError;
/**
Standard jump exceptions to save re-creating them everytime they are needed
*/
var eReturnInstance,
eBreakInstance,
eNextInstance;
/**
Ruby break statement with the given value. When no break value is needed, nil
should be passed here. An undefined/null value is not valid and will cause an
internal error.
@param {RubyObject} value The break value.
*/
Rt.B = function(value) {
eBreakInstance.$value = value;
throw eBreakInstance;
};
/**
Ruby return, with the given value. The func is the reference function which
represents the method that this statement must return from.
*/
Rt.R = function(value, func) {
eReturnInstance.$value = value;
eReturnInstance.$func = func;
throw eReturnInstance;
};
/**
Get the given constant name from the given base
*/
Rt.cg = function(base, id) {
if (base.$flags & T_OBJECT) {
base = class_real(base.$klass);
}
return const_get(base, id);
};
/**
Set constant from runtime
*/
Rt.cs = function(base, id, val) {
if (base.$flags & T_OBJECT) {
base = class_real(base.$klass);
}
return const_set(base, id, val);
};
/**
Get global by id
*/
Rt.gg = function(id) {
return gvar_get(id);
};
/**
Set global by id
*/
Rt.gs = function(id, value) {
return gvar_set(id, value);
};
function regexp_match_getter(id) {
var matched = Rt.X;
if (matched) {
if (matched.$md) {
return matched.$md;
} else {
var res = new cMatch.allocator();
res.$data = matched;
matched.$md = res;
return res;
}
} else {
return Qnil;
}
}
var cIO, stdin, stdout, stderr;
function stdio_getter(id) {
switch (id) {
case "$stdout":
return stdout;
case "$stdin":
return stdin;
case "$stderr":
return stderr;
default:
raise(eRuntimeError, "stdout_setter being used for bad variable");
}
};
function stdio_setter(id, value) {
raise(eException, "stdio_setter cannot currently set stdio variables");
switch (id) {
case "$stdout":
return stdout = value;
case "$stdin":
return stdin = value;
case "$stderr":
return stderr = value;
default:
raise(eRuntimeError, "stdout_setter being used for bad variable: " + id);
}
};
var cProc;
/**
Block passing - holds current block for runtime
f: function
p: proc
y: yield error
*/
var block = Rt.P = {
f: null,
p: null,
y: function() {
throw new Error("LocalJumpError - no block given");
}
};
block.y.$proc = [block.y];
/**
Turns the given proc/function into a lambda. This is useful for the
Proc#lambda method, but also for blocks that are turned into
methods, in Module#define_method, for example. Lambdas and methods
from blocks are the same thing. Lambdas basically wrap the passed
block function and perform stricter arg checking to make sure the
right number of args are passed. Procs are liberal in their arg
checking, and simply turned ommited args into nil. Lambdas and
methods MUST check args and throw an error if the wrong number are
given. Also, lambdas/methods must catch return statements as lambdas
capture returns.
FIXME: wrap must detect if we are the receiver of a block, and fix
the block to send it to the proc.
FIXME: need to be strict on checking proc arity
FIXME: need to catch return statements which may be thrown.
@param {Function} proc The proc/function to lambdafy.
@return {Function} Wrapped lambda function.
*/
Rt.lambda = function(proc) {
if (proc.$lambda) return proc;
var wrap = function() {
var args = Array.prototype.slice.call(arguments, 0);
return proc.apply(null, args);
};
wrap.$lambda = true;
wrap.$proc = proc.$proc;
return wrap;
};
var cRange;
/**
Returns a new ruby range. G for ranGe.
*/
Rt.G = function(beg, end, exc) {
var range = new RObject(cRange, T_OBJECT);
range.$beg = beg;
range.$end = end;
range.$exc = exc;
return range;
};
/**
Main init method. This is called once this file has fully loaded. It setups
all the core objects and classes and required runtime features.
*/
function init() {
var metaclass;
// what will be the instances of these core classes...
boot_BasicObject = boot_defclass('BasicObject');
boot_Object = boot_defclass('Object', boot_BasicObject);
boot_Module = boot_defclass('Module', boot_Object);
boot_Class = boot_defclass('Class', boot_Module);
// the actual classes
Rt.BasicObject = cBasicObject = boot_makemeta('BasicObject', boot_BasicObject, boot_Class);
Rt.Object = cObject = boot_makemeta('Object', boot_Object, cBasicObject.constructor);
Rt.Module = cModule = boot_makemeta('Module', boot_Module, cObject.constructor);
Rt.Class = cClass = boot_makemeta('Class', boot_Class, cModule.constructor);
boot_defmetameta(cBasicObject, cClass);
boot_defmetameta(cObject, cClass);
boot_defmetameta(cModule, cClass);
boot_defmetameta(cClass, cClass);
// fix superclasses
cBasicObject.$super = null;
cObject.$super = cBasicObject;
cModule.$super = cObject;
cClass.$super = cModule;
const_set(cObject, 'BasicObject', cBasicObject);
const_set(cObject, 'Object', cObject);
const_set(cObject, 'Module', cModule);
const_set(cObject, 'Class', cClass);
mKernel = Rt.Kernel = define_module('Kernel');
top_self = obj_alloc(cObject);
Rt.top = top_self;
cNilClass = define_class('NilClass', cObject);
Rt.Qnil = Qnil = obj_alloc(cNilClass);
Qnil.$r = false;
cTrueClass = define_class('TrueClass', cObject);
Rt.Qtrue = Qtrue = obj_alloc(cTrueClass);
cFalseClass = define_class('FalseClass', cObject);
Rt.Qfalse = Qfalse = obj_alloc(cFalseClass);
Qfalse.$r = false;
cArray = bridge_class(Array.prototype, T_OBJECT | T_ARRAY,
'Array', cObject);
// make all subclasses of array also have standard array js methods
var ary_inst = cArray.allocator.prototype, ary_proto = Array.prototype;
ary_inst.push = ary_proto.push;
ary_inst.pop = ary_proto.pop;
ary_inst.slice = ary_proto.slice;
ary_inst.splice = ary_proto.slice;
ary_inst.concat = ary_proto.concat;
ary_inst.shift = ary_proto.shift;
ary_inst.unshift = ary_proto.unshift;
Array.prototype.$hash = function() {
return this.$id || (this.$id = yield_hash());
};
cNumeric = bridge_class(Number.prototype,
T_OBJECT | T_NUMBER, 'Numeric', cObject);
cHash = define_class('Hash', cObject);
cString = bridge_class(String.prototype,
T_OBJECT | T_STRING, 'String', cObject);
cSymbol = define_class('Symbol', cObject);
cProc = bridge_class(Function.prototype,
T_OBJECT | T_PROC, 'Proc', cObject);
Function.prototype.$hash = function() {
return this.$id || (this.$id = yield_hash());
};
cRange = define_class('Range', cObject);
cRegexp = bridge_class(RegExp.prototype,
T_OBJECT, 'Regexp', cObject);
cMatch = define_class('MatchData', cObject);
define_hooked_variable('$~', regexp_match_getter, gvar_readonly_setter);
eException = bridge_class(Error.prototype, T_OBJECT, 'Exception', cObject);
eStandardError = define_class("StandardError", eException);
eRuntimeError = define_class("RuntimeError", eException);
eLocalJumpError = define_class("LocalJumpError", eStandardError);
Rt.TypeError = eTypeError = define_class("TypeError", eStandardError);
eNameError = define_class("NameError", eStandardError);
eNoMethodError = define_class('NoMethodError', eNameError);
eArgError = define_class('ArgumentError', eStandardError);
eScriptError = define_class('ScriptError', eException);
eLoadError = define_class('LoadError', eScriptError);
eIndexError = define_class("IndexError", eStandardError);
eKeyError = define_class("KeyError", eIndexError);
eRangeError = define_class("RangeError", eStandardError);
eBreakInstance = new Error('unexpected break');
eBreakInstance.$klass = eLocalJumpError;
eBreakInstance.$keyword = 2;
block.b = eBreakInstance;
eReturnInstance = new Error('unexpected return');
eReturnInstance.$klass = eLocalJumpError;
eReturnInstance.$keyword = 1;
eNextInstance = new Error('unexpected next');
eNextInstance.$klass = eLocalJumpError;
eNextInstance.$keyword = 3;
// need to do this after we make symbol
define_singleton_method(cClass, "new", class_s_new);
cIO = define_class('IO', cObject);
stdin = obj_alloc(cIO);
stdout = obj_alloc(cIO);
stderr = obj_alloc(cIO);
const_set(cObject, 'STDIN', stdin);
const_set(cObject, 'STDOUT', stdout);
const_set(cObject, 'STDERR', stderr);
define_hooked_variable('$stdin', stdio_getter, stdio_setter);
define_hooked_variable('$stdout', stdio_getter, stdio_setter);
define_hooked_variable('$stderr', stdio_getter, stdio_setter);
define_hooked_variable('$:', load_path_getter, gvar_readonly_setter);
define_hooked_variable('$LOAD_PATH', load_path_getter, gvar_readonly_setter);
define_method(mKernel, 'require', obj_require);
Op.loader = new Loader(Op);
Op.cache = {};
// const_set(cObject, 'RUBY_ENGINE', Op.platform.engine);
const_set(cObject, 'RUBY_ENGINE', 'opal-gem');
};
/**
Define a top level module with the given id
*/
function define_module(id) {
return define_module_under(cObject, id);
};
function define_module_under(base, id) {
var module;
if (const_defined(base, id)) {
module = const_get(base, id);
if (module.$flags & T_MODULE) {
return module;
}
throw new Error(id + " is not a module.");
}
module = define_module_id(id);
if (base == cObject) {
name_class(module, id);
} else {
name_class(module, base.__classid__ + '::' + id);
}
const_set(base, id, module);
module.$parent = base;
return module;
};
function define_module_id(id) {
var module = class_create(cModule);
make_metaclass(module, cModule);
module.$flags = T_MODULE;
module.$included_in = [];
return module;
};
function mod_create() {
return class_boot(cModule);
};
function include_module(klass, module) {
if (!klass.$included_modules) {
klass.$included_modules = [];
}
if (klass.$included_modules.indexOf(module) != -1) {
return;
}
klass.$included_modules.push(module);
if (!module.$included_in) {
module.$included_in = [];
}
module.$included_in.push(klass);
for (var method in module.$method_table) {
if (module.$method_table.hasOwnProperty(method)) {
define_raw_method(klass, method,
module.$method_table[method]);
}
}
for (var constant in module.$c) {
if (module.$c.hasOwnProperty(constant)) {
const_set(klass, constant, module.$c[constant]);
}
}
};
Rt.include_module = include_module;
function extend_module(klass, module) {
if (!klass.$extended_modules) {
klass.$extended_modules = [];
}
if (klass.$extended_modules.indexOf(module) != -1) {
return;
}