Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 10dce0a47ed4
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a6e65ace5063
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Nov 15, 2013

  1. Verified

    This commit was signed with the committer’s verified signature.
    lukekarrys Luke Karrys
    Copy the full SHA
    9139cf2 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    lukekarrys Luke Karrys
    Copy the full SHA
    a6e65ac View commit details
Showing with 49 additions and 33 deletions.
  1. +49 −33 lib/opal/nodes/iter.rb
82 changes: 49 additions & 33 deletions lib/opal/nodes/iter.rb
Original file line number Diff line number Diff line change
@@ -8,17 +8,8 @@ class IterNode < ScopeNode
children :args_sexp, :body_sexp

def compile
# opt args are last (if present) and are a s(:block)
if args.last.is_a?(Sexp) and args.last.type == :block
opt_args = args.pop
opt_args.shift
end

# does this iter define a block_pass
if args.last.is_a?(Sexp) and args.last.type == :block_pass
block_arg = args.pop
block_arg = block_arg[1][1].to_sym
end
opt_args = extract_opt_args
block_arg = extract_block_arg

# find any splat args
if args.last.is_a?(Sexp) and args.last.type == :splat
@@ -30,30 +21,13 @@ def compile
params = args_to_params(args[1..-1])
params << splat if splat

to_vars = identity = nil
to_vars = identity = body_code = nil

in_scope do
identity = scope.identify!
add_temp "self = #{identity}._s || this"

args[1..-1].each_with_index do |arg, idx|
if arg.type == :lasgn
arg = variable(arg[1])

if opt_args and current_opt = opt_args.find { |s| s[1] == arg.to_sym }
push "if (#{arg} == null) #{arg} = ", expr(current_opt[2]), ";"
else
push "if (#{arg} == null) #{arg} = nil;"
end
elsif arg.type == :array
arg[1..-1].each_with_index do |_arg, _idx|
_arg = variable(_arg[1])
push "#{_arg} = #{params[idx]}[#{_idx}];"
end
else
raise "Bad block arg type"
end
end
compile_args(args[1..-1], opt_args, params)

if splat
scope.add_arg splat
@@ -68,15 +42,57 @@ def compile
line "#{block_arg} = #{scope_name}._p || nil, #{scope_name}._p = null;"
end

line stmt(body)
body_code = stmt(body)
to_vars = scope.to_vars
end

line body_code

unshift to_vars
unshift "function(#{params.join ', '}) {"
wrap "(#{identity} = ", "}, #{identity}._s = self, #{identity})"

unshift "(#{identity} = function(#{params.join ', '}){"
push "}, #{identity}._s = self, #{identity})"
end

def compile_args(args, opt_args, params)
args.each_with_index do |arg, idx|
if arg.type == :lasgn
arg = variable(arg[1])

if opt_args and current_opt = opt_args.find { |s| s[1] == arg.to_sym }
push "if (#{arg} == null) #{arg} = ", expr(current_opt[2]), ";"
else
push "if (#{arg} == null) #{arg} = nil;"
end
elsif arg.type == :array
arg[1..-1].each_with_index do |_arg, _idx|
_arg = variable(_arg[1])
push "#{_arg} = #{params[idx]}[#{_idx}];"
end
else
raise "Bad block arg type"
end
end
end

# opt args are last (if present) and are a s(:block)
def extract_opt_args
if args.last.is_a?(Sexp) and args.last.type == :block
opt_args = args.pop
opt_args.shift
opt_args
end
end

# does this iter define a block_pass
def extract_block_arg
if args.last.is_a?(Sexp) and args.last.type == :block_pass
block_arg = args.pop
block_arg = block_arg[1][1].to_sym
end
end


def args
if Fixnum === args_sexp or args_sexp.nil?
s(:array)