mirror of
https://github.com/stashapp/stash.git
synced 2025-12-16 20:07:05 +03:00
Add console javascript object for backwards compatibility (#4944)
This commit is contained in:
26
pkg/javascript/console.go
Normal file
26
pkg/javascript/console.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VM struct {
|
type VM struct {
|
||||||
@@ -36,6 +37,17 @@ func (tfm optionalFieldNameMapper) MethodName(t reflect.Type, m reflect.Method)
|
|||||||
|
|
||||||
func NewVM() *VM {
|
func NewVM() *VM {
|
||||||
r := goja.New()
|
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)})
|
r.SetFieldNameMapper(optionalFieldNameMapper{goja.TagFieldNameMapper("json", true)})
|
||||||
return &VM{Runtime: r}
|
return &VM{Runtime: r}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user