Stack reference or primative value. More...
#include <value.h>
Public Member Functions | |
void | krk_printValueSafe (FILE *f, KrkValue value) |
Print a value without calling the VM. More... | |
int | krk_hashValue (KrkValue value, uint32_t *hashOut) |
Calculate the hash for a value. More... | |
int | krk_valuesEqual (KrkValue a, KrkValue b) |
Compare two values for equality. More... | |
static int | krk_valuesSame (KrkValue a, KrkValue b) |
Compare two values by identity. More... | |
int | krk_valuesSameOrEqual (KrkValue a, KrkValue b) |
Compare two values by identity, then by equality. More... | |
const char * | krk_typeName (KrkValue value) |
Get the name of the type of a value. More... | |
KrkClass * | krk_getType (KrkValue value) |
Get the class representing a value. More... | |
int | krk_isInstanceOf (KrkValue obj, const KrkClass *type) |
Determine if a class is an instance or subclass of a given type. More... | |
int | krk_callValue (KrkValue callee, int argCount, int callableOnStack) |
Call a callable value in the current stack context. More... | |
int | krk_isFalsey (KrkValue value) |
Determine the truth of a value. More... | |
KrkValue | krk_valueGetAttribute (KrkValue value, char *name) |
Obtain a property of an object by name. More... | |
KrkValue | krk_valueSetAttribute (KrkValue owner, char *name, KrkValue to) |
Set a property of an object by name. More... | |
KrkValue | krk_valueDelAttribute (KrkValue owner, char *name) |
Delete a property of an object by name. More... | |
Detailed Description
Stack reference or primative value.
This type stores a stack reference to an object, or the contents of a primitive type. Each VM thread's stack consists of an array of these values, and they are generally passed around in the VM through direct copying rather than as pointers, avoiding the need to track memory used by them.
Implemented through basic NaN-boxing where the top sixteen bits are used as a tag (KrkValueType) and the lower 32 or 48 bits contain the various primitive types.
Member Function Documentation
◆ krk_callValue()
int krk_callValue | ( | KrkValue | callee, |
int | argCount, | ||
int | callableOnStack | ||
) |
Call a callable value in the current stack context.
Executes the given callable object (function, bound method, object with a __call__()
method, etc.) using argCount
arguments from the stack.
- Parameters
-
callee Value referencing a callable object. argCount Arguments to retreive from stack. callableOnStack Whether callee
is on the stack below the arguments, which must be the case for bound methods, classes, and instances, as that space will be used for the implicit first argument passed to these kinds of callables.
- Returns
- An indicator of how the result should be obtained: 1: The VM must be resumed to run managed code. 2: The callable was a native function and result should be popped now. Else: The call failed. An exception may have already been set.
◆ krk_getType()
Get the class representing a value.
Returns the class object representing the type of a value. This may be the direct class of an instance, or a pseudoclass for other value types.
- Parameters
-
value Reference value to examine.
- Returns
- A pointer to the value's type's class object.
◆ krk_hashValue()
int krk_hashValue | ( | KrkValue | value, |
uint32_t * | hashOut | ||
) |
◆ krk_isFalsey()
int krk_isFalsey | ( | KrkValue | value | ) |
Determine the truth of a value.
Determines if a value represents a "falsey" value. Empty collections, 0-length strings, False, numeric 0, None, etc. are "falsey". Other values are generally "truthy".
- Parameters
-
value Value to examine.
- Returns
- 1 if falsey, 0 if truthy
◆ krk_isInstanceOf()
Determine if a class is an instance or subclass of a given type.
Searches the class hierarchy of the given value to determine if it is a subtype of type
. As this chains through the inheritence tree, the more deeply subclassed obj
is, the longer it may take to determine if it is a subtype of type
. All types should eventually be subtypes of the object type, so this condition should not be checked. For some types, convenience macros are available. If you need to check if obj
is a specific type, exclusive of subtypes, look at krk_getType()
instead of using this function.
- Parameters
-
obj Value to examine. type Class object to test for membership of.
- Returns
- 1 if
obj
is an instance oftype
or of a subclass oftype
◆ krk_printValueSafe()
void krk_printValueSafe | ( | FILE * | f, |
KrkValue | value | ||
) |
Print a value without calling the VM.
Print a string representation of 'value' to the stream 'f', avoiding calls to managed code by using simplified representations where necessary. This is intended for use in debugging code, such as during disassembly, or when printing values in an untrusted context.
- Note
- This function will truncate long strings and print them in a form closer to the 'repr()' representation, with escaped bytes, rather than directly printing them to the stream.
- Parameters
-
f Stream to write to. value Value to display.
◆ krk_typeName()
const char * krk_typeName | ( | KrkValue | value | ) |
Get the name of the type of a value.
Obtains the C string representing the name of the class associated with the given value. Useful for crafting exception messages, such as those describing TypeErrors.
- Parameters
-
value Value to examine
- Returns
- Nul-terminated C string of the type of
value
◆ krk_valueDelAttribute()
Delete a property of an object by name.
This is a convenience function that works in essentially the same way as the OP_DEL_PROPERTY instruction.
- Warning
- As this function takes a C string, it will not work with
- attribute names that have nil bytes.
- Parameters
-
owner The owner of the property to delete. name C-string of the property name to delete.
◆ krk_valueGetAttribute()
Obtain a property of an object by name.
This is a convenience function that works in essentially the same way as the OP_GET_PROPERTY instruction.
- Warning
- As this function takes a C string, it will not work with
- attribute names that have nil bytes.
- Parameters
-
value Value to examine. name C-string of the property name to query.
- Returns
- The requested property, or None with an AttributeError exception set in the current thread if the attribute was not found.
◆ krk_valuesEqual()
◆ krk_valueSetAttribute()
Set a property of an object by name.
This is a convenience function that works in essentially the same way as the OP_SET_PROPERTY instruction.
- Warning
- As this function takes a C string, it will not work with
- attribute names that have nil bytes.
- Parameters
-
owner The owner of the property to modify. name C-string of the property name to modify. to The value to assign.
- Returns
- The set value, or None with an AttributeError exception set in the current thread if the object can not have a property set.
◆ krk_valuesSame()
Compare two values by identity.
Performs a strict comparison between two values, comparing their identities. For primitive values, this is generally the same as comparing by equality. For objects, this compares pointer values directly.
- Returns
- 1 if values represent the same object or value, 0 otherwise.
◆ krk_valuesSameOrEqual()
The documentation for this struct was generated from the following files: