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

allow to set optimization level globally

also allow reading it from system properties by default :
`java.lang.System.setProperty("rhino.opt.level", "1")` 
(Java 7 embedded-rhino scripting engine compatible)
This commit is contained in:
kares 2012-12-03 17:21:23 +01:00
parent 82134aecf2
commit 8dc0781dbe
5 changed files with 37 additions and 11 deletions

View file

@ -4,8 +4,8 @@ module Rhino
# allow for rhino.jar overrides for "experimental" jrubyists
# fallback to rhino/jar_path provided therubyrhino_jar gem :
require 'rhino/jar_path' unless defined?(Rhino::JAR_PATH)
load Rhino::JAR_PATH
require 'rhino/jar_path' unless const_defined?(:JAR_PATH)
load JAR_PATH
# This module contains all the native Rhino objects implemented in Java
# e.g. Rhino::JS::NativeObject # => org.mozilla.javascript.NativeObject

View file

@ -58,6 +58,15 @@ module Rhino
@@default_factory = factory
end
@@default_optimization_level = java.lang.Integer.getInteger('rhino.opt.level')
def self.default_optimization_level
@@default_optimization_level
end
def self.default_optimization_level=(level)
@@default_optimization_level = level
end
attr_reader :scope
# Create a new javascript environment for executing javascript and ruby code.
@ -82,6 +91,9 @@ module Rhino
end
end
end
if optimization_level = options[:optimization_level] || self.class.default_optimization_level
self.optimization_level = optimization_level
end
yield(self) if block_given?
end

View file

@ -1,9 +1,9 @@
module Rhino
@@stub_class = Class.new(Object)
@@stub_class = Class.new(Object) # :nodoc
def self.const_missing(name)
def self.const_missing(name) # :nodoc
case name.to_s
when 'J' then
warn "[DEPRECATION] `Rhino::J` is deprecated, use `Rhino::JS` instead."
@ -23,7 +23,7 @@ module Rhino
@@warnings = {}
def self.warn(msg)
def self.warn(msg) # :nodoc
# only print out deprecation warnings once
if msg[0, 13] == '[DEPRECATION]'
return nil if @@warnings[msg]
@ -32,17 +32,17 @@ module Rhino
super # Kernel.warn
end
module To
module To # :nodoc
extend self
# @deprecated use {#to_ruby} instead
# #deprecated use {Rhino#to_ruby} instead
def self.ruby(object)
Rhino.warn "[DEPRECATION] `Rhino::To.ruby` is deprecated, use `Rhino.to_ruby` instead."
to_ruby(object)
end
# @deprecated use {#to_javascript} instead
# #deprecated use {Rhino#to_javascript} instead
def self.javascript(object, scope = nil)
Rhino.warn "[DEPRECATION] `Rhino::To.javascript` is deprecated, use `Rhino.to_javascript` instead."
to_javascript(object, scope)

View file

@ -272,8 +272,8 @@ module Rhino
end
RubyObject = Ruby::Object
RubyFunction = Ruby::Function
RubyConstructor = Ruby::Constructor
RubyObject = Ruby::Object # :nodoc
RubyFunction = Ruby::Function # :nodoc
RubyConstructor = Ruby::Constructor # :nodoc
end

View file

@ -140,4 +140,18 @@ describe Rhino::Context do
}.should raise_error(Rhino::RunawayScriptError)
end
it "allows to set (default) optimization level" do
context = Rhino::Context.new :optimization_level => 2
context.eval %Q{ for (var i = 0; i < 42; i++) Number(i).toString(); }
context.optimization_level.should == 2
begin
Rhino::Context.default_optimization_level = 3
context = Rhino::Context.new
context.eval %Q{ for (var i = 0; i < 42; i++) Number(i).toString(); }
context.optimization_level.should == 3
ensure
Rhino::Context.default_optimization_level = nil
end
end
end