|
55 | 55 | * scope will be the outer scope of the new klass.
|
56 | 56 | */
|
57 | 57 | function create_scope(base, klass, id) {
|
58 |
| - var const_alloc = function() {}; |
59 |
| - var const_scope = const_alloc.prototype = new base.constructor(); |
60 |
| - klass.$$scope = const_scope; |
| 58 | + var const_alloc = function() {}; |
| 59 | + var const_scope = const_alloc.prototype = new base.constructor(); |
| 60 | + |
| 61 | + klass.$$scope = const_scope; |
| 62 | + klass.$$base_module = base.base; |
| 63 | + |
61 | 64 | const_scope.base = klass;
|
62 |
| - klass.$$base_module = base.base; |
63 | 65 | const_scope.constructor = const_alloc;
|
64 | 66 | const_scope.constants = [];
|
65 | 67 |
|
|
98 | 100 | * @return [Class] new or existing ruby class
|
99 | 101 | */
|
100 | 102 | Opal.klass = function(base, superklass, id, constructor) {
|
101 |
| - |
102 | 103 | // If base is an object, use its class
|
103 | 104 | if (!base.$$is_class) {
|
104 | 105 | base = base.$$class;
|
|
113 | 114 |
|
114 | 115 | // If a constant exists in the scope, then we must use that
|
115 | 116 | if ($hasOwn.call(base.$$scope, id) && klass.$$orig_scope === base.$$scope) {
|
116 |
| - |
117 | 117 | // Make sure the existing constant is a class, or raise error
|
118 | 118 | if (!klass.$$is_class) {
|
119 | 119 | throw Opal.TypeError.$new(id + " is not a class");
|
|
158 | 158 | // Create generic class with given superclass.
|
159 | 159 | function boot_class(superklass, constructor) {
|
160 | 160 | var alloc = boot_class_alloc(null, constructor, superklass)
|
161 |
| - var klass = boot_class_object(superklass, alloc); |
162 |
| - return klass; |
| 161 | + |
| 162 | + return boot_class_object(superklass, alloc); |
163 | 163 | }
|
164 | 164 |
|
165 | 165 | // Make `boot_class` available to the JS-API
|
|
174 | 174 | * newly constructed class.
|
175 | 175 | */
|
176 | 176 | function boot_class_object(superklass, alloc) {
|
177 |
| - // |
178 | 177 | var singleton_class = function() {};
|
179 | 178 | singleton_class.prototype = superklass.constructor.prototype;
|
180 | 179 |
|
|
218 | 217 | module.$$id = unique_id++;
|
219 | 218 |
|
220 | 219 | // @property $$proto This is the prototype on which methods will be defined
|
221 |
| - module.$$proto = prototype; |
| 220 | + module.$$proto = prototype; |
222 | 221 |
|
223 | 222 | // @property constructor keeps a ref to the constructor, but apparently the
|
224 | 223 | // constructor is already set on:
|
|
229 | 228 | module.constructor = constructor;
|
230 | 229 |
|
231 | 230 | // @property $$is_class Clearly mark this as a class-like
|
232 |
| - module.$$is_class = true; |
| 231 | + module.$$is_class = true; |
233 | 232 |
|
234 | 233 | // @property $$super the superclass, doesn't get changed by module inclusions
|
235 |
| - module.$$super = superklass; |
| 234 | + module.$$super = superklass; |
236 | 235 |
|
237 | 236 | // @property $$parent direct parent class or module
|
238 | 237 | // starts with the superclass, after module inclusion is
|
239 | 238 | // the last included module
|
240 |
| - module.$$parent = superklass; |
| 239 | + module.$$parent = superklass; |
241 | 240 |
|
242 | 241 | // @property $$methods keeps track of methods defined on the class
|
243 | 242 | // but seems to be used just by `define_basic_object_method`
|
244 | 243 | // and for donating (Ruby) Object methods to bridged classes
|
245 | 244 | // TODO: check if it can be removed
|
246 |
| - module.$$methods = []; |
| 245 | + module.$$methods = []; |
247 | 246 |
|
248 | 247 | // @property $$inc included modules
|
249 |
| - module.$$inc = []; |
| 248 | + module.$$inc = []; |
250 | 249 | }
|
251 | 250 |
|
252 |
| - |
253 |
| - |
254 | 251 | // Define new module (or return existing module)
|
255 | 252 | Opal.module = function(base, id) {
|
256 | 253 | var module;
|
|
294 | 291 | var module_prototype = {};
|
295 | 292 |
|
296 | 293 | setup_module_or_class_object(module, module_constructor, ModuleClass, module_prototype);
|
297 |
| - module.$$is_mod = true; |
298 |
| - module.$$dep = []; |
| 294 | + |
| 295 | + module.$$is_mod = true; |
| 296 | + module.$$dep = []; |
299 | 297 |
|
300 | 298 | return module;
|
301 | 299 | }
|
|
306 | 304 | * @param object [Ruby Object]
|
307 | 305 | */
|
308 | 306 | Opal.get_singleton_class = function(object) {
|
309 |
| - if (object.$$meta) return object.$$meta; |
310 |
| - if (object.$$is_class) return build_class_singleton_class(object); |
| 307 | + if (object.$$meta) { |
| 308 | + return object.$$meta; |
| 309 | + } |
| 310 | + |
| 311 | + if (object.$$is_class) { |
| 312 | + return build_class_singleton_class(object); |
| 313 | + } |
| 314 | + |
311 | 315 | return build_object_singleton_class(object);
|
312 | 316 | };
|
313 | 317 |
|
|
319 | 323 | */
|
320 | 324 | function build_class_singleton_class(klass) {
|
321 | 325 | var meta = new $opal.Class.$$alloc;
|
322 |
| - meta.$$class = $opal.Class; |
323 | 326 |
|
| 327 | + meta.$$class = $opal.Class; |
324 | 328 | meta.$$proto = klass.constructor.prototype;
|
325 | 329 |
|
326 | 330 | meta.$$is_singleton = true;
|
|
408 | 412 | function boot_class_alloc(id, constructor, superklass) {
|
409 | 413 | if (superklass) {
|
410 | 414 | var ctor = function() {};
|
411 |
| - ctor.prototype = superklass.$$proto || superklass.prototype; |
412 |
| - if (id) { ctor.displayName = id; } |
| 415 | + ctor.prototype = superklass.$$proto || superklass.prototype; |
| 416 | + |
| 417 | + if (id) { |
| 418 | + ctor.displayName = id; |
| 419 | + } |
| 420 | + |
413 | 421 | constructor.prototype = new ctor();
|
414 | 422 | }
|
415 | 423 |
|
|
600 | 608 | }
|
601 | 609 | };
|
602 | 610 |
|
603 |
| - |
604 | 611 | /*
|
605 | 612 | * Add a prototype to the subscribers list, and (TODO) add previously stubbed
|
606 | 613 | * methods.
|
|
881 | 888 | if (included_in) {
|
882 | 889 | for (var i = 0, length = included_in.length; i < length; i++) {
|
883 | 890 | var includee = included_in[i];
|
884 |
| - var dest = includee.$$proto; |
| 891 | + var dest = includee.$$proto; |
885 | 892 |
|
886 | 893 | for (var j = 0, jj = defined.length; j < jj; j++) {
|
887 | 894 | var method = defined[j];
|
| 895 | + |
888 | 896 | dest[method] = klass.$$proto[method];
|
889 | 897 | dest[method].$$donated = true;
|
890 | 898 | }
|
|
1028 | 1036 | return range;
|
1029 | 1037 | };
|
1030 | 1038 |
|
1031 |
| - |
1032 | 1039 | // Require system
|
1033 | 1040 | // --------------
|
| 1041 | + (function(Opal) { |
| 1042 | + var loaded_features = ['corelib/runtime.js'], |
| 1043 | + require_table = {'corelib/runtime.js': true}, |
| 1044 | + modules = {}; |
| 1045 | + |
| 1046 | + var current_dir = '.', |
| 1047 | + current_file = '.'; |
| 1048 | + |
| 1049 | + function mark_as_loaded(filename) { |
| 1050 | + if (require_table[filename]) { |
| 1051 | + return false; |
| 1052 | + } |
| 1053 | + |
| 1054 | + loaded_features.push(filename); |
| 1055 | + require_table[filename] = true; |
1034 | 1056 |
|
1035 |
| - Opal.mark_as_loaded = function(filename) { |
1036 |
| - if (!Opal.require_table[filename]) { |
1037 |
| - Opal.loaded_features.push(filename); |
1038 |
| - Opal.require_table[filename] = true; |
1039 | 1057 | return true;
|
1040 |
| - } else { |
1041 |
| - return false; |
1042 | 1058 | }
|
1043 |
| - }; |
1044 |
| - Opal.loaded_features = ['corelib/runtime.js']; |
1045 |
| - Opal.require_table = {'corelib/runtime.js': true}; |
1046 |
| - Opal.modules = {}; |
1047 |
| - Opal.dynamic_require_severity = null; |
1048 |
| - Opal.normalize_loadable_path = function(path) { |
1049 |
| - var current_dir = Opal.current_dir; |
1050 |
| - if (current_dir !== '.') { |
1051 |
| - current_dir = current_dir.replace(/\/*$/, '/'); |
1052 |
| - path = current_dir+path; |
| 1059 | + |
| 1060 | + function normalize_loadable_path(path) { |
| 1061 | + if (current_dir !== '.') { |
| 1062 | + path = current_dir.replace(/\/*$/, '/') + path; |
| 1063 | + } |
| 1064 | + |
| 1065 | + return path; |
1053 | 1066 | }
|
1054 |
| - return path; |
1055 |
| - }; |
1056 |
| - Opal.require = function(path) { |
1057 |
| - if (Opal.require_table[path]) { |
1058 |
| - return false; |
1059 |
| - } else { |
1060 |
| - return Opal.load(path); |
| 1067 | + |
| 1068 | + function load(path) { |
| 1069 | + mark_as_loaded(path); |
| 1070 | + |
| 1071 | + var module = modules[path]; |
| 1072 | + |
| 1073 | + if (module) { |
| 1074 | + var tmp = current_file; |
| 1075 | + current_file = path; |
| 1076 | + |
| 1077 | + module(Opal); |
| 1078 | + |
| 1079 | + current_file = tmp; |
| 1080 | + } |
| 1081 | + else { |
| 1082 | + var severity = Opal.dynamic_require_severity || 'warning'; |
| 1083 | + var message = 'cannot load such file -- ' + path; |
| 1084 | + |
| 1085 | + if (severity === "error") { |
| 1086 | + Opal.LoadError ? Opal.LoadError.$new(message) : function(){throw message}(); |
| 1087 | + } |
| 1088 | + else if (severity === "warning") { |
| 1089 | + Opal.gvars.stderr.$puts('WARNING: LoadError: ' + message); |
| 1090 | + } |
| 1091 | + } |
| 1092 | + |
| 1093 | + return true; |
1061 | 1094 | }
|
1062 |
| - }; |
1063 |
| - Opal.current_dir = '.'; |
1064 |
| - Opal.current_file = '.'; |
1065 |
| - Opal.rb_load_error = function rb_load_error(message) {this.message = message;}; |
1066 |
| - Opal.load = function(path) { |
1067 |
| - var module, old_path; |
1068 |
| - Opal.mark_as_loaded(path); |
1069 |
| - module = Opal.modules[path]; |
1070 |
| - if (module) { |
1071 |
| - old_path = Opal.current_file; |
1072 |
| - Opal.current_file = path; |
1073 |
| - module(Opal); |
1074 |
| - Opal.current_file = old_path; |
1075 |
| - } else { |
1076 |
| - var severity = Opal.dynamic_require_severity || 'warning'; |
1077 |
| - var message = 'cannot load such file -- '+path; |
1078 |
| - if (severity === "error" ) Opal.LoadError ? Opal.LoadError.$new(message) : function(){throw message}(); |
1079 |
| - else if (severity === "warning") Opal.gvars.stderr.$puts('WARNING: LoadError: '+message); |
| 1095 | + |
| 1096 | + function require(path) { |
| 1097 | + if (require_table[path]) { |
| 1098 | + return false; |
| 1099 | + } |
| 1100 | + |
| 1101 | + return load(path); |
1080 | 1102 | }
|
1081 |
| - return true; |
1082 |
| - }; |
1083 | 1103 |
|
| 1104 | + Opal.modules = modules; |
| 1105 | + Opal.loaded_features = loaded_features; |
| 1106 | + |
| 1107 | + Opal.normalize_loadable_path = normalize_loadable_path; |
| 1108 | + Opal.mark_as_loaded = mark_as_loaded; |
| 1109 | + |
| 1110 | + Opal.load = load; |
| 1111 | + Opal.require = require; |
| 1112 | + })(Opal); |
1084 | 1113 |
|
1085 | 1114 | // Initialization
|
1086 | 1115 | // --------------
|
|
1113 | 1142 | function NilClass(){}
|
1114 | 1143 |
|
1115 | 1144 | // Constructors for *instances* of core objects
|
1116 |
| - // (id, constructor, superklass) |
1117 | 1145 | boot_class_alloc('BasicObject', BasicObject);
|
1118 | 1146 | boot_class_alloc('Object', Object, BasicObject);
|
1119 | 1147 | boot_class_alloc('Module', Module, Object);
|
|
0 commit comments