rogchap / v8go

Execute JavaScript from Go
https://rogchap.com/v8go
BSD 3-Clause "New" or "Revised" License
3.21k stars 221 forks source link

unable to add toString method to object #395

Open JanGordon opened 1 year ago

JanGordon commented 1 year ago

I am creating an object and I want to add the toString method but whenever using that name it seg faults. I can use another name for the method and it works as expected.

Here's the code to reproduce:

package main

import (
    "fmt"

    "rogchap.com/v8go"
)

func failingExample() {
    fmt.Println("running failing example")

    iso := v8go.NewIsolate()
    global := v8go.NewObjectTemplate(iso)
    obj := v8go.NewObjectTemplate(iso)
    global.Set("obj", obj)
    obj.Set("toString", v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
        self := info.This()
        fmt.Println(self)
        return nil
    }))

    ctx := v8go.NewContext(iso, global)
    ctx.RunScript("var p = obj.toString('hello world')", "print.js")
}

func workingExample() {
    fmt.Println("running working example")

    iso := v8go.NewIsolate()
    global := v8go.NewObjectTemplate(iso)
    obj := v8go.NewObjectTemplate(iso)
    global.Set("obj", obj)
    obj.Set("toStrin", v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
        self := info.This()
        fmt.Println(self)
        return nil
    }))

    ctx := v8go.NewContext(iso, global)
    ctx.RunScript("var p = obj.toStrin('hello world')", "print.js")
}

func main() {
    workingExample()
    failingExample()
}