gpt4 book ai didi

javascript - Golang Otto JS - 将宿主对象暴露给 JavaScript

转载 作者:IT王子 更新时间:2023-10-29 02:15:54 24 4
gpt4 key购买 nike

我正在尝试将现有的 Go 对象公开给 Otto JavaSCript 运行时。对象已公开并且我可以看到它,但是当我调用影响公开对象状态的方法时,我会出现 panic 和错误,甚至 Otto 只是默默地忽略它!

Golang 代码:

package main

import (
"bytes"
"fmt"
"log"
"os"

"github.com/robertkrimen/otto"
)

type BensObject struct {
name string
}

func (b BensObject) SetName(name string) {
fmt.Println("INT: ", name)
b.name = name
fmt.Println("INT: ", b.name)
}

func (b BensObject) GetName() string {
fmt.Println("INT: ", b.name)
return b.name
}

func (b BensObject) Age() int {
return 36
}

func (b BensObject) AddToAge(n int) int {
return 36 + n
}

func main() {
runtime := loadPluginRuntime("plugins.js")

// If we don't have a runtime all requests are accepted
if runtime == nil {
os.Exit(-1)
}

r := new(BensObject)
v, err := runtime.ToValue(*r)
if err != nil {
log.Fatal(err)
}

// By convention we will require plugins have a set name
result, err := runtime.Call("checkRequest", nil, v)
if err != nil {
log.Fatal(err)
}
// If the js function did not return a bool error out
// because the plugin is invalid
out, err := result.ToBoolean()
if err != nil {
log.Fatalf("\"checkRequest\" must return a boolean. Got %s", err)
}

fmt.Println(out)
}

func loadPluginRuntime(name string) *otto.Otto {
f, err := os.Open(name)
if err != nil {
if os.IsNotExist(err) {
return nil
}
log.Fatal(err)
}
defer f.Close()
buff := bytes.NewBuffer(nil)

if _, err := buff.ReadFrom(f); err != nil {
log.Fatal(err)
}
runtime := otto.New()
// Load the plugin file into the runtime before we
// return it for use
if _, err := runtime.Run(buff.String()); err != nil {
log.Fatal(err)
}
return runtime
}

JavaScript 代码:

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}

function checkRequest(r) {
console.log(r);
r.SetName("Ben");
console.log(r.GetName());

console.log(getKeys(r));
console.log(r.Name);
console.log(r.Age());
console.log(r.AddToAge(10));
return true
}

有没有人有什么见解?有没有人能够通过 Ottos 运行时的方法公开一个已经存在的大对象结构?我有一个非常大的 Go 包,我想直接公开给 Otto 运行时,而不必做太多繁琐和脆弱的“映射”到 JS。

最佳答案

Go-nuts 邮件列表中的 Greg Rosebury 为我回答了这个问题。我在这里逐字复制了他的回答:

Hello, try putting a pointer to your object into the runtime. Use *BensObject and change your methods to take pointer receivers: func (b *BensObject) SetName(name string) { ... } Change runtime.ToValue(*r) to runtime.ToValue(r).

Basically BensObject is getting copied every time you call a method on it, so changes aren't being reflected.

这解决了问题,并且对象正确地共享给了 Otto 运行时,并且通过这些方法进行的更改得到了正确反射(reflect)。

关于javascript - Golang Otto JS - 将宿主对象暴露给 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com