more cleanup

This commit is contained in:
2026-01-28 22:28:42 -05:00
parent ffcb6f658b
commit e04fe8cef3
4 changed files with 84 additions and 71 deletions

View File

@@ -67,7 +67,14 @@ func GetFunctionDeclarations() string {
}
func GetRegisteredFunctions() map[string]Function {
return functionRegistry
registryMutex.RLock()
defer registryMutex.RUnlock()
result := make(map[string]Function, len(functionRegistry))
for k, v := range functionRegistry {
result[k] = v
}
return result
}
func RegisterFunction[T Args, R any](name string, fn GoFunc[T, R]) {

View File

@@ -94,11 +94,14 @@ func (r *Runtime) transformCode(tsCode string) ([]byte, error) {
})
if len(result.Errors) > 0 {
var allErrs []string
for _, e := range result.Errors {
allErrs = append(allErrs, e.Text)
var b strings.Builder
for i, e := range result.Errors {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(e.Text)
}
return nil, fmt.Errorf("transpilation failed: %s", strings.Join(allErrs, ", "))
return nil, fmt.Errorf("transpilation failed: %s", b.String())
}
return result.Code, nil
@@ -112,7 +115,7 @@ func callFunc(this *qjs.This, fn functions.Function) (*qjs.Value, error) {
for i := range min(len(fnArgs), len(qjsArgs)) {
rVal, err := qjs.JsArgToGo(qjsArgs[i], fnArgs[i])
if err != nil {
panic(err)
return nil, fmt.Errorf("argument conversion failed: %w", err)
}
allArgs = append(allArgs, rVal.Interface())
}

View File

@@ -31,9 +31,6 @@ type RequestInit struct {
}
func (o *RequestInit) Validate() error {
if o.Method == "" {
o.Method = "GET"
}
return nil
}
@@ -44,35 +41,20 @@ type Response struct {
Headers map[string]string `json:"headers"`
}
type AddArgs struct {
A int `json:"a"`
B int `json:"b"`
}
func (a AddArgs) Validate() error {
return nil
}
type GreetArgs struct {
Name string `json:"name"`
}
func (g GreetArgs) Validate() error {
return nil
}
func Fetch(_ context.Context, args FetchArgs) (Response, error) {
func Fetch(ctx context.Context, args FetchArgs) (Response, error) {
method := "GET"
headers := make(map[string]string)
if args.Init != nil {
method = args.Init.Method
if args.Init.Method != "" {
method = args.Init.Method
}
if args.Init.Headers != nil {
maps.Copy(headers, args.Init.Headers)
}
}
req, err := http.NewRequest(method, args.Input, nil)
req, err := http.NewRequestWithContext(ctx, method, args.Input, nil)
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}