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:
parent
8dae8f7890
commit
654b808ac5
13 changed files with 50 additions and 43 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "setting up handles scopes" do
|
||||
include ExplicitScoper
|
||||
|
||||
before do
|
||||
def self.instance_eval(*args, &block)
|
||||
around(:each) do |example|
|
||||
V8::C::Locker() do
|
||||
cxt = V8::C::Context::New()
|
||||
begin
|
||||
cxt.Enter()
|
||||
super(*args, &block)
|
||||
example.run
|
||||
ensure
|
||||
cxt.Exit()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "can allocate handle scopes" do
|
||||
V8::C::HandleScope() do
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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>")
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
c.include V8ContextHelpers
|
||||
c.extend V8ContextHelpers::GroupMethods
|
||||
end
|
Loading…
Add table
Reference in a new issue