Class: Opal::Nodes::DefNode

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

Overview

FIXME: needs rewrite

Direct Known Subclasses

DefsNode

Constant Summary

Constants inherited from NodeWithArgs

NodeWithArgs::SEXP_TO_PARAMETERS

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, #class_variable_owner, #closest_module_node, #comments, #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

#conditional_send, #current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

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

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg



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

def block_arg
  @block_arg
end

Instance Method Details

#comments_codeObject



157
158
159
# File 'opal/lib/opal/nodes/def.rb', line 157

def comments_code
  '[' + comments.map { |comment| comment.text.inspect }.join(', ') + ']'
end

#compileObject



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
118
119
120
# File 'opal/lib/opal/nodes/def.rb', line 26

def compile
  extract_block_arg
  split_args

  inline_params = nil
  scope_name = nil

  # block name (&block)
  if block_arg
    block_name = block_arg
  end

  in_scope do
    scope.mid = mid
    scope.defs = true if @sexp.type == :defs

    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_i'
      add_local '$zuper_ii'

      line "// Prepare super implicit arguments"
      line "for($zuper_i = 0, $zuper_ii = arguments.length, $zuper = new Array($zuper_ii); $zuper_i < $zuper_ii; $zuper_i++) {"
      line "  $zuper[$zuper_i] = arguments[$zuper_i];"
      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 compiler.parse_comments?
    push ", #{scope_name}.$$comments = #{comments_code}"
  end

  if compiler.enable_source_location?
    push ", #{scope_name}.$$source_location = #{source_location}"
  end

  wrap_with_definition
end

#compile_arity_checkObject

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



142
143
144
145
146
147
148
# File 'opal/lib/opal/nodes/def.rb', line 142

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



14
15
16
17
18
19
20
21
22
23
24
# File 'opal/lib/opal/nodes/def.rb', line 14

def extract_block_arg
  *regular_args, last_arg = args.children
  if last_arg && last_arg.type == :blockarg
    @block_arg = last_arg.children[0]
    @sexp = @sexp.updated(nil, [
      mid,
      s(:args, *regular_args),
      stmts
    ])
  end
end

#source_locationObject



150
151
152
153
154
155
# File 'opal/lib/opal/nodes/def.rb', line 150

def source_location
  file = @sexp.loc.expression.source_buffer.name
  line = @sexp.loc.line

  "['#{file}.rb', #{line}]"
end

#wrap_with_definitionObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'opal/lib/opal/nodes/def.rb', line 122

def wrap_with_definition
  if    scope.iter?                   then unshift "Opal.def(self, '$#{mid}', "
  elsif scope.module? || scope.class? then unshift "Opal.defn(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 ')'

  if expr?
    wrap '(', ", nil) && '#{mid}'"
  else
    unshift "\n#{current_indent}"
  end
end