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

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.
This commit is contained in:
Georgy Angelov 2015-03-15 18:27:19 +00:00
parent 8dae8f7890
commit 654b808ac5
13 changed files with 50 additions and 43 deletions

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Array do describe V8::C::Array do
requires_v8_context
it "can store and retrieve a value" do it "can store and retrieve a value" do
o = V8::C::Object::New() o = V8::C::Object::New()
a = V8::C::Array::New() a = V8::C::Array::New()
@ -14,4 +16,4 @@ describe V8::C::Array 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

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C do describe V8::C do
requires_v8_context
it "has constant methods for Undefined, Null, True and False" do it "has constant methods for Undefined, Null, True and False" do
[:Undefined, :Null, :True, :False].each do |name| [:Undefined, :Null, :True, :False].each do |name|
constant = V8::C.send(name) constant = V8::C.send(name)

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Exception do describe V8::C::Exception do
requires_v8_context
it "can be thrown from Ruby" do it "can be thrown from Ruby" do
t = V8::C::FunctionTemplate::New(method(:explode)) t = V8::C::FunctionTemplate::New(method(:explode))
@cxt.Global().Set("explode", t.GetFunction()) @cxt.Global().Set("explode", t.GetFunction())
@ -23,4 +25,4 @@ describe V8::C::Exception do
error = V8::C::Exception::SyntaxError('did not pay syntax') error = V8::C::Exception::SyntaxError('did not pay syntax')
V8::C::ThrowException(error) V8::C::ThrowException(error)
end end
end end

View file

@ -1,9 +1,11 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::External do describe V8::C::External do
requires_v8_context
it "can store and retrieve a value" do it "can store and retrieve a value" 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

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Function do describe V8::C::Function do
requires_v8_context
it "can be called" do it "can be called" do
fn = run '(function() {return "foo"})' fn = run '(function() {return "foo"})'
fn.Call(@cxt.Global(), []).Utf8Value().should eql "foo" fn.Call(@cxt.Global(), []).Utf8Value().should eql "foo"
@ -43,4 +45,4 @@ describe V8::C::Function do
result = script.Run() result = script.Run()
result.kind_of?(V8::C::String) ? result.Utf8Value() : result result.kind_of?(V8::C::String) ? result.Utf8Value() : result
end end
end end

View file

@ -1,18 +1,14 @@
require 'spec_helper' require 'spec_helper'
describe "setting up handles scopes" do describe "setting up handles scopes" do
include ExplicitScoper around(:each) do |example|
V8::C::Locker() do
before do cxt = V8::C::Context::New()
def self.instance_eval(*args, &block) begin
V8::C::Locker() do cxt.Enter()
cxt = V8::C::Context::New() example.run
begin ensure
cxt.Enter() cxt.Exit()
super(*args, &block)
ensure
cxt.Exit()
end
end end
end end
end end
@ -32,4 +28,4 @@ describe "setting up handles scopes" do
e.message.should eql "boom!" e.message.should eql "boom!"
end end
end end
end end

View file

@ -1,8 +1,6 @@
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
V8::C::Locker::IsLocked().should be_false V8::C::Locker::IsLocked().should be_false
V8::C::Locker() do V8::C::Locker() do
@ -35,4 +33,4 @@ describe V8::C::Locker do
end end
end end
end end
end end

View file

@ -1,6 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Object do describe V8::C::Object do
requires_v8_context
it "can store and retrieve a value" do it "can store and retrieve a value" do
o = V8::C::Object::New() o = V8::C::Object::New()
@ -43,4 +44,4 @@ describe V8::C::Object do
one.Set("two", two) one.Set("two", two)
one.Get("two").should be two one.Get("two").should be two
end end
end end

View file

@ -2,6 +2,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Script do describe V8::C::Script do
requires_v8_context
it "can run a script and return a polymorphic result" do it "can run a script and return a polymorphic result" 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>")
@ -25,4 +27,4 @@ describe V8::C::Script do
data = V8::C::ScriptData::PreCompile(source, source.length) data = V8::C::ScriptData::PreCompile(source, source.length)
data.HasError().should be_true data.HasError().should be_true
end end
end end

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::String do describe V8::C::String do
requires_v8_context
it "can hold Unicode values outside the Basic Multilingual Plane" do it "can hold Unicode values outside the Basic Multilingual Plane" do
string = V8::C::String::New("\u{100000}") string = V8::C::String::New("\u{100000}")
string.Utf8Value().should eql "\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 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" V8::C::String::Concat(V8::C::String::New("forty two is "), 42).Utf8Value().should eql "forty two is 42"
end end
end end

View file

@ -1,6 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::Template do describe V8::C::Template do
requires_v8_context
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
@ -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" f.Call(receiver, [V8::C::String::New('one'), V8::C::String::New('two')]).Utf8Value().should eql "result"
end end
end end
end end

View file

@ -1,6 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe V8::C::External do describe V8::C::External do
requires_v8_context
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)
@ -48,4 +49,4 @@ describe V8::C::External do
frame.IsConstructor().should be_false frame.IsConstructor().should be_false
end end
end end
end end

View file

@ -11,35 +11,31 @@ def rputs(msg)
$stdout.flush $stdout.flush
end end
module ExplicitScoper;end module V8ContextHelpers
module Autoscope module GroupMethods
def instance_eval(*args, &block) def requires_v8_context
return super unless low_level_c_spec? && !explicitly_defines_scope? around(:each) do |example|
bootstrap_v8_context(&example)
end
end
end
def bootstrap_v8_context
V8::C::Locker() do V8::C::Locker() do
V8::C::HandleScope() do V8::C::HandleScope() do
@cxt = V8::C::Context::New() @cxt = V8::C::Context::New()
begin begin
@cxt.Enter() @cxt.Enter()
super(*args, &block) yield
ensure ensure
@cxt.Exit() @cxt.Exit()
end end
end end
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 end
RSpec.configure do |c| RSpec.configure do |c|
c.before(:each) do c.include V8ContextHelpers
extend Autoscope c.extend V8ContextHelpers::GroupMethods
end end
end