From 654b808ac524643f9b4db6a53c652fd31e028fea Mon Sep 17 00:00:00 2001 From: Georgy Angelov Date: Sun, 15 Mar 2015 18:27:19 +0000 Subject: [PATCH] Use `#around` for creation of context for C examples This fixes segmentation fault in the tests as extending the examples in a before block with override of `#instance_eval` seems to not work. Also, the context wrap is now opt-in with a helper method, instead of opt-out and relying on `#described_class` (which has slightly different behavior in RSpec 3). Tested on Ruby 2.1.5 and RSpec 2.99.2. --- spec/c/array_spec.rb | 4 +++- spec/c/constants_spec.rb | 2 ++ spec/c/exception_spec.rb | 4 +++- spec/c/external_spec.rb | 4 +++- spec/c/function_spec.rb | 4 +++- spec/c/handles_spec.rb | 22 +++++++++------------- spec/c/locker_spec.rb | 4 +--- spec/c/object_spec.rb | 3 ++- spec/c/script_spec.rb | 4 +++- spec/c/string_spec.rb | 4 +++- spec/c/template_spec.rb | 3 ++- spec/c/trycatch_spec.rb | 3 ++- spec/spec_helper.rb | 32 ++++++++++++++------------------ 13 files changed, 50 insertions(+), 43 deletions(-) diff --git a/spec/c/array_spec.rb b/spec/c/array_spec.rb index 6b406bc..aab6976 100644 --- a/spec/c/array_spec.rb +++ b/spec/c/array_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe V8::C::Array do + requires_v8_context + it "can store and retrieve a value" do o = V8::C::Object::New() a = V8::C::Array::New() @@ -14,4 +16,4 @@ describe V8::C::Array do a = V8::C::Array::New(5) a.Length().should eql 5 end -end \ No newline at end of file +end diff --git a/spec/c/constants_spec.rb b/spec/c/constants_spec.rb index 9163231..46837bf 100644 --- a/spec/c/constants_spec.rb +++ b/spec/c/constants_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe V8::C do + requires_v8_context + it "has constant methods for Undefined, Null, True and False" do [:Undefined, :Null, :True, :False].each do |name| constant = V8::C.send(name) diff --git a/spec/c/exception_spec.rb b/spec/c/exception_spec.rb index 8c6d887..1851237 100644 --- a/spec/c/exception_spec.rb +++ b/spec/c/exception_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe V8::C::Exception do + requires_v8_context + it "can be thrown from Ruby" do t = V8::C::FunctionTemplate::New(method(:explode)) @cxt.Global().Set("explode", t.GetFunction()) @@ -23,4 +25,4 @@ describe V8::C::Exception do error = V8::C::Exception::SyntaxError('did not pay syntax') V8::C::ThrowException(error) end -end \ No newline at end of file +end diff --git a/spec/c/external_spec.rb b/spec/c/external_spec.rb index 6d8f005..2553028 100644 --- a/spec/c/external_spec.rb +++ b/spec/c/external_spec.rb @@ -1,9 +1,11 @@ require 'spec_helper' describe V8::C::External do + requires_v8_context + it "can store and retrieve a value" do o = Object.new external = V8::C::External::New(o) external.Value().should be(o) end -end \ No newline at end of file +end diff --git a/spec/c/function_spec.rb b/spec/c/function_spec.rb index 3e6c48f..17835bc 100644 --- a/spec/c/function_spec.rb +++ b/spec/c/function_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe V8::C::Function do + requires_v8_context + it "can be called" do fn = run '(function() {return "foo"})' fn.Call(@cxt.Global(), []).Utf8Value().should eql "foo" @@ -43,4 +45,4 @@ describe V8::C::Function do result = script.Run() result.kind_of?(V8::C::String) ? result.Utf8Value() : result end -end \ No newline at end of file +end diff --git a/spec/c/handles_spec.rb b/spec/c/handles_spec.rb index 62a23f7..4882403 100644 --- a/spec/c/handles_spec.rb +++ b/spec/c/handles_spec.rb @@ -1,18 +1,14 @@ require 'spec_helper' describe "setting up handles scopes" do - 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 + around(:each) do |example| + V8::C::Locker() do + cxt = V8::C::Context::New() + begin + cxt.Enter() + example.run + ensure + cxt.Exit() end end end @@ -32,4 +28,4 @@ describe "setting up handles scopes" do e.message.should eql "boom!" end end -end \ No newline at end of file +end diff --git a/spec/c/locker_spec.rb b/spec/c/locker_spec.rb index e1e1616..3f7e163 100644 --- a/spec/c/locker_spec.rb +++ b/spec/c/locker_spec.rb @@ -1,8 +1,6 @@ require 'spec_helper' describe V8::C::Locker do - include ExplicitScoper - it "can lock and unlock the VM" do V8::C::Locker::IsLocked().should be_false V8::C::Locker() do @@ -35,4 +33,4 @@ describe V8::C::Locker do end end end -end \ No newline at end of file +end diff --git a/spec/c/object_spec.rb b/spec/c/object_spec.rb index bb647be..5864989 100644 --- a/spec/c/object_spec.rb +++ b/spec/c/object_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe V8::C::Object do + requires_v8_context it "can store and retrieve a value" do o = V8::C::Object::New() @@ -43,4 +44,4 @@ describe V8::C::Object do one.Set("two", two) one.Get("two").should be two end -end \ No newline at end of file +end diff --git a/spec/c/script_spec.rb b/spec/c/script_spec.rb index 8ba8aee..db1a8b7 100644 --- a/spec/c/script_spec.rb +++ b/spec/c/script_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' describe V8::C::Script do + requires_v8_context + it "can run a script and return a polymorphic result" do source = V8::C::String::New("(new Array())") filename = V8::C::String::New("") @@ -25,4 +27,4 @@ describe V8::C::Script do data = V8::C::ScriptData::PreCompile(source, source.length) data.HasError().should be_true end -end \ No newline at end of file +end diff --git a/spec/c/string_spec.rb b/spec/c/string_spec.rb index ddeab15..75184e4 100644 --- a/spec/c/string_spec.rb +++ b/spec/c/string_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe V8::C::String do + requires_v8_context + it "can hold Unicode values outside the Basic Multilingual Plane" do string = V8::C::String::New("\u{100000}") string.Utf8Value().should eql "\u{100000}" @@ -13,4 +15,4 @@ describe V8::C::String do it "can naturally translate ruby objects into v8 strings" do V8::C::String::Concat(V8::C::String::New("forty two is "), 42).Utf8Value().should eql "forty two is 42" end -end \ No newline at end of file +end diff --git a/spec/c/template_spec.rb b/spec/c/template_spec.rb index 25d7620..f1da9b3 100644 --- a/spec/c/template_spec.rb +++ b/spec/c/template_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe V8::C::Template do + requires_v8_context describe V8::C::FunctionTemplate do it "can be created with no arguments" do @@ -27,4 +28,4 @@ describe V8::C::Template do f.Call(receiver, [V8::C::String::New('one'), V8::C::String::New('two')]).Utf8Value().should eql "result" end end -end \ No newline at end of file +end diff --git a/spec/c/trycatch_spec.rb b/spec/c/trycatch_spec.rb index 329cdd3..d9ec06e 100644 --- a/spec/c/trycatch_spec.rb +++ b/spec/c/trycatch_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe V8::C::External do + requires_v8_context it "can catch javascript exceptions" do V8::C::V8::SetCaptureStackTraceForUncaughtExceptions(true, 99, V8::C::StackTrace::kDetailed) @@ -48,4 +49,4 @@ describe V8::C::External do frame.IsConstructor().should be_false end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9fb5b18..41785c8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,35 +11,31 @@ def rputs(msg) $stdout.flush end -module ExplicitScoper;end -module Autoscope - def instance_eval(*args, &block) - return super unless low_level_c_spec? && !explicitly_defines_scope? +module V8ContextHelpers + module GroupMethods + def requires_v8_context + around(:each) do |example| + bootstrap_v8_context(&example) + end + end + end + + def bootstrap_v8_context V8::C::Locker() do V8::C::HandleScope() do @cxt = V8::C::Context::New() begin @cxt.Enter() - super(*args, &block) + yield ensure @cxt.Exit() end end end end - - def low_level_c_spec? - return false unless described_class - described_class.name =~ /^V8::C::/ - end - - def explicitly_defines_scope? - is_a?(ExplicitScoper) - end end RSpec.configure do |c| - c.before(:each) do - extend Autoscope - end -end \ No newline at end of file + c.include V8ContextHelpers + c.extend V8ContextHelpers::GroupMethods +end