Class: Opal::Nodes::DefNode

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

Direct Known Subclasses

DefsNode

Instance Attribute Summary

Attributes inherited from NodeWithArgs

#arity, #original_args, #used_kwargs

Attributes inherited from ScopeNode

#block_name, #catch_return, #defs, #gvars, #has_break, #identity, #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_check_node, #compile_arity_check, #compile_block_arg, #initialize, #parameters_code

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, #has_local?, #has_rescue_else?, #has_temp?, #identify!, #in_ensure, #in_ensure?, #in_scope, #in_while?, #initialize, #iter?, #module?, #new_temp, #next_temp, #pop_while, #push_while, #queue_temp, #sclass?, #super_chain, #to_vars, #top?, #uses_block!, #uses_block?

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #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 Method Details

#comments_codeObject


100
101
102
# File 'opal/lib/opal/nodes/def.rb', line 100

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

#compileObject


12
13
14
15
16
17
18
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
# File 'opal/lib/opal/nodes/def.rb', line 12

def compile
  inline_params = nil
  scope_name = nil

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

    scope.identify!
    scope_name = scope.identity

    # Setting a default block name (later can be overwritten by a blockarg)
    scope.block_name = '$yield'

    inline_params = process(inline_args)

    stmt_code = stmt(compiler.returns(stmts))

    compile_block_arg

    add_temp 'self = this'

    compile_arity_check

    if scope.uses_zuper
      prepare_super
    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

#prepare_superObject


104
105
106
107
108
109
110
111
112
113
# File 'opal/lib/opal/nodes/def.rb', line 104

def prepare_super
  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

#source_locationObject


94
95
96
97
98
# File 'opal/lib/opal/nodes/def.rb', line 94

def source_location
  file = @sexp.loc.expression.source_buffer.name
  line = @sexp.loc.line
  "['#{file}', #{line}]"
end

#wrap_with_definitionObject


84
85
86
87
88
89
90
91
92
# File 'opal/lib/opal/nodes/def.rb', line 84

def wrap_with_definition
  wrap "Opal.def(self, '$#{mid}', ", ')'

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