Add check for the number/type of arg in #require and #require_relative
@@ -874,6 +874,11 @@ func builtinClassCommonInstanceMethods() []*BuiltinMethodObject {
Name: "require",
Fn: func(receiver Object, sourceLine int) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *normalCallFrame) Object {
+ if len(args) != 1 {
+ return t.vm.initErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got: %d", len(args))
+ }
+ switch args[0].(type) {
+ case *StringObject:
libName := args[0].(*StringObject).value
initFunc, ok := standardLibraries[libName]
@@ -884,6 +889,9 @@ func builtinClassCommonInstanceMethods() []*BuiltinMethodObject {
initFunc(t.vm)
return TRUE
+ default:
+ return t.vm.initErrorObject(errors.InternalError, sourceLine, "Can't require \"%s\". Pass a string instead", args[0].(Object).Class().Name)
+ }
}
},
},
@@ -902,6 +910,11 @@ func builtinClassCommonInstanceMethods() []*BuiltinMethodObject {
Name: "require_relative",
Fn: func(receiver Object, sourceLine int) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *normalCallFrame) Object {
+ if len(args) != 1 {
+ return t.vm.initErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got: %d", len(args))
+ }
+ switch args[0].(type) {
+ case *StringObject:
callerDir := path.Dir(t.vm.currentFilePath())
filepath := args[0].(*StringObject).value
@@ -916,6 +929,9 @@ func builtinClassCommonInstanceMethods() []*BuiltinMethodObject {
t.vm.execRequiredFile(filepath, file)
return TRUE
+ default:
+ return t.vm.initErrorObject(errors.InternalError, sourceLine, "Can't require \"%s\". Pass a string instead", args[0].(Object).Class().Name)
+ }
}
},
},
