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

Auto-lock, Auto-scope all specs, except where not.

This commit is contained in:
Charles Lowell 2012-06-05 08:00:04 -05:00
parent ec1d910b3d
commit 8976cb9fc7
11 changed files with 165 additions and 164 deletions

View file

@ -1,22 +1,17 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Array do describe V8::C::Array do
include C::ContextHelper
it "can store and retrieve a value" do it "can store and retrieve a value" do
V8::C::HandleScope() do o = V8::C::Object::New()
o = V8::C::Object::New() a = V8::C::Array::New()
a = V8::C::Array::New() a.Length().should eql 0
a.Length().should eql 0 a.Set(0, o)
a.Set(0, o) a.Length().should eql 1
a.Length().should eql 1 a.Get(0).Equals(o).should be_true
a.Get(0).Equals(o).should be_true
end
end end
it "can be initialized with a length" do it "can be initialized with a length" do
V8::C::HandleScope() do a = V8::C::Array::New(5)
a = V8::C::Array::New(5) a.Length().should eql 5
a.Length().should eql 5
end
end end
end end

View file

@ -1,15 +0,0 @@
module C
module ContextHelper
def self.included(base)
base.instance_eval do
before do
@cxt = V8::C::Context::New()
@cxt.Enter()
end
after do
@cxt.Exit()
end
end
end
end
end

View file

@ -1,12 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::External do describe V8::C::External do
include C::ContextHelper
it "can store and retrieve a value" do it "can store and retrieve a value" do
V8::C::HandleScope() do o = Object.new
o = Object.new external = V8::C::External::New(o)
external = V8::C::External::New(o) external.Value().should be(o)
external.Value().should be(o)
end
end end
end end

View file

@ -1,27 +1,20 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Function do describe V8::C::Function do
include C::ContextHelper
it "can be called" do it "can be called" do
V8::C::HandleScope() do fn = run '(function() {return "foo"})'
fn = run '(function() {return "foo"})' fn.Call(@cxt.Global(), 0, []).Utf8Value().should eql "foo"
fn.Call(@cxt.Global(), 0, []).Utf8Value().should eql "foo"
end
end end
it "can be called as a constructor" do it "can be called as a constructor" do
V8::C::HandleScope() do fn = run '(function() {this.foo = "foo"})'
fn = run '(function() {this.foo = "foo"})' fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo"
fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo"
end
end end
it "can be called as a constructor with arguments" do it "can be called as a constructor with arguments" do
V8::C::HandleScope() do fn = run '(function(foo) {this.foo = foo})'
fn = run '(function(foo) {this.foo = foo})' object = fn.NewInstance(1, [V8::C::String::New("bar")])
object = fn.NewInstance(1, [V8::C::String::New("bar")]) object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar"
object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar"
end
end end
def run(source) def run(source)

View file

@ -1,11 +1,26 @@
require 'spec_helper' require 'spec_helper'
describe "setting up handles scopes" do describe "setting up handles scopes" do
include C::ContextHelper include ExplicitScoper
before do
def self.instance_eval(*args, &block)
V8::C::Locker() do
cxt = V8::C::Context::New()
begin
cxt.Enter()
super(*args, &block)
ensure
cxt.Exit()
end
end
end
end
it "can allocate handle scopes" do it "can allocate handle scopes" do
V8::C::HandleScope() do V8::C::HandleScope() do
V8::C::Object::New() V8::C::Object::New()
end.class.should eql V8::C::Object end.class.should eql V8::C::Object
end end
it "isn't the end of the world if a ruby exception is raised inside a HandleScope" do it "isn't the end of the world if a ruby exception is raised inside a HandleScope" do

View file

@ -1,8 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Locker do describe V8::C::Locker do
include ExplicitScoper
it "can lock and unlock the VM" do it "can lock and unlock the VM" do
pending "need to figure out how to wrap rspec methods"
V8::C::Locker::IsLocked().should be_false V8::C::Locker::IsLocked().should be_false
V8::C::Locker() do V8::C::Locker() do
V8::C::Locker::IsLocked().should be_true V8::C::Locker::IsLocked().should be_true
@ -14,7 +15,6 @@ describe V8::C::Locker do
end end
it "properly unlocks if an exception is thrown inside a lock block" do it "properly unlocks if an exception is thrown inside a lock block" do
pending "need to figure out how to wrap rspec methods"
begin begin
V8::C::Locker() do V8::C::Locker() do
raise "boom!" raise "boom!"
@ -23,4 +23,16 @@ describe V8::C::Locker do
V8::C::Locker::IsLocked().should be_false V8::C::Locker::IsLocked().should be_false
end end
end end
it "properly re-locks if an exception is thrown inside an un-lock block" do
V8::C::Locker() do
begin
V8::C::Unlocker() do
raise "boom!"
end
rescue
V8::C::Locker::IsLocked().should be_true
end
end
end
end end

View file

