KrkValue Struct Reference

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...
 
KrkClasskrk_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
calleeValue referencing a callable object.
argCountArguments to retreive from stack.
callableOnStackWhether 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.

Definition at line 691 of file vm.c.

◆ krk_getType()

KrkClass * krk_getType ( KrkValue  value)

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
valueReference value to examine.
Returns
A pointer to the value's type's class object.

Definition at line 240 of file vm.c.

◆ krk_hashValue()

int krk_hashValue ( KrkValue  value,
uint32_t *  hashOut 
)

Calculate the hash for a value.

Retreives or calculates the hash value for 'value'.

Parameters
valueValue to hash.
*hashOutAn unsigned 32-bit hash value.
Returns
Status code 0 for success, 1 for unhashable type.

Definition at line 47 of file table.c.

◆ 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
valueValue to examine.
Returns
1 if falsey, 0 if truthy

Definition at line 821 of file vm.c.

◆ krk_isInstanceOf()

int krk_isInstanceOf ( KrkValue  obj,
const KrkClass type 
)

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
objValue to examine.
typeClass object to test for membership of.
Returns
1 if obj is an instance of type or of a subclass of type

Definition at line 282 of file vm.c.

◆ 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
fStream to write to.
valueValue to display.

Definition at line 21 of file debug.c.

◆ 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
valueValue to examine
Returns
Nul-terminated C string of the type of value

Definition at line 984 of file vm.c.

◆ krk_valueDelAttribute()

KrkValue krk_valueDelAttribute ( KrkValue  owner,
char *  name 
)

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
ownerThe owner of the property to delete.
nameC-string of the property name to delete.

Definition at line 1829 of file vm.c.

◆ krk_valueGetAttribute()

KrkValue krk_valueGetAttribute ( KrkValue  value,
char *  name 
)

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
valueValue to examine.
nameC-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.

Definition at line 1769 of file vm.c.

◆ krk_valuesEqual()

int krk_valuesEqual ( KrkValue  a,
KrkValue  b 
)

Compare two values for equality.

Performs a relaxed equality comparison between two values, check for equivalence by contents. This may call managed code to run eq methods.

Returns
1 if values are equivalent, 0 otherwise.

Definition at line 106 of file value.c.

◆ krk_valueSetAttribute()

KrkValue krk_valueSetAttribute ( KrkValue  owner,
char *  name,
KrkValue  to 
)

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
ownerThe owner of the property to modify.
nameC-string of the property name to modify.
toThe 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.

Definition at line 1907 of file vm.c.

◆ krk_valuesSame()

static int krk_valuesSame ( KrkValue  a,
KrkValue  b 
)
inline

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.

Definition at line 141 of file value.h.

◆ krk_valuesSameOrEqual()

int krk_valuesSameOrEqual ( KrkValue  a,
KrkValue  b 
)

Compare two values by identity, then by equality.

More efficient than just krk_valuesSame followed by krk_valuesEqual

Returns
1 if values represent the same object or value, 0 otherwise.

Definition at line 96 of file value.c.


The documentation for this struct was generated from the following files: