-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
19076 lines (14811 loc) · 561 KB
/
index.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
var Opal = this.Opal = undefined;
(function(undefined) {
if (typeof(this.Opal) !== 'undefined') {
console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.');
return this.Opal;
}
var nil;
// The actual class for BasicObject
var BasicObject;
// The actual Object class.
// The leading underscore is to avoid confusion with window.Object()
var _Object;
// The actual Module class
var Module;
// The actual Class class
var Class;
// Constructor for instances of BasicObject
function BasicObject_alloc(){}
// Constructor for instances of Object
function Object_alloc(){}
// Constructor for instances of Class
function Class_alloc(){}
// Constructor for instances of Module
function Module_alloc(){}
// Constructor for instances of NilClass (nil)
function NilClass_alloc(){}
// The Opal object that is exposed globally
Opal = this.Opal = {};
// All bridged classes - keep track to donate methods from Object
var bridges = {};
// TopScope is used for inheriting constants from the top scope
var TopScope = function(){};
// Opal just acts as the top scope
TopScope.prototype = Opal;
// To inherit scopes
Opal.constructor = TopScope;
// List top scope constants
Opal.constants = [];
// This is a useful reference to global object inside ruby files
Opal.global = this;
// Minify common function calls
var $hasOwn = Opal.hasOwnProperty;
var $slice = Opal.slice = Array.prototype.slice;
// Nil object id is always 4
var nil_id = 4;
// Generates even sequential numbers greater than 4
// (nil_id) to serve as unique ids for ruby objects
var unique_id = nil_id;
// Return next unique id
Opal.uid = function() {
unique_id += 2;
return unique_id;
};
// Table holds all class variables
Opal.cvars = {};
// Globals table
Opal.gvars = {};
// Exit function, this should be replaced by platform specific implementation
// (See nodejs and phantom for examples)
Opal.exit = function(status) { if (Opal.gvars.DEBUG) console.log('Exited with status '+status); };
// keeps track of exceptions for $!
Opal.exceptions = [];
// Get a constant on the given scope. Every class and module in Opal has a
// scope used to store, and inherit, constants. For example, the top level
// `Object` in ruby has a scope accessible as `Opal.Object.$$scope`.
//
// To get the `Array` class using this scope, you could use:
//
// Opal.Object.$$scope.get("Array")
//
// If a constant with the given name cannot be found, then a dispatch to the
// class/module's `#const_method` is called, which by default will raise an
// error.
//
// @param [String] name the name of the constant to lookup
// @return [RubyObject]
//
Opal.get = function(name) {
var constant = this[name];
if (constant == null) {
return this.base.$const_get(name);
}
return constant;
};
// Create a new constants scope for the given class with the given
// base. Constants are looked up through their parents, so the base
// scope will be the outer scope of the new klass.
//
// @param base_scope [$$scope] the scope in which the new scope should be created
// @param klass [Class]
// @param id [String, null] the name of the newly created scope
//
Opal.create_scope = function(base_scope, klass, id) {
var const_alloc = function() {};
var const_scope = const_alloc.prototype = new base_scope.constructor();
klass.$$scope = const_scope;
klass.$$base_module = base_scope.base;
const_scope.base = klass;
const_scope.constructor = const_alloc;
const_scope.constants = [];
if (id) {
Opal.cdecl(base_scope, id, klass);
const_alloc.displayName = id+"_scope_alloc";
}
}
// A `class Foo; end` expression in ruby is compiled to call this runtime
// method which either returns an existing class of the given name, or creates
// a new class in the given `base` scope.
//
// If a constant with the given name exists, then we check to make sure that
// it is a class and also that the superclasses match. If either of these
// fail, then we raise a `TypeError`. Note, superklass may be null if one was
// not specified in the ruby code.
//
// We pass a constructor to this method of the form `function ClassName() {}`
// simply so that classes show up with nicely formatted names inside debuggers
// in the web browser (or node/sprockets).
//
// The `base` is the current `self` value where the class is being created
// from. We use this to get the scope for where the class should be created.
// If `base` is an object (not a class/module), we simple get its class and
// use that as the base instead.
//
// @param base [Object] where the class is being created
// @param superklass [Class,null] superclass of the new class (may be null)
// @param id [String] the name of the class to be created
// @param constructor [Function] function to use as constructor
//
// @return new [Class] or existing ruby class
//
Opal.klass = function(base, superklass, id, constructor) {
var klass, bridged, alloc;
// If base is an object, use its class
if (!base.$$is_class && !base.$$is_module) {
base = base.$$class;
}
// If the superclass is a function then we're bridging a native JS class
if (typeof(superklass) === 'function') {
bridged = superklass;
superklass = _Object;
}
// Try to find the class in the current scope
klass = base.$$scope[id];
// If the class exists in the scope, then we must use that
if (klass && klass.$$orig_scope === base.$$scope) {
// Make sure the existing constant is a class, or raise error
if (!klass.$$is_class) {
throw Opal.TypeError.$new(id + " is not a class");
}
// Make sure existing class has same superclass
if (superklass && klass.$$super !== superklass) {
throw Opal.TypeError.$new("superclass mismatch for class " + id);
}
return klass;
}
// Class doesnt exist, create a new one with given superclass...
// Not specifying a superclass means we can assume it to be Object
if (superklass == null) {
superklass = _Object;
}
// If bridged the JS class will also be the alloc function
alloc = bridged || boot_class_alloc(id, constructor, superklass);
// Create the class object (instance of Class)
klass = boot_class_object(id, superklass, alloc);
// Name the class
klass.$$name = id;
klass.displayName = id;
// Mark the object as a class
klass.$$is_class = true;
// Every class gets its own constant scope, inherited from current scope
Opal.create_scope(base.$$scope, klass, id);
// Name new class directly onto current scope (Opal.Foo.Baz = klass)
base[id] = base.$$scope[id] = klass;
if (bridged) {
Opal.bridge(klass, alloc);
}
else {
// Copy all parent constants to child, unless parent is Object
if (superklass !== _Object && superklass !== BasicObject) {
donate_constants(superklass, klass);
}
// Call .inherited() hook with new class on the superclass
if (superklass.$inherited) {
superklass.$inherited(klass);
}
}
return klass;
};
// Create generic class with given superclass.
Opal.boot_class = function(superklass, constructor) {
var alloc = boot_class_alloc(null, constructor, superklass)
return boot_class_object(null, superklass, alloc);
}
// The class object itself (as in `Class.new`)
//
// @param superklass [(Opal) Class] Another class object (as in `Class.new`)
// @param alloc [constructor] The constructor that holds the prototype
// that will be used for instances of the
// newly constructed class.
function boot_class_object(id, superklass, alloc) {
// Grab the superclass prototype and use it to build an intermediary object
// in the prototype chain.
function Superclass_alloc_proxy() {};
Superclass_alloc_proxy.prototype = superklass.constructor.prototype;
function SingletonClass_alloc() {}
SingletonClass_alloc.prototype = new Superclass_alloc_proxy();
if (id) {
SingletonClass_alloc.displayName = "SingletonClass_alloc("+id+")";
}
// The built class is the only instance of its singleton_class
var klass = new SingletonClass_alloc();
setup_module_or_class_object(klass, SingletonClass_alloc, superklass, alloc.prototype);
// @property $$alloc This is the constructor of instances of the current
// class. Its prototype will be used for method lookup
klass.$$alloc = alloc;
// @property $$proto.$$class Make available to instances a reference to the
// class they belong to.
klass.$$proto.$$class = klass;
return klass;
}
// Adds common/required properties to a module or class object
// (as in `Module.new` / `Class.new`)
//
// @param module The module or class that needs to be prepared
//
// @param constructor The constructor of the module or class itself,
// usually it's already assigned by using `new`. Some
// ipothesis on why it's needed can be found below.
//
// @param superklass The superclass of the class/module object, for modules
// is `Module` (of `Module` in JS context)
//
// @param prototype The prototype on which the class/module methods will
// be stored.
//
function setup_module_or_class_object(module, constructor, superklass, prototype) {
// @property $$id Each class is assigned a unique `id` that helps
// comparation and implementation of `#object_id`
module.$$id = Opal.uid();
// @property $$proto This is the prototype on which methods will be defined
module.$$proto = prototype;
// @property constructor keeps a ref to the constructor, but apparently the
// constructor is already set on:
//
// `var module = new constructor` is called.
//
// Maybe there are some browsers not abiding (IE6?)
module.constructor = constructor;
if (superklass === Module) {
// @property $$is_module Clearly mark this as a module
module.$$is_module = true;
module.$$class = Module;
}
else {
// @property $$is_class Clearly mark this as a class
module.$$is_class = true;
module.$$class = Class;
}
// @property $$super the superclass, doesn't get changed by module inclusions
module.$$super = superklass;
// @property $$parent direct parent class or module
// starts with the superclass, after module inclusion is
// the last included module
module.$$parent = superklass;
// @property $$inc included modules
module.$$inc = [];
}
// Define new module (or return existing module). The given `base` is basically
// the current `self` value the `module` statement was defined in. If this is
// a ruby module or class, then it is used, otherwise if the base is a ruby
// object then that objects real ruby class is used (e.g. if the base is the
// main object, then the top level `Object` class is used as the base).
//
// If a module of the given name is already defined in the base, then that
// instance is just returned.
//
// If there is a class of the given name in the base, then an error is
// generated instead (cannot have a class and module of same name in same base).
//
// Otherwise, a new module is created in the base with the given name, and that
// new instance is returned back (to be referenced at runtime).
//
// @param base [Module, Class] class or module this definition is inside
// @param id [String] the name of the new (or existing) module
// @return [Module]
//
Opal.module = function(base, id) {
var module;
if (!base.$$is_class && !base.$$is_module) {
base = base.$$class;
}
if ($hasOwn.call(base.$$scope, id)) {
module = base.$$scope[id];
if (!module.$$is_module && module !== _Object) {
throw Opal.TypeError.$new(id + " is not a module");
}
}
else {
module = boot_module_object();
// name module using base (e.g. Foo or Foo::Baz)
module.$$name = id;
// mark the object as a module
module.$$is_module = true;
// initialize dependency tracking
module.$$dep = [];
Opal.create_scope(base.$$scope, module, id);
// Name new module directly onto current scope (Opal.Foo.Baz = module)
base[id] = base.$$scope[id] = module;
}
return module;
};
// Internal function to create a new module instance. This simply sets up
// the prototype hierarchy and method tables.
//
function boot_module_object() {
var mtor = function() {};
mtor.prototype = Module_alloc.prototype;
function module_constructor() {}
module_constructor.prototype = new mtor();
var module = new module_constructor();
var module_prototype = {};
setup_module_or_class_object(module, module_constructor, Module, module_prototype);
return module;
}
// Make `boot_module_object` available to the JS-API
Opal.boot_module_object = boot_module_object;
// Return the singleton class for the passed object.
//
// If the given object alredy has a singleton class, then it will be stored on
// the object as the `$$meta` property. If this exists, then it is simply
// returned back.
//
// Otherwise, a new singleton object for the class or object is created, set on
// the object at `$$meta` for future use, and then returned.
//
// @param [RubyObject] object the ruby object
// @return [RubyClass] the singleton class for object
//
Opal.get_singleton_class = function(object) {
if (object.$$meta) {
return object.$$meta;
}
if (object.$$is_class || object.$$is_module) {
return build_class_singleton_class(object);
}
return build_object_singleton_class(object);
};
// Build the singleton class for an existing class.
//
// NOTE: Actually in MRI a class' singleton class inherits from its
// superclass' singleton class which in turn inherits from Class.
//
// @param [RubyClass] klass
// @return [RubyClass]
//
function build_class_singleton_class(klass) {
var meta = new Opal.Class.$$alloc();
meta.$$class = Opal.Class;
meta.$$proto = klass.constructor.prototype;
meta.$$is_singleton = true;
meta.$$singleton_of = klass;
meta.$$inc = [];
meta.$$scope = klass.$$scope;
return klass.$$meta = meta;
}
// Build the singleton class for a Ruby (non class) Object.
//
// @param [RubyObject] object
// @return [RubyClass]
//
function build_object_singleton_class(object) {
var orig_class = object.$$class,
class_id = "#<Class:#<" + orig_class.$$name + ":" + orig_class.$$id + ">>";
var Singleton = function() {};
var meta = Opal.boot_class(orig_class, Singleton);
meta.$$name = class_id;
meta.$$proto = object;
meta.$$class = orig_class.$$class;
meta.$$scope = orig_class.$$scope;
meta.$$parent = orig_class;
meta.$$is_singleton = true;
meta.$$singleton_of = object;
return object.$$meta = meta;
}
// Bridges a single method.
function bridge_method(target, from, name, body) {
var ancestors, i, ancestor, length;
ancestors = target.$$bridge.$ancestors();
// order important here, we have to check for method presence in
// ancestors from the bridged class to the last ancestor
for (i = 0, length = ancestors.length; i < length; i++) {
ancestor = ancestors[i];
if ($hasOwn.call(ancestor.$$proto, name) &&
ancestor.$$proto[name] &&
!ancestor.$$proto[name].$$donated &&
!ancestor.$$proto[name].$$stub &&
ancestor !== from) {
break;
}
if (ancestor === from) {
target.prototype[name] = body
break;
}
}
}
// Bridges from *donator* to a *target*.
function _bridge(target, donator) {
var id, methods, method, i, bridged;
if (typeof(target) === "function") {
id = donator.$__id__();
methods = donator.$instance_methods();
for (i = methods.length - 1; i >= 0; i--) {
method = '$' + methods[i];
bridge_method(target, donator, method, donator.$$proto[method]);
}
if (!bridges[id]) {
bridges[id] = [];
}
bridges[id].push(target);
}
else {
bridged = bridges[target.$__id__()];
if (bridged) {
for (i = bridged.length - 1; i >= 0; i--) {
_bridge(bridged[i], donator);
}
bridges[donator.$__id__()] = bridged.slice();
}
}
}
// The actual inclusion of a module into a class.
//
// ## Class `$$parent` and `iclass`
//
// To handle `super` calls, every class has a `$$parent`. This parent is
// used to resolve the next class for a super call. A normal class would
// have this point to its superclass. However, if a class includes a module
// then this would need to take into account the module. The module would
// also have to then point its `$$parent` to the actual superclass. We
// cannot modify modules like this, because it might be included in more
// then one class. To fix this, we actually insert an `iclass` as the class'
// `$$parent` which can then point to the superclass. The `iclass` acts as
// a proxy to the actual module, so the `super` chain can then search it for
// the required method.
//
// @param [RubyModule] module the module to include
// @param [RubyClass] klass the target class to include module into
// @return [null]
//
Opal.append_features = function(module, klass) {
var iclass, donator, prototype, methods, id, i;
// check if this module is already included in the class
for (i = klass.$$inc.length - 1; i >= 0; i--) {
if (klass.$$inc[i] === module) {
return;
}
}
klass.$$inc.push(module);
module.$$dep.push(klass);
_bridge(klass, module);
// iclass
iclass = {
$$name: module.$$name,
$$proto: module.$$proto,
$$parent: klass.$$parent,
$$module: module,
$$iclass: true
};
klass.$$parent = iclass;
donator = module.$$proto;
prototype = klass.$$proto;
methods = module.$instance_methods();
for (i = methods.length - 1; i >= 0; i--) {
id = '$' + methods[i];
// if the target class already has a method of the same name defined
// and that method was NOT donated, then it must be a method defined
// by the class so we do not want to override it
if ( prototype.hasOwnProperty(id) &&
!prototype[id].$$donated &&
!prototype[id].$$stub) {
continue;
}
prototype[id] = donator[id];
prototype[id].$$donated = module;
}
donate_constants(module, klass);
};
// Boot a base class (makes instances).
function boot_class_alloc(id, constructor, superklass) {
if (superklass) {
var alloc_proxy = function() {};
alloc_proxy.prototype = superklass.$$proto || superklass.prototype;
constructor.prototype = new alloc_proxy();
}
if (id) {
constructor.displayName = id+'_alloc';
}
constructor.prototype.constructor = constructor;
return constructor;
}
// Builds the class object for core classes:
// - make the class object have a singleton class
// - make the singleton class inherit from its parent singleton class
//
// @param id [String] the name of the class
// @param alloc [Function] the constructor for the core class instances
// @param superclass [Class alloc] the constructor of the superclass
//
function boot_core_class_object(id, alloc, superclass) {
var superclass_constructor = function() {};
superclass_constructor.prototype = superclass.prototype;
var singleton_class = function() {};
singleton_class.prototype = new superclass_constructor();
singleton_class.displayName = "#<Class:"+id+">";
// the singleton_class acts as the class object constructor
var klass = new singleton_class();
setup_module_or_class_object(klass, singleton_class, superclass, alloc.prototype);
klass.$$alloc = alloc;
klass.$$name = id;
klass.displayName = id;
// Give all instances a ref to their class
alloc.prototype.$$class = klass;
Opal[id] = klass;
Opal.constants.push(id);
return klass;
}
// For performance, some core Ruby classes are toll-free bridged to their
// native JavaScript counterparts (e.g. a Ruby Array is a JavaScript Array).
//
// This method is used to setup a native constructor (e.g. Array), to have
// its prototype act like a normal Ruby class. Firstly, a new Ruby class is
// created using the native constructor so that its prototype is set as the
// target for th new class. Note: all bridged classes are set to inherit
// from Object.
//
// Example:
//
// Opal.bridge(self, Function);
//
// @param [Class] klass the Ruby class to bridge
// @param [Function] constructor native JavaScript constructor to use
// @return [Class] returns the passed Ruby class
//
Opal.bridge = function(klass, constructor) {
if (constructor.$$bridge) {
throw Opal.ArgumentError.$new("already bridged");
}
Opal.stub_subscribers.push(constructor.prototype);
constructor.prototype.$$class = klass;
constructor.$$bridge = klass;
var ancestors = klass.$ancestors();
// order important here, we have to bridge from the last ancestor to the
// bridged class
for (var i = ancestors.length - 1; i >= 0; i--) {
_bridge(constructor, ancestors[i]);
}
for (var name in BasicObject_alloc.prototype) {
var method = BasicObject_alloc.prototype[method];
if (method && method.$$stub && !(name in constructor.prototype)) {
constructor.prototype[name] = method;
}
}
return klass;
}
// Constant assignment, see also `Opal.cdecl`
//
// @param base_module [Module, Class] the constant namespace
// @param name [String] the name of the constant
// @param value [Object] the value of the constant
//
// @example Assigning a namespaced constant
// self::FOO = 'bar'
//
// @example Assigning with Module#const_set
// Foo.const_set :BAR, 123
//
Opal.casgn = function(base_module, name, value) {
function update(klass, name) {
klass.$$name = name;
for (name in klass.$$scope) {
var value = klass.$$scope[name];
if (value.$$name === nil && (value.$$is_class || value.$$is_module)) {
update(value, name)
}
}
}
var scope = base_module.$$scope;
if (value.$$is_class || value.$$is_module) {
// Only checking _Object prevents setting a const on an anonymous class
// that has a superclass that's not Object
if (value.$$is_class || value.$$base_module === _Object) {
value.$$base_module = base_module;
}
if (value.$$name === nil && value.$$base_module.$$name !== nil) {
update(value, name);
}
}
scope.constants.push(name);
return scope[name] = value;
};
// constant decl
Opal.cdecl = function(base_scope, name, value) {
if ((value.$$is_class || value.$$is_module) && value.$$orig_scope == null) {
value.$$name = name;
value.$$orig_scope = base_scope;
base_scope.constructor[name] = value;
}
base_scope.constants.push(name);
return base_scope[name] = value;
};
// When a source module is included into the target module, we must also copy
// its constants to the target.
//
function donate_constants(source_mod, target_mod) {
var source_constants = source_mod.$$scope.constants,
target_scope = target_mod.$$scope,
target_constants = target_scope.constants;
for (var i = 0, length = source_constants.length; i < length; i++) {
target_constants.push(source_constants[i]);
target_scope[source_constants[i]] = source_mod.$$scope[source_constants[i]];
}
};
// Donate methods for a module.
function donate(module, jsid) {
var included_in = module.$$dep,
body = module.$$proto[jsid],
i, length, includee, dest, current,
klass_includees, j, jj, current_owner_index, module_index;
if (!included_in) {
return;
}
for (i = 0, length = included_in.length; i < length; i++) {
includee = included_in[i];
dest = includee.$$proto;
current = dest[jsid];
if (dest.hasOwnProperty(jsid) && !current.$$donated && !current.$$stub) {
// target class has already defined the same method name - do nothing
}
else if (dest.hasOwnProperty(jsid) && !current.$$stub) {
// target class includes another module that has defined this method
klass_includees = includee.$$inc;
for (j = 0, jj = klass_includees.length; j < jj; j++) {
if (klass_includees[j] === current.$$donated) {
current_owner_index = j;
}
if (klass_includees[j] === module) {
module_index = j;
}
}
// only redefine method on class if the module was included AFTER
// the module which defined the current method body. Also make sure
// a module can overwrite a method it defined before
if (current_owner_index <= module_index) {
dest[jsid] = body;
dest[jsid].$$donated = module;
}
}
else {
// neither a class, or module included by class, has defined method
dest[jsid] = body;
dest[jsid].$$donated = module;
}
if (includee.$$dep) {
donate(includee, jsid);
}
}
};
// Methods stubs are used to facilitate method_missing in opal. A stub is a
// placeholder function which just calls `method_missing` on the receiver.
// If no method with the given name is actually defined on an object, then it
// is obvious to say that the stub will be called instead, and then in turn
// method_missing will be called.
//
// When a file in ruby gets compiled to javascript, it includes a call to
// this function which adds stubs for every method name in the compiled file.
// It should then be safe to assume that method_missing will work for any
// method call detected.
//
// Method stubs are added to the BasicObject prototype, which every other
// ruby object inherits, so all objects should handle method missing. A stub
// is only added if the given property name (method name) is not already
// defined.
//
// Note: all ruby methods have a `$` prefix in javascript, so all stubs will
// have this prefix as well (to make this method more performant).
//
// Opal.add_stubs(["$foo", "$bar", "$baz="]);
//
// All stub functions will have a private `$$stub` property set to true so
// that other internal methods can detect if a method is just a stub or not.
// `Kernel#respond_to?` uses this property to detect a methods presence.
//
// @param [Array] stubs an array of method stubs to add
//
Opal.add_stubs = function(stubs) {
var subscriber, subscribers = Opal.stub_subscribers,
i, ilength = stubs.length,
j, jlength = subscribers.length,
method_name, stub;
for (i = 0; i < ilength; i++) {
method_name = stubs[i];
stub = stub_for(method_name);
for (j = 0; j < jlength; j++) {
subscriber = subscribers[j];
if (!(method_name in subscriber)) {
subscriber[method_name] = stub;
}
}
}
};
// Keep a list of prototypes that want method_missing stubs to be added.
//
// @default [Prototype List] BasicObject_alloc.prototype
//
Opal.stub_subscribers = [BasicObject_alloc.prototype];
// Add a method_missing stub function to the given prototype for the
// given name.
//
// @param [Prototype] prototype the target prototype
// @param [String] stub stub name to add (e.g. "$foo")
//
Opal.add_stub_for = function(prototype, stub) {
var method_missing_stub = stub_for(stub);
prototype[stub] = method_missing_stub;
}
// Generate the method_missing stub for a given method name.
//
// @param [String] method_name The js-name of the method to stub (e.g. "$foo")
//
function stub_for(method_name) {
function method_missing_stub() {
// Copy any given block onto the method_missing dispatcher
this.$method_missing.$$p = method_missing_stub.$$p;
// Set block property to null ready for the next call (stop false-positives)
method_missing_stub.$$p = null;
// call method missing with correct args (remove '$' prefix on method name)
return this.$method_missing.apply(this, [method_name.slice(1)].concat($slice.call(arguments)));
}
method_missing_stub.$$stub = true;
return method_missing_stub;
}
// Arity count error dispatcher
Opal.ac = function(actual, expected, object, meth) {
var inspect = '';
if (object.$$is_class || object.$$is_module) {
inspect += object.$$name + '.';
}
else {
inspect += object.$$class.$$name + '#';
}
inspect += meth;
throw Opal.ArgumentError.$new('[' + inspect + '] wrong number of arguments(' + actual + ' for ' + expected + ')');
};
// The Array of ancestors for a given module/class
Opal.ancestors = function(module_or_class) {
var parent = module_or_class,
result = [];
while (parent) {
result.push(parent);
for (var i=0; i < parent.$$inc.length; i++) {
result = result.concat(Opal.ancestors(parent.$$inc[i]));
}
parent = parent.$$is_class ? parent.$$super : null;
}
return result;
}
// Super dispatcher
Opal.find_super_dispatcher = function(obj, jsid, current_func, iter, defs) {
var dispatcher;
if (defs) {
if (obj.$$is_class || obj.$$is_module) {
dispatcher = defs.$$super;
}
else {
dispatcher = obj.$$class.$$proto;
}
}
else {
if (obj.$$is_class || obj.$$is_module) {
dispatcher = obj.$$super;
}
else {
dispatcher = find_obj_super_dispatcher(obj, jsid, current_func);
}
}
dispatcher = dispatcher['$' + jsid];
dispatcher.$$p = iter;
return dispatcher;
};
// Iter dispatcher for super in a block
Opal.find_iter_super_dispatcher = function(obj, jsid, current_func, iter, defs) {
if (current_func.$$def) {
return Opal.find_super_dispatcher(obj, current_func.$$jsid, current_func, iter, defs);
}
else {
return Opal.find_super_dispatcher(obj, jsid, current_func, iter, defs);
}
};
function find_obj_super_dispatcher(obj, jsid, current_func) {
var klass = obj.$$meta || obj.$$class;
jsid = '$' + jsid;
while (klass) {
if (klass.$$proto[jsid] === current_func) {
// ok
break;
}
klass = klass.$$parent;
}
// if we arent in a class, we couldnt find current?
if (!klass) {
throw new Error("could not find current class for super()");
}
klass = klass.$$parent;
// else, let's find the next one