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 
141 typedef struct {
142  uint32_t bytecodeOffset;
143  uint8_t start;
144  uint8_t midStart;
145  uint8_t midEnd;
146  uint8_t end;
148 
149 struct KrkInstance;
150 
151 typedef struct {
152  uint32_t instructionOffset;
153  uint16_t intendedTarget;
154  uint8_t originalOpcode;
156 
163 typedef struct {
165  unsigned short requiredArgs;
166  unsigned short keywordArgs;
167  unsigned short potentialPositionals;
168  unsigned short totalArguments;
169  size_t upvalueCount;
176  size_t localNameCount;
186 } KrkCodeObject;
187 
188 
195 typedef struct {
197  KrkCodeObject * function;
199  size_t upvalueCount;
204 } KrkClosure;
205 
206 typedef void (*KrkCleanupCallback)(struct KrkInstance *);
207 
215 typedef struct KrkClass {
217  struct KrkClass * _class;
221  struct KrkClass * base;
222  size_t allocSize;
223  KrkCleanupCallback _ongcscan;
224  KrkCleanupCallback _ongcsweep;
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;
259 
260  KrkObj * _lt, * _gt, * _le, * _ge;
261  KrkObj * _invert, * _negate;
262  KrkObj * _set_name;
263  KrkObj * _matmul, * _rmatmul, * _imatmul;
264  KrkObj * _pos;
265  KrkObj * _setattr;
266  KrkObj * _format;
267  KrkObj * _new;
268  KrkObj * _bool;
269 
270  size_t cacheIndex;
272 
281 typedef struct KrkInstance {
286 
295 typedef struct {
300 
301 typedef KrkValue (*NativeFn)(int argCount, const KrkValue* args, int hasKwargs);
302 
309 typedef struct {
311  NativeFn function;
312  const char * name;
313  const char * doc;
314 } KrkNative;
315 
323 typedef struct {
326 } KrkTuple;
327 
335 typedef struct {
338 #ifndef KRK_DISABLE_THREADS
339  pthread_rwlock_t rwlock;
340 #endif
341 } KrkList;
342 
349 typedef struct {
352 } KrkDict;
353 
357 struct DictItems {
358  KrkInstance inst;
359  KrkValue dict;
360  size_t i;
361 };
362 
366 struct DictKeys {
367  KrkInstance inst;
368  KrkValue dict;
369  size_t i;
370 };
371 
375 struct DictValues {
376  KrkInstance inst;
377  KrkValue dict;
378  size_t i;
379 };
380 
385 struct KrkModule {
386  KrkInstance inst;
387 #ifndef KRK_STATIC_ONLY
388  krk_dlRefType libHandle;
389 #endif
390 };
391 
395 struct KrkSlice {
396  KrkInstance inst;
397  KrkValue start;
398  KrkValue end;
399  KrkValue step;
400 };
401 
419 extern KrkString * krk_takeString(char * chars, size_t length);
420 
437 extern KrkString * krk_takeStringVetted(char * chars, size_t length, size_t codesLength, KrkStringType type, uint32_t hash);
438 
454 extern KrkString * krk_copyString(const char * chars, size_t length);
455 
468 extern void * krk_unicodeString(KrkString * string);
469 
486 extern uint32_t krk_unicodeCodepoint(KrkString * string, size_t index);
487 
499 extern size_t krk_codepointToBytes(krk_integer_type value, unsigned char * out);
500 
513 extern KrkInstance * krk_buildGenerator(KrkClosure * function, KrkValue * arguments, size_t argCount);
514 
518 extern int krk_getAwaitable(void);
519 
526 extern NativeFn krk_GenericAlias;
527 
536 extern KrkCodeObject * krk_newCodeObject(void);
537 
546 extern KrkNative * krk_newNative(NativeFn function, const char * name, int type);
547 
559 extern KrkClosure * krk_newClosure(KrkCodeObject * function, KrkValue globals);
560 
570 extern KrkUpvalue * krk_newUpvalue(int slot);
571 
580 extern KrkClass * krk_newClass(KrkString * name, KrkClass * base);
581 
590 extern KrkInstance * krk_newInstance(KrkClass * _class);
591 
600 extern KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj * method);
601 
610 extern KrkTuple * krk_newTuple(size_t length);
611 
619 extern KrkBytes * krk_newBytes(size_t length, uint8_t * source);
620 
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)
642 
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.
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:295
KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj *method)
Create a new bound method.
Definition: object.c:350
KrkValue receiver
Object to pass as implicit first argument.
Definition: object.h:297
KrkObj obj
Base.
Definition: object.h:296
KrkObj * method
Function to call.
Definition: object.h:298
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:367
Opcode chunk of a code object.
Definition: chunk.h:36
Type object.
Definition: object.h:215
KrkString * filename
Filename of the original source that defined the codeobject for the class.
Definition: object.h:220
KrkObj * _tostr
__str__ Called to produce a string from an instance
Definition: object.h:230
KrkCleanupCallback _ongcsweep
C function to call when the garbage collector is discarding an instance of this class.
Definition: object.h:224
KrkObj * _delitem
__delitem__ Called when del is used with a subscript
Definition: object.h:237
KrkObj * _descget
__get__ Called when a descriptor object is bound as a property
Definition: object.h:242
KrkClass * krk_newClass(KrkString *name, KrkClass *base)
Create a new class object.
Definition: object.c:324
KrkCleanupCallback _ongcscan
C function to call when the garbage collector visits an instance of this class in the scan phase.
Definition: object.h:223
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
Definition: object.h:229
KrkString * name
Name of the class.
Definition: object.h:219
struct KrkClass * base
Pointer to base class implementation.
Definition: object.h:221
KrkObj * _eq
__eq__ Implementation for equality check (==)
Definition: object.h:233
KrkTable subclasses
Set of classes that subclass this class.
Definition: object.h:225
KrkObj * _init
__init__ Implicitly called when an instance is created
Definition: object.h:232
KrkObj * _call
__call__ Called when an instance is called like a function
Definition: object.h:231
KrkObj * _len
__len__ Generally called by len() but may be used to determine truthiness
Definition: object.h:234
size_t allocSize
Size to allocate when creating instances of this class.
Definition: object.h:222
KrkObj * _exit
__exit__ Called upon exit from a with block
Definition: object.h:236
KrkObj obj
Base.
Definition: object.h:216
KrkObj * _getattr
__getattr__ Overrides normal behavior for attribute access
Definition: object.h:239
KrkObj * _enter
__enter__ Called upon entry into a with block
Definition: object.h:235
KrkObj * _iter
__iter__ Called by for ... in ..., etc.
Definition: object.h:238
KrkObj * _descset
__set__ Called when a descriptor object is assigned to as a property
Definition: object.h:243
KrkObj * _setter
__setitem__ Called when a subscripted instance is assigned to
Definition: object.h:228
KrkTable methods
General attributes table.
Definition: object.h:218
KrkObj * _dir
__dir__ Overrides normal behavior for dir()
Definition: object.h:240
struct KrkClass * _class
Metaclass.
Definition: object.h:217
KrkObj * _contains
__contains__ Called to resolve in (as a binary operator)
Definition: object.h:241
KrkObj * _classgetitem
__class_getitem__ Class method called when a type object is subscripted; used for type hints
Definition: object.h:244
KrkObj * _hash
__hash__ Called when an instance is a key in a dict or an entry in a set
Definition: object.h:245
KrkObj * _getter
__getitem__ Called when an instance is subscripted
Definition: object.h:227
Function object.
Definition: object.h:195
KrkValue globalsOwner
Owner of the globals table for this function.
Definition: object.h:202
size_t upvalueCount
Number of entries in upvalues.
Definition: object.h:199
KrkTable * globalsTable
Pointer to globals table with owner object.
Definition: object.h:203
KrkClosure * krk_newClosure(KrkCodeObject *function, KrkValue globals)
Create a new function object.
Definition: object.c:290
KrkObj obj
Base.
Definition: object.h:196
KrkValue annotations
Dictionary of type hints.
Definition: object.h:200
KrkUpvalue ** upvalues
Array of upvalues collected from the surrounding context when the closure was created.
Definition: object.h:198
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:201
Code object.
Definition: object.h:163
unsigned short potentialPositionals
Precalculated positional arguments for complex argument processing.
Definition: object.h:167
size_t overlongJumpsCount
Number of entries in pessimal jump table.
Definition: object.h:185
KrkChunk chunk
Bytecode data.
Definition: object.h:170
size_t upvalueCount
Number of upvalues this function collects as a closure.
Definition: object.h:169
KrkValueArray positionalArgNames
Array of names for positional arguments (and *args)
Definition: object.h:173
KrkValue jumpTargets
Possibly a set of jump targets...
Definition: object.h:182
size_t localNameCapacity
Capacity of localNames.
Definition: object.h:175
KrkLocalEntry * localNames
Stores the names of local variables used in the function, for debugging.
Definition: object.h:177
unsigned short keywordArgs
Arity of keyword (default) arguments.
Definition: object.h:166
KrkOverlongJump * overlongJumps
Pessimal overlong jump container.
Definition: object.h:183
unsigned short requiredArgs
Arity of required (non-default) arguments.
Definition: object.h:165
KrkExpressionsMap * expressions
Mapping of bytecode offsets to expression spans for debugging.
Definition: object.h:181
KrkString * docstring
Docstring attached to the function.
Definition: object.h:172
KrkObj obj
Base.
Definition: object.h:164
size_t expressionsCapacity
Capacity of expressions.
Definition: object.h:179
size_t expressionsCount
Number of entries in expressions.
Definition: object.h:180
KrkValueArray keywordArgNames
Array of names for keyword-only arguments (and **kwargs)
Definition: object.h:174
KrkCodeObject * krk_newCodeObject(void)
Create a new, uninitialized code object.
Definition: object.c:264
size_t localNameCount
Number of entries in localNames.
Definition: object.h:176
KrkString * name
Name of the function.
Definition: object.h:171
size_t overlongJumpsCapacity
Number of possible entries in pessimal jump table.
Definition: object.h:184
unsigned short totalArguments
Total argument cells we can fill in complex argument processing.
Definition: object.h:168
KrkString * qualname
The dotted name of the function.
Definition: object.h:178
Flexible mapping type.
Definition: object.h:349
KrkTable entries
The actual table of values in the dict.
Definition: object.h:351
KrkInstance inst
Base.
Definition: object.h:350
Map entry of opcode offsets to expressions spans.
Definition: object.h:141
An object of a class.
Definition: object.h:281
KrkInstance * krk_newInstance(KrkClass *_class)
Create a new instance of the given class.
Definition: object.c:343
KrkClass * _class
Type.
Definition: object.h:283
KrkObj obj
Base.
Definition: object.h:282
KrkTable fields
Attributes table.
Definition: object.h:284
Mutable array of values.
Definition: object.h:335
KrkInstance inst
Base.
Definition: object.h:336
KrkValueArray values
Stores the length, capacity, and actual values of the list.
Definition: object.h:337
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:385
Managed binding to a C function.
Definition: object.h:309
const char * doc
Docstring to supply from __doc__.
Definition: object.h:313
const char * name
Name to use when repring.
Definition: object.h:312
KrkObj obj
Base.
Definition: object.h:310
KrkNative * krk_newNative(NativeFn function, const char *name, int type)
Create a native function binding object.
Definition: object.c:281
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
uint16_t intendedTarget
High bytes of the intended target.
Definition: object.h:153
uint8_t originalOpcode
Original jump opcode to execute.
Definition: object.h:154
uint32_t instructionOffset
Instruction (operand offset) this jump target applies to.
Definition: object.h:152
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:162
void * krk_unicodeString(KrkString *string)
Ensure that a codepoint representation of a string is available.
Definition: object.c:153
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:224
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:208
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:241
size_t krk_codepointToBytes(krk_integer_type value, unsigned char *out)
Convert an integer codepoint to a UTF-8 byte representation.
Definition: object.c:38
Simple hash table of arbitrary keys to values.
Definition: table.h:28
Execution state of a VM thread.
Definition: vm.h:152
Immutable sequence of arbitrary values.
Definition: object.h:323
KrkObj obj
Base.
Definition: object.h:324
KrkValueArray values
Stores the length, capacity, and actual values of the tuple.
Definition: object.h:325
KrkTuple * krk_newTuple(size_t length)
Create a new tuple.
Definition: object.c:357
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:315
Flexible vector of stack references.
Definition: value.h:75
Stack reference or primative value.
Implementation of a generic hash table.
Definitions for primitive stack references.