type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -9,22 +12,16 @@ import (
|
||||
|
||||
// FFI ...
|
||||
func FFI(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"ffi", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #1 to `ffi` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
name := arg.Value
|
||||
|
||||
arg, ok = args[1].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #2 to `ffi` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
symbol := arg.Value
|
||||
name := args[0].(*object.String).Value
|
||||
symbol := args[1].(*object.String).Value
|
||||
|
||||
p, err := plugin.Open(fmt.Sprintf("%s.so", name))
|
||||
if err != nil {
|
||||
@@ -36,5 +33,5 @@ func FFI(args ...object.Object) object.Object {
|
||||
return newError("error finding symbol: %s", err)
|
||||
}
|
||||
|
||||
return &object.Builtin{Name: symbol, Fn: v.(func(...object.Object) object.Object)}
|
||||
return &object.Builtin{Name: symbol, Fn: v.(object.BuiltinFunction)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user