Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit e704d73

Browse files
committed
add FunctionTemplate#{New,GetFunction}
1 parent 5d09e42 commit e704d73

8 files changed

Lines changed: 114 additions & 0 deletions

File tree

ext/v8/function-template.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// -*- mode: c++ -*-
2+
3+
namespace rr {
4+
class FunctionTemplate : public Ref<v8::FunctionTemplate> {
5+
public:
6+
FunctionTemplate(VALUE self) : Ref<v8::FunctionTemplate>(self) {}
7+
FunctionTemplate(v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> t) :
8+
Ref<v8::FunctionTemplate>(isolate, t) {}
9+
static inline void Init() {
10+
ClassBuilder("FunctionTemplate", Template::Class).
11+
defineSingletonMethod("New", &New).
12+
defineMethod("GetFunction", &GetFunction).
13+
store(&Class);
14+
}
15+
16+
static VALUE New(int argc, VALUE argv[], VALUE self) {
17+
VALUE r_isolate, callback, data, signature, length;
18+
rb_scan_args(argc, argv, "14", &r_isolate, &callback, &data, &signature, &length);
19+
Isolate isolate(r_isolate);
20+
Locker lock(isolate);
21+
22+
return FunctionTemplate(isolate, v8::FunctionTemplate::New(isolate));
23+
}
24+
25+
static VALUE GetFunction(VALUE self, VALUE context) {
26+
FunctionTemplate t(self);
27+
Isolate isolate(t.getIsolate());
28+
Locker lock(isolate);
29+
30+
return Function::Maybe(isolate, t->GetFunction(Context(context)));
31+
}
32+
};
33+
}

ext/v8/function.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace rr {
2424

2525
inline Function(VALUE value) : Ref<v8::Function>(value) {}
2626
inline Function(v8::Isolate* isolate, v8::Handle<v8::Function> function) : Ref<v8::Function>(isolate, function) {}
27+
typedef MaybeLocal<Function, v8::Function> Maybe;
2728
};
2829
}
2930

ext/v8/init.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
External::Init();
3434
Template::Init();
3535
ObjectTemplate::Init();
36+
FunctionTemplate::Init();
3637

3738
// Accessor::Init();
3839
// Invocation::Init();

ext/v8/rr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ inline VALUE not_implemented(const char* message) {
5555
#include "function-callback.h"
5656

5757
#include "template.h"
58+
#include "function-template.h"
5859
#include "object-template.h"
5960

6061
#endif

ext/v8/template.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace rr {
1010
inline static void Init() {
1111
ClassBuilder("Template").
1212
defineMethod("Set", &Set).
13+
defineMethod("SetAccessorProperty", &SetAccessorProperty).
1314
store(&Class);
1415
}
1516

@@ -24,6 +25,13 @@ namespace rr {
2425
Template(self)->Set(Name(name), val, attributes);
2526
return Qnil;
2627
}
28+
29+
static VALUE SetAccessorProperty(int argc, VALUE argv[], VALUE self) {
30+
VALUE r_name, r_getter, setter, attributes, access;
31+
rb_scan_args(argc, argv, "14", &r_name, &r_getter, &setter, &attributes, &access);
32+
Template(self)->SetAccessorProperty(Name(r_name));
33+
return Qnil;
34+
}
2735
};
2836
}
2937

lib/v8/c.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'v8/weak'
2+
require 'v8/c/maybe'
3+
require 'v8/c/property_attribute'

spec/c/function_template_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'c_spec_helper'
2+
3+
describe V8::C::FunctionTemplate do
4+
requires_v8_context
5+
6+
describe "New()" do
7+
describe "with no arguments" do
8+
let(:function) { template.GetFunction(@ctx).FromJust() }
9+
let(:template) { V8::C::FunctionTemplate::New(@isolate) }
10+
it "creates an empty template" do
11+
expect(template).to be
12+
end
13+
it "has an empty function" do
14+
expect(function).to be
15+
expect(function.Call(@ctx.Global(), []).StrictEquals(@ctx.Global())).to be true
16+
end
17+
end
18+
end
19+
end

spec/c/template_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'c_spec_helper'
2+
3+
describe V8::C::Template do
4+
requires_v8_context
5+
6+
let(:key) { V8::C::String::NewFromUtf8(@isolate, "key") }
7+
let(:value) { V8::C::Integer::New(@isolate, 23) }
8+
let(:template) { V8::C::ObjectTemplate::New(@isolate) }
9+
10+
describe "Set()" do
11+
12+
describe "without property attributes" do
13+
before do
14+
template.Set(key, value)
15+
end
16+
it "places those values on all new instances" do
17+
object = template.NewInstance(@ctx).FromJust()
18+
expect(object.Get(@ctx, key).FromJust().Value).to be 23
19+
end
20+
end
21+
22+
describe "with property attributes" do
23+
before do
24+
template.Set(key, value, V8::C::PropertyAttribute::ReadOnly)
25+
@object = template.NewInstance(@ctx).FromJust()
26+
@object.Set(@ctx, key, V8::C::Integer::New(@isolate, 25))
27+
end
28+
it "applies those property attributes" do
29+
expect(@object.Get(@ctx, key).FromJust().Value).to be 23
30+
end
31+
end
32+
end
33+
34+
describe "SetAccessorProperty()" do
35+
describe "with the bare minimum" do
36+
before do
37+
template.SetAccessorProperty(key)
38+
@object = template.NewInstance(@ctx).FromJust()
39+
@object.Set(@ctx, key, value)
40+
end
41+
it "cannot be shared across contexts" do
42+
ctx2 = V8::C::Context::New(@isolate)
43+
expect(@object.Get(@ctx, key).FromJust().Value).to be 23
44+
expect(@object.Get(ctx2, key).FromJust()).to be_nil
45+
end
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)