Module: __JS__::Opal

Defined in:
opal/opal/corelib/runtime.js.rb

Overview

This module is just a placeholder for showing the documentation of the internal JavaScript runtime. The methods you'll find defined below are actually JavaScript functions attached to the Opal global object

Class Method Summary collapse

Class Method Details

.add_stub_forObject

Actuall add a method_missing stub function to the given prototype for the given name. *

Parameters:

  • prototype (Prototype)

    the target prototype

  • stub (String)

    stub name to add (e.g. "$foo")



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'opal/opal/corelib/runtime.js.rb', line 139

def self.add_stub_for(*)
<<-JAVASCRIPT
   function add_stub_for(prototype, stub) {
     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, [stub.slice(1)].concat($slice.call(arguments)));
     }
   
     method_missing_stub.rb_stub = true;
     prototype[stub] = method_missing_stub;
   }
JAVASCRIPT
end

.boot_class_metaObject

class itself



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
# File 'opal/opal/corelib/runtime.js.rb', line 31

def self.boot_class_meta(*)
<<-JAVASCRIPT
   function boot_class_meta(superklass, constructor) {
     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;
   }
JAVASCRIPT
end

.boot_moduleObject

Internal function to create a new module instance. This simply sets up the prototype hierarchy and method tables.



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
# File 'opal/opal/corelib/runtime.js.rb', line 62

def self.boot_module(*)
<<-JAVASCRIPT
   function boot_module() {
     var mtor = function() {};
     mtor.prototype = RubyModule.constructor.prototype;
   
     function OpalModule() {};
     OpalModule.prototype = new mtor();
   
     var module = new OpalModule();
   
     module._id         = unique_id++;
     module._isClass    = true;
     module.constructor = OpalModule;
     module._super      = RubyModule;
     module._methods    = [];
     module.__inc__     = [];
     module.__parent    = RubyModule;
     module._proto      = {};
     module.__mod__     = true;
     module.__dep__     = [];
   
     return module;
   }
JAVASCRIPT
end

.bridge_classClass

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. * Bridged classes are tracked in bridged_classes array so that methods defined on Object can be "donated" to all bridged classes. This allows us to fake the inheritance of a native prototype from our Object prototype. * Example: * bridge_class("Proc", Function); *

Parameters:

  • name (String)

    the name of the ruby class to create

  • constructor (Function)

    native javascript constructor to use

Returns:

  • (Class)

    returns new ruby class



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'opal/opal/corelib/runtime.js.rb', line 111

def self.bridge_class(*)
<<-JAVASCRIPT
   function bridge_class(name, constructor) {
     var klass = boot_class_meta(RubyObject, constructor);
   
     klass._name = name;
   
     create_scope(Opal, klass, name);
     bridged_classes.push(klass);
   
     var object_methods = RubyBasicObject._methods.concat(RubyObject._methods);
   
     for (var i = 0, len = object_methods.length; i < len; i++) {
       var meth = object_methods[i];
       constructor.prototype[meth] = RubyObject._proto[meth];
     }
   
     return klass;
   };
JAVASCRIPT
end

.create_scopeObject

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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'opal/opal/corelib/runtime.js.rb', line 9

def self.create_scope(*)
<<-JAVASCRIPT
   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;
     klass._base_module = base.base;
     const_scope.constructor = const_alloc;
     const_scope.constants = [];
   
     if (id) {
       klass._orig_scope = base;
       base[id] = base.constructor[id] = klass;
       base.constants.push(id);
     }
   }
JAVASCRIPT
end