mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
place To module methods on Rhino itself - move deprecations (for previous version compatibility) into one place with warnings
This commit is contained in:
parent
11f4df4be9
commit
0d4d17a0a9
9 changed files with 145 additions and 69 deletions
|
@ -15,13 +15,14 @@ module Rhino
|
|||
|
||||
end
|
||||
|
||||
J = JS # (deprecated) backward compatibility
|
||||
|
||||
end
|
||||
|
||||
require 'rhino/wormhole'
|
||||
Rhino.extend Rhino::To
|
||||
|
||||
require 'rhino/object'
|
||||
require 'rhino/context'
|
||||
require 'rhino/wormhole'
|
||||
require 'rhino/rhino_ext'
|
||||
require 'rhino/ruby_object'
|
||||
require 'rhino/ruby_function'
|
||||
require 'rhino/rhino_ext'
|
||||
require 'rhino/deprecations'
|
||||
|
|
|
@ -61,7 +61,7 @@ module Rhino
|
|||
@native = context
|
||||
@global = @native.initStandardObjects(nil, options[:sealed] == true)
|
||||
if with = options[:with]
|
||||
@scope = To.javascript(with)
|
||||
@scope = Rhino.to_javascript(with)
|
||||
@scope.setParentScope(@global)
|
||||
else
|
||||
@scope = @global
|
||||
|
@ -92,15 +92,15 @@ module Rhino
|
|||
def eval(source, source_name = "<eval>", line_number = 1)
|
||||
self.open do
|
||||
begin
|
||||
scope = To.javascript(@scope)
|
||||
scope = Rhino.to_javascript(@scope)
|
||||
if IO === source || StringIO === source
|
||||
result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil)
|
||||
else
|
||||
result = @native.evaluateString(scope, source.to_s, source_name, line_number, nil)
|
||||
end
|
||||
To.ruby result
|
||||
Rhino.to_ruby(result)
|
||||
rescue JS::RhinoException => e
|
||||
raise Rhino::JavascriptError, e
|
||||
raise JavascriptError, e
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
38
lib/rhino/deprecations.rb
Normal file
38
lib/rhino/deprecations.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
|
||||
module Rhino
|
||||
|
||||
@@stub_class = Class.new(Object)
|
||||
|
||||
def self.const_missing(name)
|
||||
case name.to_s
|
||||
when 'J' then
|
||||
warn "[DEPRECATION] `Rhino::J` is deprecated, use `Rhino::JS` instead."
|
||||
return JS
|
||||
when 'NativeObject' then
|
||||
warn "[DEPRECATION] `Rhino::NativeObject` is no longer used, returning a stub."
|
||||
return @@stub_class
|
||||
when 'NativeFunction' then
|
||||
warn "[DEPRECATION] `Rhino::NativeFunction` is no longer used, returning a stub."
|
||||
return @@stub_class
|
||||
else super
|
||||
end
|
||||
end
|
||||
|
||||
module To
|
||||
|
||||
extend self
|
||||
|
||||
# @deprecated use {#to_ruby} instead
|
||||
def self.ruby(object)
|
||||
warn "[DEPRECATION] `Rhino::To.ruby` is deprecated, use `Rhino.to_ruby` instead."
|
||||
to_ruby(object)
|
||||
end
|
||||
|
||||
# @deprecated use {#to_javascript} instead
|
||||
def self.javascript(object, scope = nil)
|
||||
warn "[DEPRECATION] `Rhino::To.javascript` is deprecated, use `Rhino.to_javascript` instead."
|
||||
to_javascript(object, scope)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -15,7 +15,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
# jsobject['Take me to'] # => 'a funky town'
|
||||
#
|
||||
def [](name)
|
||||
Rhino::To.to_ruby ScriptableObject.getProperty(self, name.to_s)
|
||||
Rhino.to_ruby ScriptableObject.getProperty(self, name.to_s)
|
||||
end
|
||||
|
||||
# set a property on the javascript object, where +k+ is a string or symbol corresponding
|
||||
|
@ -29,7 +29,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
#
|
||||
def []=(key, value)
|
||||
scope = self
|
||||
ScriptableObject.putProperty(self, key.to_s, Rhino::To.to_javascript(value, scope))
|
||||
ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope))
|
||||
end
|
||||
|
||||
# enumerate the key value pairs contained in this javascript object. e.g.
|
||||
|
@ -41,7 +41,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
# outputs foo -> bar baz -> bang
|
||||
#
|
||||
def each
|
||||
getAllIds.each { |id| yield id, Rhino::To.to_ruby(get(id, self)) }
|
||||
getAllIds.each { |id| yield id, Rhino.to_ruby(get(id, self)) }
|
||||
end
|
||||
|
||||
def each_key
|
||||
|
@ -49,7 +49,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
end
|
||||
|
||||
def each_value
|
||||
getAllIds.each { |id| yield Rhino::To.to_ruby(get(id, self)) }
|
||||
getAllIds.each { |id| yield Rhino.to_ruby(get(id, self)) }
|
||||
end
|
||||
|
||||
def keys
|
||||
|
@ -84,7 +84,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
if ScriptableObject.hasProperty(self, name.to_s)
|
||||
begin
|
||||
context = Context.enter
|
||||
js_args = Rhino::To.args_to_javascript(args, self) # scope == self
|
||||
js_args = Rhino.args_to_javascript(args, self) # scope == self
|
||||
ScriptableObject.callMethod(context, self, name.to_s, js_args)
|
||||
ensure
|
||||
Context.exit
|
||||
|
@ -101,7 +101,7 @@ class Java::OrgMozillaJavascript::NativeObject
|
|||
# re-implement Map#put
|
||||
def []=(key, value)
|
||||
scope = self
|
||||
ScriptableObject.putProperty(self, key.to_s, Rhino::To.to_javascript(value, scope))
|
||||
ScriptableObject.putProperty(self, key.to_s, Rhino.to_javascript(value, scope))
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -117,7 +117,7 @@ class Java::OrgMozillaJavascript::BaseFunction
|
|||
def call(*args)
|
||||
context = Context.enter
|
||||
scope = getParentScope || context.initStandardObjects
|
||||
__call__(context, scope, scope, Rhino::To.args_to_javascript(args, scope))
|
||||
__call__(context, scope, scope, Rhino.args_to_javascript(args, scope))
|
||||
ensure
|
||||
Context.exit
|
||||
end
|
||||
|
@ -126,7 +126,7 @@ class Java::OrgMozillaJavascript::BaseFunction
|
|||
def new(*args)
|
||||
context = Context.enter
|
||||
scope = getParentScope || context.initStandardObjects
|
||||
construct(context, scope, Rhino::To.args_to_javascript(args, scope))
|
||||
construct(context, scope, Rhino.args_to_javascript(args, scope))
|
||||
ensure
|
||||
Context.exit
|
||||
end
|
||||
|
|
|
@ -14,14 +14,16 @@ module Rhino
|
|||
# override Object BaseFunction#call(Context context, Scriptable scope,
|
||||
# Scriptable thisObj, Object[] args)
|
||||
def call(context, scope, this, args)
|
||||
rb_args = To.args_to_ruby(args.to_a)
|
||||
rb_args = Rhino.args_to_ruby(args.to_a)
|
||||
begin
|
||||
result = @callable.call(*rb_args)
|
||||
rescue => e
|
||||
# ... correct wrapping thus it's try { } catch (e) works in JS :
|
||||
|
||||
raise JS::WrappedException.new(org.jruby.exceptions.RaiseException.new(e))
|
||||
|
||||
end
|
||||
To.to_javascript(result, scope)
|
||||
Rhino.to_javascript(result, scope)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -46,7 +46,7 @@ module Rhino
|
|||
return RubyFunction.new(@ruby.method(name))
|
||||
elsif @ruby.instance_variables.include?(var_name = "@#{name}")
|
||||
var_value = @ruby.instance_variable_get(var_name)
|
||||
return Rhino::To.to_javascript(var_value, self)
|
||||
return Rhino.to_javascript(var_value, self)
|
||||
end
|
||||
end
|
||||
super
|
||||
|
@ -69,7 +69,7 @@ module Rhino
|
|||
def put(name, start, value)
|
||||
if name.is_a?(String)
|
||||
if @ruby.respond_to?(set_name = "#{name}=")
|
||||
return @ruby.send(set_name, Rhino::To.to_ruby(value))
|
||||
return @ruby.send(set_name, Rhino.to_ruby(value))
|
||||
end
|
||||
end
|
||||
super
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
module Rhino
|
||||
module To
|
||||
|
||||
module_function
|
||||
|
||||
def to_ruby(object)
|
||||
case object
|
||||
when JS::Scriptable::NOT_FOUND, JS::Undefined then nil
|
||||
|
@ -13,7 +11,6 @@ module Rhino
|
|||
else object
|
||||
end
|
||||
end
|
||||
def ruby(object); to_ruby(object); end # alias
|
||||
|
||||
def to_javascript(object, scope = nil)
|
||||
case object
|
||||
|
@ -27,45 +24,46 @@ module Rhino
|
|||
else RubyObject.new(object)
|
||||
end
|
||||
end
|
||||
def javascript(object, scope = nil); to_javascript(object, scope); end # alias
|
||||
|
||||
def args_to_ruby(args)
|
||||
args.map { |arg| to_ruby(arg) }
|
||||
end
|
||||
|
||||
def array_to_ruby(js_array)
|
||||
js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) }
|
||||
end
|
||||
|
||||
def args_to_javascript(args, scope = nil)
|
||||
args.map { |arg| to_javascript(arg, scope) }.to_java
|
||||
end
|
||||
|
||||
def array_to_javascript(rb_array, scope = nil)
|
||||
if scope
|
||||
raise "no current context" unless context = JS::Context.getCurrentContext
|
||||
context.newArray(scope, rb_array.to_java)
|
||||
else
|
||||
JS::NativeArray.new(rb_array.to_java)
|
||||
end
|
||||
end
|
||||
private
|
||||
|
||||
def hash_to_javascript(rb_hash, scope = nil)
|
||||
js_object =
|
||||
def array_to_ruby(js_array)
|
||||
js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) }
|
||||
end
|
||||
|
||||
def array_to_javascript(rb_array, scope = nil)
|
||||
if scope
|
||||
raise "no current context" unless context = JS::Context.getCurrentContext
|
||||
context.newObject(scope)
|
||||
context.newArray(scope, rb_array.to_java)
|
||||
else
|
||||
JS::NativeObject.new
|
||||
JS::NativeArray.new(rb_array.to_java)
|
||||
end
|
||||
# JS::NativeObject implements Map put it's #put does :
|
||||
# throw new UnsupportedOperationException(); thus no []=
|
||||
rb_hash.each_pair do |key, val|
|
||||
js_val = to_javascript(val, scope)
|
||||
JS::ScriptableObject.putProperty(js_object, key.to_s, js_val)
|
||||
end
|
||||
js_object
|
||||
end
|
||||
|
||||
def hash_to_javascript(rb_hash, scope = nil)
|
||||
js_object =
|
||||
if scope
|
||||
raise "no current context" unless context = JS::Context.getCurrentContext
|
||||
context.newObject(scope)
|
||||
else
|
||||
JS::NativeObject.new
|
||||
end
|
||||
# JS::NativeObject implements Map put it's #put does :
|
||||
# throw new UnsupportedOperationException(); thus no []=
|
||||
rb_hash.each_pair do |key, val|
|
||||
js_val = to_javascript(val, scope)
|
||||
JS::ScriptableObject.putProperty(js_object, key.to_s, js_val)
|
||||
end
|
||||
js_object
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
37
spec/rhino/deprecations_spec.rb
Normal file
37
spec/rhino/deprecations_spec.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
||||
|
||||
require 'stringio'
|
||||
|
||||
describe 'deprecations' do
|
||||
|
||||
stderr = $stderr
|
||||
|
||||
before do
|
||||
$stderr = StringIO.new
|
||||
end
|
||||
|
||||
after do
|
||||
$stderr = stderr
|
||||
end
|
||||
|
||||
it "To ruby 42" do
|
||||
Rhino::To.ruby(42).should == 42
|
||||
end
|
||||
|
||||
it "To javascript 42" do
|
||||
Rhino::To.javascript(42).should == 42
|
||||
end
|
||||
|
||||
it "J constant still works" do
|
||||
lambda { Rhino::J::Scriptable }.should_not raise_error
|
||||
end
|
||||
|
||||
it "NativeObject constant exists" do
|
||||
lambda { Rhino::NativeObject }.should_not raise_error
|
||||
end
|
||||
|
||||
it "NativeFunction constant exists" do
|
||||
lambda { Rhino::NativeFunction }.should_not raise_error
|
||||
end
|
||||
|
||||
end
|
|
@ -5,16 +5,16 @@ describe Rhino::To do
|
|||
describe "ruby translation" do
|
||||
|
||||
it "converts javascript NOT_FOUND to ruby nil" do
|
||||
Rhino::To.ruby(Rhino::JS::Scriptable::NOT_FOUND).should be_nil
|
||||
Rhino.to_ruby(Rhino::JS::Scriptable::NOT_FOUND).should be_nil
|
||||
end
|
||||
|
||||
it "converts javascript undefined into nil" do
|
||||
Rhino::To.ruby(Rhino::JS::Undefined.instance).should be_nil
|
||||
Rhino.to_ruby(Rhino::JS::Undefined.instance).should be_nil
|
||||
end
|
||||
|
||||
it "does return javascript object" do
|
||||
Rhino::JS::NativeObject.new.tap do |js_obj|
|
||||
Rhino::To.ruby(js_obj).tap do |rb_obj|
|
||||
Rhino.to_ruby(js_obj).tap do |rb_obj|
|
||||
rb_obj.should be(js_obj)
|
||||
end
|
||||
end
|
||||
|
@ -22,7 +22,7 @@ describe Rhino::To do
|
|||
|
||||
it "wraps native javascript arrays into a ruby NativeArray wrapper" do
|
||||
Rhino::JS::NativeArray.new([1,2,4].to_java).tap do |js_array|
|
||||
Rhino::To.ruby(js_array).should == [1,2,4]
|
||||
Rhino.to_ruby(js_array).should == [1,2,4]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -31,7 +31,7 @@ describe Rhino::To do
|
|||
klass = Class.new(Rhino::JS::BaseFunction)
|
||||
|
||||
klass.new.tap do |js_fn|
|
||||
Rhino::To.ruby(js_fn).tap do |rb_fn|
|
||||
Rhino.to_ruby(js_fn).tap do |rb_fn|
|
||||
rb_fn.should be(js_fn)
|
||||
end
|
||||
end
|
||||
|
@ -40,7 +40,7 @@ describe Rhino::To do
|
|||
|
||||
it "leaves native ruby objects alone" do
|
||||
Object.new.tap do |o|
|
||||
Rhino::To.ruby(o).should be(o)
|
||||
Rhino.to_ruby(o).should be(o)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -49,7 +49,7 @@ describe Rhino::To do
|
|||
scope = cx.scope
|
||||
j_str = java.lang.String.new("Hello World")
|
||||
Rhino::JS::NativeJavaObject.new(scope, j_str, j_str.getClass()).tap do |o|
|
||||
Rhino::To.ruby(o).should == "Hello World"
|
||||
Rhino.to_ruby(o).should == "Hello World"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -59,22 +59,22 @@ describe Rhino::To do
|
|||
describe "javascript translation" do
|
||||
|
||||
it "passes primitives through to the js layer to let jruby and rhino do he thunking" do
|
||||
Rhino::To.javascript(1).should be(1)
|
||||
Rhino::To.javascript(2.5).should == 2.5
|
||||
Rhino::To.javascript("foo").should == "foo"
|
||||
Rhino::To.javascript(true).should be(true)
|
||||
Rhino::To.javascript(false).should be(false)
|
||||
Rhino::To.javascript(nil).should be_nil
|
||||
Rhino.to_javascript(1).should be(1)
|
||||
Rhino.to_javascript(2.5).should == 2.5
|
||||
Rhino.to_javascript("foo").should == "foo"
|
||||
Rhino.to_javascript(true).should be(true)
|
||||
Rhino.to_javascript(false).should be(false)
|
||||
Rhino.to_javascript(nil).should be_nil
|
||||
end
|
||||
|
||||
it "leaves native javascript objects alone" do
|
||||
Rhino::JS::NativeObject.new.tap do |o|
|
||||
Rhino::To.javascript(o).should be(o)
|
||||
Rhino.to_javascript(o).should be(o)
|
||||
end
|
||||
end
|
||||
|
||||
it "converts ruby arrays into javascript arrays" do
|
||||
Rhino::To.javascript([1,2,3,4,5]).tap do |a|
|
||||
Rhino.to_javascript([1,2,3,4,5]).tap do |a|
|
||||
a.should be_kind_of(Rhino::JS::NativeArray)
|
||||
a.get(0,a).should be(1)
|
||||
a.get(1,a).should be(2)
|
||||
|
@ -85,7 +85,7 @@ describe Rhino::To do
|
|||
end
|
||||
|
||||
it "converts ruby hashes into native objects" do
|
||||
Rhino::To.javascript({ :bare => true }).tap do |h|
|
||||
Rhino.to_javascript({ :bare => true }).tap do |h|
|
||||
h.should be_kind_of(Rhino::JS::NativeObject)
|
||||
h.get("bare", h).should be(true)
|
||||
h.prototype.should be_nil # this is how Rhino works !
|
||||
|
@ -109,7 +109,7 @@ describe Rhino::To do
|
|||
end
|
||||
|
||||
it "converts ruby arrays into javascript arrays" do
|
||||
Rhino::To.javascript([1,2,3,4,5], @scope).tap do |a|
|
||||
Rhino.to_javascript([1,2,3,4,5], @scope).tap do |a|
|
||||
a.should be_kind_of(Rhino::JS::NativeArray)
|
||||
a.get(0,a).should be(1)
|
||||
a.get(1,a).should be(2)
|
||||
|
@ -120,7 +120,7 @@ describe Rhino::To do
|
|||
end
|
||||
|
||||
it "converts ruby hashes into native objects" do
|
||||
Rhino::To.javascript({ :bare => true }, @scope).tap do |h|
|
||||
Rhino.to_javascript({ :bare => true }, @scope).tap do |h|
|
||||
h.should be_kind_of(Rhino::JS::NativeObject)
|
||||
h.get("bare", h).should be(true)
|
||||
h.prototype.should_not be_nil
|
||||
|
@ -130,14 +130,14 @@ describe Rhino::To do
|
|||
end
|
||||
|
||||
it "converts procs and methods into native functions" do
|
||||
Rhino::To.javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
||||
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
||||
f.should be_kind_of(Rhino::JS::Function)
|
||||
f.call(nil, nil, nil, [7,6]).should be(42)
|
||||
end
|
||||
|
||||
Rhino::To.javascript("foo,bar,baz".method(:split)).tap do |m|
|
||||
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |m|
|
||||
m.should be_kind_of(Rhino::JS::Function)
|
||||
Rhino::To.ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
||||
Rhino.to_ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -148,7 +148,7 @@ describe Rhino::To do
|
|||
# end
|
||||
# end
|
||||
#
|
||||
# Rhino::To.javascript(klass.new).tap do |o|
|
||||
# Rhino.to_javascript(klass.new).tap do |o|
|
||||
# o.should be_kind_of(Rhino::RubyObject)
|
||||
# o.prototype.tap do |p|
|
||||
# p.should_not be_nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue