Skip to content

Commit

Permalink
FunctionTemplate::GetFunction (rogchap#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Figueiredo authored May 8, 2021
1 parent 69de0c5 commit 7a6d001
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions function_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func NewFunctionTemplate(iso *Isolate, callback FunctionCallback) (*FunctionTemp
return &FunctionTemplate{tmpl}, nil
}

// GetFunction returns an instance of this function template bound to the given context.
func (tmpl *FunctionTemplate) GetFunction(ctx *Context) *Function {
val_ptr := C.FunctionTemplateGetFunction(tmpl.ptr, ctx.ptr)
return &Function{&Value{val_ptr, ctx}}
}

//export goFunctionCallback
func goFunctionCallback(ctxref int, cbref int, args *C.ValuePtr, argsCount int) C.ValuePtr {
ctx := getContext(ctxref)
Expand Down
29 changes: 29 additions & 0 deletions function_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ func TestFunctionTemplate(t *testing.T) {
}
}

func TestFunctionTemplateGetFunction(t *testing.T) {
t.Parallel()

iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)

var args *v8go.FunctionCallbackInfo
tmpl, _ := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args = info
reply, _ := v8go.NewValue(iso, "hello")
return reply
})
fn := tmpl.GetFunction(ctx)
ten, err := v8go.NewValue(iso, int32(10))
if err != nil {
t.Fatal(err)
}
ret, err := fn.Call(ten)
if err != nil {
t.Fatal(err)
}
if len(args.Args()) != 1 || args.Args()[0].String() != "10" {
t.Fatalf("expected args [10], got: %+v", args.Args())
}
if !ret.IsString() || ret.String() != "hello" {
t.Fatalf("expected return value of 'hello', was: %v", ret)
}
}

func ExampleFunctionTemplate() {
iso, _ := v8go.NewIsolate()
global, _ := v8go.NewObjectTemplate(iso)
Expand Down
16 changes: 16 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref) {
return static_cast<TemplatePtr>(ot);
}

ValuePtr FunctionTemplateGetFunction(TemplatePtr ptr, ContextPtr ctx_ptr) {
LOCAL_TEMPLATE(ptr);
m_ctx* ctx = static_cast<m_ctx*>(ctx_ptr);
Local<Context> local_ctx = ctx->ptr.Get(iso);
Context::Scope context_scope(local_ctx);

Local<FunctionTemplate> fn_tmpl = tmpl.As<FunctionTemplate>();
MaybeLocal<Function> fn = fn_tmpl->GetFunction(local_ctx);

m_value* val = new m_value;
val->iso = iso;
val->ctx = ctx;
val->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(iso, fn.ToLocalChecked());
return tracked_value(ctx, val);
}

/********** Context **********/

#define LOCAL_CONTEXT(ctx_ptr) \
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern TemplatePtr NewObjectTemplate(IsolatePtr iso_ptr);
extern ValuePtr ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx_ptr);

extern TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref);
extern ValuePtr FunctionTemplateGetFunction(TemplatePtr ptr, ContextPtr ctx_ptr);

extern ValuePtr NewValueInteger(IsolatePtr iso_ptr, int32_t v);
extern ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso_ptr, uint32_t v);
Expand Down

0 comments on commit 7a6d001

Please sign in to comment.