2011-12-07 13:59:39 -05:00
|
|
|
|
|
|
|
# The base class for all JavaScript objects.
|
|
|
|
class Java::OrgMozillaJavascript::ScriptableObject
|
|
|
|
|
|
|
|
import "org.mozilla.javascript"
|
|
|
|
|
|
|
|
# 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'
|
|
|
|
#
|
|
|
|
def [](name)
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_ruby ScriptableObject.getProperty(self, name.to_s)
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
def []=(key, value)
|
|
|
|
scope = self
|
2011-12-09 01:52:47 -05:00
|
|
|
ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope))
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
def each
|
2011-12-09 01:52:47 -05:00
|
|
|
getAllIds.each { |id| yield id, Rhino.to_ruby(get(id, self)) }
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def each_key
|
|
|
|
getAllIds.each { |id| yield id }
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_value
|
2011-12-09 01:52:47 -05:00
|
|
|
getAllIds.each { |id| yield Rhino.to_ruby(get(id, self)) }
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def keys
|
|
|
|
keys = []
|
|
|
|
each_key { |key| keys << key }
|
|
|
|
keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def values
|
|
|
|
vals = []
|
|
|
|
each_value { |val| vals << val }
|
|
|
|
vals
|
|
|
|
end
|
|
|
|
|
|
|
|
# Converts the native object to a hash. This isn't really a stretch since it's
|
|
|
|
# pretty much a hash in the first place.
|
|
|
|
def to_h
|
|
|
|
hash = {}
|
|
|
|
each do |key, val|
|
|
|
|
hash[key] = val.is_a?(ScriptableObject) ? val.to_h : val
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert this javascript object into a json string.
|
|
|
|
def to_json(*args)
|
|
|
|
to_h.to_json(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delegate methods to JS object if possible when called from Ruby.
|
|
|
|
def method_missing(name, *args)
|
|
|
|
if ScriptableObject.hasProperty(self, name.to_s)
|
|
|
|
begin
|
2011-12-09 13:19:55 -05:00
|
|
|
context = Rhino::JS::Context.enter
|
2011-12-09 01:52:47 -05:00
|
|
|
js_args = Rhino.args_to_javascript(args, self) # scope == self
|
2011-12-07 13:59:39 -05:00
|
|
|
ScriptableObject.callMethod(context, self, name.to_s, js_args)
|
|
|
|
ensure
|
2011-12-09 13:19:55 -05:00
|
|
|
Rhino::JS::Context.exit
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Java::OrgMozillaJavascript::NativeObject
|
|
|
|
|
2011-12-09 13:02:08 -05:00
|
|
|
import "org.mozilla.javascript"
|
|
|
|
|
2011-12-09 13:04:23 -05:00
|
|
|
def [](name)
|
|
|
|
value = Rhino.to_ruby(ScriptableObject.getProperty(self, s_name = name.to_s))
|
|
|
|
# handle { '5': 5 }.keys() ... [ 5 ] not [ '5' ] !
|
|
|
|
if value.nil? && (i_name = s_name.to_i) != 0
|
|
|
|
value = Rhino.to_ruby(ScriptableObject.getProperty(self, i_name))
|
|
|
|
end
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
# re-implement unsupported Map#put
|
2011-12-07 13:59:39 -05:00
|
|
|
def []=(key, value)
|
|
|
|
scope = self
|
2011-12-09 01:52:47 -05:00
|
|
|
ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope))
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# The base class for all JavaScript function objects.
|
|
|
|
class Java::OrgMozillaJavascript::BaseFunction
|
|
|
|
|
|
|
|
alias_method :__call__, :call # Rhino's Function#call(a1, a2, a3, a4)
|
|
|
|
|
|
|
|
# make JavaScript functions callable Ruby style e.g. `fn.call('42')`
|
|
|
|
def call(*args)
|
2011-12-09 13:19:55 -05:00
|
|
|
context = Rhino::JS::Context.enter
|
2011-12-07 13:59:39 -05:00
|
|
|
scope = getParentScope || context.initStandardObjects
|
2011-12-09 01:52:47 -05:00
|
|
|
__call__(context, scope, scope, Rhino.args_to_javascript(args, scope))
|
2011-12-07 13:59:39 -05:00
|
|
|
ensure
|
2011-12-09 13:19:55 -05:00
|
|
|
Rhino::JS::Context.exit
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# use JavaScript functions constructors from Ruby as `fn.new`
|
|
|
|
def new(*args)
|
2011-12-09 13:19:55 -05:00
|
|
|
context = Rhino::JS::Context.enter
|
2011-12-07 13:59:39 -05:00
|
|
|
scope = getParentScope || context.initStandardObjects
|
2011-12-09 01:52:47 -05:00
|
|
|
construct(context, scope, Rhino.args_to_javascript(args, scope))
|
2011-12-07 13:59:39 -05:00
|
|
|
ensure
|
2011-12-09 13:19:55 -05:00
|
|
|
Rhino::JS::Context.exit
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|