10 FUNC_SIG(list,__init__);
15 return krk_runtimeError(
vm.exceptions->argumentError,
"%s() takes %s %d argument%s (%d given)",
16 "dir",
"exactly", 1,
"", argc);
22 if (IS_INSTANCE(argv[0])) {
25 for (
size_t i = 0; i <
self->fields.capacity; ++i) {
26 if (!IS_KWARGS(self->fields.entries[i].key)) {
28 self->fields.entries[i].key);
31 }
else if (IS_CLOSURE(argv[0])) {
34 for (
size_t i = 0; i <
self->fields.capacity; ++i) {
35 if (!IS_KWARGS(self->fields.entries[i].key)) {
37 self->fields.entries[i].key);
40 }
else if (IS_CLASS(argv[0])) {
41 KrkClass * _class = AS_CLASS(argv[0]);
43 for (
size_t i = 0; i < _class->
methods.capacity; ++i) {
44 if (!IS_KWARGS(_class->
methods.entries[i].key)) {
46 _class->
methods.entries[i].key);
49 _class = _class->
base;
56 for (
size_t i = 0; i < type->
methods.capacity; ++i) {
57 if (!IS_KWARGS(type->
methods.entries[i].key)) {
81 #define IS_object(o) (1)
82 #define AS_object(o) (o)
83 #define CURRENT_CTYPE KrkValue
84 #define CURRENT_NAME self
86 KRK_Method(
object,__dir__) {
90 KRK_Method(
object,__class__) {
93 if (!IS_CLASS(argv[1]))
return krk_runtimeError(
vm.exceptions->typeError,
"'%T' object is not a class", argv[1]);
95 if (AS_CLASS(argv[1])->allocSize !=
sizeof(
KrkInstance))
return krk_runtimeError(
vm.exceptions->typeError,
"'%S' type is not assignable", AS_CLASS(argv[1])->name);
96 AS_INSTANCE(argv[0])->_class = AS_CLASS(argv[1]);
97 current = AS_CLASS(argv[1]);
99 return OBJECT_VAL(current);
102 KRK_Method(
object,__hash__) {
103 if (!IS_OBJECT(
self)) {
106 return INTEGER_VAL(hashed);
108 KrkObj * obj = AS_OBJECT(
self);
109 if (!(obj->
flags & KRK_OBJ_FLAGS_VALID_HASH)) {
110 obj->
hash = INTEGER_VAL((
int)((intptr_t)
self >> 3));
111 obj->
flags |= KRK_OBJ_FLAGS_VALID_HASH;
113 return INTEGER_VAL(obj->
hash);
116 KRK_Method(
object,__eq__) {
117 METHOD_TAKES_EXACTLY(1);
118 if (argv[0] == argv[1])
return BOOLEAN_VAL(1);
119 return NOTIMPL_VAL();
122 KRK_Function(getattr) {
123 FUNCTION_TAKES_AT_LEAST(2);
129 if (argc == 3)
return argv[2];
130 return krk_runtimeError(
vm.exceptions->attributeError,
"'%T' object has no attribute '%S'", argv[0], AS_STRING(argv[1]));
135 KRK_Function(setattr) {
136 FUNCTION_TAKES_EXACTLY(3);
142 return krk_runtimeError(
vm.exceptions->attributeError,
"'%T' object has no attribute '%S'", argv[0], AS_STRING(argv[1]));
147 KRK_Function(hasattr) {
148 FUNCTION_TAKES_AT_LEAST(2);
154 return BOOLEAN_VAL(0);
157 return BOOLEAN_VAL(1);
160 KRK_Function(delattr) {
161 FUNCTION_TAKES_AT_LEAST(2);
166 return krk_runtimeError(
vm.exceptions->attributeError,
"'%T' object has no attribute '%S'", argv[0], AS_STRING(argv[1]));
175 KRK_Method(
object,__setattr__) {
176 METHOD_TAKES_EXACTLY(2);
177 if (!IS_STRING(argv[1]))
return krk_runtimeError(
vm.exceptions->typeError,
"expected str");
179 if (!IS_INSTANCE(argv[0])) {
180 return FUNC_NAME(krk,setattr)(argc,argv,hasKw);
187 KRK_StaticMethod(
object,__new__) {
194 if (!
krk_parseArgs(
"O!*~", (
const char*[]){
"cls"},
vm.baseClasses->typeClass, &_class, &_argc, &_args)) {
200 if (_cls->_new && IS_NATIVE(OBJECT_VAL(_cls->_new)) && _cls->_new != KRK_BASE_CLASS(
object)->_new) {
201 return krk_runtimeError(
vm.exceptions->typeError,
"object.__new__(%S) is not safe, use %S.__new__()", _class->
name, _cls->
name);
206 if (_class->
_init ==
vm.baseClasses->objectClass->_init && (_argc || (hasKw && AS_DICT(argv[argc])->count))) {
213 KRK_Method(
object,__init__) {
217 KRK_StaticMethod(
object,__init_subclass__) {
218 if (!
krk_parseArgs(
".", (
const char*[]){NULL}, NULL))
return NONE_VAL();
236 KRK_Method(
object,__str__) {
243 KrkString * name = IS_STRING(qualname) ? AS_STRING(qualname) : type->name;
244 int includeModule = !(IS_NONE(module) || (IS_STRING(module) && AS_STRING(module) == S(
"builtins")));
248 if (!krk_pushStringBuilderFormat(&sb,
"<%s%s%s object",
249 includeModule ? AS_CSTRING(module) :
"",
250 includeModule ?
"." :
"",
251 name->
chars))
goto _error;
253 if (IS_OBJECT(
self) && !krk_pushStringBuilderFormat(&sb,
" at %p", (
void*)AS_OBJECT(
self))) {
265 KRK_Method(
object,__format__) {
266 METHOD_TAKES_EXACTLY(1);
267 if (!IS_STRING(argv[1]))
return TYPE_ERROR(str,argv[1]);
268 if (AS_STRING(argv[1])->length != 0)
return krk_runtimeError(
vm.exceptions->typeError,
"Unsupported format string");
279 FUNCTION_TAKES_EXACTLY(1);
281 if (IS_STRING(argv[0]))
return INTEGER_VAL(AS_STRING(argv[0])->codesLength);
282 if (IS_TUPLE(argv[0]))
return INTEGER_VAL(AS_TUPLE(argv[0])->values.count);
285 if (!type->
_len)
return krk_runtimeError(
vm.exceptions->typeError,
"object of type '%T' has no len()", argv[0]);
292 FUNCTION_TAKES_AT_MOST(1);
312 for (
size_t i = 0; i < globals->capacity; ++i) {
314 if (IS_KWARGS(entry->key))
continue;
325 FUNCTION_TAKES_EXACTLY(1);
332 #define trySlowMethod(name) do { \
333 KrkClass * type = krk_getType(argv[0]); \
336 if (krk_tableGet(&type->methods, name, &method)) { \
339 return krk_callStack(1); \
346 FUNCTION_TAKES_EXACTLY(1);
347 trySlowMethod(
vm.specialMethodNames[METHOD_ORD]);
348 return TYPE_ERROR(
string of length 1,argv[0]);
352 FUNCTION_TAKES_EXACTLY(1);
353 trySlowMethod(
vm.specialMethodNames[METHOD_CHR]);
354 return TYPE_ERROR(
int,argv[0]);
358 FUNCTION_TAKES_EXACTLY(1);
359 trySlowMethod(OBJECT_VAL(S(
"__hex__")));
360 return TYPE_ERROR(
int,argv[0]);
364 FUNCTION_TAKES_EXACTLY(1);
365 trySlowMethod(OBJECT_VAL(S(
"__oct__")));
366 return TYPE_ERROR(
int,argv[0]);
370 FUNCTION_TAKES_EXACTLY(1);
371 trySlowMethod(OBJECT_VAL(S(
"__bin__")));
372 return TYPE_ERROR(
int,argv[0]);
375 #define KRK_STRING_FAST(string,offset) (uint32_t)\
376 ((string->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) <= (KRK_OBJ_FLAGS_STRING_UCS1) ? ((uint8_t*)string->codes)[offset] : \
377 ((string->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) == (KRK_OBJ_FLAGS_STRING_UCS2) ? ((uint16_t*)string->codes)[offset] : \
378 ((uint32_t*)string->codes)[offset]))
381 if (IS_TUPLE(iterable)) {
382 if (callback(context, AS_TUPLE(iterable)->values.values, AS_TUPLE(iterable)->values.count))
return 1;
383 }
else if (IS_list(iterable)) {
384 if (callback(context, AS_LIST(iterable)->values, AS_LIST(iterable)->count))
return 1;
385 }
else if (IS_dict(iterable)) {
386 for (
size_t i = 0; i < AS_DICT(iterable)->capacity; ++i) {
387 if (!IS_KWARGS(AS_DICT(iterable)->entries[i].key)) {
388 if (callback(context, &AS_DICT(iterable)->entries[i].key, 1))
return 1;
391 }
else if (IS_STRING(iterable)) {
393 for (
size_t i = 0; i < AS_STRING(iterable)->codesLength; ++i) {
397 if (callback(context, &s, 1)) {
405 if (unlikely(!type->
_iter)) {
433 if (callback(context, &item, 1)) {
450 static int _any_callback(
void * context,
const KrkValue * values,
size_t count) {
452 for (
size_t i = 0; i < count; ++i) {
454 _context->base = BOOLEAN_VAL(1);
462 FUNCTION_TAKES_EXACTLY(1);
468 static int _all_callback(
void * context,
const KrkValue * values,
size_t count) {
470 for (
size_t i = 0; i < count; ++i) {
472 _context->base = BOOLEAN_VAL(0);
480 FUNCTION_TAKES_EXACTLY(1);
486 #define CURRENT_CTYPE KrkInstance *
487 #define CURRENT_NAME self
489 #define IS_map(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(map)))
490 #define AS_map(o) (AS_INSTANCE(o))
491 KRK_Method(map,__init__) {
492 METHOD_TAKES_AT_LEAST(2);
505 for (
int i = 2; i < argc; ++i) {
508 return krk_runtimeError(
vm.exceptions->typeError,
"'%T' object is not iterable", argv[i]);
520 KRK_Method(map,__iter__) {
522 return OBJECT_VAL(
self);
525 KRK_Method(map,__call__) {
534 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_function")), &
function))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt map object");
535 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_iterables")), &iterators) || !IS_TUPLE(iterators))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt map object");
540 for (
size_t i = 0; i < AS_TUPLE(iterators)->values.count; ++i) {
542 krk_push(AS_TUPLE(iterators)->values.values[i]);
547 for (
size_t j = 0; j < i + 1; ++j)
krk_pop();
549 return OBJECT_VAL(
self);
559 #define IS_zip(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(zip)))
560 #define AS_zip(o) (AS_INSTANCE(o))
561 KRK_Method(zip,__init__) {
562 if (hasKw && AS_DICT(argv[argc])->count)
return krk_runtimeError(
vm.exceptions->typeError,
"%s() takes no keyword arguments",
"zip");
569 for (
int i = 1; i < argc; ++i) {
572 return krk_runtimeError(
vm.exceptions->typeError,
"'%T' object is not iterable", argv[i]);
584 KRK_Method(zip,__iter__) {
586 return OBJECT_VAL(
self);
589 KRK_Method(zip,__call__) {
594 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_iterables")), &iterators) || !IS_TUPLE(iterators))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt zip object");
601 for (
size_t i = 0; i < AS_TUPLE(iterators)->values.count; ++i) {
602 krk_push(AS_TUPLE(iterators)->values.values[i]);
606 return OBJECT_VAL(
self);
615 #define IS_filter(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(filter)))
616 #define AS_filter(o) (AS_INSTANCE(o))
617 KRK_Method(filter,__init__) {
618 METHOD_TAKES_EXACTLY(2);
622 return krk_runtimeError(
vm.exceptions->typeError,
"'%T' object is not iterable", argv[2]);
632 KRK_Method(filter,__iter__) {
634 return OBJECT_VAL(
self);
637 KRK_Method(filter,__call__) {
643 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_function")), &
function))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt filter object");
644 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_iterator")), &iterator))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt filter object");
653 return OBJECT_VAL(
self);
656 if (IS_NONE(
function)) {
677 #define IS_enumerate(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(enumerate)))
678 #define AS_enumerate(o) (AS_INSTANCE(o))
679 KRK_Method(enumerate,__init__) {
680 METHOD_TAKES_EXACTLY(1);
682 if (hasKw)
krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S(
"start")), &start);
689 return krk_runtimeError(
vm.exceptions->typeError,
"'%T' object is not iterable", argv[1]);
699 KRK_Method(enumerate,__iter__) {
701 return OBJECT_VAL(
self);
705 KRK_Method(enumerate,__call__) {
711 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_counter")), &counter))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt enumerate object");
712 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"_iterator")), &iterator))
return krk_runtimeError(
vm.exceptions->valueError,
"corrupt enumerate object");
728 return OBJECT_VAL(
self);
735 krk_push(krk_operator_add(counter, INTEGER_VAL(1)));
743 static int _sum_callback(
void * context,
const KrkValue * values,
size_t count) {
745 for (
size_t i = 0; i < count; ++i) {
746 _context->base = krk_operator_add(_context->base, values[i]);
753 FUNCTION_TAKES_AT_LEAST(1);
756 krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S(
"start")), &base);
763 static int _min_callback(
void * context,
const KrkValue * values,
size_t count) {
765 for (
size_t i = 0; i < count; ++i) {
766 if (IS_KWARGS(_context->base)) _context->base = values[i];
769 if (!IS_BOOLEAN(check))
return 1;
770 else if (AS_BOOLEAN(check) == 1) _context->base = values[i];
777 FUNCTION_TAKES_AT_LEAST(1);
780 if (_min_callback(&context, argv, argc))
return NONE_VAL();
784 if (IS_KWARGS(context.base))
return krk_runtimeError(
vm.exceptions->valueError,
"empty argument to %s()",
"min");
788 static int _max_callback(
void * context,
const KrkValue * values,
size_t count) {
790 for (
size_t i = 0; i < count; ++i) {
791 if (IS_KWARGS(_context->base)) _context->base = values[i];
794 if (!IS_BOOLEAN(check))
return 1;
795 else if (AS_BOOLEAN(check) == 1) _context->base = values[i];
802 FUNCTION_TAKES_AT_LEAST(1);
805 if (_max_callback(&context, argv, argc))
return NONE_VAL();
809 if (IS_KWARGS(context.base))
return krk_runtimeError(
vm.exceptions->valueError,
"empty argument to %s()",
"max");
813 KRK_Function(print) {
816 char * sep =
" ";
size_t sepLen = 1;
817 char * end =
"\n";
size_t endLen = 1;
819 if (
krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S(
"sep")), &sepVal)) {
820 if (!IS_STRING(sepVal))
return krk_runtimeError(
vm.exceptions->typeError,
"'%s' should be a string, not '%T'",
"sep", sepVal);
821 sep = AS_CSTRING(sepVal);
822 sepLen = AS_STRING(sepVal)->length;
824 if (
krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S(
"end")), &endVal)) {
825 if (!IS_STRING(endVal))
return krk_runtimeError(
vm.exceptions->typeError,
"'%s' should be a string, not '%T'",
"end", endVal);
826 end = AS_CSTRING(endVal);
827 endLen = AS_STRING(endVal)->length;
831 for (
size_t j = 0; j < endLen; ++j) {
832 fputc(end[j], stdout);
835 for (
int i = 0; i < argc; ++i) {
837 if (IS_STRING(printable)) {
839 for (
size_t j = 0; j < AS_STRING(printable)->length; ++j) {
840 fputc(AS_CSTRING(printable)[j], stdout);
846 char * thingToPrint = (i == argc - 1) ? end : sep;
847 for (
size_t j = 0; j < ((i == argc - 1) ? endLen : sepLen); ++j) {
848 fputc(thingToPrint[j], stdout);
860 KRK_Function(globals) {
861 FUNCTION_TAKES_NONE();
877 KRK_Function(locals) {
878 FUNCTION_TAKES_AT_MOST(1);
883 if (argc > 0 && IS_INTEGER(argv[0])) {
884 if (AS_INTEGER(argv[0]) < 1) {
890 index = AS_INTEGER(argv[0]);
895 size_t offset = frame->
ip - func->
chunk.code;
905 if (func->
obj.
flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS) {
911 for (
short int i = 0; i < func->
keywordArgs; ++i) {
917 if (func->
obj.
flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS) {
936 KRK_Function(isinstance) {
937 FUNCTION_TAKES_EXACTLY(2);
938 if (IS_CLASS(argv[1])) {
940 }
else if (IS_TUPLE(argv[1])) {
941 for (
size_t i = 0; i < AS_TUPLE(argv[1])->values.count; ++i) {
942 if (IS_CLASS(AS_TUPLE(argv[1])->values.values[i]) &&
krk_isInstanceOf(argv[0], AS_CLASS(AS_TUPLE(argv[1])->values.values[i]))) {
943 return BOOLEAN_VAL(1);
946 return BOOLEAN_VAL(0);
948 return TYPE_ERROR(
class or tuple,argv[1]);
952 KRK_Function(issubclass) {
953 FUNCTION_TAKES_EXACTLY(2);
955 if (IS_CLASS(argv[1])) {
956 return BOOLEAN_VAL(krk_isSubClass(cls, AS_CLASS(argv[1])));
957 }
else if (IS_TUPLE(argv[1])) {
958 for (
size_t i = 0; i < AS_TUPLE(argv[1])->values.count; ++i) {
959 if (IS_CLASS(AS_TUPLE(argv[1])->values.values[i]) && krk_isSubClass(cls, AS_CLASS(AS_TUPLE(argv[1])->values.values[i]))) {
960 return BOOLEAN_VAL(1);
963 return BOOLEAN_VAL(0);
965 return TYPE_ERROR(
class or tuple,argv[1]);
969 #define IS_module(o) (IS_INSTANCE(o))
970 #define AS_module(o) (AS_INSTANCE(o))
972 KRK_Method(module,__repr__) {
974 krk_tableGet(&self->fields,
vm.specialMethodNames[METHOD_NAME], &name);
976 if (!IS_STRING(name)) {
977 return OBJECT_VAL(S(
"<module>"));
981 krk_tableGet(&self->fields,
vm.specialMethodNames[METHOD_FILE], &file);
985 if (!krk_pushStringBuilderFormat(&sb,
"<module '%S' ", AS_STRING(name)))
goto _error;
987 if (IS_STRING(file)) {
988 if (!krk_pushStringBuilderFormat(&sb,
"from %R>", file))
goto _error;
990 if (!krk_pushStringBuilderFormat(&sb,
"(built-in)>"))
goto _error;
999 #define IS_Helper(o) (krk_isInstanceOf(o, KRK_BASE_CLASS(Helper)))
1000 #define AS_Helper(o) (AS_INSTANCE(o))
1001 #define IS_LicenseReader(o) (krk_isInstanceOf(o, KRK_BASE_CLASS(LicenseReader)))
1002 #define AS_LicenseReader(o) (AS_INSTANCE(o))
1004 KRK_Method(Helper,__repr__) {
1005 return OBJECT_VAL(S(
"Type help() for more help, or help(obj) to describe an object."));
1008 KRK_Method(Helper,__call__) {
1009 METHOD_TAKES_AT_MOST(1);
1015 krk_tableGet(&AS_INSTANCE(helpModule)->fields, OBJECT_VAL(S(
"simple")), &callable);
1017 krk_tableGet(&AS_INSTANCE(helpModule)->fields, OBJECT_VAL(S(
"interactive")), &callable);
1020 if (!IS_NONE(callable)) {
1029 KRK_Method(LicenseReader,__repr__) {
1030 return OBJECT_VAL(S(
"Copyright 2020-2022 K. Lange <klange@toaruos.org>. Type `license()` for more information."));
1033 KRK_Method(LicenseReader,__call__) {
1034 METHOD_TAKES_NONE();
1039 krk_tableGet(&AS_INSTANCE(helpModule)->fields, OBJECT_VAL(S(
"__licenseText")), &text);
1041 if (IS_STRING(text)) {
1042 printf(
"%s\n", AS_CSTRING(text));
1049 #define IS_property(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(property)))
1050 #define AS_property(o) (AS_INSTANCE(o))
1058 static void _property_gcscan(
KrkInstance *_self) {
1064 KRK_Method(property,__init__) {
1065 METHOD_TAKES_AT_LEAST(1);
1066 METHOD_TAKES_AT_MOST(2);
1069 ((
struct Property*)
self)->fget = IS_OBJECT(argv[1]) ? AS_OBJECT(argv[1]) : NULL;
1072 if (IS_NATIVE(argv[1])) {
1074 AS_NATIVE(argv[1])->doc ? OBJECT_VAL(
krk_copyString(AS_NATIVE(argv[1])->doc, strlen(AS_NATIVE(argv[1])->doc))) : NONE_VAL());
1075 }
else if (IS_CLOSURE(argv[1])) {
1077 AS_CLOSURE(argv[1])->function->docstring ? OBJECT_VAL(AS_CLOSURE(argv[1])->function->docstring) : NONE_VAL());
1081 if (IS_NATIVE(argv[1])) {
1083 AS_NATIVE(argv[1])->name ? OBJECT_VAL(
krk_copyString(AS_NATIVE(argv[1])->name, strlen(AS_NATIVE(argv[1])->name))) : NONE_VAL());
1084 }
else if (IS_CLOSURE(argv[1])) {
1086 AS_CLOSURE(argv[1])->function->name ? OBJECT_VAL(AS_CLOSURE(argv[1])->function->name) : NONE_VAL());
1091 ((
struct Property*)
self)->fset = IS_OBJECT(argv[2]) ? AS_OBJECT(argv[2]) : NULL;
1097 KRK_Method(property,setter) {
1098 METHOD_TAKES_EXACTLY(1);
1103 KRK_Method(property,__get__) {
1104 METHOD_TAKES_AT_LEAST(1);
1106 if (IS_NONE(argv[1]))
return argv[0];
1116 if (!
krk_tableGet(&self->fields, OBJECT_VAL(S(
"fget")), &fget))
1117 return krk_runtimeError(
vm.exceptions->attributeError,
"'%T' object has no attribute '%s'", argv[0],
"fget");
1124 KRK_Method(property,__set__) {
1125 METHOD_TAKES_EXACTLY(2);
1136 if (
krk_tableGet(&self->fields, OBJECT_VAL(S(
"fset")), &fset)) {
1150 if (
krk_tableGet(&self->fields, OBJECT_VAL(S(
"fget")), &fget)) {
1160 KRK_Method(property,__setattr__) {
1161 METHOD_TAKES_EXACTLY(2);
1162 if (!IS_STRING(argv[1]))
return TYPE_ERROR(str,argv[1]);
1167 krk_tableGet(&self->fields, OBJECT_VAL(S(
"fget")), &fget);
1168 asProp->fget = (IS_CLOSURE(fget) || IS_NATIVE(fget)) ? AS_OBJECT(fget) : NULL;
1171 krk_tableGet(&self->fields, OBJECT_VAL(S(
"fset")), &fset);
1172 asProp->fset = (IS_CLOSURE(fset) || IS_NATIVE(fset)) ? AS_OBJECT(fset) : NULL;
1197 FUNCTION_TAKES_EXACTLY(1);
1198 if (!IS_OBJECT(argv[0]))
return krk_runtimeError(
vm.exceptions->typeError,
"'%T' has no identity", argv[0]);
1199 return INTEGER_VAL((
size_t)AS_OBJECT(argv[0]));
1202 KRK_Function(hash) {
1203 FUNCTION_TAKES_EXACTLY(1);
1206 return INTEGER_VAL(hashed);
1209 KRK_Function(next) {
1210 FUNCTION_TAKES_EXACTLY(1);
1216 FUNCTION_TAKES_EXACTLY(1);
1217 if (IS_INTEGER(argv[0])) {
1218 krk_integer_type i = AS_INTEGER(argv[0]);
1219 return INTEGER_VAL(i >= 0 ? i : -i);
1220 #ifndef KRK_NO_FLOAT
1221 }
else if (IS_FLOATING(argv[0])) {
1222 double i = AS_FLOATING(argv[0]);
1223 return FLOATING_VAL(i >= 0 ? i : -i);
1226 trySlowMethod(OBJECT_VAL(S(
"__abs__")));
1227 return krk_runtimeError(
vm.exceptions->typeError,
"bad operand type for 'abs()': '%T'", argv[0]);
1231 KRK_Function(format) {
1232 FUNCTION_TAKES_AT_LEAST(1);
1233 FUNCTION_TAKES_AT_MOST(2);
1237 if (!type->_format) {
1238 return krk_runtimeError(
vm.exceptions->typeError,
"'%T' has no __format__ method", argv[0]);
1242 if (argc < 2)
krk_push(OBJECT_VAL(S(
"")));
1246 if (!IS_STRING(result)) {
1247 return krk_runtimeError(
vm.exceptions->typeError,
"__format__ result was not a string");
1253 #ifndef KRK_STATIC_ONLY
1255 if (module->libHandle) {
1256 dlClose(module->libHandle);
1261 KRK_Function(__build_class__) {
1264 KrkClass * base =
vm.baseClasses->objectClass;
1265 KrkValue metaclass = OBJECT_VAL(
vm.baseClasses->typeClass);
1268 (
const char*[]){
"func",
"name",
"base",
"metaclass"},
1270 vm.baseClasses->strClass, &name,
1271 vm.baseClasses->typeClass, &base,
1276 if (IS_CLASS(metaclass)) {
1278 if (krk_isSubClass(AS_CLASS(metaclass), basemeta)) {
1280 }
else if (krk_isSubClass(basemeta, AS_CLASS(metaclass))) {
1282 metaclass = OBJECT_VAL(basemeta);
1285 "metaclass conflict: %S is not a subclass of %S", AS_CLASS(metaclass)->name, basemeta->
name);
1338 if (IS_CLOSURE(func)) {
1339 if (AS_CLOSURE(func)->upvalueCount && AS_CLOSURE(func)->upvalues[0]->location == -1 && IS_NONE(AS_CLOSURE(func)->upvalues[0]->closed)) {
1340 AS_CLOSURE(func)->upvalues[0]->closed =
krk_peek(0);
1348 #undef CURRENT_CTYPE
1349 #define CURRENT_CTYPE KrkUpvalue *
1350 #define IS_Cell(o) (krk_isObjType((o), KRK_OBJ_UPVALUE))
1351 #define AS_Cell(o) ((KrkUpvalue*)AS_OBJECT(o))
1353 KRK_StaticMethod(Cell,__new__) {
1356 if (!
krk_parseArgs(
"O!|V:Cell", (
const char*[]){
"cls",
"contents"}, KRK_BASE_CLASS(
type), &
_class, &contents)) {
1359 if (
_class != KRK_BASE_CLASS(Cell)) {
1365 return OBJECT_VAL(out);
1368 #define UPVALUE_LOCATION(upvalue) (upvalue->location == -1 ? &upvalue->closed : &upvalue->owner->stack[upvalue->location])
1369 KRK_Method(Cell,__repr__) {
1372 KrkValue contents = *UPVALUE_LOCATION(
self);
1374 if (!krk_pushStringBuilderFormat(&sb,
"<cell at %p: %T object", (
void*)
self, contents))
goto _error;
1375 if (IS_OBJECT(contents)) {
1376 if (!krk_pushStringBuilderFormat(&sb,
" at %p>", (
void*)AS_OBJECT(contents)))
goto _error;
1388 KRK_Method(Cell,cell_contents) {
1390 *UPVALUE_LOCATION(
self) = argv[1];
1392 return *UPVALUE_LOCATION(
self);
1396 void _createAndBind_builtins(
void) {
1398 krk_push(OBJECT_VAL(
vm.baseClasses->objectClass));
1400 KrkClass *
object =
vm.baseClasses->objectClass;
1401 BIND_METHOD(
object,__dir__);
1402 BIND_METHOD(
object,__str__);
1403 BIND_METHOD(
object,__hash__);
1404 BIND_METHOD(
object,__eq__);
1405 BIND_METHOD(
object,__format__);
1406 BIND_STATICMETHOD(
object,__setattr__);
1407 BIND_STATICMETHOD(
object,__new__);
1408 BIND_METHOD(
object,__init__);
1409 BIND_CLASSMETHOD(
object,__init_subclass__);
1413 "@brief Base class for all types.\n\n"
1414 "The @c object base class provides the fallback implementations of methods like "
1415 "@ref object___dir__ \"__dir__\". All object and primitive types eventually inherit from @c object."
1418 vm.baseClasses->moduleClass =
krk_newClass(S(
"module"),
vm.baseClasses->objectClass);
1420 KrkClass * module =
vm.baseClasses->moduleClass;
1426 BIND_METHOD(module,__repr__);
1429 KRK_DOC(module,
"Type of imported modules and packages.");
1440 "@brief Internal module containing built-in functions and classes.\n\n"
1441 "Classes and functions from the @c \\__builtins__ module are generally available from "
1442 "all global namespaces. Built-in names can still be shadowed by module-level globals "
1443 "and function-level locals, so none the names in this module are reserved. When "
1444 "a built-in name has been shadowed, the original can be referenced directly as "
1445 " @c \\__builtins__.name instead.\n\n"
1446 "Built-in names may be bound from several sources. Most come from the core interpreter "
1447 "directly, but some may come from loaded C extension modules or the interpreter binary. "
1448 "Kuroko source modules are also free to append new names to the built-in name space by "
1449 "attaching new properties to the @c \\__builtins__ instance."
1452 KrkClass *
property = ADD_BASE_CLASS(KRK_BASE_CLASS(property),
"property",
object);
1453 property->allocSize =
sizeof(
struct Property);
1454 property->_ongcscan = _property_gcscan;
1455 KRK_DOC(BIND_METHOD(property,__init__),
1456 "@brief Create a property object.\n"
1457 "@arguments fget,[fset]\n\n"
1458 "When a property object is obtained from an instance of the class in which it is defined, "
1459 "the function or method assigned to @p fget is called with the instance as an argument. "
1460 "If @p fset is provided, it will be called with the instance and a value when the property "
1461 "object is assigned to through an instance. For legacy compatibility reasons, a property "
1462 "object's @p fget method may also accept an additional argument to act as a setter if "
1463 "@p fset is not provided, but this functionality may be removed in the future.\n\n"
1464 "The typical use for @c property is as a decorator on methods in a class. See also "
1465 "@ref property_setter \"property.setter\" for the newer Python-style approach to decorating a companion "
1467 BIND_METHOD(property,__get__);
1468 BIND_METHOD(property,__set__);
1469 KRK_DOC(BIND_METHOD(property,setter),
1470 "@brief Assign the setter method of a property object.\n"
1471 "@arguments fset\n\n"
1472 "This should be used as a decorator from an existing property object as follows:\n\n"
1479 " def bar(self, val):\n"
1480 " print('setting bar to',val)\n"
1482 "Be sure to apply the decorator to a function or method with the same name, as this "
1483 "name will be used to assign the property to the class's attribute table; using a "
1484 "different name will create a duplicate alias.");
1488 BIND_PROP(
object,__class__);
1490 KrkClass * Helper = ADD_BASE_CLASS(KRK_BASE_CLASS(Helper),
"Helper",
object);
1492 "@brief Special object that prints a helpeful message.\n\n"
1493 "Object that prints help summary when passed to @ref repr.");
1494 KRK_DOC(BIND_METHOD(Helper,__call__),
1495 "@brief Prints help text.\n"
1496 "@arguments obj=None\n\n"
1497 "Prints the help documentation attached to @p obj or starts the interactive help system by "
1498 "importing the @ref mod_help module.");
1499 BIND_METHOD(Helper,__repr__);
1503 KrkClass * LicenseReader = ADD_BASE_CLASS(KRK_BASE_CLASS(LicenseReader),
"LicenseReader",
object);
1504 KRK_DOC(LicenseReader,
"Special object that prints Kuroko's copyright information when passed to @ref repr");
1505 KRK_DOC(BIND_METHOD(LicenseReader,__call__),
"Print the full license statement.");
1506 BIND_METHOD(LicenseReader,__repr__);
1510 KrkClass * map = ADD_BASE_CLASS(KRK_BASE_CLASS(map),
"map",
object);
1511 KRK_DOC(map,
"Return an iterator that applies a function to a series of iterables");
1512 BIND_METHOD(map,__init__);
1513 BIND_METHOD(map,__iter__);
1514 BIND_METHOD(map,__call__);
1517 KrkClass * zip = ADD_BASE_CLASS(KRK_BASE_CLASS(zip),
"zip",
object);
1519 "@brief Returns an iterator that produces tuples of the nth element of each passed iterable.\n"
1520 "@arguments *iterables\n\n"
1521 "Creates an iterator that returns a tuple of elements from each of @p iterables, until one "
1522 "of @p iterables is exhuasted.");
1523 BIND_METHOD(zip,__init__);
1524 BIND_METHOD(zip,__iter__);
1525 BIND_METHOD(zip,__call__);
1528 KrkClass * filter = ADD_BASE_CLASS(KRK_BASE_CLASS(filter),
"filter",
object);
1529 KRK_DOC(filter,
"Return an iterator that returns only the items from an iterable for which the given function returns true.");
1530 BIND_METHOD(filter,__init__);
1531 BIND_METHOD(filter,__iter__);
1532 BIND_METHOD(filter,__call__);
1535 KrkClass * enumerate = ADD_BASE_CLASS(KRK_BASE_CLASS(enumerate),
"enumerate",
object);
1536 KRK_DOC(enumerate,
"Return an iterator that produces a tuple with a count the iterated values of the passed iteratable.");
1537 BIND_METHOD(enumerate,__init__);
1538 BIND_METHOD(enumerate,__iter__);
1539 BIND_METHOD(enumerate,__call__);
1542 KrkClass * Cell = ADD_BASE_CLASS(KRK_BASE_CLASS(Cell),
"Cell",
object);
1544 Cell->
obj.
flags |= KRK_OBJ_FLAGS_NO_INHERIT;
1545 BIND_STATICMETHOD(Cell,__new__);
1546 BIND_METHOD(Cell,__repr__);
1547 BIND_PROP(Cell,cell_contents);
1550 BUILTIN_FUNCTION(
"isinstance", FUNC_NAME(krk,isinstance),
1551 "@brief Check if an object is an instance of a type.\n"
1552 "@arguments inst, cls\n\n"
1553 "Determine if an object @p inst is an instance of the given class @p cls or one if its subclasses. "
1554 "@p cls may be a single class or a tuple of classes.");
1555 BUILTIN_FUNCTION(
"issubclass", FUNC_NAME(krk,issubclass),
1556 "@brief Check if a class is a subclass of a type.\n"
1557 "@arguments cls, clsinfo\n\n"
1558 "Determine if the class @p cls is a subclass of the class @p clsinfo. @p clsinfo may be a single "
1559 "class or a tuple of classes.");
1560 BUILTIN_FUNCTION(
"globals", FUNC_NAME(krk,globals),
1561 "@brief Update and a return a mapping of names in the global namespace.\n\n"
1562 "Produces a dict mapping all of the names of the current globals namespace to their values. "
1563 "Updating this dict has no meaning, but modifying mutable values within it can affect the global namespace.");
1564 BUILTIN_FUNCTION(
"locals", FUNC_NAME(krk,locals),
1565 "@brief Update and return a mapping of names in the current local scope.\n"
1566 "@arguments callDepth=1\n\n"
1567 "Produces a dict mapping the names of the requested locals scope to their current stack values. "
1568 "If @p callDepth is provided, the locals of an outer call frame will be returned. If the requested "
1569 "call depth is out of range, an exception will be raised.");
1570 BUILTIN_FUNCTION(
"dir", FUNC_NAME(krk,dir),
1571 "@brief Return a list of known property names for a given object.\n"
1572 "@arguments [obj]\n\n"
1573 "Uses various internal methods to collect a list of property names of @p obj, returning "
1574 "that list sorted lexicographically. If no argument is given, the returned list will "
1575 "be the valid global names in the calling scope.");
1576 BUILTIN_FUNCTION(
"len", FUNC_NAME(krk,len),
1577 "@brief Return the length of a given sequence object.\n"
1578 "@arguments seq\n\n"
1579 "Returns the length of the sequence object @p seq, which must implement @c __len__.");
1580 BUILTIN_FUNCTION(
"repr", FUNC_NAME(krk,repr),
1581 "@brief Produce a string representation of the given object.\n"
1582 "@arguments val\n\n"
1583 "Return a string representation of the given object through its @c __repr__ method. "
1584 "@c repr strings should convey all information needed to recreate the object, if this is possible.");
1585 BUILTIN_FUNCTION(
"print", FUNC_NAME(krk,print),
1586 "@brief Print text to the standard output.\n"
1587 "@arguments *args,sep=' ',end='\\n'\n\n"
1588 "Prints the string representation of each argument to the standard output. "
1589 "The keyword argument @p sep specifies the string to print between values. "
1590 "The keyword argument @p end specifies the string to print after all of the values have been printed.");
1591 BUILTIN_FUNCTION(
"ord", FUNC_NAME(krk,ord),
1592 "@brief Obtain the ordinal integer value of a codepoint or byte.\n"
1593 "@arguments char\n\n"
1594 "Returns the integer codepoint value of a single-character string @p char.");
1595 BUILTIN_FUNCTION(
"chr", FUNC_NAME(krk,chr),
1596 "@brief Convert an integer codepoint to its string representation.\n"
1597 "@arguments codepoint\n\n"
1598 "Creates a single-codepoint string with the character represented by the integer codepoint @p codepoint.");
1599 BUILTIN_FUNCTION(
"hex", FUNC_NAME(krk,hex),
1600 "@brief Convert an integer value to a hexadecimal string.\n"
1602 "Returns a string representation of @p i in hexadecimal, with a leading @c 0x.");
1603 BUILTIN_FUNCTION(
"oct", FUNC_NAME(krk,oct),
1604 "@brief Convert an integer value to an octal string.\n"
1606 "Returns a string representation of @p i in octal, with a leading @c 0o.");
1607 BUILTIN_FUNCTION(
"any", FUNC_NAME(krk,any),
1608 "@brief Returns True if at least one element in the given iterable is truthy, False otherwise.\n"
1609 "@arguments iterable");
1610 BUILTIN_FUNCTION(
"all", FUNC_NAME(krk,all),
1611 "@brief Returns True if every element in the given iterable is truthy, False otherwise.\n"
1612 "@arguments iterable");
1613 BUILTIN_FUNCTION(
"getattr", FUNC_NAME(krk,getattr),
1614 "@brief Perform attribute lookup on an object using a string.\n"
1615 "@arguments obj,attribute,[default]\n\n"
1616 "Obtains the attributed named @p attribute from the object @p obj, if such an "
1617 "attribute exists. Attribute lookup ordering is complex and includes direct "
1618 "attribute tables of instances, dynamic attributes from classes, and so on. "
1619 "The use of @c getattr is equivalent to a dotted access. If @p attribute refers "
1620 "to a method of @p obj's class, a bound method will be obtained. If @p default "
1621 "is provided then the value supplied will be returned in the case where @p obj "
1622 "does not have an attribute named @p attribute, otherwise an @ref AttributeError "
1624 BUILTIN_FUNCTION(
"setattr", FUNC_NAME(krk,setattr),
1625 "@brief Set an attribute of an object using a string name.\n"
1626 "@arguments obj,attribute,value\n\n"
1627 "Sets the attribute named by @p attribute of the object @p obj to @p value. "
1628 "If @p attribute refers to a @ref property object or other descriptor, the "
1629 "descriptor's @c \\__set__ method will be called. If @p obj is a class, instance, "
1630 "or other type with its own attribute table, then the field will be updated. If "
1631 "@p obj is a type without an attribute table and no class property provides an "
1632 "overriding setter for @p attribute, an @ref AttributeError will be raised.");
1633 BUILTIN_FUNCTION(
"hasattr", FUNC_NAME(krk,hasattr),
1634 "@brief Determines if an object has an attribute.\n"
1635 "@arguments obj,attribute\n\n"
1636 "Uses @ref getattr to determine if @p obj has an attribute named @p attribute.");
1637 BUILTIN_FUNCTION(
"delattr", FUNC_NAME(krk,delattr),
1638 "@brief Delete an attribute by name.\n"
1639 "@arguments obj,attribute\n\n"
1640 "Deletes the attribute @p attribute from @p obj.");
1641 BUILTIN_FUNCTION(
"sum", FUNC_NAME(krk,sum),
1642 "@brief add the elements of an iterable.\n"
1643 "@arguments iterable,start=0\n\n"
1644 "Continuously adds all of the elements from @p iterable to @p start and returns the result "
1645 "when @p iterable has been exhausted.");
1646 BUILTIN_FUNCTION(
"min", FUNC_NAME(krk,min),
1647 "@brief Return the lowest value in an iterable or the passed arguments.\n"
1648 "@arguments iterable");
1649 BUILTIN_FUNCTION(
"max", FUNC_NAME(krk,max),
1650 "@brief Return the highest value in an iterable or the passed arguments.\n"
1651 "@arguments iterable");
1652 BUILTIN_FUNCTION(
"id", FUNC_NAME(krk,
id),
1653 "@brief Returns the identity of an object.\n"
1654 "@arguments val\n\n"
1655 "Returns the internal identity for @p val. Note that not all objects have "
1656 "identities; primitive values such as @c int or @c float do not have identities. "
1657 "Internally, this is the pointer value for a heap object, but this is an implementation detail.");
1658 BUILTIN_FUNCTION(
"hash", FUNC_NAME(krk,hash),
1659 "@brief Returns the hash of a value, used for table indexing.\n"
1660 "@arguments val\n\n"
1661 "If @p val is hashable, its hash value will be calculated if necessary and returned. "
1662 "If @p val is not hashable, @ref TypeError will be raised.");
1663 BUILTIN_FUNCTION(
"bin", FUNC_NAME(krk,bin),
1664 "@brief Convert an integer value to a binary string.\n"
1666 "Produces a string representation of @p i in binary, with a leading @p 0b.");
1667 BUILTIN_FUNCTION(
"next", FUNC_NAME(krk,next),
1668 "@brief Compatibility function. Calls an iterable.\n"
1669 "@arguments iterable");
1670 BUILTIN_FUNCTION(
"abs", FUNC_NAME(krk,abs),
1671 "@brief Obtain the absolute value of a numeric.\n"
1672 "@arguments iterable");
1673 BUILTIN_FUNCTION(
"format", FUNC_NAME(krk,format),
1674 "@brief Format a value for string printing.\n"
1675 "@arguments value[,format_spec]");
1676 BUILTIN_FUNCTION(
"__build_class__", FUNC_NAME(krk,__build_class__),
1677 "@brief Internal function to build a type object.\n"
1678 "@arguments func, name, base=object, metaclass=type");
Functions for debugging bytecode execution.
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Functions for dealing with garbage collection and memory allocation.
void krk_markObject(KrkObj *object)
During a GC scan cycle, mark an object as used.
Represents a managed call state in a VM thread.
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.
KrkClass * krk_newClass(KrkString *name, KrkClass *base)
Create a new class object.
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 * _init
__init__ Implicitly called when an instance is created
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 * _iter
__iter__ Called by for ... in ..., etc.
KrkTable methods
General attributes table.
KrkObj * _dir
__dir__ Overrides normal behavior for dir()
void krk_finalizeClass(KrkClass *_class)
Finalize a class by collecting pointers to core methods.
struct KrkClass * _class
Metaclass.
KrkCodeObject * function
The codeobject containing the bytecode run when this function is called.
unsigned short potentialPositionals
Precalculated positional arguments for complex argument processing.
KrkChunk chunk
Bytecode data.
KrkValueArray positionalArgNames
Array of names for positional arguments (and *args)
KrkLocalEntry * localNames
Stores the names of local variables used in the function, for debugging.
unsigned short keywordArgs
Arity of keyword (default) arguments.
KrkValueArray keywordArgNames
Array of names for keyword-only arguments (and **kwargs)
size_t localNameCount
Number of entries in localNames.
KrkValue krk_dict_of(int argc, const KrkValue argv[], int hasKw)
Create a dict object.
KrkInstance * krk_newInstance(KrkClass *_class)
Create a new instance of the given class.
KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw)
Create a list object.
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.
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.
Immutable sequence of Unicode codepoints.
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.
char * chars
UTF8 canonical data.
One (key,value) pair in a table.
Simple hash table of arbitrary keys to values.
int krk_tableGet(KrkTable *table, KrkValue key, KrkValue *value)
Obtain the value associated with a key in a table.
int krk_tableSet(KrkTable *table, KrkValue key, KrkValue value)
Assign a value to a key in a table.
void krk_attachNamedObject(KrkTable *table, const char name[], KrkObj *obj)
Attach an object to an attribute table.
KrkNative * krk_defineNative(KrkTable *table, const char *name, NativeFn function)
Attach a native C function to an attribute table.
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value to an attribute table.
KrkNative * krk_defineNativeProperty(KrkTable *table, const char *name, NativeFn func)
Attach a native dynamic property to an attribute table.
void krk_tableAddAll(KrkTable *from, KrkTable *to)
Add all key-value pairs from 'from' into 'to'.
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.
KrkValue closed
Heap storage for closed value.
KrkUpvalue * krk_newUpvalue(int slot)
Create an upvalue slot.
void krk_writeValueArray(KrkValueArray *array, KrkValue value)
Add a value to a value array.
Stack reference or primative value.
int krk_isInstanceOf(KrkValue obj, const KrkClass *type)
Determine if a class is an instance or subclass of a given type.
int krk_valuesEqual(KrkValue a, KrkValue b)
Compare two values for equality.
void krk_printValue(FILE *f, KrkValue value)
Print a string representation of a value.
int krk_valuesSame(KrkValue a, KrkValue b)
Compare two values by identity.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
int krk_isFalsey(KrkValue value)
Determine the truth of a value.
int krk_hashValue(KrkValue value, uint32_t *hashOut)
Calculate the hash for a value.
KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw)
Create a set object.
Inline flexible string array.
Utilities for creating native bindings.
#define krk_parseArgs(f, n,...)
Parse arguments to a function while accepting keyword arguments.
KrkValue krk_discardStringBuilder(struct StringBuilder *sb)
Discard the contents of a string builder.
int krk_unpackIterable(KrkValue iterable, void *context, int callback(void *, const KrkValue *, size_t))
Unpack an iterable.
KrkValue krk_finishStringBuilder(struct StringBuilder *sb)
Finalize a string builder into a string object.
void krk_pushStringBuilder(struct StringBuilder *sb, char c)
Add a character to the end of a string builder.
#define KRK_DOC(thing, text)
Attach documentation to a thing of various types.
Definitions for primitive stack references.
Core API for the bytecode virtual machine.
int krk_getAttribute(KrkString *name)
Implementation of the GET_PROPERTY instruction.
int krk_setAttribute(KrkString *name)
Implementation of the SET_PROPERTY instruction.
KrkValue krk_callStack(int argCount)
Call a callable on the stack with argCount arguments.
#define vm
Convenience macro for namespacing.
int krk_doRecursiveModuleLoad(KrkString *name)
Load a module by a dotted name.
int krk_delAttribute(KrkString *name)
Implementation of the DEL_PROPERTY instruction.
KrkValue krk_operator_lt(KrkValue, KrkValue)
Compare two values, returning True if the left is less than the right.
KrkValue krk_pop(void)
Pop the top of the stack.
threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
KrkValue krk_valueGetAttribute_default(KrkValue value, char *name, KrkValue defaultVal)
See krk_valueGetAttribute.
void krk_swap(int distance)
Swap the top of the stack of the value distance slots down.
KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw)
Obtain a list of properties for an object.
void krk_push(KrkValue value)
Push a stack value.
KrkValue krk_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.
KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString *name, KrkValue to)
Set an attribute of an instance object, bypassing __setattr__.
KrkValue krk_peek(int distance)
Peek down from the top of the stack.
KrkValue krk_operator_gt(KrkValue, KrkValue)
Compare to values, returning True if the left is greater than the right.