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

support for setting JS interpreter version

This commit is contained in:
kares 2011-12-07 12:50:43 +01:00
parent 55bda78f6f
commit 032666b074
2 changed files with 42 additions and 1 deletions

View file

@ -137,6 +137,31 @@ module Rhino
@native.setOptimizationLevel(level)
end
# Get the JS interpreter version.
# Returns a number e.g. 1.7, nil if unknown and 0 for default.
def version
case const_value = @native.getLanguageVersion
when -1 then nil # VERSION_UNKNOWN
when 0 then 0 # VERSION_DEFAULT
else const_value / 100.0 # VERSION_1_1 (1.1 = 110 / 100)
end
end
# Sets interpreter mode a.k.a. JS language version e.g. 1.7 (if supported).
def version=(version)
const = version.to_s.gsub('.', '_').upcase
const = "VERSION_#{const}" if const[0, 7] != 'VERSION'
js_context = @native.class # Context
if js_context.constants.include?(const)
const_value = js_context.const_get(const)
@native.setLanguageVersion(const_value)
const_value
else
@native.setLanguageVersion(js_context::VERSION_DEFAULT)
nil
end
end
# Enter this context for operations. Some methods such as eval() will
# fail unless this context is open
def open

View file

@ -46,8 +46,24 @@ describe Rhino::Context do
it "allows java integration to be turned on when initializing standard objects" do
Rhino::Context.open(:java => true) do |cxt|
cxt["Packages"].should_not be_nil
cxt["Packages"].should_not be_nil
end
end
end
it "should get default interpreter version" do
context = Rhino::Context.new
context.version.should == 0
end
it "should set interpreter version" do
context = Rhino::Context.new
context.version = 1.6
context.version.should == 1.6
context.version = '1.7'
context.version.should == 1.7
end
end