The Croc Programming Language
Value types

Macros

#define croc_getNum   croc_asFloat
 

Functions

word_t croc_pushNull (CrocThread *t)
 
word_t croc_pushBool (CrocThread *t, int v)
 
word_t croc_pushInt (CrocThread *t, crocint_t v)
 
word_t croc_pushFloat (CrocThread *t, crocfloat_t v)
 
word_t croc_pushString (CrocThread *t, const char *v)
 
word_t croc_pushStringn (CrocThread *t, const char *v, uword_t len)
 
int croc_tryPushString (CrocThread *t, const char *v)
 
int croc_tryPushStringn (CrocThread *t, const char *v, uword_t len)
 
word_t croc_pushChar (CrocThread *t, crocchar_t c)
 
word_t croc_pushFormat (CrocThread *t, const char *fmt,...)
 
word_t croc_vpushFormat (CrocThread *t, const char *fmt, va_list args)
 
word_t croc_pushNativeobj (CrocThread *t, void *o)
 
word_t croc_pushThread (CrocThread *t, CrocThread *o)
 
int croc_getBool (CrocThread *t, word_t slot)
 
crocint_t croc_getInt (CrocThread *t, word_t slot)
 
crocfloat_t croc_getFloat (CrocThread *t, word_t slot)
 
crocchar_t croc_getChar (CrocThread *t, word_t slot)
 
const char * croc_getString (CrocThread *t, word_t slot)
 
const char * croc_getStringn (CrocThread *t, word_t slot, uword_t *len)
 
void * croc_getNativeobj (CrocThread *t, word_t slot)
 
CrocThreadcroc_getThread (CrocThread *t, word_t slot)
 

Detailed Description

Pushing and getting the Croc value types.

Macro Definition Documentation

#define croc_getNum   croc_asFloat

An alias for croc_asFloat.

Function Documentation

word_t croc_pushNull ( CrocThread t)

Pushes a null onto the stack.

Returns
the stack index of the pushed value.
word_t croc_pushBool ( CrocThread t,
int  v 
)

Pushes a bool onto the stack with the given truth value.

Returns
the stack index of the pushed value.
word_t croc_pushInt ( CrocThread t,
crocint_t  v 
)

Pushes an int onto the stack with the given value.

Returns
the stack index of the pushed value.
word_t croc_pushFloat ( CrocThread t,
crocfloat_t  v 
)

Pushes a float onto the stack with the given value.

Returns
the stack index of the pushed value.
word_t croc_pushString ( CrocThread t,
const char *  v 
)

Pushes a string onto the stack with the given value.

The string length will be determined with strlen. Croc makes its own copy of string data, so you don't need to keep the data you passed around.

The string must be valid UTF-8, or an exception will be thrown. (ASCII is valid UTF-8 by design.)

Returns
the stack index of the pushed value.
word_t croc_pushStringn ( CrocThread t,
const char *  v,
uword_t  len 
)

Just like croc_pushString, but you give the length of the string instead of having it determined with strlen.

Parameters
vis a pointer to the string.
lenis the length of the string data in bytes.
Returns
the stack index of the pushed value.
int croc_tryPushString ( CrocThread t,
const char *  v 
)

Similar to croc_pushString, but instead of throwing an exception for invalid text encoding, returns a boolean value indicating whether or not pushing was successful.

This way you can deallocate buffers if the string is not valid and throw your own exception (or handle it however).

Returns
nonzero if the string was successfully pushed, or 0 if it failed (in which case the stack is unchanged).
int croc_tryPushStringn ( CrocThread t,
const char *  v,
uword_t  len 
)

Just like croc_tryPushString, but you give the length of the string instead of having it determined with strlen.

Parameters
vis a pointer to the string.
lenis the length of the string data in bytes.
Returns
nonzero if the string was successfully pushed, or 0 if it failed (in which case the stack is unchanged).
word_t croc_pushChar ( CrocThread t,
crocchar_t  c 
)

Pushes a one-codepoint-long string which contains the given codepoint c.

Returns
the stack index of the pushed value.
word_t croc_pushFormat ( CrocThread t,
const char *  fmt,
  ... 
)

Pushes a formatted string onto the stack.

This uses vsnprintf internally, so it uses the same formatting specifiers as the printf family. You don't have to worry about dealing with the length of the output string or anything; it'll be automatically determined for you.

Returns
the stack index of the pushed value.
word_t croc_vpushFormat ( CrocThread t,
const char *  fmt,
va_list  args 
)

Same as croc_pushFormat, but takes a va_list instead of variadic arguments.

word_t croc_pushNativeobj ( CrocThread t,
void *  o 
)

Pushes a nativeobj onto the stack.

Returns
the stack index of the pushed value.
word_t croc_pushThread ( CrocThread t,
CrocThread o 
)

Pushes a thread o onto t's stack.

It is an error if the threads belong to different VMs.

Returns
the stack index of the pushed value.
int croc_getBool ( CrocThread t,
word_t  slot 
)
Returns
the bool at the given slot (1 for true, 0 for false), or throws an exception if it isn't one.
crocint_t croc_getInt ( CrocThread t,
word_t  slot 
)
Returns
the int at the given slot, or throws an exception if it isn't one.
crocfloat_t croc_getFloat ( CrocThread t,
word_t  slot 
)
Returns
the float at the given slot, or throws an exception if it isn't one.
crocchar_t croc_getChar ( CrocThread t,
word_t  slot 
)

If the given slot contains a string, and that string is exactly one codepoint long, returns the codepoint.

Otherwise, throws an exception.

const char* croc_getString ( CrocThread t,
word_t  slot 
)
Returns
a pointer to the string in the given slot, or throws an exception if it isn't one.

Since Croc strings can contain embedded NUL (codepoint 0) characters, you may be missing the entire string's data with this function. If you really need the whole string, use croc_getStringn.

The string returned from this points into Croc's memory. Do not modify this string, and do not store the pointer unless you know it won't be collected!

const char* croc_getStringn ( CrocThread t,
word_t  slot,
uword_t len 
)

Like croc_getString, but returns the length of the string in bytes through the len parameter.

The string returned from this points into Croc's memory. Do not modify this string, and do not store the pointer unless you know it won't be collected!

void* croc_getNativeobj ( CrocThread t,
word_t  slot 
)
Returns
the nativeobj at the given slot, or throws an exception if it isn't one.
CrocThread* croc_getThread ( CrocThread t,
word_t  slot 
)
Returns
the thread at the given slot, or throws an exception if it isn't one.

The pointer returned from this points into Croc's memory. Do not modify it, and do not store the pointer unless you know it won't be collected! The only thread that will never be collected is the VM's main thread. If you want to keep a thread around, use the native reference mechanism (croc_ref_create).