Class: Opal::Nodes::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
opal/lib/opal/nodes/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize(sexp, level, compiler) ⇒ Base

Returns a new instance of Base



33
34
35
36
37
38
# File 'opal/lib/opal/nodes/base.rb', line 33

def initialize(sexp, level, compiler)
  @sexp = sexp
  @type = sexp.type
  @level = level
  @compiler = compiler
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler



31
32
33
# File 'opal/lib/opal/nodes/base.rb', line 31

def compiler
  @compiler
end

#typeObject (readonly)

Returns the value of attribute type



31
32
33
# File 'opal/lib/opal/nodes/base.rb', line 31

def type
  @type
end

Class Method Details

.children(*names) ⇒ Object



19
20
21
22
23
24
25
# File 'opal/lib/opal/nodes/base.rb', line 19

def self.children(*names)
  names.each_with_index do |name, idx|
    define_method(name) do
      @sexp.children[idx]
    end
  end
end

.handle(*types) ⇒ Object



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

def self.handle(*types)
  types.each do |type|
    Base.handlers[type] = self
  end
end

.handlersObject



9
10
11
# File 'opal/lib/opal/nodes/base.rb', line 9

def self.handlers
  @handlers ||= {}
end

.truthy_optimize?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'opal/lib/opal/nodes/base.rb', line 27

def self.truthy_optimize?
  false
end

Instance Method Details

#add_gvar(name) ⇒ Object



131
132
133
# File 'opal/lib/opal/nodes/base.rb', line 131

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



127
128
129
# File 'opal/lib/opal/nodes/base.rb', line 127

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



123
124
125
# File 'opal/lib/opal/nodes/base.rb', line 123

def add_local(name)
  scope.add_scope_local name.to_sym
end

#add_temp(temp) ⇒ Object



135
136
137
# File 'opal/lib/opal/nodes/base.rb', line 135

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



40
41
42
# File 'opal/lib/opal/nodes/base.rb', line 40

def children
  @sexp.children
end

#class_variable_ownerObject



175
176
177
178
179
180
181
# File 'opal/lib/opal/nodes/base.rb', line 175

def class_variable_owner
  if closest_module_node
    "$#{closest_module_node.name}"
  else
    "Opal.Object"
  end
end

#closest_module_nodeObject



167
168
169
170
171
172
173
# File 'opal/lib/opal/nodes/base.rb', line 167

def closest_module_node
  current = scope
  while current && !current.class_scope?
    current = current.parent
  end
  current
end

#commentsObject



183
184
185
# File 'opal/lib/opal/nodes/base.rb', line 183

def comments
  compiler.comments[@sexp.loc]
end

#compileObject



52
53
54
# File 'opal/lib/opal/nodes/base.rb', line 52

def compile
  raise "Not Implemented"
end

#compile_to_fragmentsObject



44
45
46
47
48
49
50
# File 'opal/lib/opal/nodes/base.rb', line 44

def compile_to_fragments
  return @fragments if defined?(@fragments)

  @fragments = []
  self.compile
  @fragments
end

#error(msg) ⇒ Object



79
80
81
# File 'opal/lib/opal/nodes/base.rb', line 79

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



107
108
109
# File 'opal/lib/opal/nodes/base.rb', line 107

def expr(sexp)
  @compiler.process sexp, :expr
end

#expr?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'opal/lib/opal/nodes/base.rb', line 91

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



119
120
121
# File 'opal/lib/opal/nodes/base.rb', line 119

def expr_or_nil(sexp)
  sexp ? expr(sexp) : "nil"
end

#fragment(str) ⇒ Object



75
76
77
# File 'opal/lib/opal/nodes/base.rb', line 75

def fragment(str)
  Opal::Fragment.new str, scope, @sexp
end

#has_rescue_else?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'opal/lib/opal/nodes/base.rb', line 155

def has_rescue_else?
  scope.has_rescue_else?
end

#helper(name) ⇒ Object



139
140
141
# File 'opal/lib/opal/nodes/base.rb', line 139

def helper(name)
  @compiler.helper name
end

#in_ensure(&block) ⇒ Object



159
160
161
# File 'opal/lib/opal/nodes/base.rb', line 159

def in_ensure(&block)
  scope.in_ensure(&block)
end

#in_ensure?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'opal/lib/opal/nodes/base.rb', line 163

def in_ensure?
  scope.in_ensure?
end

#in_while?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'opal/lib/opal/nodes/base.rb', line 147

def in_while?
  @compiler.in_while?
end

#process(sexp, level = :expr) ⇒ Object



103
104
105
# File 'opal/lib/opal/nodes/base.rb', line 103

def process(sexp, level = :expr)
  @compiler.process sexp, level
end

#push(*strs) ⇒ Object



56
57
58
59
60
61
# File 'opal/lib/opal/nodes/base.rb', line 56

def push(*strs)
  strs.each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments << str
  end
end

#recv(sexp) ⇒ Object



111
112
113
# File 'opal/lib/opal/nodes/base.rb', line 111

def recv(sexp)
  @compiler.process sexp, :recv
end

#recv?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'opal/lib/opal/nodes/base.rb', line 95

def recv?
  @level == :recv
end

#s(*args) ⇒ Object



87
88
89
# File 'opal/lib/opal/nodes/base.rb', line 87

def s(*args)
  @compiler.s(*args)
end

#scopeObject



83
84
85
# File 'opal/lib/opal/nodes/base.rb', line 83

def scope
  @compiler.scope
end

#stmt(sexp) ⇒ Object



115
116
117
# File 'opal/lib/opal/nodes/base.rb', line 115

def stmt(sexp)
  @compiler.process sexp, :stmt
end

#stmt?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'opal/lib/opal/nodes/base.rb', line 99

def stmt?
  @level == :stmt
end

#unshift(*strs) ⇒ Object



63
64
65
66
67
68
# File 'opal/lib/opal/nodes/base.rb', line 63

def unshift(*strs)
  strs.reverse.each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments.unshift str
  end
end

#while_loopObject



151
152
153
# File 'opal/lib/opal/nodes/base.rb', line 151

def while_loop
  @compiler.instance_variable_get(:@while_loop)
end

#with_temp(&block) ⇒ Object



143
144
145
# File 'opal/lib/opal/nodes/base.rb', line 143

def with_temp(&block)
  @compiler.with_temp(&block)
end

#wrap(pre, post) ⇒ Object



70
71
72
73
# File 'opal/lib/opal/nodes/base.rb', line 70

def wrap(pre, post)
  unshift pre
  push post
end