dongling0519 2015-06-26 14:22
浏览 217
已采纳

Golang Otto JS-将宿主对象公开到JavaScript

I'm trying to expose an existing Go Object to the Otto JavaSCript runtime. The object is exposed and I can see it, but when I call methods that affect the state of the exposed object, I get panics and errors or even just silent ignoring of it by Otto!

Golang code:

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 code:

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
}

Does anyone have any insight? Has anyone been able to expose an already existing large object structure with methods to Ottos runtime? I have a very large Go package I'd like to expose directly to the Otto runtime without have to do too much fiddly and fragile "mapping" to JS.

  • 写回答

1条回答 默认 最新

  • dongyun51582 2015-06-30 12:02
    关注

    Greg Rosebury on the Go-nuts mailing list answered this question for me. I've reproduced his answer here verbatim:

    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.

    This solved the problem and the object properly shared to the Otto runtime, and changes via the methods are reflected correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记