pls
This commit is contained in:
@@ -5,8 +5,6 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"modernc.org/quickjs"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -15,34 +13,29 @@ var (
|
||||
collector *typeCollector
|
||||
)
|
||||
|
||||
func registerBuiltin[T Args, R any](name string, isAsync bool, fn Func[T, R]) {
|
||||
func registerBuiltin[A Args, R any](name string, isAsync bool, fn Func[A, R]) {
|
||||
registryMutex.Lock()
|
||||
defer registryMutex.Unlock()
|
||||
|
||||
if collector == nil {
|
||||
collector = newTypeCollector()
|
||||
}
|
||||
|
||||
var zeroT T
|
||||
tType := reflect.TypeOf(zeroT)
|
||||
|
||||
tType := reflect.TypeFor[A]()
|
||||
if tType.Kind() != reflect.Struct {
|
||||
panic(fmt.Sprintf("builtin %s: argument must be a struct type, got %v", name, tType))
|
||||
}
|
||||
|
||||
fnType := reflect.TypeOf(fn)
|
||||
|
||||
wrapper := createWrapper(fn, isAsync)
|
||||
types := collector.collectTypes(tType, fnType)
|
||||
paramTypes := collector.getParamTypes()
|
||||
|
||||
registryMutex.Lock()
|
||||
b := Builtin{
|
||||
Name: name,
|
||||
Function: wrapper,
|
||||
Definition: generateTypeScriptDefinition(name, tType, fnType, isAsync, paramTypes),
|
||||
Types: types,
|
||||
ParamTypes: paramTypes,
|
||||
builtinRegistry[name] = &builtinImpl[A, R]{
|
||||
name: name,
|
||||
fn: fn,
|
||||
types: types,
|
||||
definition: generateTypeScriptDefinition(name, tType, fnType, isAsync, paramTypes),
|
||||
}
|
||||
builtinRegistry[name] = b
|
||||
registryMutex.Unlock()
|
||||
}
|
||||
|
||||
func GetBuiltinsDeclarations() string {
|
||||
@@ -54,13 +47,13 @@ func GetBuiltinsDeclarations() string {
|
||||
var functionDecls []string
|
||||
|
||||
for _, builtin := range builtinRegistry {
|
||||
for _, t := range builtin.Types {
|
||||
for _, t := range builtin.Types() {
|
||||
if !typeDefinitions[t] {
|
||||
typeDefinitions[t] = true
|
||||
typeDefs = append(typeDefs, t)
|
||||
}
|
||||
}
|
||||
functionDecls = append(functionDecls, builtin.Definition)
|
||||
functionDecls = append(functionDecls, builtin.Definition())
|
||||
}
|
||||
|
||||
result := strings.Join(typeDefs, "\n\n")
|
||||
@@ -80,14 +73,6 @@ func RegisterAsyncBuiltin[T Args, R any](name string, fn Func[T, R]) {
|
||||
registerBuiltin(name, true, fn)
|
||||
}
|
||||
|
||||
func RegisterBuiltins(vm *quickjs.VM) {
|
||||
registryMutex.RLock()
|
||||
defer registryMutex.RUnlock()
|
||||
|
||||
for name, builtin := range builtinRegistry {
|
||||
err := vm.RegisterFunc(name, builtin.Function, false)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to register builtin %s: %v", name, err))
|
||||
}
|
||||
}
|
||||
func GetBuiltins() map[string]Builtin {
|
||||
return builtinRegistry
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user