class Java::OrgMozillaJavascript::ScriptableObject

The base class for all JavaScript objects.

Public Instance Methods

==(other) click to toggle source
# File lib/rhino/rhino_ext.rb, line 83
def ==(other)
  equivalentValues(other) == true # JS ==
end
[](name) click to toggle source

get a property from this javascript object, where k is a string or symbol corresponding to the property name e.g.

jsobject = Context.open do |cxt|
  cxt.eval('({foo: 'bar', 'Take me to': 'a funky town'})')
end
jsobject[:foo] # => 'bar'
jsobject['foo'] # => 'bar'
jsobject['Take me to'] # => 'a funky town'
# File lib/rhino/rhino_ext.rb, line 17
def [](name)
  Rhino.to_ruby ScriptableObject.getProperty(self, name.to_s)
end
[]=(key, value) click to toggle source

set a property on the javascript object, where k is a string or symbol corresponding to the property name, and v is the value to set. e.g.

jsobject = eval_js "new Object()"
jsobject['foo'] = 'bar'
Context.open(:with => jsobject) do |cxt|
  cxt.eval('foo') # => 'bar'
end
# File lib/rhino/rhino_ext.rb, line 30
def []=(key, value)
  scope = self
  ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope))
end
each() { |key, to_ruby| ... } click to toggle source

enumerate the key value pairs contained in this javascript object. e.g.

eval_js("{foo: 'bar', baz: 'bang'}").each do |key,value|
  puts "#{key} -> #{value} "
end

outputs foo -> bar baz -> bang

# File lib/rhino/rhino_ext.rb, line 43
def each
  each_raw { |key, val| yield key, Rhino.to_ruby(val) }
end
each_key() { |key| ... } click to toggle source
# File lib/rhino/rhino_ext.rb, line 47
def each_key
  each_raw { |key, val| yield key }
end
each_raw() { |id, get(id, self)| ... } click to toggle source
# File lib/rhino/rhino_ext.rb, line 55
def each_raw
  for id in getAllIds do
    yield id, get(id, self)
  end
end
each_value() { |to_ruby| ... } click to toggle source
# File lib/rhino/rhino_ext.rb, line 51
def each_value
  each_raw { |key, val| yield Rhino.to_ruby(val) }
end
eql?(other) click to toggle source
# File lib/rhino/rhino_ext.rb, line 87
def eql?(other)
  self.class == other.class && self.==(other)
end
inspect() click to toggle source

make sure inspect prints the same as to_s (on –1.8) otherwise JRuby might play it a little smart e.g. : “#<#<Class:0xd790a8>:0x557c15>” instead of “Error: bar”

# File lib/rhino/rhino_ext.rb, line 99
def inspect
  toString
end
keys() click to toggle source
# File lib/rhino/rhino_ext.rb, line 61
def keys
  keys = []
  each_key { |key| keys << key }
  keys
end
method_missing(name, *args) click to toggle source

Delegate methods to JS object if possible when called from Ruby.

Calls superclass method
# File lib/rhino/rhino_ext.rb, line 104
def method_missing(name, *args)
  name_str = name.to_s
  if name_str[-1, 1] == '=' && args.size == 1 # writer -> JS put
    self[ name_str[0...-1] ] = args[0]
  else
    if property = self[name_str]
      if property.is_a?(Rhino::JS::Function)
        with_context do |context|
          js_args = Rhino.args_to_javascript(args, self) # scope == self
          Rhino.to_ruby property.__call__(context, current_scope(context), self, js_args)
        end
      else
        if args.size > 0
          raise ArgumentError, "can't call '#{name_str}' with args: #{args.inspect} as it's a property"
        end
        Rhino.to_ruby property
      end
    else
      super
    end
  end
end
to_h() click to toggle source

Converts the native object to a hash. This isn't really a stretch since it's pretty much a hash in the first place.

# File lib/rhino/rhino_ext.rb, line 75
def to_h
  hash = {}
  each do |key, val|
    hash[key] = val.is_a?(ScriptableObject) ? val.to_h : val
  end
  hash
end
to_json(*args) click to toggle source

Convert this javascript object into a json string.

# File lib/rhino/rhino_ext.rb, line 92
def to_json(*args)
  to_h.to_json(*args)
end
values() click to toggle source
# File lib/rhino/rhino_ext.rb, line 67
def values
  vals = []
  each_value { |val| vals << val }
  vals    
end

Protected Instance Methods

current_scope(context) click to toggle source
# File lib/rhino/rhino_ext.rb, line 143
def current_scope(context)
  getParentScope || Rhino::JS::ScriptRuntime.getGlobal(context)
end
with_context() { |context| ... } click to toggle source
# File lib/rhino/rhino_ext.rb, line 129
def with_context
  context = Rhino::JS::Context.getCurrentContext
  unless context
    context = Rhino::JS::Context.enter
    context.initStandardObjects
    context_exit = true
  end
  begin
    yield(context)
  ensure
    Rhino::JS::Context.exit if context_exit
  end
end