12 #ifndef KRK_DISABLE_THREADS
48 #define KRK_OBJ_FLAGS_STRING_MASK 0x0003
49 #define KRK_OBJ_FLAGS_STRING_ASCII 0x0000
50 #define KRK_OBJ_FLAGS_STRING_UCS1 0x0001
51 #define KRK_OBJ_FLAGS_STRING_UCS2 0x0002
52 #define KRK_OBJ_FLAGS_STRING_UCS4 0x0003
54 #define KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS 0x0001
55 #define KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS 0x0002
56 #define KRK_OBJ_FLAGS_CODEOBJECT_IS_GENERATOR 0x0004
57 #define KRK_OBJ_FLAGS_CODEOBJECT_IS_COROUTINE 0x0008
59 #define KRK_OBJ_FLAGS_FUNCTION_MASK 0x0003
60 #define KRK_OBJ_FLAGS_FUNCTION_IS_CLASS_METHOD 0x0001
61 #define KRK_OBJ_FLAGS_FUNCTION_IS_STATIC_METHOD 0x0002
63 #define KRK_OBJ_FLAGS_NO_INHERIT 0x0200
64 #define KRK_OBJ_FLAGS_SECOND_CHANCE 0x0100
65 #define KRK_OBJ_FLAGS_IS_MARKED 0x0010
66 #define KRK_OBJ_FLAGS_IN_REPR 0x0020
67 #define KRK_OBJ_FLAGS_IMMORTAL 0x0040
68 #define KRK_OBJ_FLAGS_VALID_HASH 0x0080
142 uint32_t bytecodeOffset;
206 typedef void (*KrkCleanupCallback)(
struct KrkInstance *);
247 KrkObj * _add, * _radd, * _iadd;
248 KrkObj * _sub, * _rsub, * _isub;
249 KrkObj * _mul, * _rmul, * _imul;
250 KrkObj * _or, * _ror, * _ior;
251 KrkObj * _xor, * _rxor, * _ixor;
252 KrkObj * _and, * _rand, * _iand;
253 KrkObj * _mod, * _rmod, * _imod;
254 KrkObj * _pow, * _rpow, * _ipow;
255 KrkObj * _lshift, * _rlshift, * _ilshift;
256 KrkObj * _rshift, * _rrshift, * _irshift;
257 KrkObj * _truediv, * _rtruediv, * _itruediv;
258 KrkObj * _floordiv, * _rfloordiv, * _ifloordiv;
260 KrkObj * _lt, * _gt, * _le, * _ge;
261 KrkObj * _invert, * _negate;
263 KrkObj * _matmul, * _rmatmul, * _imatmul;
301 typedef KrkValue (*NativeFn)(
int argCount,
const KrkValue* args,
int hasKwargs);
338 #ifndef KRK_DISABLE_THREADS
339 pthread_rwlock_t rwlock;
387 #ifndef KRK_STATIC_ONLY
388 krk_dlRefType libHandle;
621 #define krk_isObjType(v,t) (IS_OBJECT(v) && (AS_OBJECT(v)->type == (t)))
622 #define OBJECT_TYPE(value) (AS_OBJECT(value)->type)
623 #define IS_STRING(value) krk_isObjType(value, KRK_OBJ_STRING)
624 #define AS_STRING(value) ((KrkString *)AS_OBJECT(value))
625 #define AS_CSTRING(value) (((KrkString *)AS_OBJECT(value))->chars)
626 #define IS_BYTES(value) krk_isObjType(value, KRK_OBJ_BYTES)
627 #define AS_BYTES(value) ((KrkBytes*)AS_OBJECT(value))
628 #define IS_NATIVE(value) krk_isObjType(value, KRK_OBJ_NATIVE)
629 #define AS_NATIVE(value) ((KrkNative *)AS_OBJECT(value))
630 #define IS_CLOSURE(value) krk_isObjType(value, KRK_OBJ_CLOSURE)
631 #define AS_CLOSURE(value) ((KrkClosure *)AS_OBJECT(value))
632 #define IS_CLASS(value) krk_isObjType(value, KRK_OBJ_CLASS)
633 #define AS_CLASS(value) ((KrkClass *)AS_OBJECT(value))
634 #define IS_INSTANCE(value) krk_isObjType(value, KRK_OBJ_INSTANCE)
635 #define AS_INSTANCE(value) ((KrkInstance *)AS_OBJECT(value))
636 #define IS_BOUND_METHOD(value) krk_isObjType(value, KRK_OBJ_BOUND_METHOD)
637 #define AS_BOUND_METHOD(value) ((KrkBoundMethod*)AS_OBJECT(value))
638 #define IS_TUPLE(value) krk_isObjType(value, KRK_OBJ_TUPLE)
639 #define AS_TUPLE(value) ((KrkTuple *)AS_OBJECT(value))
640 #define AS_LIST(value) (&((KrkList *)AS_OBJECT(value))->values)
641 #define AS_DICT(value) (&((KrkDict *)AS_OBJECT(value))->entries)
643 #define IS_codeobject(value) krk_isObjType(value, KRK_OBJ_CODEOBJECT)
644 #define AS_codeobject(value) ((KrkCodeObject *)AS_OBJECT(value))
Structures and enums for bytecode chunks.
Top-level header with configuration macros.
NativeFn krk_GenericAlias
Special value for type hint expressions.
struct KrkClass KrkClass
Type object.
struct KrkString KrkString
Immutable sequence of Unicode codepoints.
KrkStringType
String compact storage type.
struct KrkInstance KrkInstance
An object of a class.
struct KrkObj KrkObj
The most basic object type.
struct KrkUpvalue KrkUpvalue
Storage for values referenced from nested functions.
KrkObjType
Union tag for heap objects.
int krk_getAwaitable(void)
Calls await
A function that has been attached to an object to serve as a method.
KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj *method)
Create a new bound method.
KrkValue receiver
Object to pass as implicit first argument.
KrkObj * method
Function to call.
Immutable sequence of bytes.
size_t length
Length of data in bytes.
uint8_t * bytes
Pointer to separately-stored bytes data.
KrkBytes * krk_newBytes(size_t length, uint8_t *source)
Create a new byte array.
Opcode chunk of a code object.
KrkString * filename
Filename of the original source that defined the codeobject for the class.
KrkObj * _tostr
__str__ Called to produce a string from an instance
KrkCleanupCallback _ongcsweep
C function to call when the garbage collector is discarding an instance of this class.
KrkObj * _delitem
__delitem__ Called when del is used with a subscript
KrkObj * _descget
__get__ Called when a descriptor object is bound as a property
KrkClass * krk_newClass(KrkString *name, KrkClass *base)
Create a new class object.
KrkCleanupCallback _ongcscan
C function to call when the garbage collector visits an instance of this class in the scan phase.
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
KrkString * name
Name of the class.
struct KrkClass * base
Pointer to base class implementation.
KrkObj * _eq
__eq__ Implementation for equality check (==)
KrkTable subclasses
Set of classes that subclass this class.
KrkObj * _init
__init__ Implicitly called when an instance is created
KrkObj * _call
__call__ Called when an instance is called like a function
KrkObj * _len
__len__ Generally called by len() but may be used to determine truthiness
size_t allocSize
Size to allocate when creating instances of this class.
KrkObj * _exit
__exit__ Called upon exit from a with block
KrkObj * _getattr
__getattr__ Overrides normal behavior for attribute access
KrkObj * _enter
__enter__ Called upon entry into a with block
KrkObj * _iter
__iter__ Called by for ... in ..., etc.
KrkObj * _descset
__set__ Called when a descriptor object is assigned to as a property
KrkObj * _setter
__setitem__ Called when a subscripted instance is assigned to
KrkTable methods
General attributes table.
KrkObj * _dir
__dir__ Overrides normal behavior for dir()
struct KrkClass * _class
Metaclass.
KrkObj * _contains
__contains__ Called to resolve in (as a binary operator)
KrkObj * _classgetitem
__class_getitem__ Class method called when a type object is subscripted; used for type hints
KrkObj * _hash
__hash__ Called when an instance is a key in a dict or an entry in a set
KrkObj * _getter
__getitem__ Called when an instance is subscripted
KrkValue globalsOwner
Owner of the globals table for this function.
size_t upvalueCount
Number of entries in upvalues.
KrkTable * globalsTable
Pointer to globals table with owner object.
KrkClosure * krk_newClosure(KrkCodeObject *function, KrkValue globals)
Create a new function object.
KrkValue annotations
Dictionary of type hints.
KrkUpvalue ** upvalues
Array of upvalues collected from the surrounding context when the closure was created.
KrkInstance * krk_buildGenerator(KrkClosure *function, KrkValue *arguments, size_t argCount)
Convert a function into a generator with the given arguments.
KrkTable fields
Object attributes table.
unsigned short potentialPositionals
Precalculated positional arguments for complex argument processing.
size_t overlongJumpsCount
Number of entries in pessimal jump table.
KrkChunk chunk
Bytecode data.
size_t upvalueCount
Number of upvalues this function collects as a closure.
KrkValueArray positionalArgNames
Array of names for positional arguments (and *args)
KrkValue jumpTargets
Possibly a set of jump targets...
size_t localNameCapacity
Capacity of localNames.
KrkLocalEntry * localNames
Stores the names of local variables used in the function, for debugging.
unsigned short keywordArgs
Arity of keyword (default) arguments.
KrkOverlongJump * overlongJumps
Pessimal overlong jump container.
unsigned short requiredArgs
Arity of required (non-default) arguments.
KrkExpressionsMap * expressions
Mapping of bytecode offsets to expression spans for debugging.
KrkString * docstring
Docstring attached to the function.
size_t expressionsCapacity
Capacity of expressions.
size_t expressionsCount
Number of entries in expressions.
KrkValueArray keywordArgNames
Array of names for keyword-only arguments (and **kwargs)
KrkCodeObject * krk_newCodeObject(void)
Create a new, uninitialized code object.
size_t localNameCount
Number of entries in localNames.
KrkString * name
Name of the function.
size_t overlongJumpsCapacity
Number of possible entries in pessimal jump table.
unsigned short totalArguments
Total argument cells we can fill in complex argument processing.
KrkString * qualname
The dotted name of the function.
KrkTable entries
The actual table of values in the dict.
Map entry of opcode offsets to expressions spans.
KrkInstance * krk_newInstance(KrkClass *_class)
Create a new instance of the given class.
KrkTable fields
Attributes table.
KrkValueArray values
Stores the length, capacity, and actual values of the list.
Metadata on a local variable name in a function.
size_t birthday
Instruction offset that this local name became valid on.
KrkString * name
Name of the local.
size_t id
Local ID as used by opcodes; offset from the frame's stack base.
size_t deathday
Instruction offset that this local name becomes invalid on.
Representation of a loaded module.
Managed binding to a C function.
const char * doc
Docstring to supply from __doc__.
const char * name
Name to use when repring.
KrkNative * krk_newNative(NativeFn function, const char *name, int type)
Create a native function binding object.
The most basic object type.
uint32_t hash
Cached hash value for table keys.
uint16_t flags
General object flags, mostly related to garbage collection.
uint16_t type
Tag indicating core type.
struct KrkObj * next
Invasive linked list of all objects in the VM.
uint16_t intendedTarget
High bytes of the intended target.
uint8_t originalOpcode
Original jump opcode to execute.
uint32_t instructionOffset
Instruction (operand offset) this jump target applies to.
Immutable sequence of Unicode codepoints.
uint32_t krk_unicodeCodepoint(KrkString *string, size_t index)
Obtain the codepoint at a given index in a string.
void * krk_unicodeString(KrkString *string)
Ensure that a codepoint representation of a string is available.
KrkString * krk_copyString(const char *chars, size_t length)
Obtain a string object representation of the given C string.
KrkString * krk_takeString(char *chars, size_t length)
Yield ownership of a C string to the GC and obtain a string object.
size_t codesLength
String length in Unicode codepoints.
char * chars
UTF8 canonical data.
void * codes
Codepoint data.
size_t length
String length in bytes.
KrkString * krk_takeStringVetted(char *chars, size_t length, size_t codesLength, KrkStringType type, uint32_t hash)
Like krk_takeString but for when the caller has already calculated code lengths, hash,...
size_t krk_codepointToBytes(krk_integer_type value, unsigned char *out)
Convert an integer codepoint to a UTF-8 byte representation.
Simple hash table of arbitrary keys to values.
Execution state of a VM thread.
Immutable sequence of arbitrary values.
KrkValueArray values
Stores the length, capacity, and actual values of the tuple.
KrkTuple * krk_newTuple(size_t length)
Create a new tuple.
Storage for values referenced from nested functions.
int location
Stack offset or -1 if closed.
KrkValue closed
Heap storage for closed value.
struct KrkThreadState * owner
The thread that owns the stack this upvalue belongs in.
struct KrkUpvalue * next
Invasive linked list pointer to next upvalue.
KrkUpvalue * krk_newUpvalue(int slot)
Create an upvalue slot.
Flexible vector of stack references.
Stack reference or primative value.
Implementation of a generic hash table.
Definitions for primitive stack references.