opal/corelib/runtime.js in opal-0.7.0.beta1 vs opal/corelib/runtime.js in opal-0.7.0.beta2

- old
+ new

@@ -3,11 +3,11 @@ console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.'); return this.Opal; } // The Opal object that is exposed globally - var Opal = this.Opal = {}, $opal = Opal; + var Opal = this.Opal = {}; // All bridged classes - keep track to donate methods from Object var bridged_classes = Opal.bridged_classes = []; // TopScope is used for inheriting constants from the top scope @@ -325,13 +325,13 @@ * * NOTE: Actually in MRI a class' singleton class inherits from its * superclass' singleton class which in turn inherits from Class; */ function build_class_singleton_class(klass) { - var meta = new $opal.Class.$$alloc; + var meta = new Opal.Class.$$alloc; - meta.$$class = $opal.Class; + meta.$$class = Opal.Class; meta.$$proto = klass.constructor.prototype; meta.$$is_singleton = true; meta.$$inc = []; meta.$$methods = []; @@ -404,17 +404,16 @@ prototype[method].$$donated = true; } } if (klass.$$dep) { - $opal.donate(klass, methods.slice(), true); + Opal.donate(klass, methods.slice(), true); } - $opal.donate_constants(module, klass); + Opal.donate_constants(module, klass); }; - // Boot a base class (makes instances). function boot_class_alloc(id, constructor, superklass) { if (superklass) { var ctor = function() {}; ctor.prototype = superklass.$$proto || superklass.prototype; @@ -748,17 +747,17 @@ * an expression, or even inside a block which must "return" to the outer * method. This helper simply throws an error which is then caught by the * method. This approach is expensive, so it is only used when absolutely * needed. */ - Opal.$return = function(val) { + Opal.ret = function(val) { Opal.returner.$v = val; throw Opal.returner; }; // handles yield calls for 1 yielded arg - Opal.$yield1 = function(block, arg) { + Opal.yield1 = function(block, arg) { if (typeof(block) !== "function") { throw Opal.LocalJumpError.$new("no block given"); } if (block.length > 1 && arg.$$is_array) { @@ -768,11 +767,11 @@ return block(arg); } }; // handles yield for > 1 yielded arg - Opal.$yieldX = function(block, args) { + Opal.yieldX = function(block, args) { if (typeof(block) !== "function") { throw Opal.LocalJumpError.$new("no block given"); } if (block.length > 1 && args.length == 1) { @@ -788,23 +787,26 @@ return block.apply(null, args); }; // Finds the corresponding exception match in candidates. Each candidate can // be a value, or an array of values. Returns null if not found. - Opal.$rescue = function(exception, candidates) { - for (var i = 0; i != candidates.length; i++) { + Opal.rescue = function(exception, candidates) { + for (var i = 0; i < candidates.length; i++) { var candidate = candidates[i]; + if (candidate.$$is_array) { - var subresult = Opal.$rescue(exception, candidate); - if (subresult) { - return subresult; + var result = Opal.rescue(exception, candidate); + + if (result) { + return result; } } else if (candidate['$==='](exception)) { return candidate; } } + return null; }; Opal.is_a = function(object, klass) { if (object.$$meta === klass) { @@ -911,10 +913,14 @@ Opal.defn = function(obj, jsid, body) { if (obj.$$is_mod) { obj.$$proto[jsid] = body; Opal.donate(obj, [jsid]); + + if (obj.$$module_function) { + obj[jsid] = body; + } } else if (obj.$$is_class) { obj.$$proto[jsid] = body; if (obj === BasicObjectClass) { @@ -953,16 +959,18 @@ Opal.hash = function() { if (arguments.length == 1 && arguments[0].$$class == Opal.Hash) { return arguments[0]; } - var hash = new Opal.Hash.$$alloc(), - keys = [], - assocs = {}, - key, obj, length; + var hash = new Opal.Hash.$$alloc(), + keys = [], + _map = {}, + smap = {}, + key, obj, length, khash; - hash.map = assocs; + hash.map = _map; + hash.smap = smap; hash.keys = keys; if (arguments.length == 1) { if (arguments[0].$$is_array) { var args = arguments[0]; @@ -975,21 +983,30 @@ } key = pair[0]; obj = pair[1]; - if (assocs[key] == null) { + if (key.$$is_string) { + khash = key; + map = smap; + } else { + khash = key.$hash(); + map = _map; + } + + if (map[khash] == null) { keys.push(key); } - assocs[key] = obj; + map[khash] = obj; } } else { obj = arguments[0]; for (key in obj) { - assocs[key] = obj[key]; + khash = key.$hash(); + map[khash] = obj[khash]; keys.push(key); } } } else { @@ -1000,15 +1017,23 @@ for (var j = 0; j < length; j++) { key = arguments[j]; obj = arguments[++j]; - if (assocs[key] == null) { + if (key.$$is_string) { + khash = key; + map = smap; + } else { + khash = key.$hash(); + map = _map; + } + + if (map[khash] == null) { keys.push(key); } - assocs[key] = obj; + map[khash] = obj; } } return hash; }; @@ -1021,11 +1046,12 @@ */ Opal.hash2 = function(keys, map) { var hash = new Opal.Hash.$$alloc(); hash.keys = keys; - hash.map = map; + hash.map = {}; + hash.smap = map; return hash; }; /* @@ -1046,12 +1072,11 @@ (function(Opal) { var loaded_features = ['corelib/runtime.js'], require_table = {'corelib/runtime.js': true}, modules = {}; - var current_dir = '.', - current_file = '.'; + var current_dir = '.'; function mark_as_loaded(filename) { if (require_table[filename]) { return false; } @@ -1071,10 +1096,11 @@ parts = path.split(SEPARATOR); for (var i = 0, ii = parts.length; i < ii; i++) { part = parts[i]; + if (part == '') continue; (part === '..') ? new_parts.pop() : new_parts.push(part) } return new_parts.join(SEPARATOR); } @@ -1083,26 +1109,21 @@ mark_as_loaded(path); var module = modules[path]; if (module) { - var tmp = current_file; - current_file = path; - module(Opal); - - current_file = tmp; } else { var severity = Opal.dynamic_require_severity || 'warning'; var message = 'cannot load such file -- ' + path; if (severity === "error") { Opal.LoadError ? Opal.LoadError.$new(message) : function(){throw message}(); } else if (severity === "warning") { - Opal.gvars.stderr.$puts('WARNING: LoadError: ' + message); + console.warn('WARNING: LoadError: ' + message); } } return true; } @@ -1121,12 +1142,10 @@ Opal.normalize_loadable_path = normalize_loadable_path; Opal.mark_as_loaded = mark_as_loaded; Opal.load = load; Opal.require = require; - - Opal.current_file = current_file; })(Opal); // Initialization // -------------- @@ -1208,12 +1227,14 @@ ObjectClass.$$proto.$require = Opal.require; Opal.top = new ObjectClass.$$alloc(); + // Nil + var nil_id = Opal.uid(); // nil id is traditionally 4 Opal.klass(ObjectClass, ObjectClass, 'NilClass', NilClass); - var nil = Opal.nil = new NilClass(); + nil.$$id = nil_id; nil.call = nil.apply = function() { throw Opal.LocalJumpError.$new('no block given'); }; Opal.breaker = new Error('unexpected break'); Opal.returner = new Error('unexpected return');