Functions | |
uword_t | croc_call (CrocThread *t, word_t slot, word_t numReturns) |
uword_t | croc_methodCall (CrocThread *t, word_t slot, const char *name, word_t numReturns) |
int | croc_tryCall (CrocThread *t, word_t slot, word_t numReturns) |
int | croc_tryMethodCall (CrocThread *t, word_t slot, const char *name, word_t numReturns) |
Function calling and the native equivalent of 'try'.
uword_t croc_call | ( | CrocThread * | t, |
word_t | slot, | ||
word_t | numReturns | ||
) |
Performs a function call.
The process of calling a function goes something like this:
croc_call
with the slot of the thing you want to call.As an example, let's call the toString
function on an integer:
When you use croc_call
, all the slots above slot
are considered its parameters. All the slots starting at slot
are removed from the stack and replaced with the return values upon return.
slot | is the slot to call. There must be at least one slot on the stack above this slot for the 'this' parameter. |
numReturns | is how many return values you want left on the stack. This can be -1, which means "as many values as were returned". |
numReturns
>= 0, then this is the same as numReturns
, but if it was -1 this is how you find out how many values are sitting on top of the stack. uword_t croc_methodCall | ( | CrocThread * | t, |
word_t | slot, | ||
const char * | name, | ||
word_t | numReturns | ||
) |
Performs a method call.
The way you use this is almost identical to croc_call, except instead of pushing the thing to call, you push the thing to call the method on. For example, let's call the "writeln" method of console.stderr:
Notice that you still have to push a slot after the object. The value of this slot will always be ignored and overwritten with the object. It's just there to make the API the same as croc_call and to simplify the implementation of this function (so it doesn't have to shift the parameters up).
slot | is the slot holding the object on which the method will be called. |
name | is the name of the method to call. |
numReturns | is how many return values you want left on the stack, or -1 to get all values returned. |
int croc_tryCall | ( | CrocThread * | t, |
word_t | slot, | ||
word_t | numReturns | ||
) |
Just like croc_call, but sets up an exception frame around the call.
This is like using a try-catch block in Croc. The way you use it is exactly the same as croc_call, except for the return value. If the call completed successfully, the return value will be >= 0. If an exception was thrown (and caught by this exception frame), it will return CrocCallRet_Error. In this case, the exception object will be sitting on top of the stack (replacing the function and its parameters, just like when it returns values normally). You can then do whatever you want, such as handling the exception, or doing some cleanup and rethrowing it with croc_eh_rethrow.
It's also good practice to see if the exception you caught was a HaltException
. If so, you should rethrow it. You can use the croc_ex_isHaltException function to easily see if it is.
int croc_tryMethodCall | ( | CrocThread * | t, |
word_t | slot, | ||
const char * | name, | ||
word_t | numReturns | ||
) |
Just like croc_methodCall, but sets up an exception frame around the call like croc_tryCall.