Functions for reading and writing global variables and module variables. Globals live in the state’s global table; module variables live in the exports table of a named module. These functions are shortcuts for reaching those tables without pushing the keys by hand.

tea_get_global

Signature

bool tea_get_global(tea_State* T, const char* name);
[-0, +(1|0), -]

Description

...
...value

Pushes the value of the global variable name. Returns true if the global exists and was pushed. If it does not, returns false and pushes nothing.

Parameters

T
Teascript state
name
Name of the global variable

Returns

true if the global exists and its value was pushed; false otherwise.

Example

tea_push_integer(T, 42);
tea_set_global(T, "answer");              /* globals.answer = 42 */

bool ok = tea_get_global(T, "answer");    /* ok = true, pushes 42 */
tea_pop(T, 1);
ok = tea_get_global(T, "missing");        /* ok = false, nothing pushed */

See also

tea_set_global, tea_get_var

tea_set_global

Signature

void tea_set_global(tea_State* T, const char* name);
[-1, +0, m]

Description

...value
...

Pops the value on top of the stack and assigns it to the global variable name, creating the global or overwriting it.

Parameters

T
Teascript state
name
Name of the global variable

Errors

May raise a memory error.

Example

tea_push_string(T, "hello");
tea_set_global(T, "greeting");   /* globals.greeting = "hello" */

tea_push_integer(T, 7);
tea_set_global(T, "count");      /* globals.count = 7 */

See also

tea_get_global, tea_set_var

tea_get_var

Signature

bool tea_get_var(tea_State* T, const char* name, const char* var);
[-0, +(1|0), -]

Description

...
...value

Pushes the variable var from the exports table of the module called name. Returns true if the module exists and has the variable. Otherwise returns false and pushes nothing.

Parameters

T
Teascript state
name
Name of the module that owns the variable
var
Name of the exported variable

Returns

true if the module and the variable exist and the value was pushed; false otherwise.

Example

/* assume the module "mathx" is loaded and exports "pi" */
bool ok = tea_get_var(T, "mathx", "pi");    /* ok = true, pushes 3.14159... */
tea_pop(T, 1);
ok = tea_get_var(T, "mathx", "tau");        /* ok = false, nothing pushed */

See also

tea_set_var, tea_get_global, tea_import

tea_set_var

Signature

void tea_set_var(tea_State* T, const char* name, const char* var);
[-1, +0, m]

Description

...value
...

Pops the value on top of the stack and assigns it to the variable var in the exports table of the module called name, creating the entry or overwriting it.

Parameters

T
Teascript state
name
Name of the module that owns the variable
var
Name of the exported variable

Errors

May raise a memory error.

Example

/* assume the module "config" exists */
tea_push_bool(T, true);
tea_set_var(T, "config", "debug");       /* config.debug = true */

tea_push_string(T, "1.0.0");
tea_set_var(T, "config", "version");     /* config.version = "1.0.0" */

See also

tea_get_var, tea_set_global