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

support for v8::Function

This commit is contained in:
Charles Lowell 2012-05-18 11:56:15 -05:00
parent 125a37a5d3
commit f0db59d723
5 changed files with 119 additions and 1 deletions

60
ext/v8/function.cc Normal file
View file

@ -0,0 +1,60 @@
#include "rr.h"
namespace rr {
void Function::Init() {
ClassBuilder("Function", Object::Class).
defineMethod("NewInstance", &NewInstance).
defineMethod("Call", &Call).
defineMethod("SetName", &SetName).
defineMethod("GetName", &GetName).
defineMethod("GetInferredName", &GetInferredName).
defineMethod("GetScriptLineNumber", &GetScriptLineNumber).
defineMethod("GetScriptColumnNumber", &GetScriptColumnNumber).
defineMethod("GetScriptId", &GetScriptId).
defineMethod("GetScriptOrigin", &GetScriptOrigin).
store(&Class);
}
VALUE Function::NewInstance(int i, VALUE v[], VALUE self) {
if (i == 0) {
return Object(Function(self)->NewInstance());
} else {
VALUE argc; VALUE argv;
rb_scan_args(i,v,"2", &argc, &argv);
std::vector< v8::Handle<v8::Value> > arguments(Int(argc));
return Object(Function(self)->NewInstance(Int(argc), Value::array(argv, arguments)));
}
}
VALUE Function::Call(VALUE self, VALUE receiver, VALUE argc, VALUE argv) {
std::vector< v8::Handle<v8::Value> > arguments(Int(argc));
return Value(Function(self)->Call(Object(receiver), Int(argc), Value::array(argv, arguments)));
}
VALUE Function::SetName(VALUE self, VALUE name) {
Void(Function(self)->SetName(String(name)));
}
VALUE Function::GetName(VALUE self) {
return Value(Function(self)->GetName());
}
VALUE Function::GetInferredName(VALUE self) {
return Value(Function(self)->GetInferredName());
}
VALUE Function::GetScriptLineNumber(VALUE self) {
return INT2FIX(Function(self)->GetScriptLineNumber());
}
VALUE Function::GetScriptColumnNumber(VALUE self) {
return INT2FIX(Function(self)->GetScriptColumnNumber());
}
VALUE Function::GetScriptId(VALUE self) {
return Value(Function(self)->GetScriptId());
}
VALUE Function::GetScriptOrigin(VALUE self) {
return not_implemented("GetScriptOrigin");
}
}

View file

@ -17,6 +17,7 @@ extern "C" {
String::Init();
Object::Init();
Array::Init();
Function::Init();
External::Init();
Script::Init();
Template::Init();

View file

@ -131,7 +131,7 @@ VALUE Object::SetAccessor(int argc, VALUE* argv, VALUE self) {
Object::operator VALUE() {
if (handle->IsFunction()) {
// return Function(handle);
return Function((v8::Handle<v8::Function>) v8::Function::Cast(*handle));
}
if (handle->IsArray()) {
return Array((v8::Handle<v8::Array>)v8::Array::Cast(*handle));

View file

@ -327,6 +327,23 @@ public:
inline Array(VALUE value) : Ref<v8::Array>(value) {}
};
class Function : public Ref<v8::Function> {
public:
static void Init();
static VALUE NewInstance(int i, VALUE v[], VALUE self);
static VALUE Call(VALUE self, VALUE receiver, VALUE argc, VALUE argv);
static VALUE SetName(VALUE self, VALUE name);
static VALUE GetName(VALUE self);
static VALUE GetInferredName(VALUE self);
static VALUE GetScriptLineNumber(VALUE self);
static VALUE GetScriptColumnNumber(VALUE self);
static VALUE GetScriptId(VALUE self);
static VALUE GetScriptOrigin(VALUE self);
inline Function(VALUE value) : Ref<v8::Function>(value) {}
inline Function(v8::Handle<v8::Function> function) : Ref<v8::Function>(function) {}
};
class Signature : public Ref<v8::Signature> {
static void Init();
static VALUE New(int argc, VALUE argv[], VALUE self);

40
spec/c/function_spec.rb Normal file
View file

@ -0,0 +1,40 @@
require 'spec_helper'
describe V8::C::Function do
before do
@cxt = V8::C::Context::New()
@cxt.Enter()
end
after do
@cxt.Exit()
end
it "can be called" do
V8::C::HandleScope() do
fn = run '(function() {return "foo"})'
fn.Call(@cxt.Global(), 0, []).Utf8Value().should eql "foo"
end
end
it "can be called as a constructor" do
V8::C::HandleScope() do
fn = run '(function() {this.foo = "foo"})'
fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo"
end
end
it "can be called as a constructor with arguments" do
V8::C::HandleScope() do
fn = run '(function(foo) {this.foo = foo})'
object = fn.NewInstance(1, [V8::C::String::New("bar")])
object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar"
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