Go 转到duktape调用函数的示例

Go 转到duktape调用函数的示例,go,duktape,Go,Duktape,存储库没有“问题”部分,也没有其他需要帮助的内容 自述文件中没有有用的例子 我试图在javascript文件中定义一个函数(带有参数和返回值),并从go调用该函数 Javascript文件: function hello(name, age){ return "Hello " + name + " you are " + age; } 这是我第一次使用这段代码,可能还有另一种不使用json获取整个对象的方法,但到目前为止,我还没有找到它。我希望我帮助过你 包干管 import (

存储库没有“问题”部分,也没有其他需要帮助的内容

自述文件中没有有用的例子

我试图在javascript文件中定义一个函数(带有参数和返回值),并从go调用该函数

Javascript文件:

function hello(name, age){
    return "Hello " + name + " you are " + age;
}

这是我第一次使用这段代码,可能还有另一种不使用json获取整个对象的方法,但到目前为止,我还没有找到它。我希望我帮助过你

包干管

import (
  "github.com/olebedev/go-duktape"
  "fmt"
  "time"
)

func main() {
  start := time.Now()
  ctx := duktape.New()
  ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int {
    i := -1
    for {

      if c.GetType(i).IsNone() == true {
        break
      }

      if i < -100 {
        break
      }

      if c.GetType(i).IsString() == true {
        fmt.Println(c.SafeToString(i))
      }

      if c.GetType(i).IsNumber() == true {
        fmt.Println(c.GetNumber(i))
      }

      if c.GetType(i).IsObject() == true {
        c.ToObject(i)
        fmt.Printf( "%v\n", c.JsonEncode(i) )
      }

      i -= 1
    }

    return 0
  })
  ctx.PevalString(`log({'hello':'word'})`)
  ctx.DestroyHeap()
  elapsed := time.Since(start)
  fmt.Printf("total time: %s\n", elapsed)
}
导入(
“github.com/olebedev/go duktape”
“fmt”
“时间”
)
func main(){
开始:=时间。现在()
ctx:=duktape.New()
PushGlobalGoFunction(“log”,func(c*duktape.Context)int{
i:=-1
为了{
如果c.GetType(i).IsNone()==true{
打破
}
如果我<-100{
打破
}
如果c.GetType(i).IsString()==true{
fmt.Println(c.SafeToString(一))
}
如果c.GetType(i).IsNumber()==true{
格式打印(c.GetNumber(i))
}
如果c.GetType(i).IsObject()==true{
c、 ToObject(一)
fmt.Printf(“%v\n”,c.JsonEncode(i))
}
i-=1
}
返回0
})
PevalString(`log({'hello':'word'})`)
ctx.destroy堆()
已用时间:=自(开始)起的时间
fmt.Printf(“总时间:%s\n”,已用)
}

这是另一种方法

package main

import (
  "github.com/olebedev/go-duktape"
  "fmt"
  "time"
)

func print_name (c *duktape.Context) int {
  c.PushThis()
  c.GetPropString(-1, "name")
  fmt.Printf( "My name is: %v\n", c.SafeToString(-1) )
  return 0
}

func myobject_constructor (c *duktape.Context) int {

  if c.IsConstructorCall() == false {
    return duktape.ErrRetType
  }

  c.PushThis()
  c.Dup( 0 )
  c.PutPropString(-2, "name")
  return 0
}

func main() {

  start := time.Now()

  ctx := duktape.New()

  /*
  function MyObject(name) {
      // When called as new MyObject(), "this" will be bound to the default
      // instance object here.

      this.name = name;

      // Return undefined, which causes the default instance to be used.
  }

  // For an ECMAScript function an automatic MyObject.prototype value will be
  // set.  That object will also have a .constructor property pointing back to
  // Myobject.  You can add instance methods to MyObject.prototype.

  MyObject.prototype.printName = function () {
      print('My name is: ' + this.name);
  };

  var obj = new MyObject('test object');
  obj.printName();  // My name is: test object
  */

  ctx.PushGoFunction(myobject_constructor)
  ctx.PushObject()
  ctx.PushGoFunction(print_name)
  ctx.PutPropString(-2, "printName")
  ctx.PutPropString(-2, "prototype")
  ctx.PutGlobalString("MyObject")

  /*
  make this
  */
  ctx.EvalString( "var a = new MyObject('test name one');" )
  ctx.EvalString( "a.printName();" )


  /*
  or make this
  */
  ctx.EvalString( "new MyObject('test name two');" )
  ctx.GetPropString(-1, "printName")
  ctx.Dup(-2)
  ctx.CallMethod(0)
  ctx.Pop()
  ctx.Pop()
  ctx.GetGlobalString("MyObject")


  ctx.DestroyHeap()
  elapsed := time.Since(start)
  fmt.Printf("total time: %s\n", elapsed)
}
也许看电视是有帮助的。