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'
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
end

View file

@ -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)

View file

@ -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
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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("<eval>")
@ -25,4 +27,4 @@ describe V8::C::Script do
data = V8::C::ScriptData::PreCompile(source, source.length)
data.HasError().should be_true
end
end
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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
end

View file

@ -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
c.include V8ContextHelpers
c.extend V8ContextHelpers::GroupMethods
end