class JavaScript::Scope

Public Class Methods

new(parent = nil, target = nil, locals = {}) click to toggle source
# File lib/javascript.rb, line 164
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

Public Instance Methods

__eval__(*args, &block) click to toggle source
# File lib/javascript.rb, line 188
def __eval__(*args, &block)
  JavaScript.current_scope = self

  # convert the block to a lambda, so that +return+ works correctly
  temp      = :"block_#{Time.now.to_i}"
  metaclass = __method__(:singleton_class).call

  metaclass.send(:define_method, temp, &block)

  if block.arity.zero?
    __method__(:send).call(temp)
  else
    __method__(:send).call(temp, *args)
  end
ensure
  JavaScript.current_scope = @parent
  metaclass.send(:remove_method, temp)
end
__global__() click to toggle source
# File lib/javascript.rb, line 176
def __global__
  @global
end
__spawn__(target = nil, locals = {}) click to toggle source
# File lib/javascript.rb, line 184
def __spawn__(target = nil, locals = {})
  Scope.new(self, target, locals)
end
__target__() click to toggle source
# File lib/javascript.rb, line 180
def __target__
  @target
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/javascript.rb, line 208
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