Add optional message text parameter for assert()

This commit is contained in:
VasiliyRyabtsev 2017-11-29 11:34:59 +07:00
parent 938655a90b
commit c10a777285
2 changed files with 11 additions and 4 deletions

View File

@ -53,9 +53,9 @@ returns the const table of the VM.
sets the const table of the VM; returns the previous const table.
.. js:function:: assert(exp)
.. js:function:: assert(exp, [message])
throws an exception if exp is null
throws an exception if exp is null or false. Throws "assertion failed" string by default, or message if specified.
.. js:function:: print(x)

View File

@ -156,6 +156,13 @@ static SQInteger base_getstackinfos(HSQUIRRELVM v)
static SQInteger base_assert(HSQUIRRELVM v)
{
if(SQVM::IsFalse(stack_get(v,2))){
SQInteger top = sq_gettop(v);
if (top>2 && SQ_SUCCEEDED(sq_tostring(v,3))) {
const SQChar *str = 0;
if (SQ_SUCCEEDED(sq_getstring(v,-1,&str))) {
return sq_throwerror(v, str);
}
}
return sq_throwerror(v, _SC("assertion failed"));
}
return 0;
@ -283,7 +290,7 @@ static const SQRegFunction base_funcs[]={
{_SC("setroottable"),base_setroottable,2, NULL},
{_SC("getconsttable"),base_getconsttable,1, NULL},
{_SC("setconsttable"),base_setconsttable,2, NULL},
{_SC("assert"),base_assert,2, NULL},
{_SC("assert"),base_assert,-2, NULL},
{_SC("print"),base_print,2, NULL},
{_SC("error"),base_error,2, NULL},
{_SC("compilestring"),base_compilestring,-2, _SC(".ss")},