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

#await_encountered, #block_name, #catch_return, #defs, #gvars, #has_break, #has_retry, #identity, #ivars, #locals, #methods, #mid, #name, #parent, #rescue_else_sexp, #scope_name

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from NodeWithArgs

#arity_check_node, #compile_arity_check, #compile_block_arg, #compile_body_or_shortcut, define_shortcut, #initialize, #parameters_code, shortcuts_for, #simple_value?

Methods inherited from ScopeNode

#accepts_using?, #add_arg, #add_proto_ivar, #add_scope_gvar, #add_scope_ivar, #add_scope_local, #add_scope_temp, #class?, #class_scope?, #collect_refinements_temps, #current_rescue, #def?, #def_in_class?, #defines_lambda, #find_parent_def, #gen_retry_id, #has_local?, #has_rescue_else?, #has_temp?, #identify!, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_scope, #in_while?, #initialize, #is_lambda!, #iter?, #lambda?, #lambda_definition?, #module?, #nesting, #new_refinements_temp, #new_temp, #next_temp, #pop_while, #prepare_block, #prepend_scope_temp, #push_while, #queue_temp, #refinements_temp, #relative_access, #sclass?, #scope_locals, #self, #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_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #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



90
91
92
# File 'opal/lib/opal/nodes/def.rb', line 90

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

def compile
  compile_body_or_shortcut

  blockopts = []

  blockopts << "$$arity: #{arity}"

  if compiler.arity_check?
    blockopts << "$$parameters: #{parameters_code}"
  end

  if compiler.parse_comments?
    blockopts << "$$comments: #{comments_code}"
  end

  if compiler.enable_source_location?
    blockopts << "$$source_location: #{source_location}"
  end

  if blockopts.length == 1
    push ", #{arity}"
  elsif blockopts.length > 1
    push ', {', blockopts.join(', '), '}'
  end

  wrap_with_definition

  scope.nesting if @define_nesting
  scope.relative_access if @define_relative_access
end

#compile_bodyObject



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

def compile_body
  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)

    in_closure(Closure::DEF | Closure::JS_FUNCTION) do
      stmt_code = stmt(compiler.returns(stmts))

      compile_block_arg

      add_temp 'self = this' if @define_self

      compile_arity_check

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

      line stmt_code
    end
  end

  unshift ') {'
  unshift(inline_params)
  unshift "function #{scope_name}("
  if await_encountered
    unshift "async "
  end
  line '}'
end

#wrap_with_definitionObject



83
84
85
86
87
88
# File 'opal/lib/opal/nodes/def.rb', line 83

def wrap_with_definition
  helper :def
  wrap "$def(#{scope.self}, '$#{mid}', ", ')'

  unshift "\n#{current_indent}" unless expr?
end