1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-03 00:46:51 +02:00

Feature/pre compiled eval scripts (#658)

* Added support of pre-compiled eval expressions

* Added unit tests for eval.Function

* Added RemoteType and RemoteObjectType enums

* Refactored function generation

* Refactored Document and Element loading logic

* Removed redundant fields from cdp.Page

* Exposed eval.Runtime to external callers

* Added new eval.RemoteValue interface
This commit is contained in:
Tim Voronov
2021-09-19 19:35:54 -04:00
committed by GitHub
parent 90427cd537
commit 847dda1f10
23 changed files with 1264 additions and 417 deletions

View File

@ -1,81 +1,14 @@
package eval
import (
"strconv"
"strings"
"github.com/mafredri/cdp/protocol/runtime"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func CastToValue(input interface{}) (core.Value, error) {
value, ok := input.(core.Value)
if !ok {
return values.None, core.Error(core.ErrInvalidType, "eval return type")
}
return value, nil
}
func CastToReference(input interface{}) (runtime.RemoteObject, error) {
value, ok := input.(runtime.RemoteObject)
if !ok {
return runtime.RemoteObject{}, core.Error(core.ErrInvalidType, "eval return type")
}
return value, nil
}
func wrapExp(exp string, args int) string {
if args == 0 {
return "() => {\n" + exp + "\n}"
}
var buf strings.Builder
lastIndex := args - 1
for i := 0; i < args; i++ {
buf.WriteString("arg")
buf.WriteString(strconv.Itoa(i + 1))
if i != lastIndex {
buf.WriteString(",")
}
}
return "(" + buf.String() + ") => {\n" + exp + "\n}"
}
func Unmarshal(obj runtime.RemoteObject) (core.Value, error) {
switch obj.Type {
case "string":
str, err := strconv.Unquote(string(obj.Value))
if err != nil {
return values.None, err
}
return values.NewString(str), nil
case "object":
if obj.Subtype != nil {
subtype := *obj.Subtype
if subtype == "null" || subtype == "undefined" {
return values.None, nil
}
}
return values.Unmarshal(obj.Value)
default:
return values.Unmarshal(obj.Value)
}
}
func parseRuntimeException(details *runtime.ExceptionDetails) error {
if details == nil || details.Exception == nil {
return nil