Files
monkey/internal/builtins/ffi.go
Chuck Smith 12d43c9835
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Refactor Type to be an int
2024-03-29 11:01:15 -04:00

38 lines
721 B
Go

package builtins
import (
"monkey/internal/object"
"monkey/internal/typing"
)
import (
"fmt"
"plugin"
)
// FFI ...
func FFI(args ...object.Object) object.Object {
if err := typing.Check(
"ffi", args,
typing.ExactArgs(2),
typing.WithTypes(object.StringType, object.StringType),
); err != nil {
return newError(err.Error())
}
name := args[0].(*object.String).Value
symbol := args[1].(*object.String).Value
p, err := plugin.Open(fmt.Sprintf("%s.so", name))
if err != nil {
return newError("error loading plugin: %s", err)
}
v, err := p.Lookup(symbol)
if err != nil {
return newError("error finding symbol: %s", err)
}
return &object.Builtin{Name: symbol, Fn: v.(object.BuiltinFunction)}
}