crazy2be/gojs — pull request #3 by sqs (2013-10-25 21:03:50+00:00)
Title: WrapContext func to allow use of externally obtained JSContextRef (e.g., from WebKit)

I am modifying https://github.com/mattn/go-webkit to expose the JSGlobalContextRef in a WebKit frame, and I would like to use gojs to evaluate scripts in the frame. To use gojs, it needs to be able to accept an externally obtained JSContextRef. This PR updates it to do so. There are probably a lot of ways to do this, and I know the API is undergoing changes, so feel free to reject this or suggest another way.

Specifically, what will become possible is this, which is essentially the core features of PhantomJS available in Go.

```go
package webkit

import (
"github.com/mattn/go-gtk/gtk"
"github.com/sqs/gojs"
"testing"
)

func TestEvaluateScript(t *testing.T) {
gtk.Init(nil)
webview := NewWebView()

loaded := false
webview.LoadHtmlString("

foo

", "http://example.com")
webview.Connect("resource-load-finished", func() {
defer gtk.MainQuit()
loaded = true
ctx := webview.GetMainFrame().GetGlobalContext()
ctx2 := gojs.WrapContext(gojs.ContextRef(ctx.ref))
ret, err := ctx2.EvaluateScript(`"foo2"`, nil, ".", 0)
if err != nil {
t.Errorf("error: %s", err)
}
retstr := ctx2.ToStringOrDie(ret)
wantRes := "foo"
if wantRes != retstr {
t.Errorf("want result %q, got %q", wantRes, retstr)
}
})
gtk.Main()

if !loaded {
t.Errorf("!loaded")
}
}
```

Thanks for the great lib!