Skip to content

Commit b658f09

Browse files
committedOct 10, 2014
Some coding style cleanup in the runtime
1 parent c84f507 commit b658f09

File tree

2 files changed

+100
-72
lines changed

2 files changed

+100
-72
lines changed
 

‎lib/opal/builder_processors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def source_map
5050
end
5151

5252
def mark_as_required(filename)
53-
"Opal.mark_as_loaded( Opal.normalize_loadable_path(#{filename.to_s.inspect}) );"
53+
"Opal.mark_as_loaded(Opal.normalize_loadable_path(#{filename.to_s.inspect}));"
5454
end
5555
end
5656

‎opal/corelib/runtime.js

+99-71
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@
5555
* scope will be the outer scope of the new klass.
5656
*/
5757
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+
6164
const_scope.base = klass;
62-
klass.$$base_module = base.base;
6365
const_scope.constructor = const_alloc;
6466
const_scope.constants = [];
6567

@@ -98,7 +100,6 @@
98100
* @return [Class] new or existing ruby class
99101
*/
100102
Opal.klass = function(base, superklass, id, constructor) {
101-
102103
// If base is an object, use its class
103104
if (!base.$$is_class) {
104105
base = base.$$class;
@@ -113,7 +114,6 @@
113114

114115
// If a constant exists in the scope, then we must use that
115116
if ($hasOwn.call(base.$$scope, id) && klass.$$orig_scope === base.$$scope) {
116-
117117
// Make sure the existing constant is a class, or raise error
118118
if (!klass.$$is_class) {
119119
throw Opal.TypeError.$new(id + " is not a class");
@@ -158,8 +158,8 @@
158158
// Create generic class with given superclass.
159159
function boot_class(superklass, constructor) {
160160
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);
163163
}
164164

165165
// Make `boot_class` available to the JS-API
@@ -174,7 +174,6 @@
174174
* newly constructed class.
175175
*/
176176
function boot_class_object(superklass, alloc) {
177-
//
178177
var singleton_class = function() {};
179178
singleton_class.prototype = superklass.constructor.prototype;
180179

@@ -218,7 +217,7 @@
218217
module.$$id = unique_id++;
219218

220219
// @property $$proto This is the prototype on which methods will be defined
221-
module.$$proto = prototype;
220+
module.$$proto = prototype;
222221

223222
// @property constructor keeps a ref to the constructor, but apparently the
224223
// constructor is already set on:
@@ -229,28 +228,26 @@
229228
module.constructor = constructor;
230229

231230
// @property $$is_class Clearly mark this as a class-like
232-
module.$$is_class = true;
231+
module.$$is_class = true;
233232

234233
// @property $$super the superclass, doesn't get changed by module inclusions
235-
module.$$super = superklass;
234+
module.$$super = superklass;
236235

237236
// @property $$parent direct parent class or module
238237
// starts with the superclass, after module inclusion is
239238
// the last included module
240-
module.$$parent = superklass;
239+
module.$$parent = superklass;
241240

242241
// @property $$methods keeps track of methods defined on the class
243242
// but seems to be used just by `define_basic_object_method`
244243
// and for donating (Ruby) Object methods to bridged classes
245244
// TODO: check if it can be removed
246-
module.$$methods = [];
245+
module.$$methods = [];
247246

248247
// @property $$inc included modules
249-
module.$$inc = [];
248+
module.$$inc = [];
250249
}
251250

252-
253-
254251
// Define new module (or return existing module)
255252
Opal.module = function(base, id) {
256253
var module;
@@ -294,8 +291,9 @@
294291
var module_prototype = {};
295292

296293
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 = [];
299297

300298
return module;
301299
}
@@ -306,8 +304,14 @@
306304
* @param object [Ruby Object]
307305
*/
308306
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+
311315
return build_object_singleton_class(object);
312316
};
313317

@@ -319,8 +323,8 @@
319323
*/
320324
function build_class_singleton_class(klass) {
321325
var meta = new $opal.Class.$$alloc;
322-
meta.$$class = $opal.Class;
323326

327+
meta.$$class = $opal.Class;
324328
meta.$$proto = klass.constructor.prototype;
325329

326330
meta.$$is_singleton = true;
@@ -408,8 +412,12 @@
408412
function boot_class_alloc(id, constructor, superklass) {
409413
if (superklass) {
410414
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+
413421
constructor.prototype = new ctor();
414422
}
415423

@@ -600,7 +608,6 @@
600608
}
601609
};
602610

603-
604611
/*
605612
* Add a prototype to the subscribers list, and (TODO) add previously stubbed
606613
* methods.
@@ -881,10 +888,11 @@
881888
if (included_in) {
882889
for (var i = 0, length = included_in.length; i < length; i++) {
883890
var includee = included_in[i];
884-
var dest = includee.$$proto;
891+
var dest = includee.$$proto;
885892

886893
for (var j = 0, jj = defined.length; j < jj; j++) {
887894
var method = defined[j];
895+
888896
dest[method] = klass.$$proto[method];
889897
dest[method].$$donated = true;
890898
}
@@ -1028,59 +1036,80 @@
10281036
return range;
10291037
};
10301038

1031-
10321039
// Require system
10331040
// --------------
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;
10341056

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;
10391057
return true;
1040-
} else {
1041-
return false;
10421058
}
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;
10531066
}
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;
10611094
}
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);
10801102
}
1081-
return true;
1082-
};
10831103

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);
10841113

10851114
// Initialization
10861115
// --------------
@@ -1113,7 +1142,6 @@
11131142
function NilClass(){}
11141143

11151144
// Constructors for *instances* of core objects
1116-
// (id, constructor, superklass)
11171145
boot_class_alloc('BasicObject', BasicObject);
11181146
boot_class_alloc('Object', Object, BasicObject);
11191147
boot_class_alloc('Module', Module, Object);

0 commit comments

Comments
 (0)
Please sign in to comment.