From 60446af1456730c9d747f8577e5ad237afa4b2f1 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:53:51 +1000 Subject: [PATCH] Add console javascript object for backwards compatibility (#4944) --- pkg/javascript/console.go | 26 ++++++++++++++++++++++++++ pkg/javascript/vm.go | 12 ++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 pkg/javascript/console.go diff --git a/pkg/javascript/console.go b/pkg/javascript/console.go new file mode 100644 index 000000000..067a2f0f4 --- /dev/null +++ b/pkg/javascript/console.go @@ -0,0 +1,26 @@ +package javascript + +import "fmt" + +type console struct { + Log +} + +func (c *console) AddToVM(globalName string, vm *VM) error { + console := vm.NewObject() + if err := SetAll(console, + ObjectValueDef{"log", c.logInfo}, + ObjectValueDef{"error", c.logError}, + ObjectValueDef{"warn", c.logWarn}, + ObjectValueDef{"info", c.logInfo}, + ObjectValueDef{"debug", c.logDebug}, + ); err != nil { + return err + } + + if err := vm.Set(globalName, console); err != nil { + return fmt.Errorf("unable to set console: %w", err) + } + + return nil +} diff --git a/pkg/javascript/vm.go b/pkg/javascript/vm.go index ce84b820d..556edb1d6 100644 --- a/pkg/javascript/vm.go +++ b/pkg/javascript/vm.go @@ -7,6 +7,7 @@ import ( "reflect" "github.com/dop251/goja" + "github.com/stashapp/stash/pkg/logger" ) type VM struct { @@ -36,6 +37,17 @@ func (tfm optionalFieldNameMapper) MethodName(t reflect.Type, m reflect.Method) func NewVM() *VM { r := goja.New() + + // enable console for backwards compatibility + c := console{ + Log{ + Logger: logger.Logger, + }, + } + + // there should not be any reason for this to fail + _ = c.AddToVM("console", &VM{Runtime: r}) + r.SetFieldNameMapper(optionalFieldNameMapper{goja.TagFieldNameMapper("json", true)}) return &VM{Runtime: r} }