Class: JavaScript::Scope

Inherits:
BlankObject show all
Includes:
Internals, Syntax
Defined in:
lib/javascript.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, target = nil, locals = {}) ⇒ Scope

Returns a new instance of Scope.



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/javascript.rb', line 157

def initialize(parent = nil, target = nil, locals = {})
  @parent = parent
  @global = parent ? parent.__global__ : GlobalObject.new
  @locals = locals

  if Scope === target
    @target = target.__target__
  else
    @target = target || global
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/javascript.rb', line 189

def method_missing(name, *args, &block)
  found   = false
  value   = nil
  defined = __caller__.local_variable_defined?(name) rescue nil

  if defined
    found = true
    value = __caller__.local_variable_get(name)
  elsif @locals.key?(name)
    found = true
    value = @locals[name]
  elsif !@parent && @global.__defined__?(name)
    found = true
    value = @global[name]
  end

  if found && Function === value
    value.apply(nil, args)
  elsif found
    value
  elsif @parent
    @parent.__send__(name, *args, &block)
  elsif block.nil?
    Identifier.new(name, args)
  else
    Function.new(name, args, block)
  end
end

Instance Method Details

#__eval__(*args, &block) ⇒ Object



181
182
183
184
185
186
# File 'lib/javascript.rb', line 181

def __eval__(*args, &block)
  JavaScript.current_scope = self
  __method__(:instance_exec).call(*args, &block)
ensure
  JavaScript.current_scope = @parent
end

#__global__Object



169
170
171
# File 'lib/javascript.rb', line 169

def __global__
  @global
end

#__spawn__(target = nil, locals = {}) ⇒ Object



177
178
179
# File 'lib/javascript.rb', line 177

def __spawn__(target = nil, locals = {})
  Scope.new(self, target, locals)
end

#__target__Object



173
174
175
# File 'lib/javascript.rb', line 173

def __target__
  @target
end