class Opal::Nodes::ResBodyNode

def compile

def compile
  push "if (Opal.rescue($err, ["
  if rescue_exprs.empty?
    # if no expressions are given, then catch StandardError only
    push expr(Sexp.new([:const, :StandardError]))
  else
    rescue_exprs.each_with_index do |rexpr, idx|
      push ', ' unless idx == 0
      push expr(rexpr)
    end
  end
  push "])) {"
  indent do
    if variable = rescue_variable
      variable[2] = s(:js_tmp, '$err')
      push expr(variable), ';'
    end
    # Need to ensure we clear the current exception out after the rescue block ends
    line "try {"
    indent do
      line process(rescue_body, @level)
    end
    line '} finally { Opal.pop_exception() }'
  end
  line "}"
end

def rescue_body

def rescue_body
  body_code = (body || s(:nil))
  body_code = compiler.returns(body_code) unless stmt?
  body_code
end

def rescue_exprs

def rescue_exprs
  exprs = args.dup
  exprs.pop if rescue_variable?(exprs.last)
  exprs.children
end

def rescue_variable

def rescue_variable
  rescue_variable?(args.last) ? args.last.dup : nil
end

def rescue_variable?(variable)

def rescue_variable?(variable)
  Sexp === variable and [:lasgn, :iasgn].include?(variable.type)
end