Sha256: 5baa050afc542fe98b34c293b9f5bfebb98ad424866aebd67fff848b80d979ca

Contents?: true

Size: 1.45 KB

Versions: 34

Compression:

Stored size: 1.45 KB

Contents

require 'spec_helper'

describe V8::C::Function do
  it "can be called" do
    fn = run '(function() {return "foo"})'
    fn.Call(@cxt.Global(), []).Utf8Value().should eql "foo"
  end

  it "can be called with arguments and context" do
    fn = run '(function(one, two, three) {this.one = one; this.two = two; this.three = three})'
    one = V8::C::Object::New()
    two = V8::C::Object::New()
    fn.Call(@cxt.Global(), [one, two, 3])
    @cxt.Global().Get("one").should eql one
    @cxt.Global().Get("two").should eql two
    @cxt.Global().Get("three").should eql 3
  end

  it "can be called as a constructor" do
    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
    fn = run '(function(foo) {this.foo = foo})'
    object = fn.NewInstance([V8::C::String::New("bar")])
    object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar"
  end

  it "doesn't kill the world if invoking it throws a javascript exception" do
    V8::C::TryCatch() do
      fn = run '(function() { throw new Error("boom!")})'
      fn.Call(@cxt.Global(), [])
      fn.NewInstance([])
    end
  end


  def run(source)
    source = V8::C::String::New(source.to_s)
    filename = V8::C::String::New("<eval>")
    script = V8::C::Script::New(source, filename)
    result = script.Run()
    result.kind_of?(V8::C::String) ? result.Utf8Value() : result
  end
end

Version data entries

34 entries across 34 versions & 7 rubygems

Version Path
arcabouco-0.2.13 vendor/bundle/gems/therubyracer-0.12.2/spec/c/function_spec.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/therubyracer-0.11.4/spec/c/function_spec.rb
therubyracer-tmpfork-0.12.2 spec/c/function_spec.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/therubyracer-0.12.2/spec/c/function_spec.rb
therubyracer-0.12.2 spec/c/function_spec.rb
therubyracer-0.12.1 spec/c/function_spec.rb
therubyracer-discourse-0.12.0 spec/c/function_spec.rb
therubyracer-0.12.0 spec/c/function_spec.rb
therubyracer-0.11.4 spec/c/function_spec.rb
therubyracer-0.11.3 spec/c/function_spec.rb
therubyracer-0.11.2 spec/c/function_spec.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/therubyracer-0.11.0/spec/c/function_spec.rb
therubyracer-0.11.1 spec/c/function_spec.rb
therubyracer-0.11.1beta1 spec/c/function_spec.rb
therubyracer-0.11.0 spec/c/function_spec.rb
therubyracer-0.11.0beta8-x86-freebsd-9 spec/c/function_spec.rb
therubyracer-0.11.0beta8-x86_64-linux spec/c/function_spec.rb
therubyracer-0.11.0beta8-x86-linux spec/c/function_spec.rb
therubyracer-0.11.0beta8 spec/c/function_spec.rb
therubyracer-0.11.0beta7-x86_64-linux spec/c/function_spec.rb