Skip to content

Commit

Permalink
Add initial truthy? and falsy? inlined compiler helpers (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 15, 2013
1 parent db3fbfe commit f8d7d1d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/opal/nodes/call.rb
@@ -1,5 +1,6 @@
require 'set'
require 'opal/nodes/base'
require 'opal/nodes/runtime_helpers'

module Opal
module Nodes
Expand Down Expand Up @@ -99,6 +100,9 @@ def handle_special
push result
return true
end
elsif RuntimeHelpers.compatible?(recvr, meth, arglist)
push(RuntimeHelpers.new(@sexp, @level, @compiler).compile)
return true
end
end

Expand Down
45 changes: 45 additions & 0 deletions lib/opal/nodes/runtime_helpers.rb
@@ -0,0 +1,45 @@
require 'set'
require 'opal/nodes/base'

module Opal
module Nodes
class RuntimeHelpers < Base
HELPERS = Set.new

children :recvr, :meth, :arglist

def self.compatible?(recvr, meth, arglist)
recvr == [:const, :Opal] and HELPERS.include?(meth.to_sym)
end

def self.helper(name, &block)
HELPERS << name
define_method("compile_#{name}", &block)
end

def compile
if HELPERS.include?(meth.to_sym)
__send__("compile_#{meth}")
else
raise "Helper not supported: #{meth}"
end
end

helper :truthy? do
unless sexp = arglist[1]
raise "truthy? requires an object"
end

js_truthy(sexp)
end

helper :falsy? do
unless sexp = arglist[1]
raise "falsy? requires an object"
end

js_falsy(sexp)
end
end
end
end
16 changes: 0 additions & 16 deletions opal/core/helpers.rb
Expand Up @@ -51,22 +51,6 @@ def self.fits_array!(value)
end
end

def self.truthy?(value)
if value
true
else
false
end
end

def self.falsy?(value)
if value
false
else
true
end
end

def self.destructure(args)
%x{
if (args.length == 1) {
Expand Down

0 comments on commit f8d7d1d

Please sign in to comment.