KrkClass Struct Reference

Type object. More...

#include <object.h>

Inheritance diagram for KrkClass:

Public Member Functions

KrkClasskrk_newClass (KrkString *name, KrkClass *base)
 Create a new class object. More...
 
int krk_bindMethod (KrkClass *_class, KrkString *name)
 Perform method binding on the stack. More...
 
int krk_bindMethodSuper (KrkClass *baseClass, KrkString *name, KrkClass *realClass)
 Bind a method with super() semantics. More...
 
KrkClasskrk_makeClass (KrkInstance *module, KrkClass **_class, const char *name, KrkClass *base)
 Convenience function for creating new types. More...
 
void krk_finalizeClass (KrkClass *_class)
 Finalize a class by collecting pointers to core methods. More...
 

Data Fields

struct KrkClass_class
 Metaclass.
 
KrkTable methods
 General attributes table.
 
KrkStringname
 Name of the class.
 
KrkStringfilename
 Filename of the original source that defined the codeobject for the class.
 
struct KrkClassbase
 Pointer to base class implementation.
 
size_t allocSize
 Size to allocate when creating instances of this class.
 
KrkCleanupCallback _ongcscan
 C function to call when the garbage collector visits an instance of this class in the scan phase.
 
KrkCleanupCallback _ongcsweep
 C function to call when the garbage collector is discarding an instance of this class.
 
KrkTable subclasses
 Set of classes that subclass this class.
 
KrkObj_getter
 __getitem__ Called when an instance is subscripted
 
KrkObj_setter
 __setitem__ Called when a subscripted instance is assigned to
 
KrkObj_reprer
 __repr__ Called to create a reproducible string representation of an instance
 
KrkObj_tostr
 __str__ Called to produce a string from an instance
 
KrkObj_call
 __call__ Called when an instance is called like a function
 
KrkObj_init
 __init__ Implicitly called when an instance is created
 
KrkObj_eq
 __eq__ Implementation for equality check (==)
 
KrkObj_len
 __len__ Generally called by len() but may be used to determine truthiness
 
KrkObj_enter
 __enter__ Called upon entry into a with block
 
KrkObj_exit
 __exit__ Called upon exit from a with block
 
KrkObj_delitem
 __delitem__ Called when del is used with a subscript
 
KrkObj_iter
 __iter__ Called by for ... in ..., etc.
 
KrkObj_getattr
 __getattr__ Overrides normal behavior for attribute access
 
KrkObj_dir
 __dir__ Overrides normal behavior for dir()
 
KrkObj_contains
 __contains__ Called to resolve in (as a binary operator)
 
KrkObj_descget
 __get__ Called when a descriptor object is bound as a property
 
KrkObj_descset
 __set__ Called when a descriptor object is assigned to as a property
 
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_add
 
KrkObj_radd
 
KrkObj_iadd
 
KrkObj_sub
 
KrkObj_rsub
 
KrkObj_isub
 
KrkObj_mul
 
KrkObj_rmul
 
KrkObj_imul
 
KrkObj_or
 
KrkObj_ror
 
KrkObj_ior
 
KrkObj_xor
 
KrkObj_rxor
 
KrkObj_ixor
 
KrkObj_and
 
KrkObj_rand
 
KrkObj_iand
 
KrkObj_mod
 
KrkObj_rmod
 
KrkObj_imod
 
KrkObj_pow
 
KrkObj_rpow
 
KrkObj_ipow
 
KrkObj_lshift
 
KrkObj_rlshift
 
KrkObj_ilshift
 
KrkObj_rshift
 
KrkObj_rrshift
 
KrkObj_irshift
 
KrkObj_truediv
 
KrkObj_rtruediv
 
KrkObj_itruediv
 
KrkObj_floordiv
 
KrkObj_rfloordiv
 
KrkObj_ifloordiv
 
KrkObj_lt
 
KrkObj_gt
 
KrkObj_le
 
KrkObj_ge
 
KrkObj_invert
 
KrkObj_negate
 
KrkObj_set_name
 
KrkObj_matmul
 
KrkObj_rmatmul
 
KrkObj_imatmul
 
KrkObj_pos
 
KrkObj_setattr
 
KrkObj_format
 
KrkObj_new
 
KrkObj_bool
 
size_t cacheIndex
 
- Data Fields inherited from KrkObj
uint16_t type
 Tag indicating core type.
 
uint16_t flags
 General object flags, mostly related to garbage collection.
 
uint32_t hash
 Cached hash value for table keys.
 
struct KrkObjnext
 Invasive linked list of all objects in the VM.
 

Protected Attributes

KrkObj obj
 Base.
 

Detailed Description

Type object.

Represents classes defined in user code as well as classes defined by C extensions to represent method tables for new types.

Definition at line 215 of file object.h.

Member Function Documentation

◆ krk_bindMethod()

int krk_bindMethod ( KrkClass _class,
KrkString name 
)

Perform method binding on the stack.

Performs attribute lookup from the class _class for name. If name is not a valid member, the binding fails. If name is a valid method, the method will be retrieved and bound to the instance on the top of the stack, replacing it with a BoundMethod object. If name is not a method, the unbound attribute is returned. If name is a descriptor, the __get__ method is executed.

Parameters
_classClass object to resolve methods from.
nameString object with the name of the method to resolve.
Returns
1 if the method has been bound, 0 if binding failed.

Definition at line 1655 of file vm.c.

◆ krk_bindMethodSuper()

int krk_bindMethodSuper ( KrkClass baseClass,
KrkString name,
KrkClass realClass 
)

Bind a method with super() semantics.

See also
krk_bindMethod

Allows binding potential class methods with the correct class object while searching from a base class. Used by the super() mechanism.

Parameters
baseClassThe superclass to begin searching from.
nameThe name of the member to look up.
realClassThe class to bind if a class method is found.
Returns
1 if a member has been found, 0 if binding fails.

Definition at line 1626 of file vm.c.

◆ krk_finalizeClass()

void krk_finalizeClass ( KrkClass _class)

Finalize a class by collecting pointers to core methods.

Scans through the methods table of a class object to find special methods and assign them to the class object's pointer table so they can be referenced directly without performing hash lookups.

Parameters
_classClass object to finalize.

Definition at line 189 of file vm.c.

◆ krk_makeClass()

KrkClass * krk_makeClass ( KrkInstance module,
KrkClass **  _class,
const char *  name,
KrkClass base 
)

Convenience function for creating new types.

Creates a class object, output to _class, setting its name to name, inheriting from base, and attaching it with its name to the fields table of the given module.

Parameters
modulePointer to an instance for a module to attach to, or NULL to skip attaching.
_classOutput pointer to assign the new class object to.
nameName of the new class.
basePointer to class object to inherit from.
Returns
A pointer to the class object, equivalent to the value assigned to _class.

Definition at line 164 of file vm.c.

◆ krk_newClass()

KrkClass * krk_newClass ( KrkString name,
KrkClass base 
)

Create a new class object.

Creates a new class with the give name and base class. Generally, you will want to use krk_makeClass instead, which handles binding the class to a module.

Definition at line 324 of file object.c.


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