Class: Opal::Nodes::DefNode

Inherits:
NodeWithArgs show all
Defined in:
opal/lib/opal/nodes/def.rb

Overview

FIXME: needs rewrite

Constant Summary

Constants inherited from NodeWithArgs

NodeWithArgs::SEXP_TO_PARAMETERS

Constants included from Helpers

Helpers::BASIC_IDENTIFIER_RULES, Helpers::ES3_RESERVED_WORD_EXCLUSIVE, Helpers::ES51_RESERVED_WORD, Helpers::IMMUTABLE_PROPS, Helpers::PROTO_SPECIAL_METHODS, Helpers::PROTO_SPECIAL_PROPS, Helpers::RESERVED_FUNCTION_NAMES

Instance Attribute Summary collapse

Attributes inherited from NodeWithArgs

#inline_args, #kwargs_initialized, #mlhs_args, #mlhs_mapping, #post_args, #used_kwargs, #working_arguments

Attributes inherited from ScopeNode

#block_name, #catch_return, #defs, #gvars, #has_break, #ivars, #locals, #methods, #mid, #name, #parent, #rescue_else_sexp, #scope_name, #uses_super, #uses_zuper

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from NodeWithArgs

#arity, #arity_checks, #build_parameter, #compile_block_arg, #compile_inline_args, #compile_post_args, #has_only_optional_kwargs?, #has_required_kwargs?, #in_mlhs, #in_mlhs?, #initialize, #inline_args_sexp, #keyword_args, #negative_arity, #opt_args, #optimize_args!, #parameters_code, #positive_arity, #post_args_sexp, #rest_arg, #split_args, #with_inline_args

Methods inherited from ScopeNode

#add_arg, #add_proto_ivar, #add_scope_gvar, #add_scope_ivar, #add_scope_local, #add_scope_temp, #class?, #class_scope?, #def?, #def_in_class?, #find_parent_def, #get_super_chain, #has_local?, #has_rescue_else?, #has_temp?, #identify!, #identity, #in_ensure, #in_ensure?, #in_scope, #in_while?, #initialize, #iter?, #module?, #new_temp, #next_temp, #pop_while, #proto, #push_while, #queue_temp, #sclass?, #to_vars, #top?, #uses_block!, #uses_block?

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

#current_indent, #empty_line, #indent, #ivar, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_ivar_name?, #valid_name?, #variable

Constructor Details

This class inherits a constructor from Opal::Nodes::NodeWithArgs

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg


11
12
13
# File 'opal/lib/opal/nodes/def.rb', line 11

def block_arg
  @block_arg
end

Instance Method Details

#compileObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'opal/lib/opal/nodes/def.rb', line 19

def compile
  extract_block_arg
  split_args

  inline_params = nil
  scope_name = nil

  # block name (&block)
  if block_arg
    block_name = variable(block_arg).to_sym
  end

  in_scope do
    scope.mid = mid
    scope.defs = true if recvr

    if block_name
      scope.uses_block!
      scope.add_arg block_name
    end

    scope.block_name = block_name || '$yield'

    inline_params = process(inline_args_sexp)
    stmt_code = stmt(compiler.returns(stmts))

    add_temp 'self = this'

    compile_inline_args
    compile_post_args

    scope.identify!
    scope_name = scope.identity

    compile_block_arg

    if compiler.arity_check?
      compile_arity_check
    end

    if scope.uses_zuper
      add_local '$zuper'
      add_local '$zuper_index'
      add_local '$zuper_length'

      line "$zuper = [];"
      line
      line "for($zuper_index = 0; $zuper_index < arguments.length; $zuper_index++) {"
      line "  $zuper[$zuper_index] = arguments[$zuper_index];"
      line "}"
    end

    unshift "\n#{current_indent}", scope.to_vars

    line stmt_code

    if scope.catch_return
      unshift "try {\n"
      line "} catch ($returner) { if ($returner === Opal.returner) { return $returner.$v }"
      push " throw $returner; }"
    end
  end

  # There are some special utf8 chars that can be used as valid JS
  # identifiers, some examples:
  #
  # utf8_pond = 'ⵌ'
  # utf8_question = 'ʔ̣'
  # utf8_exclamation 'ǃ'
  #
  # For now we're just using $, to maintain compatibility with older IEs.
  function_name = valid_name?(mid) ? " $$#{mid}" : ''

  unshift ") {"
  unshift(inline_params)
  unshift "function#{function_name}("
  unshift "#{scope_name} = " if scope_name
  line "}"

  push ", #{scope_name}.$$arity = #{arity}"

  if compiler.arity_check?
    push ", #{scope_name}.$$parameters = #{parameters_code}"
  end

  if    recvr                         then unshift 'Opal.defs(', recv(recvr), ", '$#{mid}', "
  elsif scope.iter?                   then unshift "Opal.def(self, '$#{mid}', "
  elsif scope.module? || scope.class? then unshift "Opal.defn(self, '$#{mid}', "
  elsif scope.sclass? && scope.defs   then unshift "Opal.defs(self, '$#{mid}', "
  elsif scope.sclass?                 then unshift "Opal.defn(self, '$#{mid}', "
  elsif compiler.eval?                then unshift "Opal.def(self, '$#{mid}', "
  elsif scope.top?                    then unshift "Opal.defn(Opal.Object, '$#{mid}', "
  elsif scope.def?                    then unshift "Opal.def(self, '$#{mid}', "
  else raise "Unsupported use of `def`; please file a bug at https://github.com/opal/opal/issues/new reporting this message."
  end
  push ')'

  wrap '(', ", nil) && '#{mid}'" if expr?
end

#compile_arity_checkObject

Returns code used in debug mode to check arity of method call


120
121
122
123
124
125
126
# File 'opal/lib/opal/nodes/def.rb', line 120

def compile_arity_check
  if arity_checks.size > 0
    meth = scope.mid.to_s.inspect
    line "var $arity = arguments.length;"
    push " if (#{arity_checks.join(' || ')}) { Opal.ac($arity, #{arity}, this, #{meth}); }"
  end
end

#extract_block_argObject


13
14
15
16
17
# File 'opal/lib/opal/nodes/def.rb', line 13

def extract_block_arg
  if args.last.is_a?(Sexp) && args.last.type == :blockarg
    @block_arg = args.pop[1]
  end
end