diff --git a/module/Sqrat/sqratFunction.h b/module/Sqrat/sqratFunction.h index fdc1307e..797d3381 100644 --- a/module/Sqrat/sqratFunction.h +++ b/module/Sqrat/sqratFunction.h @@ -318,6 +318,45 @@ struct Function { sq_settop(vm, top); return ret; } + // Runs the Function and returns its value as R + template R Evaluate_(SqTypeIdentity, Args &&... args) const { + static constexpr unsigned ARGC = sizeof...(Args) + 1; // + environment + HSQUIRRELVM vm = SqVM(); + const SQInteger top = sq_gettop(vm); + // Push the environment followed by the function + sq_pushobject(vm, mObj); + sq_pushobject(vm, mEnv); + // Validate the funtion parameter count +#if !defined (SCRAT_NO_ERROR_CHECKING) + SQInteger nparams; + SQInteger nfreevars; + if (SQ_SUCCEEDED(sq_getclosureinfo(vm, -2, &nparams, &nfreevars)) && + SqGlobalParamInspect< ArgFwd::HASOPT >::Invalid(nparams, ARGC)) { + sq_pop(vm, 2); + SQTHROW(vm, _SC("wrong number of parameters")); + return R(); + } +#endif + // Push the arguments + PushVars(vm, std::forward(args)...); + +#if !defined (SCRAT_NO_ERROR_CHECKING) + SQRESULT result = sq_call(vm, ARGC, true, ErrorHandling::IsEnabled()); + + //handle an error: pop the stack and throw the exception + if (SQ_FAILED(result)) { + sq_settop(vm, top); + SQTHROW(vm, LastErrorString(vm)); + return R(); + } +#else + sq_call(vm, ARGC, true, ErrorHandling::IsEnabled()); +#endif + + Var r(vm, -1); + sq_settop(vm, top); + return r.value; + } // Runs the Function template< class... Args > void Execute(Args &&... args) const {