goja require 模块加载试用

写这篇文章的目的主要是试用下goja,同时golang 也有另外一个otto 的实现,都是很不错的选择,因为otto集成了underscore 感觉很不错
所以打算给goja 也集成下,同时学习下使用

otto underscore 的使用

因为默认otto的代码中已经包含了underscore,使用是很简单的

 
import (
    "github.com/robertkrimen/otto"
    _ "github.com/robertkrimen/otto/underscore"
)

otto underscore 的大致原理解析

engine 核心还是加载script 内容,为了方便otto基于go-bindata 潜入资源到代码中了,同时otto 也有一个类似npm 的require机制

goja underscore 特性的支持

因为goja提供了一个node 兼容的require 扩展,所以我们需要做的基本就和otto对于underscore的类似,同时因为goja没有otto 那么方便
的机制,我们需要自己调整下加载资源的模式(为了方便,同时也使用了类似otto 的go-bindata机制,同时使用了goja require 的自定义加载模式)
参考代码
资源加载基于go-bindata 提供的Asset 保证也本地资源文件一致,同时也方便软件分发
生成go-bindata 的方式
安装cli

 
go get -u github.com/go-bindata/go-bindata/...

生成代码

go-bindata dalong.js app.js underscore-min.js

js 代码文件加载

registry := require.NewRegistryWithLoader(func(path string) ([]byte, error) {
    return Asset(path)
  })

为了方便使用定义了一个简单的vm struct, 同时提供了一个init 方法,方便注入一些扩展(比如underscore)实际可以结合自己的场景调整

package main
import (
   "fmt"
   "github.com/dop251/goja"
)
// MyVM myjvm
type MyVM struct {
   jsRuntime *goja.Runtime
   script    string
}
func (vm *MyVM) init() {
   vm.jsRuntime.Set("login", func(name, password string) string {
      return fmt.Sprintf("%s-%s", name, password)
   })
   // load underscore for global user
   m, _ :=myrequire.Require("underscore-min.js")
   vm.jsRuntime.Set("_", m)
}
// Exec script
func (vm *MyVM) Exec() (goja.Value, error) {
   return vm.jsRuntime.RunString(vm.script)
}
 

为了方便在使用的时候直接就用引用underscore 暴露的操作能力,我们直接使用了init 函数方便自动加载

package main
import (
   "github.com/dop251/goja"
   "github.com/dop251/goja_nodejs/require"
)
var (
   myvm      MyVM
   myrequire *require.RequireModule
)
func init() {
   // registry := new(require.Registry)
   // use custom srcloader
   registry := require.NewRegistryWithLoader(func(path string) ([]byte, error) {
      return Asset(path)
   })
   myvm = MyVM{
      jsRuntime: goja.New(),
      script:    `require("dalong.js")("dalong","ddddd")`,
   }
   myrequire = registry.Enable(myvm.jsRuntime)
   myvm.init()
}

使用

  • 入口
package main
import (
   "fmt"
   "log"
)
func main() {
   value, _ := myvm.Exec()
   fmt.Println(value)
   m, err := myrequire.Require("app.js")
   if err != nil {
      log.Panic(err)
   } else {
      ob := m.ToObject(myvm.jsRuntime)
      fmt.Print(ob.Get("filteruser").String())
   }
}

app.js 说明
加载dalong.js模块,同时使用underscore

 
var dalong  = require("dalong.js")
var users = [
      {
          name:"dalong",
          age:333
      }, 
      {
        name:"rong",
        age:22
      },
     {
        name:"mydemo",
        age:44
     }
]
var evens = _.filter(users, function(user){ return user.age % 2 == 0; });
module.exports = {
    version:"v1",
    type_info:"system",
    token: dalong("dalong","demoapp"),
    filteruser: JSON.stringify(evens)
} 
  • 运行效果

说明

以上是一个简单的给予goja 引入三方npm 模块的一个尝试,实际使用的时候我们可以会依赖一些三方依赖,解决方法:
就用browserify 打包依赖的npm 模块,同时基于ffi模式运行,同时在结合go-bindata 基本就可以搞定了,后边写一个简单的
demo,验证下方案的可行性

参考资料

https://github.com/rongfengliang/goja-require-learning
https://github.com/dop251/goja
https://github.com/dop251/goja_nodejs
https://github.com/robertkrimen/otto
https://www.npmjs.com/package/browserify

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/13210101.html