@ -1,47 +1,40 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Object do describe V8::C::Object do
include C::ContextHelper
it "can store and retrieve a value" do it "can store and retrieve a value" do
V8::C::HandleScope() do o = V8::C::Object::New()
o = V8::C::Object::New() key = V8::C::String::New("foo")
key = V8::C::String::New("foo") value = V8::C::String::New("bar")
value = V8::C::String::New("bar") o.Set(key, value)
o.Set(key, value) o.Get(key).Utf8Value().should eql "bar"
o.Get(key).Utf8Value().should eql "bar"
end
end end
it "can retrieve all property names" do it "can retrieve all property names" do
V8::C::HandleScope() do o = V8::C::Object::New()
o = V8::C::Object::New() o.Set(V8::C::String::New("foo"), V8::C::String::New("bar"))
o.Set(V8::C::String::New("foo"), V8::C::String::New("bar")) o.Set(V8::C::String::New("baz"), V8::C::String::New("bang"))
o.Set(V8::C::String::New("baz"), V8::C::String::New("bang")) names = o.GetPropertyNames()
names = o.GetPropertyNames() names.Length().should eql 2
names.Length().should eql 2 names.Get(0).Utf8Value().should eql "foo"
names.Get(0).Utf8Value().should eql "foo" names.Get(1).Utf8Value().should eql "baz"
names.Get(1).Utf8Value().should eql "baz"
end
end end
it "can set an accessor from ruby" do it "can set an accessor from ruby" do
V8::C::HandleScope() do o = V8::C::Object::New()
o = V8::C::Object::New() property = V8::C::String::New("statement")
property = V8::C::String::New("statement") callback_data = V8::C::String::New("I am Legend")
callback_data = V8::C::String::New("I am Legend") left = V8::C::String::New("Yo! ")
left = V8::C::String::New("Yo! ") getter = proc do |name, info|
getter = proc do |name, info| info.This().StrictEquals(o).should be_true
info.This().StrictEquals(o).should be_true info.Holder().StrictEquals(o).should be_true
info.Holder().StrictEquals(o).should be_true V8::C::String::Concat(left, info.Data())
V8::C::String::Concat(left, info.Data())
end
setter = proc do |name, value, info|
left = value
end
o.SetAccessor(property, getter, setter, callback_data)
o.Get(property).Utf8Value().should eql "Yo! I am Legend"
o.Set(property, V8::C::String::New("Bro! "))
o.Get(property).Utf8Value().should eql "Bro! I am Legend"
end end
setter = proc do |name, value, info|
left = value
end
o.SetAccessor(property, getter, setter, callback_data)
o.Get(property).Utf8Value().should eql "Yo! I am Legend"
o.Set(property, V8::C::String::New("Bro! "))
o.Get(property).Utf8Value().should eql "Bro! I am Legend"
end end
end end

View file

@ -1,14 +1,11 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::External do describe V8::C::Script do
include C::ContextHelper
it "can run a script and return a polymorphic result" do it "can run a script and return a polymorphic result" do
V8::C::HandleScope() do source = V8::C::String::New("(new Array())")
source = V8::C::String::New("(new Array())") filename = V8::C::String::New("<eval>")
filename = V8::C::String::New("<eval>") script = V8::C::Script::New(source, filename)
script = V8::C::Script::New(source, filename) result = script.Run()
result = script.Run() result.should be_kind_of V8::C::Array
result.should be_kind_of V8::C::Array
end
end end
end end

View file

@ -1,35 +1,30 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Template do describe V8::C::Template do
include C::ContextHelper
describe V8::C::FunctionTemplate do describe V8::C::FunctionTemplate do
it "can be created with no arguments" do it "can be created with no arguments" do
V8::C::HandleScope() do t = V8::C::FunctionTemplate::New()
t = V8::C::FunctionTemplate::New() t.GetFunction().Call(@cxt.Global(),0, []).StrictEquals(@cxt.Global()).should be_true
t.GetFunction().Call(@cxt.Global(),0, []).StrictEquals(@cxt.Global()).should be_true
end
end end
it "can be created with a callback" do it "can be created with a callback" do
V8::C::HandleScope() do receiver = V8::C::Object::New()
receiver = V8::C::Object::New() f = nil
f = nil callback = lambda do |arguments|
callback = lambda do |arguments| arguments.Length().should be(2)
arguments.Length().should be(2) arguments[0].Utf8Value().should eql 'one'
arguments[0].Utf8Value().should eql 'one' arguments[1].Utf8Value().should eql 'two'
arguments[1].Utf8Value().should eql 'two' arguments.Callee().StrictEquals(f).should be_true
arguments.Callee().StrictEquals(f).should be_true arguments.This().StrictEquals(receiver).should be_true
arguments.This().StrictEquals(receiver).should be_true arguments.Holder().StrictEquals(receiver).should be_true
arguments.Holder().StrictEquals(receiver).should be_true arguments.IsConstructCall().should be_false
arguments.IsConstructCall().should be_false arguments.Data().Value().should be(42)
arguments.Data().Value().should be(42) V8::C::String::New("result")
V8::C::String::New("result")
end
t = V8::C::FunctionTemplate::New(callback, V8::C::External::New(42))
f = t.GetFunction()
f.Call(receiver, 2, [V8::C::String::New('one'), V8::C::String::New('two')]).Utf8Value().should eql "result"
end end
t = V8::C::FunctionTemplate::New(callback, V8::C::External::New(42))
f = t.GetFunction()
f.Call(receiver, 2, [V8::C::String::New('one'), V8::C::String::New('two')]).Utf8Value().should eql "result"
end end
end end
end end

