6 #define KRK_VERSION_MAJOR 1
7 #define KRK_VERSION_MINOR 4
8 #define KRK_VERSION_PATCH 1
9 #define KRK_VERSION_LEVEL 0xa
10 #define KRK_VERSION_SERIAL 0x0
12 #define KRK_VERSION_EXTRA_BASE "a0"
14 #ifndef KRK_STATIC_ONLY
15 #define KRK_VERSION_EXTRA KRK_VERSION_EXTRA_BASE
17 #define KRK_VERSION_EXTRA KRK_VERSION_EXTRA_BASE "-static"
20 #define KRK_BUILD_DATE __DATE__ " at " __TIME__
22 #if (defined(__GNUC__) || defined(__GNUG__)) && !(defined(__clang__) || defined(__INTEL_COMPILER))
23 # define KRK_BUILD_COMPILER "GCC " __VERSION__
24 #elif (defined(__clang__))
25 # define KRK_BUILD_COMPILER "clang " __clang_version__
27 # define KRK_BUILD_COMPILER ""
31 #ifndef KRK_DISABLE_DEBUG
32 KRK_Function(set_tracing) {
37 "|$pp", (
const char *[]){
"tracing",
"disassembly"},
38 &tracing, &disassembly)) {
42 #define SET_THREAD(arg,flag) do { if (arg != -1) { if (arg) krk_currentThread.flags |= KRK_THREAD_ENABLE_ ## flag; else krk_currentThread.flags &= ~KRK_THREAD_ENABLE_ ## flag; } } while (0)
43 SET_THREAD(tracing,TRACING);
44 SET_THREAD(disassembly,DISASSEMBLY);
47 return BOOLEAN_VAL(1);
50 KRK_Function(set_tracing) {
51 return krk_runtimeError(
vm.exceptions->typeError,
"Debugging is not enabled in this build.");
55 KRK_Function(getsizeof) {
56 if (argc < 1 || !IS_OBJECT(argv[0]))
return INTEGER_VAL(0);
58 switch (AS_OBJECT(argv[0])->type) {
59 case KRK_OBJ_STRING: {
61 mySize +=
sizeof(
KrkString) + self->length + 1;
62 if (self->codes && self->chars != self->codes) {
63 if ((self->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) <= KRK_OBJ_FLAGS_STRING_UCS1) mySize += self->codesLength;
64 else if ((self->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) == KRK_OBJ_FLAGS_STRING_UCS2) mySize += 2 * self->codesLength;
65 else if ((self->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) == KRK_OBJ_FLAGS_STRING_UCS4) mySize += 4 * self->codesLength;
69 case KRK_OBJ_CODEOBJECT: {
73 mySize +=
sizeof(uint8_t) * self->chunk.capacity;
74 mySize +=
sizeof(
KrkLineMap) *
self->chunk.linesCapacity;
75 mySize +=
sizeof(
KrkValue) * self->chunk.constants.capacity;
77 mySize +=
sizeof(
KrkValue) *
self->positionalArgNames.capacity;
79 mySize +=
sizeof(
KrkValue) * self->keywordArgNames.capacity;
84 case KRK_OBJ_NATIVE: {
86 mySize +=
sizeof(
KrkNative) + strlen(self->name) + 1;
89 case KRK_OBJ_CLOSURE: {
94 case KRK_OBJ_UPVALUE: {
100 case KRK_OBJ_CLASS: {
101 KrkClass *
self = AS_CLASS(argv[0]);
107 case KRK_OBJ_INSTANCE: {
115 mySize +=
sizeof(
KrkValue) * AS_LIST(argv[0])->capacity;
117 mySize +=
sizeof(
KrkTableEntry) * AS_DICT(argv[0])->capacity;
121 case KRK_OBJ_BOUND_METHOD: {
125 case KRK_OBJ_TUPLE: {
126 KrkTuple *
self = AS_TUPLE(argv[0]);
130 case KRK_OBJ_BYTES: {
131 KrkBytes *
self = AS_BYTES(argv[0]);
132 mySize +=
sizeof(
KrkBytes) + self->length;
137 return INTEGER_VAL(mySize);
140 KRK_Function(set_clean_output) {
141 if (!argc || (IS_BOOLEAN(argv[0]) && AS_BOOLEAN(argv[0]))) {
142 vm.globalFlags |= KRK_GLOBAL_CLEAN_OUTPUT;
144 vm.globalFlags &= ~KRK_GLOBAL_CLEAN_OUTPUT;
149 KRK_Function(importmodule) {
150 FUNCTION_TAKES_EXACTLY(1);
151 if (!IS_STRING(argv[0]))
return TYPE_ERROR(str,argv[0]);
156 KRK_Function(modules) {
157 FUNCTION_TAKES_NONE();
160 for (
size_t i = 0; i <
vm.modules.capacity; ++i) {
162 if (IS_KWARGS(entry->key))
continue;
168 KRK_Function(unload) {
169 FUNCTION_TAKES_EXACTLY(1);
170 if (!IS_STRING(argv[0]))
return TYPE_ERROR(str,argv[0]);
177 KRK_Function(inspect_value) {
178 FUNCTION_TAKES_EXACTLY(1);
182 KRK_Function(members) {
184 if (!
krk_parseArgs(
"V", (
const char*[]){
"obj"}, &val))
return NONE_VAL();
191 if (IS_INSTANCE(val) || IS_CLASS(val)) {
192 src = &AS_INSTANCE(val)->fields;
193 }
else if (IS_CLOSURE(val)) {
194 src = &AS_CLOSURE(val)->fields;
216 KRK_DOC(
vm.system,
"@brief System module.");
218 #define STR(x) STR_(x)
220 (
KrkObj*)S(STR(KRK_VERSION_MAJOR)
"." STR(KRK_VERSION_MINOR)
"." STR(KRK_VERSION_PATCH) KRK_VERSION_EXTRA));
224 INTEGER_VAL((KRK_VERSION_MAJOR << 24) | (KRK_VERSION_MINOR << 16) | (KRK_VERSION_PATCH << 8) | (KRK_VERSION_LEVEL << 4) | (KRK_VERSION_SERIAL)));
227 "@brief Calculate the approximate size of an object in bytes.\n"
228 "@arguments value\n\n"
229 "@param value Value to examine.");
230 KRK_DOC(BIND_FUNC(
vm.system,set_clean_output),
231 "@brief Disables terminal escapes in some output from the VM.\n"
232 "@arguments clean=True\n\n"
233 "@param clean Whether to remove escapes.");
234 KRK_DOC(BIND_FUNC(
vm.system,set_tracing),
235 "@brief Toggle debugging modes.\n"
236 "@arguments tracing=None,disassembly=None\n\n"
237 "Enables or disables tracing options for the current thread.\n\n"
238 "@param tracing Enables instruction tracing.\n"
239 "@param disassembly Prints bytecode disassembly after compilation.");
240 KRK_DOC(BIND_FUNC(
vm.system,importmodule),
241 "@brief Import a module by string name\n"
242 "@arguments module\n\n"
243 "Imports the dot-separated module @p module as if it were imported by the @c import statement and returns the resulting module object.\n\n"
244 "@param module A string with a dot-separated package or module name");
246 "Get the list of valid names from the module table");
248 "Removes a module from the module table. It is not necessarily garbage collected if other references to it exist.");
249 KRK_DOC(BIND_FUNC(
vm.system,inspect_value),
250 "Obtain the memory representation of a stack value.");
252 "Obtain a copy of a dict of the direct members of an object.");
258 #ifndef KRK_NO_FILESYSTEM
261 char * dir = strdup(
vm.binpath);
263 char * slash = strrchr(dir,
'/');
264 if (slash) *slash =
'\0';
265 if (strstr(dir,
"/bin") == (dir + strlen(dir) - 4)) {
266 slash = strrchr(dir,
'/');
267 if (slash) *slash =
'\0';
273 char * backslash = strrchr(dir,
'\\');
274 if (backslash) *backslash =
'\0';
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Struct definitions for core object types.
struct KrkClass KrkClass
Type object.
struct KrkString KrkString
Immutable sequence of Unicode codepoints.
struct KrkUpvalue KrkUpvalue
Storage for values referenced from nested functions.
A function that has been attached to an object to serve as a method.
Immutable sequence of bytes.
KrkBytes * krk_newBytes(size_t length, uint8_t *source)
Create a new byte array.
size_t allocSize
Size to allocate when creating instances of this class.
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.
Map entry of instruction offsets to line numbers.
KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw)
Create a list object.
Metadata on a local variable name in a function.
Managed binding to a C function.
The most basic object type.
Immutable sequence of Unicode codepoints.
KrkString * krk_copyString(const char *chars, size_t length)
Obtain a string object representation of the given C string.
One (key,value) pair in a table.
Simple hash table of arbitrary keys to values.
int krk_tableDelete(KrkTable *table, KrkValue key)
Remove a key from a hash table.
void krk_attachNamedObject(KrkTable *table, const char name[], KrkObj *obj)
Attach an object to an attribute table.
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value 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.
Storage for values referenced from nested functions.
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.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
Utilities for creating native bindings.
#define krk_parseArgs(f, n,...)
Parse arguments to a function while accepting keyword arguments.
#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.
#define vm
Convenience macro for namespacing.
int krk_doRecursiveModuleLoad(KrkString *name)
Load a module by a dotted name.
KrkValue krk_pop(void)
Pop the top of the stack.
void krk_module_init_kuroko(void)
Initialize the built-in 'kuroko' module.
void krk_push(KrkValue value)
Push a stack value.