summaryrefslogtreecommitdiff
path: root/src/core/script/Function.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/script/Function.cpp')
-rw-r--r--src/core/script/Function.cpp52
1 files changed, 50 insertions, 2 deletions
diff --git a/src/core/script/Function.cpp b/src/core/script/Function.cpp
index d72df4c..ce6e6b6 100644
--- a/src/core/script/Function.cpp
+++ b/src/core/script/Function.cpp
@@ -21,9 +21,57 @@
namespace ousia {
namespace script {
-std::pair<bool, std::vector<Variant>> validate(const std::vector<Variant> &args)
+std::pair<bool, std::vector<Variant>> ArgumentValidator::setError(int idx,
+ const std::string &msg, std::vector<Variant> &res)
{
- return std::make_pair(true, std::vector<Variant>{});
+ errorIndex = idx;
+ errorMessage = msg;
+ return std::make_pair(false, res);
+}
+
+void ArgumentValidator::resetError()
+{
+ errorIndex = -1;
+ errorMessage = "";
+}
+
+std::pair<bool, std::vector<Variant>> ArgumentValidator::validate(
+ const std::vector<Variant> &args)
+{
+ std::vector<Variant> res;
+
+ // Reset any old error
+ resetError();
+
+ // Sanity check: Do not allow too many arguments
+ if (args.size() > descriptors.size()) {
+ return setError(descriptors.size(), "Expected " + std::to_string(descriptors.size()) +
+ " arguments but got " + std::to_string(args.size()), res);
+ }
+
+ // Iterate over the given arguments and check their type
+ res.reserve(descriptors.size());
+ for (unsigned int i = 0; i < args.size(); i++) {
+ // TODO: Implicit type conversion
+ const VariantType tGiven = args[i].getType();
+ const VariantType tExpected = descriptors[i].type;
+ if (tGiven != tExpected) {
+ return setError(i, std::string("Expected type ") + Variant::getTypeName(tExpected)
+ + " but got " + Variant::getTypeName(tGiven), res);
+ }
+ res.push_back(args[i]);
+ }
+
+ // Make sure the remaining arguments have a default value, and if they have
+ // one, add it to the result
+ for (unsigned int i = args.size(); i < descriptors.size(); i++) {
+ if (!descriptors[i].hasDefault) {
+ return setError(i, "Expected argument " + std::to_string(i), res);
+ }
+ res.push_back(descriptors[i].defaultValue);
+ }
+
+ return std::make_pair(true, res);
}
}