Skip to content

Commit

Permalink
Add support for console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Jan 6, 2025
1 parent 66465ea commit 7a9207d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 114 deletions.
27 changes: 20 additions & 7 deletions engine/core/processors_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,36 @@ import (

// LookupVerts starts query by looking on vertices
type FlatMap struct {
source string
func_name string
args map[string]any
Source string
Func_name string
Args map[string]any
}

// Process LookupVerts
func (fm *FlatMap) Process(ctx context.Context, man gdbi.Manager, in gdbi.InPipe, out gdbi.OutPipe) context.Context {

vm := goja.New()
_, err := vm.RunString(fm.source)

// Add support for console.log for debugging purposes
console := vm.NewObject()
_ = console.Set("log", func(call goja.FunctionCall) goja.Value {
args := make([]any, len(call.Arguments))
for i, arg := range call.Arguments {
args[i] = arg.Export()
}
log.Infof("JS log: %v", args...)
return goja.Undefined()
})
_ = vm.Set("console", console)

_, err := vm.RunString(fm.Source)
if err != nil {
log.Errorf("User function compile error: %s", err)
}

jobj := vm.Get(fm.func_name)
jobj := vm.Get(fm.Func_name)
if jobj == nil {
log.Errorf("User Function not found: %s", fm.func_name)
log.Errorf("User Function not found: %s", fm.Func_name)
}

jfunc, ok := goja.AssertFunction(jobj)
Expand All @@ -45,7 +58,7 @@ func (fm *FlatMap) Process(ctx context.Context, man gdbi.Manager, in gdbi.InPipe
data := src.ToDict()
dataObj := vm.ToValue(data)

argsObj := vm.ToValue(fm.args)
argsObj := vm.ToValue(fm.Args)

fout, err := jfunc(goja.Null(), dataObj, argsObj)

Expand Down
2 changes: 1 addition & 1 deletion engine/core/statement_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (sc *DefaultStmtCompiler) Aggregate(stmt *gripql.GraphStatement_Aggregate,
}

func (sc *DefaultStmtCompiler) FlatMap(stmt *gripql.GraphStatement_FlatMap, ps *gdbi.State) (gdbi.Processor, error) {
return &FlatMap{source: stmt.FlatMap.Source, func_name: stmt.FlatMap.Function, args: stmt.FlatMap.Args.AsMap()}, nil
return &FlatMap{Source: stmt.FlatMap.Source, Func_name: stmt.FlatMap.Function, Args: stmt.FlatMap.Args.AsMap()}, nil
}

func (sc *DefaultStmtCompiler) Custom(gs *gripql.GraphStatement, ps *gdbi.State) (gdbi.Processor, error) {
Expand Down
Loading

0 comments on commit 7a9207d

Please sign in to comment.