natiny_script_fail
void natiny_script_fail(NatinyScript script, const char* message);
Reports, from inside a bound C function, that the call failed. The error is raised in the script after your function returns.
Parameters
| Name | Type | Description |
|---|---|---|
script |
NatinyScript |
The state the call came from - the one your function was handed |
message |
const char* |
What went wrong; NULL gives script function failed |
Example
static uint32_t open_save(NatinyScript script,
const NatinyScriptValue* args, uint32_t count,
NatinyScriptValue* results) {
FILE* file;
uint32_t bytes_read;
if (count < 1 || args[0].type != NATINY_SCRIPT_STRING) {
natiny_script_fail(script, "open_save: expected a file name");
return 0;
}
file = fopen(natiny_script_to_string(args[0]), "rb");
if (!file) {
natiny_script_fail(script, "open_save: no such save");
return 0;
}
bytes_read = read_save(file);
fclose(file);
results[0] = natiny_script_make_number(bytes_read);
return 1;
}
local ok, err = pcall(open_save, name)
Notes
- This function returns normally; the bound callback must release its C resources and then return. The script error is raised after the callback returns.
- Calling it and then returning normally from a nested call does not leak the failure outwards: each bound call clears the flag before it starts.