-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
274 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.