From dbb94be50c67ce2d4a132b0811c2a8dac825b49b Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sat, 25 Oct 2014 00:01:56 +0000 Subject: started unit test for Object, fixed possible used of already freed ArgumentValidator when copying ValidatingFunction git-svn-id: file:///var/local/svn/basicwriter@77 daaaf23c-2e50-4459-9457-1e69db5a47bf --- src/core/script/Function.cpp | 48 ++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src/core/script/Function.cpp') diff --git a/src/core/script/Function.cpp b/src/core/script/Function.cpp index ce6e6b6..84473a8 100644 --- a/src/core/script/Function.cpp +++ b/src/core/script/Function.cpp @@ -21,8 +21,10 @@ namespace ousia { namespace script { -std::pair> ArgumentValidator::setError(int idx, - const std::string &msg, std::vector &res) +/* Class ArgumentValidator */ + +std::pair> ArgumentValidator::setError( + int idx, const std::string &msg, std::vector &res) { errorIndex = idx; errorMessage = msg; @@ -36,7 +38,7 @@ void ArgumentValidator::resetError() } std::pair> ArgumentValidator::validate( - const std::vector &args) + const std::vector &args) { std::vector res; @@ -44,36 +46,56 @@ std::pair> ArgumentValidator::validate( 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); + if (args.size() > signature.size()) { + return setError(signature.size(), + "Expected " + std::to_string(signature.size()) + + " arguments but got " + std::to_string(args.size()), + res); } // Iterate over the given arguments and check their type - res.reserve(descriptors.size()); + res.reserve(signature.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; + const VariantType tExpected = signature[i].type; if (tGiven != tExpected) { - return setError(i, std::string("Expected type ") + Variant::getTypeName(tExpected) - + " but got " + Variant::getTypeName(tGiven), res); + 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) { + for (unsigned int i = args.size(); i < signature.size(); i++) { + if (!signature[i].hasDefault) { return setError(i, "Expected argument " + std::to_string(i), res); } - res.push_back(descriptors[i].defaultValue); + res.push_back(signature[i].defaultValue); } return std::make_pair(true, res); } +/* Class ValidatingFunction */ + +Variant ValidatingFunction::call(const std::vector &args) const +{ + if (validate) { + ArgumentValidator validator{signature}; + + std::pair> res = validator.validate(args); + if (!res.first) { + throw validator.error(); + } + return validatedCall(res.second); + } + + return validatedCall(args); +} } } -- cgit v1.2.3