augustoroman / v8

A Go API for the V8 javascript engine.
MIT License
395 stars 78 forks source link

Is there way to import go functions with receiver? #50

Open eaglexiang opened 5 years ago

eaglexiang commented 5 years ago

I'd like to implementate and import a go-code lib, but I found no property or method of v8.CallbackArgs to get method caller.

In other words, I can get function args with v8.CallbackArgs.Args or v8.CallbackArgs.Arg(int), but I cannot get receiver(sometimes called caller), which is usually used like below:

$("test").each(f);

If I implementate $ in golang, that's easy, how can I implementate the each method? or how can I get value of caller $("test) in a standard v8-style callback function?

func each(args v8.CallbackArgs)(*v8.Value, error) {
    // how can I get value of $("test") here?
}
eaglexiang commented 5 years ago

In a go-based JS runtime I used before called otto, I can use method like:

func each(call otto.FunctionCall) otto.Value {
    this := call.This // this is caller
}
augustoroman commented 5 years ago

Unfortunately it's not possible with the current code, but it doesn't look too hard to add.