2023-09-16 16:02:17 +05:30
|
|
|
package gojs
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-09 14:47:26 -05:00
|
|
|
"context"
|
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
|
|
"github.com/Mzack9999/goja"
|
2025-08-20 05:28:23 +05:30
|
|
|
"github.com/projectdiscovery/utils/errkit"
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2025-08-25 15:06:58 +07:00
|
|
|
ErrInvalidFuncOpts = errkit.New("invalid function options")
|
|
|
|
|
ErrNilRuntime = errkit.New("runtime is nil")
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FuncOpts struct {
|
|
|
|
|
Name string
|
|
|
|
|
Signatures []string
|
|
|
|
|
Description string
|
|
|
|
|
FuncDecl interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// valid checks if the function options are valid
|
|
|
|
|
func (f *FuncOpts) valid() bool {
|
|
|
|
|
return f.Name != "" && f.FuncDecl != nil && len(f.Signatures) > 0 && f.Description != ""
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
// wrapWithContext wraps a Go function with context injection
|
|
|
|
|
// nolint
|
|
|
|
|
func wrapWithContext(runtime *goja.Runtime, fn interface{}) interface{} {
|
|
|
|
|
fnType := reflect.TypeOf(fn)
|
|
|
|
|
if fnType.Kind() != reflect.Func {
|
|
|
|
|
return fn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only wrap if first parameter is context.Context
|
2025-08-28 01:01:04 +08:00
|
|
|
if fnType.NumIn() == 0 || fnType.In(0) != reflect.TypeFor[context.Context]() {
|
2025-07-09 14:47:26 -05:00
|
|
|
return fn // Return original function unchanged if it doesn't have context.Context as first arg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create input and output type slices
|
|
|
|
|
inTypes := make([]reflect.Type, fnType.NumIn())
|
|
|
|
|
for i := 0; i < fnType.NumIn(); i++ {
|
|
|
|
|
inTypes[i] = fnType.In(i)
|
|
|
|
|
}
|
|
|
|
|
outTypes := make([]reflect.Type, fnType.NumOut())
|
|
|
|
|
for i := 0; i < fnType.NumOut(); i++ {
|
|
|
|
|
outTypes[i] = fnType.Out(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new function with same signature
|
|
|
|
|
newFnType := reflect.FuncOf(inTypes, outTypes, fnType.IsVariadic())
|
|
|
|
|
newFn := reflect.MakeFunc(newFnType, func(args []reflect.Value) []reflect.Value {
|
|
|
|
|
// Get context from runtime
|
|
|
|
|
var ctx context.Context
|
|
|
|
|
if ctxVal := runtime.Get("context"); ctxVal != nil {
|
|
|
|
|
if ctxObj, ok := ctxVal.Export().(context.Context); ok {
|
|
|
|
|
ctx = ctxObj
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add execution ID to context if available
|
|
|
|
|
if execID := runtime.Get("executionId"); execID != nil {
|
|
|
|
|
ctx = context.WithValue(ctx, "executionId", execID.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace first argument (context) with our context
|
|
|
|
|
args[0] = reflect.ValueOf(ctx)
|
|
|
|
|
|
|
|
|
|
// Call original function with modified arguments
|
|
|
|
|
return reflect.ValueOf(fn).Call(args)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return newFn.Interface()
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:02:17 +05:30
|
|
|
// RegisterFunc registers a function with given name, signatures and description
|
|
|
|
|
func RegisterFuncWithSignature(runtime *goja.Runtime, opts FuncOpts) error {
|
2024-04-01 19:18:21 +05:30
|
|
|
if runtime == nil {
|
|
|
|
|
return ErrNilRuntime
|
|
|
|
|
}
|
2023-09-16 16:02:17 +05:30
|
|
|
if !opts.valid() {
|
2025-08-25 15:06:58 +07:00
|
|
|
return errkit.Newf("invalid function options: name: %s, signatures: %v, description: %s", opts.Name, opts.Signatures, opts.Description)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
|
|
|
|
|
// Wrap the function with context injection
|
|
|
|
|
// wrappedFn := wrapWithContext(runtime, opts.FuncDecl)
|
|
|
|
|
return runtime.Set(opts.Name, opts.FuncDecl /* wrappedFn */)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|