Functions for accessing the attributes of an object (
obj.key) and for indexing it (obj[key]). Unlike the Field Access functions, which work on the map directly, these respect the object’s special methods: they may call getters, setters and the [] and []= operator methods, so they can run Teascript code and raise errors.tea_has_attr
Signature
bool tea_has_attr(tea_State* T, int obj, const char* key);Description
Checks whether the object at
obj has an attribute named key.Parameters
T- Teascript state
obj- Stack index of the object
key- Name of the attribute to look up
Returns
true if the attribute exists; false otherwise.
Example
tea_new_module(T, "mymod");
tea_push_integer(T, 42);
tea_set_attr(T, -2, "answer");
bool has = tea_has_attr(T, -1, "answer"); /* true */
has = tea_has_attr(T, -1, "missing"); /* false */
See also
tea_get_attr
Signature
void tea_get_attr(tea_State* T, int obj, const char* key);Description
Pushes the value of the attribute
key of the object at obj. This respects the object’s special methods, so it may call a getter.Parameters
T- Teascript state
obj- Stack index of the object
key- Name of the attribute to retrieve
Example
tea_new_module(T, "mymod");
tea_push_integer(T, 7);
tea_set_attr(T, -2, "version");
tea_get_attr(T, -1, "version"); /* pushes 7 */
See also
tea_set_attr
Signature
void tea_set_attr(tea_State* T, int obj, const char* key);Description
Pops the value on top of the stack and assigns it to the attribute
key of the object at obj. This respects the object’s special methods, so it may call a setter, or have no effect if the attribute is read-only.Parameters
T- Teascript state
obj- Stack index of the object
key- Name of the attribute to set
Example
tea_new_module(T, "mymod"); /* [module] */
tea_push_integer(T, 1);
tea_set_attr(T, -2, "version"); /* module.version = 1 */
tea_push_string(T, "beta");
tea_set_attr(T, -2, "channel"); /* module.channel = "beta" */
See also
tea_delete_attr
Signature
void tea_delete_attr(tea_State* T, int obj, const char* key);Description
Deletes the attribute
key from the object at obj.Parameters
T- Teascript state
obj- Stack index of the object
key- Name of the attribute to delete
Example
tea_new_module(T, "mymod");
tea_push_integer(T, 1);
tea_set_attr(T, -2, "version");
tea_delete_attr(T, -1, "version"); /* module.version is removed */
See also
tea_get_index
Signature
void tea_get_index(tea_State* T, int obj);Description
Pops a key from the top of the stack and pushes the result of indexing the object at
obj with it, using the [] operator method if there is one. It is the equivalent of obj[key] in Teascript.Parameters
T- Teascript state
obj- Stack index of the object to index
Example
tea_new_map(T); /* [map] */
tea_push_string(T, "Tea");
tea_set_key(T, -2, "name"); /* map["name"] = "Tea" */
tea_push_string(T, "name"); /* [map, "name"] */
tea_get_index(T, -2); /* [map, "Tea"] */
See also
tea_set_index
Signature
void tea_set_index(tea_State* T, int obj);Description
Pops a value and a key from the stack and performs
obj[key] = value on the object at obj, using the []= operator method if there is one. The value is on top, with the key just below it.Parameters
T- Teascript state
obj- Stack index of the object to index
Example
tea_new_map(T); /* [map] */
tea_push_string(T, "lang");
tea_push_string(T, "Tea");
tea_set_index(T, -3); /* map["lang"] = "Tea" */