object.h
Go to the documentation of this file.
1 #pragma once
6 #include <stdio.h>
7 #include "kuroko.h"
8 #include "value.h"
9 #include "chunk.h"
10 #include "table.h"
11 
12 #ifndef KRK_DISABLE_THREADS
13 #include <pthread.h>
14 #endif
15 
21 typedef enum {
22  KRK_OBJ_CODEOBJECT,
23  KRK_OBJ_NATIVE,
24  KRK_OBJ_CLOSURE,
25  KRK_OBJ_STRING,
26  KRK_OBJ_UPVALUE,
27  KRK_OBJ_CLASS,
28  KRK_OBJ_INSTANCE,
29  KRK_OBJ_BOUND_METHOD,
30  KRK_OBJ_TUPLE,
31  KRK_OBJ_BYTES,
32 } KrkObjType;
33 
34 #undef KrkObj
41 typedef struct KrkObj {
42  uint16_t type;
43  uint16_t flags;
44  uint32_t hash;
45  struct KrkObj * next;
47 
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
53 
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
58 
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
62 
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
69 
70 
81 typedef enum {
82  /* For compatibility */
83  KRK_STRING_ASCII = KRK_OBJ_FLAGS_STRING_ASCII,
84  KRK_STRING_UCS1 = KRK_OBJ_FLAGS_STRING_UCS1,
85  KRK_STRING_UCS2 = KRK_OBJ_FLAGS_STRING_UCS2,
86  KRK_STRING_UCS4 = KRK_OBJ_FLAGS_STRING_UCS4,
88 
93 typedef struct KrkString {
95  size_t length;
96  size_t codesLength;
97  char * chars;
98  void * codes;
100 
105 typedef struct {
107  size_t length;
108  uint8_t * bytes;
109 } KrkBytes;
110 
115 typedef struct KrkUpvalue {
117  int location;
119  struct KrkUpvalue * next;
122 
129 typedef struct {
130  size_t id;
131  size_t birthday;
132  size_t deathday;
134 } KrkLocalEntry;
135 
136 struct KrkInstance;
137 
144 typedef struct {
146  unsigned short requiredArgs;
147  unsigned short keywordArgs;
148  unsigned short potentialPositionals;
149  unsigned short totalArguments;
150  size_t upvalueCount;
157  size_t localNameCount;
160 } KrkCodeObject;
161 
162 
169 typedef struct {
171  KrkCodeObject * function;
173  size_t upvalueCount;
178 } KrkClosure;
179 
180 typedef void (*KrkCleanupCallback)(struct KrkInstance *);
181 
189 typedef struct KrkClass {
191  struct KrkClass * _class;
195  struct KrkClass * base;
196  size_t allocSize;
197  KrkCleanupCallback _ongcscan;
198  KrkCleanupCallback _ongcsweep;
221  KrkObj * _add, * _radd, * _iadd;
222  KrkObj * _sub, * _rsub, * _isub;
223  KrkObj * _mul, * _rmul, * _imul;
224  KrkObj * _or, * _ror, * _ior;
225  KrkObj * _xor, * _rxor, * _ixor;
226  KrkObj * _and, * _rand, * _iand;
227  KrkObj * _mod, * _rmod, * _imod;
228  KrkObj * _pow, * _rpow, * _ipow;
229  KrkObj * _lshift, * _rlshift, * _ilshift;
230  KrkObj * _rshift, * _rrshift, * _irshift;
231  KrkObj * _truediv, * _rtruediv, * _itruediv;
232  KrkObj * _floordiv, * _rfloordiv, * _ifloordiv;
233 
234  KrkObj * _lt, * _gt, * _le, * _ge;
235  KrkObj * _invert, * _negate;
236  KrkObj * _set_name;
237  KrkObj * _matmul, * _rmatmul, * _imatmul;
238  KrkObj * _pos;
239  KrkObj * _setattr;
240  KrkObj * _format;
241  KrkObj * _new;
242  KrkObj * _bool;
243 
244  size_t cacheIndex;
246 
255 typedef struct KrkInstance {
260 
269 typedef struct {
274 
275 typedef KrkValue (*NativeFn)(int argCount, const KrkValue* args, int hasKwargs);
276 
283 typedef struct {
285  NativeFn function;
286  const char * name;
287  const char * doc;
288 } KrkNative;
289 
297 typedef struct {
300 } KrkTuple;
301 
309 typedef struct {
312 #ifndef KRK_DISABLE_THREADS
313  pthread_rwlock_t rwlock;
314 #endif
315 } KrkList;
316 
323 typedef struct {
326 } KrkDict;
327 
331 struct DictItems {
332  KrkInstance inst;
333  KrkValue dict;
334  size_t i;
335 };
336 
340 struct DictKeys {
341  KrkInstance inst;
342  KrkValue dict;
343  size_t i;
344 };
345 
349 struct DictValues {
350  KrkInstance inst;
351  KrkValue dict;
352  size_t i;
353 };
354 
359 struct KrkModule {
360  KrkInstance inst;
361 #ifndef KRK_STATIC_ONLY
362  dlRefType libHandle;
363 #endif
364 };
365 
369 struct KrkSlice {
370  KrkInstance inst;
371  KrkValue start;
372  KrkValue end;
373  KrkValue step;
374 };
375 
393 extern KrkString * krk_takeString(char * chars, size_t length);
394 
411 extern KrkString * krk_takeStringVetted(char * chars, size_t length, size_t codesLength, KrkStringType type, uint32_t hash);
412 
428 extern KrkString * krk_copyString(const char * chars, size_t length);
429 
442 extern void * krk_unicodeString(KrkString * string);
443 
460 extern uint32_t krk_unicodeCodepoint(KrkString * string, size_t index);
461 
473 extern size_t krk_codepointToBytes(krk_integer_type value, unsigned char * out);
474 
487 extern KrkInstance * krk_buildGenerator(KrkClosure * function, KrkValue * arguments, size_t argCount);
488 
492 extern int krk_getAwaitable(void);
493 
500 extern NativeFn krk_GenericAlias;
501 
510 extern KrkCodeObject * krk_newCodeObject(void);
511 
520 extern KrkNative * krk_newNative(NativeFn function, const char * name, int type);
521 
533 extern KrkClosure * krk_newClosure(KrkCodeObject * function, KrkValue globals);
534 
544 extern KrkUpvalue * krk_newUpvalue(int slot);
545 
554 extern KrkClass * krk_newClass(KrkString * name, KrkClass * base);
555 
564 extern KrkInstance * krk_newInstance(KrkClass * _class);
565 
574 extern KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj * method);
575 
584 extern KrkTuple * krk_newTuple(size_t length);
585 
593 extern KrkBytes * krk_newBytes(size_t length, uint8_t * source);
594 
595 #define krk_isObjType(v,t) (IS_OBJECT(v) && (AS_OBJECT(v)->type == (t)))
596 #define OBJECT_TYPE(value) (AS_OBJECT(value)->type)
597 #define IS_STRING(value) krk_isObjType(value, KRK_OBJ_STRING)
598 #define AS_STRING(value) ((KrkString *)AS_OBJECT(value))
599 #define AS_CSTRING(value) (((KrkString *)AS_OBJECT(value))->chars)
600 #define IS_BYTES(value) krk_isObjType(value, KRK_OBJ_BYTES)
601 #define AS_BYTES(value) ((KrkBytes*)AS_OBJECT(value))
602 #define IS_NATIVE(value) krk_isObjType(value, KRK_OBJ_NATIVE)
603 #define AS_NATIVE(value) ((KrkNative *)AS_OBJECT(value))
604 #define IS_CLOSURE(value) krk_isObjType(value, KRK_OBJ_CLOSURE)
605 #define AS_CLOSURE(value) ((KrkClosure *)AS_OBJECT(value))
606 #define IS_CLASS(value) krk_isObjType(value, KRK_OBJ_CLASS)
607 #define AS_CLASS(value) ((KrkClass *)AS_OBJECT(value))
608 #define IS_INSTANCE(value) krk_isObjType(value, KRK_OBJ_INSTANCE)
609 #define AS_INSTANCE(value) ((KrkInstance *)AS_OBJECT(value))
610 #define IS_BOUND_METHOD(value) krk_isObjType(value, KRK_OBJ_BOUND_METHOD)
611 #define AS_BOUND_METHOD(value) ((KrkBoundMethod*)AS_OBJECT(value))
612 #define IS_TUPLE(value) krk_isObjType(value, KRK_OBJ_TUPLE)
613 #define AS_TUPLE(value) ((KrkTuple *)AS_OBJECT(value))
614 #define AS_LIST(value) (&((KrkList *)AS_OBJECT(value))->values)
615 #define AS_DICT(value) (&((KrkDict *)AS_OBJECT(value))->entries)
616 
617 #define IS_codeobject(value) krk_isObjType(value, KRK_OBJ_CODEOBJECT)
618 #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.
Definition: obj_typing.c:67
struct KrkClass KrkClass
Type object.
struct KrkString KrkString
Immutable sequence of Unicode codepoints.
KrkStringType
String compact storage type.
Definition: object.h:81
@ KRK_STRING_UCS2
Definition: object.h:85
@ KRK_STRING_UCS4
Definition: object.h:86
@ KRK_STRING_UCS1
Definition: object.h:84
@ KRK_STRING_ASCII
Definition: object.h:83
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.
Definition: object.h:21
int krk_getAwaitable(void)
Calls await
Definition: obj_gen.c:235
A function that has been attached to an object to serve as a method.
Definition: object.h:269
KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj *method)
Create a new bound method.
Definition: object.c:346
KrkValue receiver
Object to pass as implicit first argument.
Definition: object.h:271
KrkObj obj
Base.
Definition: object.h:270
KrkObj * method
Function to call.
Definition: object.h:272
Immutable sequence of bytes.
Definition: object.h:105
KrkObj obj
Base.
Definition: object.h:106
size_t length
Length of data in bytes.
Definition: object.h:107
uint8_t * bytes
Pointer to separately-stored bytes data.
Definition: object.h:108
KrkBytes * krk_newBytes(size_t length, uint8_t *source)
Create a new byte array.
Definition: object.c:363
Opcode chunk of a code object.
Definition: chunk.h:36
Type object.
Definition: object.h:189
KrkString * filename
Filename of the original source that defined the codeobject for the class.
Definition: object.h:194
KrkObj * _tostr
__str__ Called to produce a string from an instance
Definition: object.h:204
KrkCleanupCallback _ongcsweep
C function to call when the garbage collector is discarding an instance of this class.
Definition: object.h:198
KrkObj * _delitem
__delitem__ Called when del is used with a subscript
Definition: object.h:211
KrkObj * _descget
__get__ Called when a descriptor object is bound as a property
Definition: object.h:216
KrkClass * krk_newClass(KrkString *name, KrkClass *base)
Create a new class object.
Definition: object.c:320
KrkCleanupCallback _ongcscan
C function to call when the garbage collector visits an instance of this class in the scan phase.
Definition: object.h:197
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
Definition: object.h:203
KrkString * name
Name of the class.
Definition: object.h:193
struct KrkClass * base
Pointer to base class implementation.
Definition: object.h:195
KrkObj * _eq
__eq__ Implementation for equality check (==)
Definition: object.h:207
KrkTable subclasses
Set of classes that subclass this class.
Definition: object.h:199
KrkObj * _init
__init__ Implicitly called when an instance is created
Definition: object.h:206
KrkObj * _call
__call__ Called when an instance is called like a function
Definition: object.h:205
KrkObj * _len
__len__ Generally called by len() but may be used to determine truthiness
Definition: object.h:208
size_t allocSize
Size to allocate when creating instances of this class.
Definition: object.h:196
KrkObj * _exit
__exit__ Called upon exit from a with block
Definition: object.h:210
KrkObj obj
Base.
Definition: object.h:190
KrkObj * _getattr
__getattr__ Overrides normal behavior for attribute access
Definition: object.h:213
KrkObj * _enter
__enter__ Called upon entry into a with block
Definition: object.h:209
KrkObj * _iter
__iter__ Called by for ... in ..., etc.
Definition: object.h:212
KrkObj * _descset
__set__ Called when a descriptor object is assigned to as a property
Definition: object.h:217
KrkObj * _setter
__setitem__ Called when a subscripted instance is assigned to
Definition: object.h:202
KrkTable methods
General attributes table.
Definition: object.h:192
KrkObj * _dir
__dir__ Overrides normal behavior for dir()
Definition: object.h:214
struct KrkClass * _class
Metaclass.
Definition: object.h:191
KrkObj * _contains
__contains__ Called to resolve in (as a binary operator)
Definition: object.h:215
KrkObj * _classgetitem
__class_getitem__ Class method called when a type object is subscripted; used for type hints
Definition: object.h:218
KrkObj * _hash
__hash__ Called when an instance is a key in a dict or an entry in a set
Definition: object.h:219
KrkObj * _getter
__getitem__ Called when an instance is subscripted
Definition: object.h:201
Function object.
Definition: object.h:169
KrkValue globalsOwner
Owner of the globals table for this function.
Definition: object.h:176
size_t upvalueCount
Number of entries in upvalues.
Definition: object.h:173
KrkTable * globalsTable
Pointer to globals table with owner object.
Definition: object.h:177
KrkClosure * krk_newClosure(KrkCodeObject *function, KrkValue globals)
Create a new function object.
Definition: object.c:286
KrkObj obj
Base.
Definition: object.h:170
KrkValue annotations
Dictionary of type hints.
Definition: object.h:174
KrkUpvalue ** upvalues
Array of upvalues collected from the surrounding context when the closure was created.
Definition: object.h:172
KrkInstance * krk_buildGenerator(KrkClosure *function, KrkValue *arguments, size_t argCount)
Convert a function into a generator with the given arguments.
Definition: obj_gen.c:82
KrkTable fields
Object attributes table.
Definition: object.h:175
Code object.
Definition: object.h:144
unsigned short potentialPositionals
Precalculated positional arguments for complex argument processing.
Definition: object.h:148
KrkChunk chunk
Bytecode data.
Definition: object.h:151
size_t upvalueCount
Number of upvalues this function collects as a closure.
Definition: object.h:150
KrkValueArray positionalArgNames
Array of names for positional arguments (and *args)
Definition: object.h:154
size_t localNameCapacity
Capacity of localNames.
Definition: object.h:156
KrkLocalEntry * localNames
Stores the names of local variables used in the function, for debugging.
Definition: object.h:158
unsigned short keywordArgs
Arity of keyword (default) arguments.
Definition: object.h:147
unsigned short requiredArgs
Arity of required (non-default) arguments.
Definition: object.h:146
KrkString * docstring
Docstring attached to the function.
Definition: object.h:153
KrkObj obj
Base.
Definition: object.h:145
KrkValueArray keywordArgNames
Array of names for keyword-only arguments (and **kwargs)
Definition: object.h:155
KrkCodeObject * krk_newCodeObject(void)
Create a new, uninitialized code object.
Definition: object.c:261
size_t localNameCount
Number of entries in localNames.
Definition: object.h:157
KrkString * name
Name of the function.
Definition: object.h:152
unsigned short totalArguments
Total argument cells we can fill in complex argument processing.
Definition: object.h:149
KrkString * qualname
The dotted name of the function.
Definition: object.h:159
Flexible mapping type.
Definition: object.h:323
KrkTable entries
The actual table of values in the dict.
Definition: object.h:325
KrkInstance inst
Base.
Definition: object.h:324
An object of a class.
Definition: object.h:255
KrkInstance * krk_newInstance(KrkClass *_class)
Create a new instance of the given class.
Definition: object.c:339
KrkClass * _class
Type.
Definition: object.h:257
KrkObj obj
Base.
Definition: object.h:256
KrkTable fields
Attributes table.
Definition: object.h:258
Mutable array of values.
Definition: object.h:309
KrkInstance inst
Base.
Definition: object.h:310
KrkValueArray values
Stores the length, capacity, and actual values of the list.
Definition: object.h:311
Metadata on a local variable name in a function.
Definition: object.h:129
size_t birthday
Instruction offset that this local name became valid on.
Definition: object.h:131
KrkString * name
Name of the local.
Definition: object.h:133
size_t id
Local ID as used by opcodes; offset from the frame's stack base.
Definition: object.h:130
size_t deathday
Instruction offset that this local name becomes invalid on.
Definition: object.h:132
Representation of a loaded module.
Definition: object.h:359
Managed binding to a C function.
Definition: object.h:283
const char * doc
Docstring to supply from __doc__.
Definition: object.h:287
const char * name
Name to use when repring.
Definition: object.h:286
KrkObj obj
Base.
Definition: object.h:284
KrkNative * krk_newNative(NativeFn function, const char *name, int type)
Create a native function binding object.
Definition: object.c:277
The most basic object type.
Definition: object.h:41
uint32_t hash
Cached hash value for table keys.
Definition: object.h:44
uint16_t flags
General object flags, mostly related to garbage collection.
Definition: object.h:43
uint16_t type
Tag indicating core type.
Definition: object.h:42
struct KrkObj * next
Invasive linked list of all objects in the VM.
Definition: object.h:45
Immutable sequence of Unicode codepoints.
Definition: object.h:93
uint32_t krk_unicodeCodepoint(KrkString *string, size_t index)
Obtain the codepoint at a given index in a string.
Definition: object.c:161
void * krk_unicodeString(KrkString *string)
Ensure that a codepoint representation of a string is available.
Definition: object.c:152
KrkObj obj
Base.
Definition: object.h:94
KrkString * krk_copyString(const char *chars, size_t length)
Obtain a string object representation of the given C string.
Definition: object.c:221
KrkString * krk_takeString(char *chars, size_t length)
Yield ownership of a C string to the GC and obtain a string object.
Definition: object.c:205
size_t codesLength
String length in Unicode codepoints.
Definition: object.h:96
char * chars
UTF8 canonical data.
Definition: object.h:97
void * codes
Codepoint data.
Definition: object.h:98
size_t length
String length in bytes.
Definition: object.h:95
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,...
Definition: object.c:238
size_t krk_codepointToBytes(krk_integer_type value, unsigned char *out)
Convert an integer codepoint to a UTF-8 byte representation.
Definition: object.c:37
Simple hash table of arbitrary keys to values.
Definition: table.h:28
Execution state of a VM thread.
Definition: vm.h:161
Immutable sequence of arbitrary values.
Definition: object.h:297
KrkObj obj
Base.
Definition: object.h:298
KrkValueArray values
Stores the length, capacity, and actual values of the tuple.
Definition: object.h:299
KrkTuple * krk_newTuple(size_t length)
Create a new tuple.
Definition: object.c:353
Storage for values referenced from nested functions.
Definition: object.h:115
int location
Stack offset or -1 if closed.
Definition: object.h:117
KrkValue closed
Heap storage for closed value.
Definition: object.h:118
struct KrkThreadState * owner
The thread that owns the stack this upvalue belongs in.
Definition: object.h:120
struct KrkUpvalue * next
Invasive linked list pointer to next upvalue.
Definition: object.h:119
KrkObj obj
Base.
Definition: object.h:116
KrkUpvalue * krk_newUpvalue(int slot)
Create an upvalue slot.
Definition: object.c:311
Flexible vector of stack references.
Definition: value.h:70
Stack reference or primative value.
Implementation of a generic hash table.
Definitions for primitive stack references.