Skip to content

Commit

Permalink
Add console javascript object for backwards compatibility (#4944)
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants authored Jun 7, 2024
1 parent dbfa450 commit 60446af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/javascript/console.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions pkg/javascript/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"

"github.com/dop251/goja"
"github.com/stashapp/stash/pkg/logger"
)

type VM struct {
Expand Down Expand Up @@ -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}
}
Expand Down

0 comments on commit 60446af

Please sign in to comment.