2011-12-07 13:59:39 -05:00
|
|
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2009-09-24 20:06:31 -04:00
|
|
|
|
2011-11-28 16:23:30 -05:00
|
|
|
describe Rhino::Context do
|
|
|
|
|
2009-10-06 09:52:45 -04:00
|
|
|
describe "Initalizing Standard Javascript Objects" do
|
|
|
|
it "provides the standard objects without java integration by default" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open do |cxt|
|
2009-11-11 01:50:36 -05:00
|
|
|
cxt["Object"].should_not be_nil
|
|
|
|
cxt["Math"].should_not be_nil
|
|
|
|
cxt["String"].should_not be_nil
|
|
|
|
cxt["Function"].should_not be_nil
|
|
|
|
cxt["Packages"].should be_nil
|
|
|
|
cxt["java"].should be_nil
|
|
|
|
cxt["org"].should be_nil
|
|
|
|
cxt["com"].should be_nil
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2011-11-28 16:23:30 -05:00
|
|
|
|
2009-10-06 09:52:45 -04:00
|
|
|
it "provides unsealed standard object by default" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open do |cxt|
|
2009-11-11 01:50:36 -05:00
|
|
|
cxt.eval("Object.foop = 'blort'")
|
|
|
|
cxt["Object"]['foop'].should == 'blort'
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2011-11-28 16:23:30 -05:00
|
|
|
|
2011-09-21 18:07:34 -04:00
|
|
|
it "allows you to scope the context to an object" do
|
|
|
|
class MyScope
|
2012-01-07 11:01:38 -05:00
|
|
|
def foo(*args); args && 'bar'; end
|
2011-09-21 18:07:34 -04:00
|
|
|
end
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open(:with => MyScope.new) do |ctx|
|
2011-09-21 18:07:34 -04:00
|
|
|
ctx.eval("foo()").should == 'bar'
|
|
|
|
end
|
|
|
|
end
|
2011-11-28 16:23:30 -05:00
|
|
|
|
2009-10-06 09:52:45 -04:00
|
|
|
it "allows you to seal the standard objects so that they cannot be modified" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open(:sealed => true) do |cxt|
|
2009-11-11 01:50:36 -05:00
|
|
|
lambda {
|
2011-11-28 16:23:30 -05:00
|
|
|
cxt.eval("Object.foop = 'blort'")
|
|
|
|
}.should raise_error(Rhino::JSError)
|
|
|
|
|
2009-11-11 01:50:36 -05:00
|
|
|
lambda {
|
2011-11-28 16:23:30 -05:00
|
|
|
cxt.eval("Object.prototype.toString = function() {}")
|
|
|
|
}.should raise_error(Rhino::JSError)
|
2009-10-06 09:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2011-11-28 16:23:30 -05:00
|
|
|
|
2009-10-06 09:52:45 -04:00
|
|
|
it "allows java integration to be turned on when initializing standard objects" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open(:java => true) do |cxt|
|
2011-12-07 06:50:43 -05:00
|
|
|
cxt["Packages"].should_not be_nil
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
2011-11-28 16:23:30 -05:00
|
|
|
end
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|
2011-12-07 06:50:43 -05:00
|
|
|
|
|
|
|
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
|
2012-02-15 05:04:14 -05:00
|
|
|
|
|
|
|
it "should have a (shared) factory by default" do
|
|
|
|
context1 = Rhino::Context.new
|
|
|
|
context1.factory.should_not be nil
|
|
|
|
context1.factory.should be_a(Rhino::JS::ContextFactory)
|
|
|
|
|
|
|
|
context1.factory.should be Rhino::Context.default_factory
|
|
|
|
|
|
|
|
context2 = Rhino::Context.new
|
|
|
|
context2.factory.should be context1.factory
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows limiting instruction count" do
|
|
|
|
context = Rhino::Context.new :restrictable => true
|
|
|
|
context.instruction_limit = 100
|
|
|
|
lambda {
|
|
|
|
context.eval %Q{ for (var i = 0; i < 100; i++) Number(i).toString(); }
|
|
|
|
}.should raise_error(Rhino::RunawayScriptError)
|
|
|
|
|
|
|
|
context.instruction_limit = nil
|
|
|
|
lambda {
|
|
|
|
context.eval %Q{ for (var i = 0; i < 100; i++) Number(i).toString(); }
|
|
|
|
}.should_not raise_error(Rhino::RunawayScriptError)
|
|
|
|
end
|
2011-12-07 06:50:43 -05:00
|
|
|
|
2009-09-24 20:06:31 -04:00
|
|
|
end
|