This section covers functions used to push values onto the Tea value stack from C. These are the primary means of passing data from C code into Teascript, whether as arguments to functions, fields in tables, or standalone values. All push functions increment the stack top and may raise a memory error if allocation fails.
tea_push_nil()
void tea_push_nil(tea_State* T);
......nilPushes a nil value onto the stack.
Arguments
T: Teascript state
Example
tea_push_nil(T); /* [nil] */
See Also
tea_push_bool, tea_push_number
tea_push_true()
void tea_push_true(tea_State* T);
......truePushes the boolean value true (1 in C) onto the stack. Equivalent to calling tea_push_bool(T, true).
Arguments
T: Teascript state
Example
tea_push_true(T);
See Also
tea_push_false, tea_push_bool
tea_push_false()
void tea_push_false(tea_State* T);
......falsePushes the boolean value false (0 in C) onto the stack. Equivalent to calling tea_push_bool(T, false);
Arguments
T: Teascript state
Example
tea_push_false(T); /* [false] */
See Also
tea_push_true, tea_push_bool
tea_push_bool()
void tea_push_bool(tea_State* T, bool b);
......true......falsePushes a boolean value onto the stack. If b is non-zero, pushes true; otherwise pushes false.
Arguments
T: Teascript stateb: The boolean value to push
Example
tea_push_bool(T, false); /* [false] */
tea_push_bool(T, true); /* [false, true] */
See Also
tea_push_true, tea_push_false
tea_push_number()
void tea_push_number(tea_State* T, tea_Number n);
......numPushes a number (IEEE double) onto the stack.
If n is a NaN, it may be normalized into another NaN form.
Arguments
T: Teascript staten: The number to push
Example
tea_push_number(T, 547.98);
See Also
tea_push_integer
tea_push_integer()
void tea_push_integer(tea_State* T, tea_Integer n);
......integerPushes an integer value onto the stack. The integer is stored internally as a double, so large integers may lose precision.
Arguments
T: Teascript staten: The integer value to push
Example
tea_push_integer(T, 987);
See Also
tea_push_number
tea_push_pointer()
void tea_push_pointer(tea_State* T, void* p);
......ptrPushes a raw C pointer onto the stack. The pointer is not managed by the garbage collector and must be kept alive by the caller.
Arguments
T: Teascript statep: The pointer to push
Example
int data = 42;
tea_push_pointer(T, &data); /* [ptr] */
See Also
tea_push_userdata, tea_new_userdata
tea_push_lstring()
const char* tea_push_lstring(tea_State* T, const char* s, size_t len);
......strPushes a string of explicit length len onto the stack. Unlike tea_push_string(), this function can handle strings containing embedded NUL characters.
Arguments
T: Teascript states: Pointer to the string datalen: Length of the string in bytes
Returns
A pointer to the interned string data on the stack.
Example
const char* str = "foo\0bar";
tea_push_lstring(T, str, 7); /* ["foo\0bar"] */
See Also
tea_push_string, tea_push_fstring
tea_push_string()
const char* tea_push_string(tea_State* T, const char* s);
......strPushes a NUL-terminated C string onto the stack. The string length is determined automatically using a strlen equivalent. A pointer to the interned string data is returned.
If input string might contain internal NUL characters, use tea_push_lstring() instead.
Arguments
T: Teascript states: Pointer to the NUL-terminated string
Returns
A pointer to the interned string data on the stack.
Example
tea_push_string(T, "foo"); /* ["foo"] */
tea_push_string(T, "foo\0bar"); /* ["foo", "foo"] */
tea_push_string(T, ""); /* ["foo", "foo", ""] */
See Also
tea_push_lstring, tea_push_fstring
tea_push_fstring()
const char* tea_push_fstring(tea_State* T, const char* fmt, ...);
......strPushes a formatted string onto the stack using a printf-style format string and variadic arguments. This is a convenience function that internally calls tea_push_vfstring.
Arguments
T: Teascript statefmt: Format string (limitedprintf-like syntax)...: Variable arguments corresponding to the format specifiers
Returns
A pointer to the interned string data on the stack.
Example
tea_push_fstring(T, "Hello, %s! You have %d messages.", "Alice", 5);
/* ["Hello, Alice! You have 5 messages."] */
See Also
tea_push_vfstring, tea_push_string
tea_push_vfstring()
const char* tea_push_vfstring(tea_State* T, const char* fmt, va_list args);
......strPushes a formatted string onto the stack using a printf-style format string and a va_list of arguments. This is the va_list variant of tea_push_fstring.
Arguments
T: Teascript statefmt: Format string (limitedprintf-like syntax)args: Ava_listcontaining the arguments
Returns
A pointer to the interned string data on the stack.
Example
void log_message(tea_State* T, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
tea_push_vfstring(T, fmt, args);
va_end(args);
/* ... use the string on the stack ... */
}
See Also
tea_push_fstring
tea_push_range()
void tea_push_range(tea_State* T, tea_Number start, tea_Number end, tea_Number step);
......rangePushes a range object onto the stack. A range represents a numeric sequence defined by a start value, an end value, and a step increment.
Arguments
T: Teascript statestart: The starting value of the rangeend: The ending value of the rangestep: The step increment between values
Example
tea_push_range(T, 1, 10, 2); /* [1..10 step 2] */
tea_push_range(T, 0, 1, 0.1); /* [0..1 step 0.1] */
See Also
tea_get_range, tea_check_range
tea_push_cclosure()
void tea_push_cclosure(tea_State* T, tea_CFunction fn, int nupvalues, int nargs, int nopts);
...upval1...upvalN...cclosurePushes a new C closure onto the stack. A C closure is a C function together with a set of upvalues. The nupvalues values at the top of the stack are all popped and stored as the closure’s upvalues.
The nargs parameter specifies the number of required arguments, and nopts specifies the number of optional arguments. Use TEA_VARG for variadic functions.
Arguments
T: Teascript statefn: The C function to be called the closure is invokednupvalues: Number of upvalues to pop from the top of the stacknargs: Number of required argumentsnopts: Number of optional arguments, orTEA_VARGfor variadic
Example
tea_push_integer(T, 100); /* upvalue 1 */
tea_push_integer(T, 200); /* upvalue 2 */
tea_push_cclosure(T, my_func, 2, 1, 0);
/* [cclosure] with upvalues [100, 200] */
See Also
tea_push_cfunction, tea_set_funcs
tea_push_cfunction()
void tea_push_cfunction(tea_State* T, tea_CFunction fn, int nargs, int nopts);
......cfunctionPushes a new C function onto the stack. This is a convenience function equivalent to calling tea_push_cclosure with zero upvalues.
The nargs parameter specifies the number of required arguments, and nopts specifies the number of optional arguments. Use TEA_VARG for variadic functions.
Arguments
T: Teascript statefn: The C function to pushnargs: Number of required argumentsnopts: Number of optional arguments, orTEA_VARGfor variadic
Example
tea_push_cfunction(T, my_func, 2, 1); /* [cfunction] */
See Also
tea_push_cclosure, tea_set_funcs