From 82a5d6d2929e774271c5ff3a59cef0e0f916d654 Mon Sep 17 00:00:00 2001 From: Adam Beynon Date: Sun, 3 Jun 2012 16:42:36 +0100 Subject: [PATCH] Revert "Map nil to native null object." This reverts commit 896f5f445d03a9e4ecbbdab76abbd5025429db0f. --- core/array.rb | 52 ++++++++++++++-------------- core/basic_object.rb | 4 +-- core/boolean.rb | 6 ++-- core/class.rb | 8 ++--- core/enumerable.rb | 56 +++++++++++++++--------------- core/hash.rb | 34 +++++++++--------- core/kernel.rb | 8 ++--- core/module.rb | 14 ++++---- core/nil_class.rb | 6 ++-- core/proc.rb | 2 +- core/regexp.rb | 2 +- core/string.rb | 10 +++--- lib/opal/parser.rb | 59 +++++++++++++++----------------- lib/opal/scope.rb | 12 +++---- test/core/array/allocate_spec.rb | 2 -- 15 files changed, 133 insertions(+), 142 deletions(-) diff --git a/core/array.rb b/core/array.rb index a68367b9a8..431806ab7f 100644 --- a/core/array.rb +++ b/core/array.rb @@ -120,7 +120,7 @@ def [](index, length = undefined) index = index.begin; if (index > size) { - return null; + return nil; } if (length < 0) { @@ -141,14 +141,14 @@ def [](index, length = undefined) if (length !== undefined) { if (length < 0 || index > size || index < 0) { - return null; + return nil; } return this.slice(index, index + length); } else { if (index >= size || index < 0) { - return null; + return nil; } return this[index]; @@ -177,7 +177,7 @@ def assoc(object) } } - return null; + return nil; } end @@ -188,7 +188,7 @@ def at(index) } if (index < 0 || index >= this.length) { - return null; + return nil; } return this[index]; @@ -244,7 +244,7 @@ def compact var result = []; for (var i = 0, length = this.length, item; i < length; i++) { - if ((item = this[i]) !== null) { + if ((item = this[i]) !== nil) { result.push(item); } } @@ -258,7 +258,7 @@ def compact! var original = this.length; for (var i = 0, length = this.length; i < length; i++) { - if (this[i] === null) { + if (this[i] === nil) { this.splice(i, 1); length--; @@ -266,7 +266,7 @@ def compact! } } - return this.length === original ? null : this; + return this.length === original ? nil : this; } end @@ -311,7 +311,7 @@ def delete(object) } } - return this.length === original ? null : object; + return this.length === original ? nil : object; } end @@ -322,7 +322,7 @@ def delete_at(index) } if (index < 0 || index >= this.length) { - return null; + return nil; } var result = this[index]; @@ -342,7 +342,7 @@ def delete_if(&block) return __breaker.$v; } - if (value !== false && value !== null) { + if (value !== false && value !== nil) { this.splice(i, 1); length--; @@ -367,7 +367,7 @@ def drop_while(&block) return $breaker.$v; } - if (value === false || value === null) { + if (value === false || value === nil) { return this.slice(i); } } @@ -429,7 +429,7 @@ def fetch(index, defaults = undefined, &block) } if (block !== null) { - return block.call($context, null, original); + return block.call($context, nil, original); } throw RubyIndexError.$new('Array#fetch'); @@ -442,7 +442,7 @@ def first(count = undefined) return this.slice(0, count); } - return this.length === 0 ? null : this[0]; + return this.length === 0 ? nil : this[0]; } end @@ -478,7 +478,7 @@ def flatten!(level = undefined) var size = this.length; #{replace flatten level}; - return size === this.length ? null : this; + return size === this.length ? nil : this; } end @@ -580,7 +580,7 @@ def insert(index, *objects) } if (index > this.length) { for (var i = this.length; i < index; i++) { - this.push(null); + this.push(nil); } } @@ -640,7 +640,7 @@ def last(count = undefined) var length = this.length; if (count === undefined) { - return length === 0 ? null : this[length - 1]; + return length === 0 ? nil : this[length - 1]; } else if (count < 0) { throw RubyArgError.$new('negative count given'); @@ -667,7 +667,7 @@ def pop(count = undefined) var length = this.length; if (count === undefined) { - return length === 0 ? null : this.pop(); + return length === 0 ? nil : this.pop(); } if (count < 0) { @@ -700,7 +700,7 @@ def rassoc(object) } } - return null; + return nil; } end @@ -715,7 +715,7 @@ def reject(&block) return __breaker.$v; } - if (value === false || value === null) { + if (value === false || value === nil) { result.push(this[i]); } } @@ -734,7 +734,7 @@ def reject!(&block) return __breaker.$v; } - if (value !== false && value !== null) { + if (value !== false && value !== nil) { this.splice(i, 1); length--; @@ -742,7 +742,7 @@ def reject!(&block) } } - return original === this.length ? null : this; + return original === this.length ? nil : this; } end @@ -949,7 +949,7 @@ def uniq! } } - return this.length === original ? null : this; + return this.length === original ? nil : this; } end @@ -974,7 +974,7 @@ def zip(*others, &block) o = others[j][i]; if (o === undefined) { - o = null; + o = nil; } part[j + 1] = o; @@ -983,12 +983,12 @@ def zip(*others, &block) result[i] = part; } - if (block !== null) { + if (block !== nil) { for (var i = 0; i < size; i++) { block.call(__context, result[i]); } - return null; + return nil; } return result; diff --git a/core/basic_object.rb b/core/basic_object.rb index 24faeea2a8..092d8e1248 100644 --- a/core/basic_object.rb +++ b/core/basic_object.rb @@ -21,7 +21,7 @@ def __send__(symbol, *args, &block) def instance_eval(string, &block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } @@ -31,7 +31,7 @@ def instance_eval(string, &block) def instance_exec(*args, &block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } diff --git a/core/boolean.rb b/core/boolean.rb index f862b0dcb3..65a9c0648a 100644 --- a/core/boolean.rb +++ b/core/boolean.rb @@ -4,15 +4,15 @@ class Boolean < `Boolean` } def &(other) - `(this == true) ? (other !== false && other != null) : false` + `(this == true) ? (other !== false && other !== nil) : false` end def |(other) - `(this == true) ? true : (other !== false && other != null)` + `(this == true) ? true : (other !== false && other !== nil)` end def ^(other) - `(this == true) ? (other === false || other == null) : (other !== false && other != null)` + `(this == true) ? (other === false || other === nil) : (other !== false && other !== nil)` end def ==(other) diff --git a/core/class.rb b/core/class.rb index 3f9845b0ab..a93fc30ad0 100644 --- a/core/class.rb +++ b/core/class.rb @@ -2,13 +2,13 @@ class Class def self.new(sup = Object, &block) %x{ var klass = boot_class(sup); - klass._name = null; + klass._name = nil; make_metaclass(klass, sup._klass); sup.$inherited(klass); - if (block != null) { + if (block !== nil) { block.call(klass); } @@ -38,7 +38,7 @@ def superclass if (!sup) { if (this === RubyBasicObject) { - return null; + return nil; } throw RubyRuntimeError.$new('uninitialized class'); @@ -49,7 +49,7 @@ def superclass } if (!sup) { - return null; + return nil; } return sup; diff --git a/core/enumerable.rb b/core/enumerable.rb index b46fd185c2..a017e1ad89 100644 --- a/core/enumerable.rb +++ b/core/enumerable.rb @@ -3,7 +3,7 @@ def all?(&block) %x{ var result = true, proc; - if (block != null) { + if (block !== nil) { proc = function(obj) { var value; @@ -11,9 +11,9 @@ def all?(&block) return __breaker.$v; } - if (value === false || value == null) { + if (value === false || value === nil) { result = false; - __breaker.$v = null; + __breaker.$v = nil; return __breaker; } @@ -21,9 +21,9 @@ def all?(&block) } else { proc = function(obj) { - if (obj === false || obj == null) { + if (obj === false || obj === nil) { result = false; - __breaker.$v = null; + __breaker.$v = nil; return __breaker; } @@ -41,7 +41,7 @@ def any?(&block) %x{ var result = false, proc; - if (block != null) { + if (block !== nil) { proc = function(obj) { var value; @@ -49,9 +49,9 @@ def any?(&block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { result = true; - __breaker.$v = null; + __breaker.$v = nil; return __breaker; } @@ -59,9 +59,9 @@ def any?(&block) } else { proc = function(obj) { - if (obj !== false && obj != null) { + if (obj !== false && obj !== nil) { result = true; - __breaker.$v = null; + __breaker.$v = nil; return __breaker; } @@ -102,7 +102,7 @@ def count(object = undefined, &block) %x{ var result = 0; - if (block == null) { + if (block === nil) { if (object == null) { block = function() { return true; }; } @@ -118,7 +118,7 @@ def count(object = undefined, &block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { result++; } } @@ -134,7 +134,7 @@ def detect(ifnone = undefined, &block) return enum_for :detect, ifnone unless block %x{ - var result = null; + var result = nil; this.$each._p = function(obj) { var value; @@ -143,9 +143,9 @@ def detect(ifnone = undefined, &block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { result = obj; - __breaker.$v = null; + __breaker.$v = nil; return __breaker; } @@ -153,7 +153,7 @@ def detect(ifnone = undefined, &block) this.$each(); - if (result != null) { + if (result !== nil) { return result; } @@ -161,7 +161,7 @@ def detect(ifnone = undefined, &block) return ifnone.$call(); } - return ifnone === undefined ? null : ifnone; + return ifnone === undefined ? nil : ifnone; } end @@ -197,7 +197,7 @@ def drop_while(&block) return __breaker; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { result.push(obj); } else { @@ -229,7 +229,7 @@ def each_with_index(&block) this.$each(); - return null; + return nil; } end @@ -280,9 +280,9 @@ def find_all(&block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { //result = obj; - //__breaker.$v = null; + //__breaker.$v = nil; //return __breaker; result.push(obj); @@ -297,7 +297,7 @@ def find_all(&block) def find_index(object = undefined, &block) %x{ - var proc, result = null, index = 0; + var proc, result = nil, index = 0; if (object != null) { proc = function (obj) { @@ -308,7 +308,7 @@ def find_index(object = undefined, &block) index += 1; }; } - else if (block == null) { + else if (block === nil) { return this.$enum_for("find_index"); } else { proc = function(obj) { @@ -318,7 +318,7 @@ def find_index(object = undefined, &block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { result = index; __breaker.$v = index; @@ -343,7 +343,7 @@ def first(number = undefined) proc; if (number == null) { - result = null; + result = nil; proc = function(obj) { result = obj; return __breaker; }; @@ -371,11 +371,11 @@ def grep(pattern, &block) %x{ var result = []; - this.$each._p = (block != null + this.$each._p = (block !== nil ? function(obj) { var value = pattern.$eqq$(obj); - if (value !== false && value != null) { + if (value !== false && value !== nil) { if ((value = block.call(__context, obj)) === __breaker) { return __breaker.$v; } @@ -386,7 +386,7 @@ def grep(pattern, &block) : function(obj) { var value = pattern.$eqq$(obj); - if (value !== false && value != null) { + if (value !== false && value !== nil) { result.push(obj); } }); diff --git a/core/hash.rb b/core/hash.rb index 9ffe526810..957d31d0b6 100644 --- a/core/hash.rb +++ b/core/hash.rb @@ -10,8 +10,8 @@ class Hash assocs = {}; hash.map = assocs; - hash.none = null; - hash.proc = null; + hash.none = nil; + hash.proc = nil; if (args.length == 1 && args[0]._isArray) { args = args[0]; @@ -52,7 +52,7 @@ def self.new(defaults = undefined, &block) if (defaults != null) { hash.none = defaults; } - else if (block != null) { + else if (block !== nil) { hash.proc = block; } @@ -120,7 +120,7 @@ def assoc(object) } } - return null; + return nil; } end @@ -190,7 +190,7 @@ def delete_if(&block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { delete map[assoc]; } } @@ -277,7 +277,7 @@ def fetch(key, defaults = undefined, &block) return bucket[1]; } - if (block != null) { + if (block !== nil) { var value; if ((value = block.call(__context, key)) === __breaker) { @@ -287,7 +287,7 @@ def fetch(key, defaults = undefined, &block) return value; } - if (defaults !== undefined) { + if (defaults != null) { return defaults; } @@ -356,7 +356,7 @@ def index(object) } } - return null; + return nil; } end @@ -424,7 +424,7 @@ def keep_if(&block) return $breaker.$v; } - if (value === false || value == null) { + if (value === false || value === nil) { delete map[assoc]; } } @@ -477,7 +477,7 @@ def merge(other, &block) map = other.map; - if (block == null) { + if (block === nil) { for (var assoc in map) { var bucket = map[assoc]; @@ -505,7 +505,7 @@ def merge!(other, &block) var map = this.map, map2 = other.map; - if (block == null) { + if (block === nil) { for (var assoc in map2) { var bucket = map2[assoc]; @@ -540,7 +540,7 @@ def rassoc(object) } } - return null; + return nil; } end @@ -558,7 +558,7 @@ def reject(&block) return __breaker.$v; } - if (value === false || value == null) { + if (value === false || value === nil) { map2[bucket[0]] = [bucket[0], bucket[1]]; } } @@ -595,7 +595,7 @@ def select(&block) return __breaker.$v; } - if (value !== false && value != null) { + if (value !== false && value !== nil) { map2[bucket[0]] = [bucket[0], bucket[1]]; } } @@ -608,7 +608,7 @@ def select!(&block) return enum_for :select! unless block_given? %x{ - var map = this.map, result = null; + var map = this.map, result = nil; for (var assoc in map) { var bucket = map[assoc], @@ -618,7 +618,7 @@ def select!(&block) return __breaker.$v; } - if (value === false || value == null) { + if (value === false || value === nil) { delete map[assoc]; result = this; } @@ -638,7 +638,7 @@ def shift return [bucket[0], bucket[1]]; } - return null; + return nil; } end diff --git a/core/kernel.rb b/core/kernel.rb index 3f8b4627d0..c3f267b43f 100644 --- a/core/kernel.rb +++ b/core/kernel.rb @@ -35,7 +35,7 @@ def class def define_singleton_method(name, &body) %x{ - if (body == null) { + if (body === nil) { no_block_given(); } @@ -84,7 +84,7 @@ def instance_variable_get(name) %x{ var ivar = this[name.substr(1)]; - return ivar == null ? null : ivar; + return ivar == null ? nil : ivar; } end @@ -158,7 +158,7 @@ def private(*) def proc(&block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } @@ -235,7 +235,7 @@ def singleton_class def tap(&block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } diff --git a/core/module.rb b/core/module.rb index 73a6641b09..d071e8f788 100644 --- a/core/module.rb +++ b/core/module.rb @@ -92,7 +92,7 @@ def append_features(klass) klass._alloc.prototype[get_jsid] = function() { var res = this[name]; - return res == null ? null : res; + return res == null ? nil : res; }; __donate(klass, [get_jsid]); @@ -116,7 +116,7 @@ def attr_accessor(*attrs) define_attr(this, attrs[i], true, true); } - return null; + return nil; } end @@ -126,7 +126,7 @@ def attr_reader(*attrs) define_attr(this, attrs[i], true, false); } - return null; + return nil; } end @@ -136,7 +136,7 @@ def attr_writer(*attrs) define_attr(this, attrs[i], false, true); } - return null; + return nil; } end @@ -148,7 +148,7 @@ def attr(name, setter = false) def define_method(name, &block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } @@ -158,7 +158,7 @@ def define_method(name, &block) this._alloc.prototype[jsid] = block; __donate(this, [jsid]); - return null; + return nil; } end @@ -187,7 +187,7 @@ def included(mod) def module_eval(&block) %x{ - if (block == null) { + if (block === nil) { no_block_given(); } diff --git a/core/nil_class.rb b/core/nil_class.rb index cb9c7a2a67..31ec143f9a 100644 --- a/core/nil_class.rb +++ b/core/nil_class.rb @@ -4,15 +4,15 @@ def &(other) end def |(other) - `other !== false && other != null` + `other !== false && other !== nil` end def ^(other) - `other !== false && other != null` + `other !== false && other !== nil` end def ==(other) - `other == null` + `other === nil` end def inspect diff --git a/core/proc.rb b/core/proc.rb index 3dc2a5eb3d..456d37058e 100644 --- a/core/proc.rb +++ b/core/proc.rb @@ -4,7 +4,7 @@ class Proc < `Function` } def self.new(&block) - `if (block == null) no_block_given();` + `if (block === nil) no_block_given();` block end diff --git a/core/regexp.rb b/core/regexp.rb index ca2388bbf0..a04200b342 100644 --- a/core/regexp.rb +++ b/core/regexp.rb @@ -29,7 +29,7 @@ def =~(string) #{$~ = nil}; } - return result ? result.index : null; + return result ? result.index : nil; } end diff --git a/core/string.rb b/core/string.rb index dbcd8f69cb..e23d302c35 100644 --- a/core/string.rb +++ b/core/string.rb @@ -48,7 +48,7 @@ def +(other) def <=>(other) %x{ if (typeof other !== 'string') { - return null; + return nil; } return this > other ? 1 : (this < other ? -1 : 0); @@ -97,7 +97,7 @@ def [](index, length = undefined) } if (index >= this.length || index < 0) { - return null; + return nil; } return this.substr(index, 1); @@ -108,7 +108,7 @@ def [](index, length = undefined) } if (index > this.length || index < 0) { - return null; + return nil; } return this.substr(index, length); @@ -269,7 +269,7 @@ def index(what, offset = undefined) } } - return result === -1 ? null : result; + return result === -1 ? nil : result; } end @@ -382,7 +382,7 @@ def sub(pattern, replace = undefined, &block) if (typeof(replace) === 'string') { return this.replace(pattern, replace); } - if (block != null) { + if (block !== nil) { return this.replace(pattern, function(str) { //$opal.match_data = arguments diff --git a/lib/opal/parser.rb b/lib/opal/parser.rb index 5e3d3a92c4..71433675be 100644 --- a/lib/opal/parser.rb +++ b/lib/opal/parser.rb @@ -101,8 +101,7 @@ def parse(source, file = '(file)') @file = file @helpers = { :breaker => true, - :slice => true, - :nil => true + :slice => true } @grammar = Grammar.new @@ -154,6 +153,7 @@ def top(sexp, options = {}) vars << "__opal = Opal" vars << "__scope = __opal" + vars << "nil = __opal.nil" vars.concat @helpers.keys.map { |h| "__#{h} = __opal.#{h}" } code = "var #{vars.join ', '};\n" + @scope.to_vars + "\n" + code @@ -290,7 +290,7 @@ def process_scope(sexp, level) stmt = returns stmt unless @scope.donates_methods code = process stmt, :stmt else - code = "null" + code = "nil" end code @@ -324,7 +324,7 @@ def process_operator(sexp, level) def js_block_given(sexp, level) @scope.uses_block! - "(#{@scope.block_name} != null)" + "(#{@scope.block_name} !== nil)" end # s(:lit, 1) @@ -399,7 +399,7 @@ def process_defined(sexp, level) when :call mid = mid_to_jsid part[2].to_s recv = part[1] ? process(part[1], :expr) : 'this' - "(#{recv}.#{mid} ? 'method' : null)" + "(#{recv}.#{mid} ? 'method' : nil)" else raise "bad defined? part: #{part[0]}" end @@ -445,7 +445,7 @@ def process_iter(sexp, level) args[1..-1].each do |arg| arg = arg[1] arg = "#{arg}$" if RESERVED.include? arg.to_s - # code += "if (#{arg} == null) #{arg} = null;\n" + code += "if (#{arg} == null) #{arg} = nil;\n" end params = js_block_args(args[1..-1]) @@ -522,7 +522,7 @@ def attr_optimize(meth, attrs) unless meth == :attr_writer attr = mid_to_jsid ivar - check = "this.#{ivar} == null ? null : this.#{ivar}" + check = "this.#{ivar} == null ? nil : this.#{ivar}" out << "def.#{attr} = function() { return #{check}; }" end @@ -563,8 +563,6 @@ def process_call(sexp, level) splat = arglist[1..-1].any? { |a| a.first == :splat } - tmprecv = @scope.new_temp - if Array === arglist.last and arglist.last.first == :block_pass block = process s(:js_tmp, process(arglist.pop, :expr)), :expr elsif iter @@ -573,11 +571,11 @@ def process_call(sexp, level) recv ||= [:self] - # if block - - # elsif splat or true# and recv != [:self] and recv[0] != :lvar - # tmprecv = @scope.new_temp - # end + if block + tmprecv = @scope.new_temp + elsif splat and recv != [:self] and recv[0] != :lvar + tmprecv = @scope.new_temp + end recv_code = process recv, :recv args = "" @@ -596,7 +594,7 @@ def process_call(sexp, level) "%s(%s))" % [dispatch, args] end else - dispatch = tmprecv ? "((#{tmprecv} = #{recv_code}) == null ? __nil : #{tmprecv}).#{mid}" : "(#{recv_code} == null ? __nil : #{recv_code}).#{mid}" + dispatch = tmprecv ? "(#{tmprecv} = #{recv_code}).#{mid}" : "#{recv_code}.#{mid}" splat ? "#{dispatch}.apply(#{tmprecv || recv_code}, #{args})" : "#{dispatch}(#{args})" end end @@ -816,7 +814,7 @@ def js_def(recvr, mid, args, stmts, line, end_line) if @scope.uses_block? @scope.add_temp '__context' @scope.add_temp yielder - blk = "\n#{@indent}#{yielder} = #{scope_name}._p || null;\n#{@indent}__context = (#{yielder} && #{yielder}._s)" + blk = "\n#{@indent}#{yielder} = #{scope_name}._p || nil;\n#{@indent}__context = #{yielder}._s" blk += ";\n#{@indent}#{scope_name}._p = null;\n#{@indent}" code = blk + code end @@ -883,13 +881,10 @@ def process_self(sexp, level) 'this' end - def process_nil(sexp, level) - 'null' - end - # s(:true) # => true # s(:false) # => false - %w(true false).each do |name| + # s(:nil) # => nil + %w(true false nil).each do |name| define_method "process_#{name}" do |exp, level| name end @@ -965,7 +960,7 @@ def process_while(sexp, level) @scope.queue_temp redo_var if stmt_level == :stmt_closure - code = "(function() {#{code}; return null;}).call(this)" + code = "(function() {#{code}; return nil;}).call(this)" end code @@ -1001,7 +996,7 @@ def process_until(exp, level) @scope.queue_temp redo_var if stmt_level == :stmt_closure - code = "(function() {#{code}; return null;}).call(this)" + code = "(function() {#{code}; return nil;}).call(this)" end code @@ -1047,7 +1042,7 @@ def process_masgn(sexp, level) code << process(s, :expr) else if idx >= len - l << s(:js_tmp, "(#{tmp}[#{idx}] == null ? null : #{tmp}[#{idx}])") + l << s(:js_tmp, "(#{tmp}[#{idx}] == null ? nil : #{tmp}[#{idx}])") else l << s(:js_tmp, "#{tmp}[#{idx}]") end @@ -1211,7 +1206,7 @@ def process_if(sexp, level) indent { code += "\n#@indent} else {\n#@indent#{process falsy, :stmt}" } if falsy code += "\n#@indent}" - code = "(function() { #{code}; return null; }).call(this)" if returnable + code = "(function() { #{code}; return nil; }).call(this)" if returnable code end @@ -1235,7 +1230,7 @@ def js_truthy(sexp) tmp = @scope.new_temp @scope.queue_temp tmp - "(%s = %s) !== false && %s != null" % [tmp, process(sexp, :expr), tmp] + "(%s = %s) !== false && %s !== nil" % [tmp, process(sexp, :expr), tmp] end # s(:and, lhs, rhs) @@ -1253,7 +1248,7 @@ def process_and(sexp, level) @scope.queue_temp tmp - "(%s = %s, %s !== false && %s != null ? %s : %s)" % + "(%s = %s, %s !== false && %s !== nil ? %s : %s)" % [tmp, process(lhs, :expr), tmp, tmp, process(rhs, :expr), tmp] end @@ -1272,7 +1267,7 @@ def process_or(sexp, level) @scope.queue_temp tmp - "(%s = %s, %s !== false && %s != null ? %s : %s)" % + "(%s = %s, %s !== false && %s !== nil ? %s : %s)" % [tmp, process(lhs, :expr), tmp, tmp, tmp, process(rhs, :expr)] end @@ -1304,7 +1299,7 @@ def process_yield(sexp, level) end def process_break(exp, level) - val = exp.empty? ? 'null' : process(exp.shift, :expr) + val = exp.empty? ? 'nil' : process(exp.shift, :expr) if in_while? if @while_loop[:closure] "return #{val};" @@ -1339,7 +1334,7 @@ def process_case(exp, level) end end - code << "else {return null}" if returnable and !done_else + code << "else {return nil}" if returnable and !done_else code = "$case = #{expr};#{code.join "\n"}" code = "(function() { #{code} }).call(this)" if returnable @@ -1394,7 +1389,7 @@ def process_match3(sexp, level) def process_cvar(exp, level) tmp = @scope.new_temp @scope.queue_temp tmp - "((%s = Opal.cvars[%s]) == null ? null : %s)" % [tmp, exp.shift.to_s.inspect, tmp] + "((%s = Opal.cvars[%s]) == null ? nil : %s)" % [tmp, exp.shift.to_s.inspect, tmp] end # @@name = rhs @@ -1559,7 +1554,7 @@ def process_begin(exp, level) end def process_next(exp, level) - val = exp.empty? ? 'null' : process(exp.shift, :expr) + val = exp.empty? ? 'nil' : process(exp.shift, :expr) if in_while? "continue;" else diff --git a/lib/opal/scope.rb b/lib/opal/scope.rb index 35fc0a664c..66cc436056 100644 --- a/lib/opal/scope.rb +++ b/lib/opal/scope.rb @@ -68,18 +68,16 @@ def to_vars vars << 'def = (this._isObject ? this._klass._proto : this._proto)' if @defines_defn end - # locals.each { |l| vars << "#{l} = nil" } - locals.each { |l| vars << l } + locals.each { |l| vars << "#{l} = nil" } temps.each { |t| vars << t } - # iv = ivars.map do |ivar| - # "if (this#{ivar} == null) this#{ivar} = nil;\n" - # end + iv = ivars.map do |ivar| + "if (this#{ivar} == null) this#{ivar} = nil;\n" + end indent = @parser.parser_indent res = vars.empty? ? '' : "var #{vars.join ', '}; " - res - # ivars.empty? ? res : "#{res}\n#{indent}#{iv.join indent}" + ivars.empty? ? res : "#{res}\n#{indent}#{iv.join indent}" end # Generates code for this module to donate methods diff --git a/test/core/array/allocate_spec.rb b/test/core/array/allocate_spec.rb index da4c609f19..c0aee8d035 100644 --- a/test/core/array/allocate_spec.rb +++ b/test/core/array/allocate_spec.rb @@ -1,6 +1,4 @@ describe "Array.allocate" do - `console.log("in here")` - `console.log(this)` it "returns an instance of Array" do ary = Array.allocate ary.should be_kind_of(Array)