-
-
Notifications
You must be signed in to change notification settings - Fork 329
/
runtime.js
523 lines (396 loc) · 12.8 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
(function(undefined) {
// The Opal object that is exposed globally
var Opal = this.Opal = {};
// Very root class
function BasicObject(){}
// Core Object class
function Object(){}
// Class' class
function Class(){}
// Module's class
function Module(){}
// the class of nil
function NilClass(){}
// 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;
// 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;
// Generates unique id for every ruby object
var unique_id = 0;
// Return next unique id
Opal.uid = function() {
return unique_id++;
};
// Table holds all class variables
Opal.cvars = {};
// Globals table
Opal.gvars = {};
/*
* 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.
*/
function create_scope(base, klass, id) {
var const_alloc = function() {};
var const_scope = const_alloc.prototype = new base.constructor();
klass._scope = const_scope;
const_scope.base = klass;
const_scope.constructor = const_alloc;
if (id) {
base[id] = base.constructor[id] = klass;
}
}
Opal.klass = function(base, superklass, id, constructor) {
var klass;
if (!base._isClass) {
base = base._klass;
}
if (superklass === null) {
superklass = ObjectClass;
}
if ($hasOwn.call(base._scope, id)) {
klass = base._scope[id];
if (!klass._isClass) {
throw Opal.TypeError.$new(id + " is not a class");
}
if (superklass !== klass._super && superklass !== ObjectClass) {
throw Opal.TypeError.$new("superclass mismatch for class " + id);
}
}
else {
klass = boot_class(superklass, constructor);
klass._name = (base === ObjectClass ? id : base._name + '::' + id);
create_scope(base._scope, klass);
base[id] = base._scope[id] = klass;
if (superklass.$inherited) {
superklass.$inherited(klass);
}
}
return klass;
};
// Create generic class with given superclass.
var boot_class = Opal.boot = function(superklass, constructor) {
// instances
var ctor = function() {};
ctor.prototype = superklass._proto;
constructor.prototype = new ctor();
constructor.prototype.constructor = constructor;
// class itself
var mtor = function() {};
mtor.prototype = superklass.constructor.prototype;
function OpalClass() {};
OpalClass.prototype = new mtor();
var klass = new OpalClass();
klass._id = unique_id++;
klass._alloc = constructor;
klass._isClass = true;
klass.constructor = OpalClass;
klass._super = superklass;
klass._methods = [];
klass.__inc__ = [];
klass.__parent = superklass;
klass._proto = constructor.prototype;
constructor.prototype._klass = klass;
return klass;
};
// Define new module (or return existing module)
Opal.module = function(base, id, constructor) {
var module;
if (!base._isClass) {
base = base._klass;
}
if ($hasOwn.call(base._scope, id)) {
module = base._scope[id];
if (!module._mod$ && module !== ObjectClass) {
throw Opal.TypeError.$new(id + " is not a module")
}
}
else {
module = boot_module()
module._name = (base === ObjectClass ? id : base._name + '::' + id);
create_scope(base._scope, module, id);
}
return module;
};
function boot_module() {
var mtor = function() {};
mtor.prototype = ModuleClass.constructor.prototype;
function OpalModule() {};
OpalModule.prototype = new mtor();
var module = new OpalModule();
module._id = unique_id++;
module._isClass = true;
module.constructor = OpalModule;
module._super = ModuleClass;
module._methods = [];
module.__inc__ = [];
module.__parent = ModuleClass;
module._proto = {};
module._mod$ = true;
module.__dep__ = [];
return module;
}
// Boot a base class (makes instances).
var boot_defclass = function(id, constructor, superklass) {
if (superklass) {
var ctor = function() {};
ctor.prototype = superklass.prototype;
constructor.prototype = new ctor();
}
constructor.prototype.constructor = constructor;
return constructor;
};
// Boot the actual (meta?) classes of core classes
var boot_makemeta = function(id, constructor, superklass) {
var mtor = function() {};
mtor.prototype = superklass.prototype;
function OpalClass() {};
OpalClass.prototype = new mtor();
var klass = new OpalClass();
klass._id = unique_id++;
klass._alloc = constructor;
klass._isClass = true;
klass._name = id;
klass._super = superklass;
klass.constructor = OpalClass;
klass._methods = [];
klass.__inc__ = [];
klass.__parent = superklass;
klass._proto = constructor.prototype;
constructor.prototype._klass = klass;
Opal[id] = klass;
return klass;
};
var bridge_class = function(name, constructor) {
var klass = boot_class(ObjectClass, constructor), idx, length, mid;
bridged_classes.push(klass);
var table = ObjectClass._proto, methods = ObjectClass._methods;
for (idx = 0, len = methods.length; idx < len; idx++) {
mid = methods[idx];
constructor.prototype[mid] = table[mid];
}
klass._name = name;
create_scope(Opal, klass, name);
return klass;
};
Opal.puts = function(a) { console.log(a); };
Opal.add_stubs = function(stubs) {
for (var i = 0, length = stubs.length; i < length; i++) {
var stub = stubs[i];
if (!BasicObject.prototype[stub]) {
BasicObject.prototype[stub] = true;
add_stub_for(BasicObject.prototype, stub);
}
}
};
function add_stub_for(prototype, stub) {
function method_missing_stub() {
this.$method_missing._p = method_missing_stub._p;
method_missing_stub._p = null;
return this.$method_missing.apply(this, [stub.slice(1)].concat($slice.call(arguments)));
}
method_missing_stub.rb_stub = true;
prototype[stub] = method_missing_stub;
}
Opal.add_stub_for = add_stub_for;
// Const missing dispatcher
Opal.cm = function(name) {
return this.base.$const_missing(name);
};
// Arity count error dispatcher
Opal.ac = function(actual, expected, object, meth) {
var inspect = (object._isClass ? object._name + '.' : object._klass._name + '#') + meth;
var msg = '[' + inspect + '] wrong number of arguments(' + actual + ' for ' + expected + ')';
throw Opal.ArgumentError.$new(msg);
};
// Super dispatcher
Opal.dispatch_super = function(obj, jsid, current_func, args, iter, defs) {
var dispatcher;
if (defs) {
dispatcher = obj._isClass ? defs._super : obj._klass._proto;
}
else {
if (obj._isClass) {
dispatcher = obj._klass;
}
else {
dispatcher = find_obj_super_dispatcher(obj, jsid, current_func);
}
}
dispatcher = dispatcher['$' + jsid];
dispatcher._p = iter;
return dispatcher.apply(obj, args);
};
var find_obj_super_dispatcher = function(obj, jsid, current_func) {
var klass = obj._klass;
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
while (klass) {
var working = klass._proto['$' + jsid];
if (working && working !== current_func) {
// ok
break;
}
klass = klass.__parent;
}
return klass._proto;
};
// return helper
Opal.$return = function(val) {
Opal.returner.$v = val;
throw Opal.returner;
};
// handles yield calls for 1 yielded arg
Opal.$yield1 = function(block, arg) {
if (typeof(block) !== "function") {
throw Opal.LocalJumpError.$new("no block given");
}
if (block.length > 1) {
if (arg._isArray) {
return block.apply(null, arg);
}
else {
return block(arg);
}
}
else {
return block(arg);
}
};
// handles yield for > 1 yielded arg
Opal.$yieldX = function(block, args) {
if (block.length > 1 && args.length == 1) {
if (args[0]._isArray) {
return block.apply(null, args[0]);
}
}
return block.apply(null, args);
};
// Helper to convert the given object to an array
Opal.to_ary = function(value) {
if (value._isArray) {
return value;
}
return [value];
};
/*
Call a ruby method on a ruby object with some arguments:
var my_array = [1, 2, 3, 4]
Opal.send(my_array, 'length') # => 4
Opal.send(my_array, 'reverse!') # => [4, 3, 2, 1]
A missing method will be forwarded to the object via
method_missing.
The result of either call with be returned.
@param [Object] recv the ruby object
@param [String] mid ruby method to call
*/
Opal.send = function(recv, mid) {
var args = $slice.call(arguments, 2),
func = recv['$' + mid];
if (func) {
return func.apply(recv, args);
}
return recv.$method_missing.apply(recv, [mid].concat(args));
};
Opal.block_send = function(recv, mid, block) {
var args = $slice.call(arguments, 3),
func = recv['$' + mid];
if (func) {
func._p = block;
return func.apply(recv, args);
}
return recv.$method_missing.apply(recv, [mid].concat(args));
};
/**
* Donate methods for a class/module
*/
Opal.donate = function(klass, defined, indirect) {
var methods = klass._methods, included_in = klass.__dep__;
// if (!indirect) {
klass._methods = methods.concat(defined);
// }
if (included_in) {
for (var i = 0, length = included_in.length; i < length; i++) {
var includee = included_in[i];
var dest = includee._proto;
for (var j = 0, jj = defined.length; j < jj; j++) {
var method = defined[j];
dest[method] = klass._proto[method];
}
if (includee.__dep__) {
Opal.donate(includee, defined, true);
}
}
}
};
// Initialization
// --------------
// Constructors for *instances* of core objects
boot_defclass('BasicObject', BasicObject);
boot_defclass('Object', Object, BasicObject);
boot_defclass('Module', Module, Object);
boot_defclass('Class', Class, Module);
// Constructors for *classes* of core objects
var BasicObjectClass = boot_makemeta('BasicObject', BasicObject, Class);
var ObjectClass = boot_makemeta('Object', Object, BasicObjectClass.constructor);
var ModuleClass = boot_makemeta('Module', Module, ObjectClass.constructor);
var ClassClass = boot_makemeta('Class', Class, ModuleClass.constructor);
// Fix booted classes to use their metaclass
BasicObjectClass._klass = ClassClass;
ObjectClass._klass = ClassClass;
ModuleClass._klass = ClassClass;
ClassClass._klass = ClassClass;
// Fix superclasses of booted classes
BasicObjectClass._super = null;
ObjectClass._super = BasicObjectClass;
ModuleClass._super = ObjectClass;
ClassClass._super = ModuleClass;
// Defines methods onto Object (which are then donated to bridged classes)
ObjectClass._defn = function (mid, body) {
this._proto[mid] = body;
Opal.donate(this, [mid]);
};
var bridged_classes = ObjectClass.__dep__ = [];
Opal.base = ObjectClass;
BasicObjectClass._scope = ObjectClass._scope = Opal;
Opal.Kernel = ObjectClass;
create_scope(Opal, ModuleClass);
create_scope(Opal, ClassClass);
ObjectClass._proto.toString = function() {
return this.$to_s();
};
ClassClass._proto._defn = function(mid, body) { this._proto[mid] = body; };
Opal.top = new ObjectClass._alloc();
Opal.klass(ObjectClass, ObjectClass, 'NilClass', NilClass);
var nil = Opal.nil = new NilClass;
nil.call = nil.apply = function() { throw Opal.LocalJumpError.$new('no block given'); };
Opal.breaker = new Error('unexpected break');
Opal.returner = new Error('unexpected return');
bridge_class('Array', Array);
bridge_class('Boolean', Boolean);
bridge_class('Numeric', Number);
bridge_class('String', String);
bridge_class('Proc', Function);
bridge_class('Exception', Error);
bridge_class('Regexp', RegExp);
bridge_class('Time', Date);
TypeError._super = Error;
}).call(this);