diff --git a/spec/c/array_spec.rb b/spec/c/array_spec.rb index 138e081..6b406bc 100644 --- a/spec/c/array_spec.rb +++ b/spec/c/array_spec.rb @@ -1,22 +1,17 @@ require 'spec_helper' describe V8::C::Array do - include C::ContextHelper it "can store and retrieve a value" do - V8::C::HandleScope() do - o = V8::C::Object::New() - a = V8::C::Array::New() - a.Length().should eql 0 - a.Set(0, o) - a.Length().should eql 1 - a.Get(0).Equals(o).should be_true - end + o = V8::C::Object::New() + a = V8::C::Array::New() + a.Length().should eql 0 + a.Set(0, o) + a.Length().should eql 1 + a.Get(0).Equals(o).should be_true end it "can be initialized with a length" do - V8::C::HandleScope() do - a = V8::C::Array::New(5) - a.Length().should eql 5 - end + a = V8::C::Array::New(5) + a.Length().should eql 5 end end \ No newline at end of file diff --git a/spec/c/context_helper.rb b/spec/c/context_helper.rb deleted file mode 100644 index 69c196b..0000000 --- a/spec/c/context_helper.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/spec/c/external_spec.rb b/spec/c/external_spec.rb index 741772e..6d8f005 100644 --- a/spec/c/external_spec.rb +++ b/spec/c/external_spec.rb @@ -1,12 +1,9 @@ require 'spec_helper' describe V8::C::External do - include C::ContextHelper it "can store and retrieve a value" do - V8::C::HandleScope() do - o = Object.new - external = V8::C::External::New(o) - external.Value().should be(o) - end + o = Object.new + external = V8::C::External::New(o) + external.Value().should be(o) end end \ No newline at end of file diff --git a/spec/c/function_spec.rb b/spec/c/function_spec.rb index b063124..600b122 100644 --- a/spec/c/function_spec.rb +++ b/spec/c/function_spec.rb @@ -1,27 +1,20 @@ require 'spec_helper' describe V8::C::Function do - include C::ContextHelper it "can be called" do - V8::C::HandleScope() do - fn = run '(function() {return "foo"})' - fn.Call(@cxt.Global(), 0, []).Utf8Value().should eql "foo" - end + fn = run '(function() {return "foo"})' + fn.Call(@cxt.Global(), 0, []).Utf8Value().should eql "foo" end it "can be called as a constructor" do - V8::C::HandleScope() do - fn = run '(function() {this.foo = "foo"})' - fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo" - end + fn = run '(function() {this.foo = "foo"})' + fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo" end it "can be called as a constructor with arguments" do - V8::C::HandleScope() do - fn = run '(function(foo) {this.foo = foo})' - object = fn.NewInstance(1, [V8::C::String::New("bar")]) - object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar" - end + fn = run '(function(foo) {this.foo = foo})' + object = fn.NewInstance(1, [V8::C::String::New("bar")]) + object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar" end def run(source) diff --git a/spec/c/handles_spec.rb b/spec/c/handles_spec.rb index 1d76341..62a23f7 100644 --- a/spec/c/handles_spec.rb +++ b/spec/c/handles_spec.rb @@ -1,11 +1,26 @@ require 'spec_helper' 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 - V8::C::HandleScope() do - V8::C::Object::New() - end.class.should eql V8::C::Object + V8::C::HandleScope() do + V8::C::Object::New() + end.class.should eql V8::C::Object end it "isn't the end of the world if a ruby exception is raised inside a HandleScope" do diff --git a/spec/c/locker_spec.rb b/spec/c/locker_spec.rb index 1848877..e1e1616 100644 --- a/spec/c/locker_spec.rb +++ b/spec/c/locker_spec.rb @@ -1,8 +1,9 @@ require 'spec_helper' describe V8::C::Locker do + include ExplicitScoper + 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() do V8::C::Locker::IsLocked().should be_true @@ -14,7 +15,6 @@ describe V8::C::Locker do end it "properly unlocks if an exception is thrown inside a lock block" do - pending "need to figure out how to wrap rspec methods" begin V8::C::Locker() do raise "boom!" @@ -23,4 +23,16 @@ describe V8::C::Locker do V8::C::Locker::IsLocked().should be_false 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 \ No newline at end of file diff --git a/spec/c/object_spec.rb b/spec/c/object_spec.rb index 8863a89..aab245e 100644 --- a/spec/c/object_spec.rb +++ b/spec/c/object_spec.rb @@ -1,47 +1,40 @@ require 'spec_helper' describe V8::C::Object do - include C::ContextHelper it "can store and retrieve a value" do - V8::C::HandleScope() do - o = V8::C::Object::New() - key = V8::C::String::New("foo") - value = V8::C::String::New("bar") - o.Set(key, value) - o.Get(key).Utf8Value().should eql "bar" - end + o = V8::C::Object::New() + key = V8::C::String::New("foo") + value = V8::C::String::New("bar") + o.Set(key, value) + o.Get(key).Utf8Value().should eql "bar" end it "can retrieve all property names" do - V8::C::HandleScope() do - o = V8::C::Object::New() - o.Set(V8::C::String::New("foo"), V8::C::String::New("bar")) - o.Set(V8::C::String::New("baz"), V8::C::String::New("bang")) - names = o.GetPropertyNames() - names.Length().should eql 2 - names.Get(0).Utf8Value().should eql "foo" - names.Get(1).Utf8Value().should eql "baz" - end + o = V8::C::Object::New() + o.Set(V8::C::String::New("foo"), V8::C::String::New("bar")) + o.Set(V8::C::String::New("baz"), V8::C::String::New("bang")) + names = o.GetPropertyNames() + names.Length().should eql 2 + names.Get(0).Utf8Value().should eql "foo" + names.Get(1).Utf8Value().should eql "baz" end it "can set an accessor from ruby" do - V8::C::HandleScope() do - o = V8::C::Object::New() - property = V8::C::String::New("statement") - callback_data = V8::C::String::New("I am Legend") - left = V8::C::String::New("Yo! ") - getter = proc do |name, info| - info.This().StrictEquals(o).should be_true - info.Holder().StrictEquals(o).should be_true - 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" + o = V8::C::Object::New() + property = V8::C::String::New("statement") + callback_data = V8::C::String::New("I am Legend") + left = V8::C::String::New("Yo! ") + getter = proc do |name, info| + info.This().StrictEquals(o).should be_true + info.Holder().StrictEquals(o).should be_true + 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 \ No newline at end of file diff --git a/spec/c/script_spec.rb b/spec/c/script_spec.rb index d9fb669..ec07f36 100644 --- a/spec/c/script_spec.rb +++ b/spec/c/script_spec.rb @@ -1,14 +1,11 @@ require 'spec_helper' -describe V8::C::External do - include C::ContextHelper +describe V8::C::Script do it "can run a script and return a polymorphic result" do - V8::C::HandleScope() do - source = V8::C::String::New("(new Array())") - filename = V8::C::String::New("") - script = V8::C::Script::New(source, filename) - result = script.Run() - result.should be_kind_of V8::C::Array - end + source = V8::C::String::New("(new Array())") + filename = V8::C::String::New("") + script = V8::C::Script::New(source, filename) + result = script.Run() + result.should be_kind_of V8::C::Array end end \ No newline at end of file diff --git a/spec/c/template_spec.rb b/spec/c/template_spec.rb index a2847df..6ad8cd6 100644 --- a/spec/c/template_spec.rb +++ b/spec/c/template_spec.rb @@ -1,35 +1,30 @@ require 'spec_helper' describe V8::C::Template do - include C::ContextHelper describe V8::C::FunctionTemplate do it "can be created with no arguments" do - V8::C::HandleScope() do - t = V8::C::FunctionTemplate::New() - t.GetFunction().Call(@cxt.Global(),0, []).StrictEquals(@cxt.Global()).should be_true - end + t = V8::C::FunctionTemplate::New() + t.GetFunction().Call(@cxt.Global(),0, []).StrictEquals(@cxt.Global()).should be_true end it "can be created with a callback" do - V8::C::HandleScope() do - receiver = V8::C::Object::New() - f = nil - callback = lambda do |arguments| - arguments.Length().should be(2) - arguments[0].Utf8Value().should eql 'one' - arguments[1].Utf8Value().should eql 'two' - arguments.Callee().StrictEquals(f).should be_true - arguments.This().StrictEquals(receiver).should be_true - arguments.Holder().StrictEquals(receiver).should be_true - arguments.IsConstructCall().should be_false - arguments.Data().Value().should be(42) - 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" + receiver = V8::C::Object::New() + f = nil + callback = lambda do |arguments| + arguments.Length().should be(2) + arguments[0].Utf8Value().should eql 'one' + arguments[1].Utf8Value().should eql 'two' + arguments.Callee().StrictEquals(f).should be_true + arguments.This().StrictEquals(receiver).should be_true + arguments.Holder().StrictEquals(receiver).should be_true + arguments.IsConstructCall().should be_false + arguments.Data().Value().should be(42) + 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 end \ No newline at end of file diff --git a/spec/c/trycatch_spec.rb b/spec/c/trycatch_spec.rb index 7bbda72..329cdd3 100644 --- a/spec/c/trycatch_spec.rb +++ b/spec/c/trycatch_spec.rb @@ -1,54 +1,51 @@ require 'spec_helper' describe V8::C::External do - include C::ContextHelper it "can catch javascript exceptions" do V8::C::V8::SetCaptureStackTraceForUncaughtExceptions(true, 99, V8::C::StackTrace::kDetailed) V8::C::TryCatch() do |trycatch| - V8::C::HandleScope() do - source = V8::C::String::New(<<-JS) - function one() { - two() - } - function two() { - three() - } - function three() { - boom() - } - function boom() { - throw new Error('boom!') - } - eval('one()') - JS - filename = V8::C::String::New("") - script = V8::C::Script::New(source, filename) - result = script.Run() - trycatch.HasCaught().should be_true - trycatch.CanContinue().should be_true - exception = trycatch.Exception() - exception.should_not be_nil - exception.IsNativeError().should be_true - trycatch.StackTrace().Utf8Value().should match /boom.*three.*two.*one/m - message = trycatch.Message(); - message.should_not be_nil - message.Get().Utf8Value().should eql "Uncaught Error: boom!" - message.GetSourceLine().Utf8Value().should eql " throw new Error('boom!')" - message.GetScriptResourceName().Utf8Value().should eql "" - message.GetLineNumber().should eql 11 - stack = message.GetStackTrace() - stack.should_not be_nil - stack.GetFrameCount().should eql 6 - frame = stack.GetFrame(0) - frame.GetLineNumber().should eql 11 - frame.GetColumn().should eql 17 - frame.GetScriptName().Utf8Value().should eql "" - frame.GetScriptNameOrSourceURL().Utf8Value().should eql "" - frame.IsEval().should be_false - stack.GetFrame(4).IsEval().should be_true - frame.IsConstructor().should be_false - end + source = V8::C::String::New(<<-JS) + function one() { + two() + } + function two() { + three() + } + function three() { + boom() + } + function boom() { + throw new Error('boom!') + } + eval('one()') + JS + filename = V8::C::String::New("") + script = V8::C::Script::New(source, filename) + result = script.Run() + trycatch.HasCaught().should be_true + trycatch.CanContinue().should be_true + exception = trycatch.Exception() + exception.should_not be_nil + exception.IsNativeError().should be_true + trycatch.StackTrace().Utf8Value().should match /boom.*three.*two.*one/m + message = trycatch.Message(); + message.should_not be_nil + message.Get().Utf8Value().should eql "Uncaught Error: boom!" + message.GetSourceLine().Utf8Value().should eql " throw new Error('boom!')" + message.GetScriptResourceName().Utf8Value().should eql "" + message.GetLineNumber().should eql 11 + stack = message.GetStackTrace() + stack.should_not be_nil + stack.GetFrameCount().should eql 6 + frame = stack.GetFrame(0) + frame.GetLineNumber().should eql 11 + frame.GetColumn().should eql 15 + frame.GetScriptName().Utf8Value().should eql "" + frame.GetScriptNameOrSourceURL().Utf8Value().should eql "" + frame.IsEval().should be_false + stack.GetFrame(4).IsEval().should be_true + frame.IsConstructor().should be_false end end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b972703..a1d884d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,4 @@ require 'v8' -load File.expand_path '../c/context_helper.rb', __FILE__ def run_v8_gc while !V8::C::V8::IdleNotification() do @@ -10,3 +9,26 @@ def rputs(msg) puts "
#{ERB::Util.h(msg)}
" $stdout.flush 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 \ No newline at end of file