[Include] i_errors – Raise any AMX error in a simple fashion
#1

i_errors
Download
Introduction
Statements like "sleep" (not working) and "assert" put the AMX machine (the one that executes compiled Pawn scripts) in a special state, breaking execution in the middle of the function (if assertation fails) and showing an error message. I've always wondered how these functions are implemented, and it turns out they are all done using the "halt" instruction (also sleep puts the argument into the primary register). It turns out any possible AMX error can be raised using this instruction, so I've made a list of all possible error codes, put them into an include and equipped it with a supplementary raise function, which triggers an error "#emit-free" for the user.

Error codes
All these codes can be accessed via a simple name:
Code:
enum {
  AMX_ERR_NONE,
  AMX_ERR_EXIT,         /* forced exit */
  AMX_ERR_ASSERT,       /* assertion failed */
  AMX_ERR_STACKERR,     /* stack/heap collision */
  AMX_ERR_BOUNDS,       /* index out of bounds */
  AMX_ERR_MEMACCESS,    /* invalid memory access */
  AMX_ERR_INVINSTR,     /* invalid instruction */
  AMX_ERR_STACKLOW,     /* stack underflow */
  AMX_ERR_HEAPLOW,      /* heap underflow */
  AMX_ERR_CALLBACK,     /* no callback, or invalid callback */
  AMX_ERR_NATIVE,       /* native function failed */
  AMX_ERR_DIVIDE,       /* divide by zero */
  AMX_ERR_SLEEP,        /* go into sleepmode - code can be restarted */
  AMX_ERR_MEMORY = 16,  /* out of memory */
  AMX_ERR_FORMAT,       /* invalid file format */
  AMX_ERR_VERSION,      /* file is for a newer version of the AMX */
  AMX_ERR_NOTFOUND,     /* function not found */
  AMX_ERR_INDEX,        /* invalid index parameter (bad entry point) */
  AMX_ERR_DEBUG,        /* debugger cannot run */
  AMX_ERR_INIT,         /* AMX not initialized (or doubly initialized) */
  AMX_ERR_USERDATA,     /* unable to set user data field (table full) */
  AMX_ERR_INIT_JIT,     /* cannot initialize the JIT */
  AMX_ERR_PARAMS,       /* parameter error */
  AMX_ERR_DOMAIN,       /* domain error, expression result does not fit in range */
  AMX_ERR_GENERAL,      /* general error (unknown or unspecific error) */
}
Functions
Code:
stock raise(errcode)
Raises an error specified by errorcode. This typically stops the execution of the current function and all functions calling it up to the original callback. This doesn't crash the server (only if enabled with crashdetect). For most of the codes, it also displays their accompanying error message in the log, and stack trace if crashdetect is installed. This function never returns (obviously).

Because of the way this function is implemented (a simple switch statement), only defined error codes can be used. Other codes will default to -1 (unknown). I guess raising any possible error code is doable, but it would require either a huge switch or modifying the running code, neither of which's costs are outweighed by the benefits.

Code:
stock errormsg(errcode)
Returns a message which would be displayed for a specific errcode. May be useful in debugging cases where you either want the code to stop or continue, but display the error message nevertheless.
Reply
#2

This sounds interesting, I'll look into it later and say what I think about it
Reply
#3

This remember me about Zeex and ****** work because the AMX stuff.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)