Class: RubyRunJs::OPCODES::TRY_CATCH_FINALLY

Inherits:
OP_CODE
  • Object
show all
Defined in:
lib/ruby_run_js/opcodes.rb

Instance Method Summary collapse

Methods inherited from OP_CODE

#to_s

Methods included from RubyRunJs::Operation

#abstract_equality_op, #abstract_inequality_op, #abstract_relational_comparison, #add_op, #binary_operation, #bit_and_op, #bit_bshift_op, #bit_invert_uop, #bit_lshift_op, #bit_or_op, #bit_rshift_op, #bit_xor_op, #div_op, #float, #greater_eq_op, #greater_op, #in_op, #instanceof_op, #less_eq_op, #less_op, #logical_negation_uop, #minus_uop, #mod_op, #mul_op, #plus_uop, #strict_equality_op, #strict_inequality_op, #sub_op, #typeof_uop, #unary_operation, #void_op

Methods included from Helper

#check_object, #get_member, #get_member_dot, #is_accessor_descriptor, #is_callable, #is_data_descriptor, #is_generic_descriptor, #is_primitive, #make_error, #strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Constructor Details

#initialize(label_try, label_catch, catch_var_name, label_finally, has_finally, label_end) ⇒ TRY_CATCH_FINALLY

Returns a new instance of TRY_CATCH_FINALLY.



750
751
752
753
754
755
756
757
758
# File 'lib/ruby_run_js/opcodes.rb', line 750

def initialize(label_try, label_catch, catch_var_name, label_finally,
            has_finally, label_end)
  @label_try = label_try
  @label_catch = label_catch
  @catch_var_name = catch_var_name
  @label_finally = label_finally
  @has_finally = has_finally
  @label_end = label_end
end

Instance Method Details

#eval(ctx) ⇒ status, value

status = 0 : normal status = 1 : return status = 2 : jump out status = 3 : error

Returns:

  • (status, value)


765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/ruby_run_js/opcodes.rb', line 765

def eval(ctx)
  
  ctx.stack.pop()

  # execute try statement
  try_status = ctx.builtin.executor.run_under_control(
      ctx, @label_try, @label_catch)

  errors = try_status[0] == 3

  # catch
  if errors and @catch_var_name != nil
    # generate catch block context...
    catch_scope = LocalScope.new(ctx, ctx.builtin)
    js_error = try_status[1].throw_value.nil? ? ctx.builtin.new_error(try_status[1].type, try_status[1].msg) : try_status[1].throw_value
    catch_scope.own[@catch_var_name] = js_error
    catch_scope.this_binding = ctx.this_binding
    catch_status = ctx.builtin.executor.run_under_control(
      catch_scope, @label_catch, @label_finally)
  else
    catch_status = nil
  end

  # finally
  if @has_finally
    finally_status = ctx.builtin.executor.run_under_control(
        ctx, @label_finally, @label_end)
  else
    finally_status = nil
  end

  # now return controls
  other_status = catch_status || try_status
  if finally_status == nil || (finally_status[0] == 0 \
                                && other_status[0] != 0)
    winning_status = other_status
  else
    winning_status = finally_status
  end

  type, return_value, label = winning_status
  if type == 0  # normal
    ctx.stack.append(return_value)
    return @label_end
  elsif type == 1  # return
    ctx.stack.append(return_value)
    return :Return  # send return signal
  elsif type == 2  # jump outside
    ctx.stack.append(return_value)
    return label
  elsif type == 3
      # throw is made with empty stack as usual
    raise return_value
  else
    raise "Unexpected Type: #{type}"
  end
end