-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
40 lines (34 loc) · 814 Bytes
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package internal
import (
"fmt"
"github.com/dop251/goja"
)
type AST struct {
v goja.Value
M map[string]any
}
type ASTResult struct {
AST []AST
}
func NewASTResult(vm *goja.Runtime, val goja.Value) (*ASTResult, error) {
var slice []AST
if v, ok := val.Export().([]any); ok {
for _, item := range v {
if _, ok := item.(map[string]any); !ok {
return nil, fmt.Errorf("invalid AST result: %v", item)
}
ast := item.(map[string]any)
slice = append(slice, AST{vm.ToValue(ast), ast})
}
} else if _, ok := val.Export().(map[string]any); ok {
var ast map[string]any
err := vm.ExportTo(val, &ast)
if err != nil {
return nil, err
}
slice = append(slice, AST{val, ast})
} else {
return nil, fmt.Errorf("invalid AST result: %v", val.Export())
}
return &ASTResult{slice}, nil
}