1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00

add referential integrity to objects being passed into Rhino.

This commit is contained in:
Charles Lowell 2011-10-06 06:09:00 -05:00
parent 478d81831f
commit 3e31402a77
3 changed files with 47 additions and 24 deletions

View file

@ -203,6 +203,8 @@ module Rhino
end
end
JSError = JavascriptError
class RunawayScriptError < StandardError # :nodoc:
end
end

View file

@ -11,13 +11,19 @@ module Rhino
#
class NativeFunction < NativeObject
def call(*args)
begin
cxt = J::Context.enter()
scope = @j.getParentScope() || cxt.initStandardObjects()
@j.call(cxt, scope, scope, args.map {|o| To.javascript(o)})
ensure
J::Context.exit()
end
cxt = J::Context.enter()
scope = @j.getParentScope() || cxt.initStandardObjects()
@j.call(cxt, scope, scope, args.map {|o| To.javascript(o)})
ensure
J::Context.exit()
end
def methodcall(this, *args)
cxt = J::Context.enter()
scope = @j.getParentScope() || cxt.initStandardObjects()
@j.call(cxt, scope, To.javascript(this), args.map {|o| To.javascript(o)})
ensure
J::Context.exit()
end
end
end

View file

@ -3,6 +3,8 @@ module Rhino
module To
JS_UNDEF = [J::Scriptable::NOT_FOUND, J::Undefined]
module_function
def ruby(object)
case object
when *JS_UNDEF then nil
@ -10,8 +12,8 @@ module Rhino
when J::NativeArray then array(object)
when J::NativeDate then Time.at(object.getJSTimeValue() / 1000)
when J::Regexp::NativeRegExp then object
when J::Function then NativeFunction.new(object)
when J::Scriptable then NativeObject.new(object)
when J::Function then r2j(object) {|o| NativeFunction.new(o)}
when J::Scriptable then r2j(object) {|o| NativeObject.new(o)}
else object
end
end
@ -43,6 +45,19 @@ module Rhino
native_object.j
end
module_function :ruby, :javascript, :array, :ruby_hash_to_native
@@r2j = {}
def r2j(value)
if ref = @@r2j[value.object_id]
if peer = ref.get()
return peer
end
else
yield(value).tap do |peer|
@@r2j[value.object_id] = java.lang.ref.WeakReference.new(peer)
end
end
end
end
end