rogchap / v8go

Execute JavaScript from Go

Home Page:https://rogchap.com/v8go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unable to add toString method to object

JanGordon opened this issue · comments

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()
}