1
0
Fork 0
golang-github-dop251-goja-n.../console/module.go
Daniel Baumann 21b930d007
Adding upstream version 0.0~git20250409.f7acab6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-22 11:36:18 +02:00

72 lines
1.6 KiB
Go

package console
import (
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/require"
"github.com/dop251/goja_nodejs/util"
)
const ModuleName = "console"
type Console struct {
runtime *goja.Runtime
util *goja.Object
printer Printer
}
type Printer interface {
Log(string)
Warn(string)
Error(string)
}
func (c *Console) log(p func(string)) func(goja.FunctionCall) goja.Value {
return func(call goja.FunctionCall) goja.Value {
if format, ok := goja.AssertFunction(c.util.Get("format")); ok {
ret, err := format(c.util, call.Arguments...)
if err != nil {
panic(err)
}
p(ret.String())
} else {
panic(c.runtime.NewTypeError("util.format is not a function"))
}
return nil
}
}
func Require(runtime *goja.Runtime, module *goja.Object) {
requireWithPrinter(defaultStdPrinter)(runtime, module)
}
func RequireWithPrinter(printer Printer) require.ModuleLoader {
return requireWithPrinter(printer)
}
func requireWithPrinter(printer Printer) require.ModuleLoader {
return func(runtime *goja.Runtime, module *goja.Object) {
c := &Console{
runtime: runtime,
printer: printer,
}
c.util = require.Require(runtime, util.ModuleName).(*goja.Object)
o := module.Get("exports").(*goja.Object)
o.Set("log", c.log(c.printer.Log))
o.Set("error", c.log(c.printer.Error))
o.Set("warn", c.log(c.printer.Warn))
o.Set("info", c.log(c.printer.Log))
o.Set("debug", c.log(c.printer.Log))
}
}
func Enable(runtime *goja.Runtime) {
runtime.Set("console", require.Require(runtime, ModuleName))
}
func init() {
require.RegisterCoreModule(ModuleName, Require)
}