Adding upstream version 0.0~git20250409.f7acab6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
b9b5d88025
commit
21b930d007
51 changed files with 11229 additions and 0 deletions
72
console/module.go
Normal file
72
console/module.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
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)
|
||||
}
|
78
console/module_test.go
Normal file
78
console/module_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package console
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/require"
|
||||
)
|
||||
|
||||
func TestConsole(t *testing.T) {
|
||||
vm := goja.New()
|
||||
|
||||
new(require.Registry).Enable(vm)
|
||||
Enable(vm)
|
||||
|
||||
if c := vm.Get("console"); c == nil {
|
||||
t.Fatal("console not found")
|
||||
}
|
||||
|
||||
if _, err := vm.RunString("console.log('')"); err != nil {
|
||||
t.Fatal("console.log() error", err)
|
||||
}
|
||||
|
||||
if _, err := vm.RunString("console.error('')"); err != nil {
|
||||
t.Fatal("console.error() error", err)
|
||||
}
|
||||
|
||||
if _, err := vm.RunString("console.warn('')"); err != nil {
|
||||
t.Fatal("console.warn() error", err)
|
||||
}
|
||||
|
||||
if _, err := vm.RunString("console.info('')"); err != nil {
|
||||
t.Fatal("console.info() error", err)
|
||||
}
|
||||
|
||||
if _, err := vm.RunString("console.debug('')"); err != nil {
|
||||
t.Fatal("console.debug() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsoleWithPrinter(t *testing.T) {
|
||||
var stdoutStr, stderrStr string
|
||||
|
||||
printer := StdPrinter{
|
||||
StdoutPrint: func(s string) { stdoutStr += s },
|
||||
StderrPrint: func(s string) { stderrStr += s },
|
||||
}
|
||||
|
||||
vm := goja.New()
|
||||
|
||||
registry := new(require.Registry)
|
||||
registry.Enable(vm)
|
||||
registry.RegisterNativeModule(ModuleName, RequireWithPrinter(printer))
|
||||
Enable(vm)
|
||||
|
||||
if c := vm.Get("console"); c == nil {
|
||||
t.Fatal("console not found")
|
||||
}
|
||||
|
||||
_, err := vm.RunString(`
|
||||
console.log('a')
|
||||
console.error('b')
|
||||
console.warn('c')
|
||||
console.debug('d')
|
||||
console.info('e')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if want := "ade"; stdoutStr != want {
|
||||
t.Fatalf("Unexpected stdout output: got %q, want %q", stdoutStr, want)
|
||||
}
|
||||
|
||||
if want := "bc"; stderrStr != want {
|
||||
t.Fatalf("Unexpected stderr output: got %q, want %q", stderrStr, want)
|
||||
}
|
||||
}
|
38
console/std_printer.go
Normal file
38
console/std_printer.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package console
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
stderrLogger = log.Default() // the default logger output to stderr
|
||||
stdoutLogger = log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
||||
defaultStdPrinter Printer = &StdPrinter{
|
||||
StdoutPrint: func(s string) { stdoutLogger.Print(s) },
|
||||
StderrPrint: func(s string) { stderrLogger.Print(s) },
|
||||
}
|
||||
)
|
||||
|
||||
// StdPrinter implements the console.Printer interface
|
||||
// that prints to the stdout or stderr.
|
||||
type StdPrinter struct {
|
||||
StdoutPrint func(s string)
|
||||
StderrPrint func(s string)
|
||||
}
|
||||
|
||||
// Log prints s to the stdout.
|
||||
func (p StdPrinter) Log(s string) {
|
||||
p.StdoutPrint(s)
|
||||
}
|
||||
|
||||
// Warn prints s to the stderr.
|
||||
func (p StdPrinter) Warn(s string) {
|
||||
p.StderrPrint(s)
|
||||
}
|
||||
|
||||
// Error prints s to the stderr.
|
||||
func (p StdPrinter) Error(s string) {
|
||||
p.StderrPrint(s)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue