From 68b3a687d4e8ab8efb7ecb5210cfcea19f95e6d6 Mon Sep 17 00:00:00 2001 From: oqammx86 <84847714+oq-x@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:47:27 +0300 Subject: [PATCH] switch to goja instead of otto --- go.mod | 6 +- go.sum | 59 +++++++++++- server/plugins-js.go | 80 ++++++++++++++++ server/plugins-lua.go | 114 ++++++++++++++++++++++ server/plugins.go | 218 ++++-------------------------------------- 5 files changed, 274 insertions(+), 203 deletions(-) create mode 100644 server/plugins-js.go create mode 100644 server/plugins-lua.go diff --git a/go.mod b/go.mod index 5eeea88..a422444 100644 --- a/go.mod +++ b/go.mod @@ -10,13 +10,15 @@ require ( require ( github.com/Shopify/go-lua v0.0.0-20221004153744-91867de107cf github.com/aimjel/minecraft v0.0.0-20230913195637-f88fef694bb7 - github.com/robertkrimen/otto v0.2.1 + github.com/dop251/goja v0.0.0-20230919151941-fc55792775de ) require ( + github.com/dlclark/regexp2 v1.7.0 // indirect + github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect + github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.4.0 // indirect - gopkg.in/sourcemap.v1 v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index e589c0a..9470a87 100644 --- a/go.sum +++ b/go.sum @@ -2,11 +2,34 @@ github.com/Shopify/go-lua v0.0.0-20221004153744-91867de107cf h1:VSUCKpFV0AfYGslg github.com/Shopify/go-lua v0.0.0-20221004153744-91867de107cf/go.mod h1:UCCi6u6YA8oXfJ15vEMiMGIaWWfLiOacinBPHxLXtEU= github.com/aimjel/minecraft v0.0.0-20230913195637-f88fef694bb7 h1:XPhfvCmyVpja/BJvhZZ20LL3XrBNQsdhCp4SX2Y/woQ= github.com/aimjel/minecraft v0.0.0-20230913195637-f88fef694bb7/go.mod h1:/Y9/YBqxNOU7IFInEjDbsCVkdPNYJCPxC26jK+w3Phc= +github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= +github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= +github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= +github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk= +github.com/dop251/goja v0.0.0-20230919151941-fc55792775de h1:lA38Xtzr1Wo+iQdkN2E11ziKXJYRxLlzK/e2/fdxoEI= +github.com/dop251/goja v0.0.0-20230919151941-fc55792775de/go.mod h1:QMWlm50DNe14hD7t24KEqZuUdC9sOTy8W6XbCU1mlw4= +github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y= +github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= +github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U= +github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= +github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -16,8 +39,7 @@ github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6 github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0= -github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -25,15 +47,44 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= -gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server/plugins-js.go b/server/plugins-js.go new file mode 100644 index 0000000..5250e0c --- /dev/null +++ b/server/plugins-js.go @@ -0,0 +1,80 @@ +package server + +import ( + "os" + + "github.com/dop251/goja" + "github.com/dynamitemc/dynamite/logger" +) + +type PluginData struct { + Identifier string `js:"identifier"` +} + +func makeJSMap[K comparable, V any](vm *goja.Runtime, from *map[K]V) *goja.Object { + m := vm.NewObject() + m.Set("set", func(key K, value V) { + (*from)[key] = value + }) + m.Set("delete", func(key K) { + delete(*from, key) + }) + m.Set("get", func(key K) V { + return (*from)[key] + }) + return m +} + +func at[T any](arr []T, index int) (val T) { + if len(arr) <= index { + return val + } + return arr[index] +} + +func getJavaScriptVM(logger logger.Logger, plugin *Plugin) *goja.Runtime { + vm := goja.New() + vm.SetFieldNameMapper(goja.TagFieldNameMapper("js", true)) + server := vm.NewObject() + log := vm.NewObject() + + server.Set("close", func(c int64) { + var code int64 = c + os.Exit(int(code)) + }) + + log.Set("info", func(format string, a ...interface{}) { + logger.Info(format, a...) + }) + + log.Set("warn", func(format string, a ...interface{}) { + logger.Warn(format, a...) + }) + + log.Set("debug", func(format string, a ...interface{}) { + logger.Debug(format, a...) + }) + + log.Set("error", func(format string, a ...interface{}) { + logger.Error(format, a...) + }) + + log.Set("print", func(format string, a ...interface{}) { + logger.Print(format, a...) + }) + + vm.Set("Plugin", func(data *PluginData) { + if data == nil { + logger.Error("Failed to load plugin %s: invalid plugin data", plugin.Filename) + } else { + if data.Identifier == "" { + logger.Error("Failed to load plugin %s: identifier was not specified", plugin.Filename) + } + plugin.Identifier = data.Identifier + plugin.Initialized = true + } + }) + server.Set("logger", log) + vm.Set("server", server) + return vm +} diff --git a/server/plugins-lua.go b/server/plugins-lua.go new file mode 100644 index 0000000..5c4c85c --- /dev/null +++ b/server/plugins-lua.go @@ -0,0 +1,114 @@ +package server + +import ( + "os" + + "github.com/Shopify/go-lua" + "github.com/dynamitemc/dynamite/logger" +) + +func luaCreateFunction(l *lua.State, k string, f lua.Function) { + l.PushGoFunction(f) + l.SetField(-2, k) +} + +func luaCreateGlobalFunction(l *lua.State, k string, f lua.Function) { + l.PushGoFunction(f) + l.SetGlobal(k) +} + +func getLuaVM(logger logger.Logger, plugin *Plugin) *lua.State { + l := lua.NewState() + l.NewTable() + luaCreateFunction(l, "close", func(state *lua.State) int { + code := 0 + if c, ok := state.ToInteger(1); ok { + code = c + } + os.Exit(code) + return 0 + }) + l.NewTable() + luaCreateFunction(l, "info", func(state *lua.State) int { + text, ok := state.ToString(1) + if !ok { + return 0 + } + var data []interface{} + for i := 2; ; i++ { + val := state.ToValue(i) + if val == nil { + break + } + data = append(data, val) + } + logger.Info(text, data...) + return 0 + }) + luaCreateFunction(l, "error", func(state *lua.State) int { + text, ok := state.ToString(1) + if !ok { + return 0 + } + var data []interface{} + for i := 2; ; i++ { + val := state.ToValue(i) + if val == nil { + break + } + data = append(data, val) + } + logger.Error(text, data...) + return 0 + }) + luaCreateFunction(l, "debug", func(state *lua.State) int { + text, ok := state.ToString(1) + if !ok { + return 0 + } + var data []interface{} + for i := 2; ; i++ { + val := state.ToValue(i) + if val == nil { + break + } + data = append(data, val) + } + logger.Debug(text, data...) + return 0 + }) + luaCreateFunction(l, "warn", func(state *lua.State) int { + text, ok := state.ToString(1) + if !ok { + return 0 + } + var data []interface{} + for i := 2; ; i++ { + val := state.ToValue(i) + if val == nil { + break + } + data = append(data, val) + } + logger.Warn(text, data...) + return 0 + }) + l.SetField(-2, "logger") + l.SetGlobal("server") + + luaCreateGlobalFunction(l, "Plugin", func(state *lua.State) int { + if state.IsTable(1) { + l.Field(1, "identifier") + identifier, ok := l.ToString(-1) + if !ok { + logger.Error("Failed to load plugin %s: identifier was not specified", plugin.Filename) + } + plugin.Identifier = identifier + plugin.Initialized = true + } else { + logger.Error("Failed to load plugin %s: invalid plugin data", plugin.Filename) + } + return 0 + }) + return l +} diff --git a/server/plugins.go b/server/plugins.go index 604c8d4..a0be9b0 100644 --- a/server/plugins.go +++ b/server/plugins.go @@ -8,11 +8,11 @@ import ( "strings" lua "github.com/Shopify/go-lua" - "github.com/dynamitemc/dynamite/logger" - "github.com/robertkrimen/otto" + "github.com/dop251/goja" ) var unrecognizedScript = errors.New("unrecognized scripting language") +var uninitalized = errors.New("plugin was not initialized") const ( PluginTypeJavaScript = iota @@ -20,198 +20,12 @@ const ( ) type Plugin struct { - Identifier string - Filename string - JSLoader *otto.Otto - LuaLoader *lua.State - Type int -} - -func luaCreateFunction(l *lua.State, k string, f lua.Function) { - l.PushGoFunction(f) - l.SetField(-2, k) -} - -func luaCreateGlobalFunction(l *lua.State, k string, f lua.Function) { - l.PushGoFunction(f) - l.SetGlobal(k) -} - -func getLuaVM(logger logger.Logger, plugin *Plugin) *lua.State { - l := lua.NewState() - l.NewTable() - luaCreateFunction(l, "close", func(state *lua.State) int { - code := 0 - if c, ok := state.ToInteger(1); ok { - code = c - } - os.Exit(code) - return 0 - }) - l.NewTable() - luaCreateFunction(l, "info", func(state *lua.State) int { - text, ok := state.ToString(1) - if !ok { - return 0 - } - var data []interface{} - for i := 2; ; i++ { - val := state.ToValue(i) - if val == nil { - break - } - data = append(data, val) - } - logger.Info(text, data...) - return 0 - }) - luaCreateFunction(l, "error", func(state *lua.State) int { - text, ok := state.ToString(1) - if !ok { - return 0 - } - var data []interface{} - for i := 2; ; i++ { - val := state.ToValue(i) - if val == nil { - break - } - data = append(data, val) - } - logger.Error(text, data...) - return 0 - }) - luaCreateFunction(l, "debug", func(state *lua.State) int { - text, ok := state.ToString(1) - if !ok { - return 0 - } - var data []interface{} - for i := 2; ; i++ { - val := state.ToValue(i) - if val == nil { - break - } - data = append(data, val) - } - logger.Debug(text, data...) - return 0 - }) - luaCreateFunction(l, "warn", func(state *lua.State) int { - text, ok := state.ToString(1) - if !ok { - return 0 - } - var data []interface{} - for i := 2; ; i++ { - val := state.ToValue(i) - if val == nil { - break - } - data = append(data, val) - } - logger.Warn(text, data...) - return 0 - }) - l.SetField(-2, "logger") - l.SetGlobal("server") - - luaCreateGlobalFunction(l, "Plugin", func(state *lua.State) int { - if state.IsTable(1) { - l.Field(1, "identifier") - identifier, ok := l.ToString(-1) - if !ok { - logger.Error("Failed to load plugin %s: identifier was not specified", plugin.Filename) - } - plugin.Identifier = identifier - } else { - logger.Error("Failed to load plugin %s: invalid plugin data", plugin.Filename) - } - return 0 - }) - return l -} - -func getJavaScriptVM(logger logger.Logger, plugin *Plugin) *otto.Otto { - vm := otto.New() - server, _ := vm.Object("server = {}") - log, _ := vm.Object("server.logger = {}") - - server.Set("close", func(call otto.FunctionCall) otto.Value { - var code int64 = 0 - if c := call.Argument(0); c.IsNumber() { - code, _ = c.ToInteger() - } - os.Exit(int(code)) - return otto.UndefinedValue() - }) - - log.Set("info", func(call otto.FunctionCall) otto.Value { - if s, err := call.Argument(0).ToString(); err != nil { - return otto.UndefinedValue() - } else { - var data []interface{} - for _, a := range call.ArgumentList[1:] { - data = append(data, a) - } - logger.Info(s, data...) - return otto.UndefinedValue() - } - }) - - log.Set("debug", func(call otto.FunctionCall) otto.Value { - if s, err := call.Argument(0).ToString(); err != nil { - return otto.UndefinedValue() - } else { - var data []interface{} - for _, a := range call.ArgumentList[1:] { - data = append(data, a) - } - logger.Debug(s, data...) - return otto.UndefinedValue() - } - }) - - log.Set("warn", func(call otto.FunctionCall) otto.Value { - if s, err := call.Argument(0).ToString(); err != nil { - return otto.UndefinedValue() - } else { - var data []interface{} - for _, a := range call.ArgumentList[1:] { - data = append(data, a) - } - logger.Warn(s, data...) - return otto.UndefinedValue() - } - }) - - log.Set("error", func(call otto.FunctionCall) otto.Value { - if s, err := call.Argument(0).ToString(); err != nil { - return otto.UndefinedValue() - } else { - var data []interface{} - for _, a := range call.ArgumentList[1:] { - data = append(data, a) - } - logger.Error(s, data...) - return otto.UndefinedValue() - } - }) - - vm.Set("Plugin", func(call otto.FunctionCall) otto.Value { - if obj := call.Argument(0); obj.IsObject() { - data := obj.Object() - identifier, _ := data.Get("identifier") - if identifier.IsUndefined() { - logger.Error("Failed to load plugin %s: identifier was not specified", plugin.Filename) - } - plugin.Identifier, _ = identifier.ToString() - } else { - logger.Error("Failed to load plugin %s: invalid plugin data", plugin.Filename) - } - return otto.UndefinedValue() - }) - return vm + Identifier string + Initialized bool + Filename string + JSLoader *goja.Runtime + LuaLoader *lua.State + Type int } func (srv *Server) LoadPlugins() error { @@ -227,6 +41,7 @@ func (srv *Server) LoadPlugins() error { return err } for _, file := range dir { + srv.Logger.Debug("Loading plugin %s", file.Name()) if plugin, err := srv.LoadPlugin("plugins/" + file.Name()); err != nil { return err } else { @@ -234,7 +49,7 @@ func (srv *Server) LoadPlugins() error { continue } srv.Plugins[plugin.Identifier] = plugin - srv.Logger.Info("Finished loading plugin %s", plugin.Identifier) + srv.Logger.Debug("Finished loading plugin %s", plugin.Identifier) } } return nil @@ -254,18 +69,27 @@ func (srv *Server) LoadPlugin(path string) (*Plugin, error) { plugin := &Plugin{Filename: filename, Type: PluginTypeJavaScript} js := getJavaScriptVM(srv.Logger, plugin) plugin.JSLoader = js - _, err := js.Run(string(file)) + _, err := js.RunString(string(file)) if err != nil { srv.Logger.Error("Failed to load plugin %s: %s", filename, err) + return nil, err + } + if !plugin.Initialized { + srv.Logger.Error("Failed to load plugin %s: %s", filename, uninitalized) + return nil, uninitalized } return plugin, nil } case strings.HasSuffix(path, ".lua"): { - plugin := &Plugin{Filename: filename, Type: PluginTypeJavaScript} + plugin := &Plugin{Filename: filename, Type: PluginTypeLua} l := getLuaVM(srv.Logger, plugin) plugin.LuaLoader = l lua.DoString(l, string(file)) + if !plugin.Initialized { + srv.Logger.Error("Failed to load plugin %s: %s", filename, uninitalized) + return nil, uninitalized + } return plugin, nil } }