View file

@ -1,54 +1,51 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::External do describe V8::C::External do
include C::ContextHelper
it "can catch javascript exceptions" do it "can catch javascript exceptions" do
V8::C::V8::SetCaptureStackTraceForUncaughtExceptions(true, 99, V8::C::StackTrace::kDetailed) V8::C::V8::SetCaptureStackTraceForUncaughtExceptions(true, 99, V8::C::StackTrace::kDetailed)
V8::C::TryCatch() do |trycatch| V8::C::TryCatch() do |trycatch|
V8::C::HandleScope() do source = V8::C::String::New(<<-JS)
source = V8::C::String::New(<<-JS) function one() {
function one() { two()
two() }
} function two() {
function two() { three()
three() }
} function three() {
function three() { boom()
boom() }
} function boom() {
function boom() { throw new Error('boom!')
throw new Error('boom!') }
} eval('one()')
eval('one()') JS
JS filename = V8::C::String::New("<eval>")
filename = V8::C::String::New("<eval>") script = V8::C::Script::New(source, filename)
script = V8::C::Script::New(source, filename) result = script.Run()
result = script.Run() trycatch.HasCaught().should be_true
trycatch.HasCaught().should be_true trycatch.CanContinue().should be_true
trycatch.CanContinue().should be_true exception = trycatch.Exception()
exception = trycatch.Exception() exception.should_not be_nil
exception.should_not be_nil exception.IsNativeError().should be_true
exception.IsNativeError().should be_true trycatch.StackTrace().Utf8Value().should match /boom.*three.*two.*one/m
trycatch.StackTrace().Utf8Value().should match /boom.*three.*two.*one/m message = trycatch.Message();
message = trycatch.Message(); message.should_not be_nil
message.should_not be_nil message.Get().Utf8Value().should eql "Uncaught Error: boom!"
message.Get().Utf8Value().should eql "Uncaught Error: boom!" message.GetSourceLine().Utf8Value().should eql " throw new Error('boom!')"
message.GetSourceLine().Utf8Value().should eql " throw new Error('boom!')" message.GetScriptResourceName().Utf8Value().should eql "<eval>"
message.GetScriptResourceName().Utf8Value().should eql "<eval>" message.GetLineNumber().should eql 11
message.GetLineNumber().should eql 11 stack = message.GetStackTrace()
stack = message.GetStackTrace() stack.should_not be_nil
stack.should_not be_nil stack.GetFrameCount().should eql 6
stack.GetFrameCount().should eql 6 frame = stack.GetFrame(0)
frame = stack.GetFrame(0) frame.GetLineNumber().should eql 11
frame.GetLineNumber().should eql 11 frame.GetColumn().should eql 15
frame.GetColumn().should eql 17 frame.GetScriptName().Utf8Value().should eql "<eval>"
frame.GetScriptName().Utf8Value().should eql "<eval>" frame.GetScriptNameOrSourceURL().Utf8Value().should eql "<eval>"
frame.GetScriptNameOrSourceURL().Utf8Value().should eql "<eval>" frame.IsEval().should be_false
frame.IsEval().should be_false stack.GetFrame(4).IsEval().should be_true
stack.GetFrame(4).IsEval().should be_true frame.IsConstructor().should be_false
frame.IsConstructor().should be_false
end
end end
end end
end end

View file

@ -1,5 +1,4 @@
require 'v8' require 'v8'
load File.expand_path '../c/context_helper.rb', __FILE__
def run_v8_gc def run_v8_gc
while !V8::C::V8::IdleNotification() do while !V8::C::V8::IdleNotification() do
@ -10,3 +9,26 @@ def rputs(msg)
puts "<pre>#{ERB::Util.h(msg)}</pre>" puts "<pre>#{ERB::Util.h(msg)}</pre>"
$stdout.flush $stdout.flush
end end
module ExplicitScoper;end
module Autoscope
def instance_eval(*args, &block)
V8::C::Locker() do
V8::C::HandleScope() do
@cxt = V8::C::Context::New()
begin
@cxt.Enter()
super(*args, &block)
ensure
@cxt.Exit()
end
end
end
end
end
RSpec.configure do |c|
c.before(:each) do
extend Autoscope unless is_a? ExplicitScoper
end
end