2020-12-26 03:32:21 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include "vm.h"
|
2020-12-26 08:33:34 +03:00
|
|
|
#include "table.h"
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
#define ALLOCATE_OBJECT(type, objectType) \
|
|
|
|
(type*)allocateObject(sizeof(type), objectType)
|
|
|
|
|
|
|
|
static KrkObj * allocateObject(size_t size, ObjType type) {
|
|
|
|
KrkObj * object = (KrkObj*)krk_reallocate(NULL, 0, size);
|
|
|
|
object->type = type;
|
2020-12-27 09:58:32 +03:00
|
|
|
object->isMarked = 0;
|
2020-12-26 03:32:21 +03:00
|
|
|
object->next = vm.objects;
|
|
|
|
vm.objects = object;
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2021-01-12 13:23:14 +03:00
|
|
|
#define UTF8_ACCEPT 0
|
|
|
|
#define UTF8_REJECT 1
|
|
|
|
|
|
|
|
static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
|
|
|
|
static int state_table[32] = {
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xxxxxxx */
|
|
|
|
1,1,1,1,1,1,1,1, /* 10xxxxxx */
|
|
|
|
2,2,2,2, /* 110xxxxx */
|
|
|
|
3,3, /* 1110xxxx */
|
|
|
|
4, /* 11110xxx */
|
|
|
|
1 /* 11111xxx */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mask_bytes[32] = {
|
|
|
|
0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
|
|
|
|
0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
|
|
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
|
0x1F,0x1F,0x1F,0x1F,
|
|
|
|
0x0F,0x0F,
|
|
|
|
0x07,
|
|
|
|
0x00
|
|
|
|
};
|
|
|
|
|
|
|
|
static int next[5] = {
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
3
|
|
|
|
};
|
|
|
|
|
|
|
|
if (*state == UTF8_ACCEPT) {
|
|
|
|
*codep = byte & mask_bytes[byte >> 3];
|
|
|
|
*state = state_table[byte >> 3];
|
|
|
|
} else if (*state > 0) {
|
|
|
|
*codep = (byte & 0x3F) | (*codep << 6);
|
|
|
|
*state = next[*state];
|
|
|
|
}
|
|
|
|
return *state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int checkString(const char * chars, size_t length, size_t *codepointCount) {
|
|
|
|
uint32_t state = 0;
|
|
|
|
uint32_t codepoint = 0;
|
|
|
|
unsigned char * end = (unsigned char *)chars + length;
|
|
|
|
uint32_t maxCodepoint = 0;
|
|
|
|
for (unsigned char * c = (unsigned char *)chars; c < end; ++c) {
|
|
|
|
if (!decode(&state, &codepoint, *c)) {
|
|
|
|
if (codepoint > maxCodepoint) maxCodepoint = codepoint;
|
|
|
|
(*codepointCount)++;
|
|
|
|
} else if (state == UTF8_REJECT) {
|
|
|
|
state = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxCodepoint > 0xFFFF) {
|
|
|
|
return KRK_STRING_UCS4;
|
|
|
|
} else if (maxCodepoint > 0xFF) {
|
|
|
|
return KRK_STRING_UCS2;
|
|
|
|
} else if (maxCodepoint > 0x7F) {
|
|
|
|
return KRK_STRING_UCS1;
|
|
|
|
} else {
|
|
|
|
return KRK_STRING_ASCII;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GENREADY(size,type) \
|
|
|
|
static void _readyUCS ## size (KrkString * string) { \
|
|
|
|
uint32_t state = 0; \
|
|
|
|
uint32_t codepoint = 0; \
|
|
|
|
unsigned char * end = (unsigned char *)string->chars + string->length; \
|
|
|
|
string->codes = malloc(sizeof(type) * string->codesLength); \
|
|
|
|
type *outPtr = (type *)string->codes; \
|
|
|
|
for (unsigned char * c = (unsigned char *)string->chars; c < end; ++c) { \
|
|
|
|
if (!decode(&state, &codepoint, *c)) { \
|
|
|
|
*(outPtr++) = (type)codepoint; \
|
|
|
|
} else if (state == UTF8_REJECT) { \
|
|
|
|
state = 0; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
GENREADY(1,uint8_t)
|
|
|
|
GENREADY(2,uint16_t)
|
|
|
|
GENREADY(4,uint32_t)
|
|
|
|
#undef GENREADY
|
|
|
|
|
|
|
|
void * krk_unicodeString(KrkString * string) {
|
|
|
|
if (string->codes) return string->codes;
|
|
|
|
if (string->type == KRK_STRING_UCS1) _readyUCS1(string);
|
|
|
|
else if (string->type == KRK_STRING_UCS2) _readyUCS2(string);
|
|
|
|
else if (string->type == KRK_STRING_UCS4) _readyUCS4(string);
|
|
|
|
else krk_runtimeError(vm.exceptions.valueError, "Internal string error.");
|
|
|
|
return string->codes;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t krk_unicodeCodepoint(KrkString * string, size_t index) {
|
|
|
|
krk_unicodeString(string);
|
|
|
|
switch (string->type) {
|
|
|
|
case KRK_STRING_ASCII: return string->chars[index];
|
|
|
|
case KRK_STRING_UCS1: return ((uint8_t*)string->codes)[index];
|
|
|
|
case KRK_STRING_UCS2: return ((uint16_t*)string->codes)[index];
|
|
|
|
case KRK_STRING_UCS4: return ((uint32_t*)string->codes)[index];
|
|
|
|
}
|
|
|
|
krk_runtimeError(vm.exceptions.valueError, "Invalid string.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:33:34 +03:00
|
|
|
static KrkString * allocateString(char * chars, size_t length, uint32_t hash) {
|
2020-12-26 03:32:21 +03:00
|
|
|
KrkString * string = ALLOCATE_OBJECT(KrkString, OBJ_STRING);
|
|
|
|
string->length = length;
|
|
|
|
string->chars = chars;
|
2020-12-26 08:33:34 +03:00
|
|
|
string->hash = hash;
|
2021-01-12 13:23:14 +03:00
|
|
|
string->codesLength = 0;
|
|
|
|
string->type = checkString(chars,length,&string->codesLength);
|
|
|
|
string->codes = NULL;
|
|
|
|
if (string->type == KRK_STRING_ASCII) string->codes = string->chars;
|
2020-12-27 09:58:32 +03:00
|
|
|
krk_push(OBJECT_VAL(string));
|
2020-12-26 08:33:34 +03:00
|
|
|
krk_tableSet(&vm.strings, OBJECT_VAL(string), NONE_VAL());
|
2020-12-27 09:58:32 +03:00
|
|
|
krk_pop();
|
2020-12-26 03:32:21 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:33:34 +03:00
|
|
|
static uint32_t hashString(const char * key, size_t length) {
|
|
|
|
uint32_t hash = 0;
|
|
|
|
/* This is the so-called "sdbm" hash. It comes from a piece of
|
|
|
|
* public domain code from a clone of ndbm. */
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
hash = (int)key[i] + (hash << 6) + (hash << 16) - hash;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * krk_takeString(char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * interned = krk_tableFindString(&vm.strings, chars, length, hash);
|
2020-12-26 08:33:34 +03:00
|
|
|
if (interned != NULL) {
|
|
|
|
FREE_ARRAY(char, chars, length + 1);
|
|
|
|
return interned;
|
|
|
|
}
|
|
|
|
return allocateString(chars, length, hash);
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * krk_copyString(const char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * interned = krk_tableFindString(&vm.strings, chars, length, hash);
|
2020-12-27 09:58:32 +03:00
|
|
|
if (interned) return interned;
|
2020-12-26 03:32:21 +03:00
|
|
|
char * heapChars = ALLOCATE(char, length + 1);
|
|
|
|
memcpy(heapChars, chars, length);
|
|
|
|
heapChars[length] = '\0';
|
2020-12-26 08:33:34 +03:00
|
|
|
return allocateString(heapChars, length, hash);
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkFunction * krk_newFunction() {
|
2020-12-27 03:33:28 +03:00
|
|
|
KrkFunction * function = ALLOCATE_OBJECT(KrkFunction, OBJ_FUNCTION);
|
2020-12-30 02:00:48 +03:00
|
|
|
function->requiredArgs = 0;
|
2021-01-03 06:09:41 +03:00
|
|
|
function->keywordArgs = 0;
|
2020-12-27 07:02:26 +03:00
|
|
|
function->upvalueCount = 0;
|
2020-12-27 03:33:28 +03:00
|
|
|
function->name = NULL;
|
2020-12-30 10:59:21 +03:00
|
|
|
function->docstring = NULL;
|
2021-01-03 10:02:50 +03:00
|
|
|
function->collectsArguments = 0;
|
|
|
|
function->collectsKeywords = 0;
|
2021-01-06 09:03:56 +03:00
|
|
|
function->localNameCount = 0;
|
|
|
|
function->localNames = NULL;
|
2021-01-07 03:38:07 +03:00
|
|
|
function->globalsContext = NULL;
|
2021-01-03 06:09:41 +03:00
|
|
|
krk_initValueArray(&function->requiredArgNames);
|
|
|
|
krk_initValueArray(&function->keywordArgNames);
|
2020-12-27 03:33:28 +03:00
|
|
|
krk_initChunk(&function->chunk);
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:01:58 +03:00
|
|
|
KrkNative * krk_newNative(NativeFn function, const char * name, int type) {
|
2020-12-27 03:33:28 +03:00
|
|
|
KrkNative * native = ALLOCATE_OBJECT(KrkNative, OBJ_NATIVE);
|
|
|
|
native->function = function;
|
2021-01-01 10:01:58 +03:00
|
|
|
native->isMethod = type;
|
|
|
|
native->name = name;
|
2020-12-27 03:33:28 +03:00
|
|
|
return native;
|
|
|
|
}
|
2020-12-27 07:02:26 +03:00
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkClosure * krk_newClosure(KrkFunction * function) {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue ** upvalues = ALLOCATE(KrkUpvalue*, function->upvalueCount);
|
|
|
|
for (size_t i = 0; i < function->upvalueCount; ++i) {
|
|
|
|
upvalues[i] = NULL;
|
|
|
|
}
|
|
|
|
KrkClosure * closure = ALLOCATE_OBJECT(KrkClosure, OBJ_CLOSURE);
|
|
|
|
closure->function = function;
|
|
|
|
closure->upvalues = upvalues;
|
|
|
|
closure->upvalueCount = function->upvalueCount;
|
|
|
|
return closure;
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:02:39 +03:00
|
|
|
KrkUpvalue * krk_newUpvalue(int slot) {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue * upvalue = ALLOCATE_OBJECT(KrkUpvalue, OBJ_UPVALUE);
|
|
|
|
upvalue->location = slot;
|
|
|
|
upvalue->next = NULL;
|
|
|
|
upvalue->closed = NONE_VAL();
|
|
|
|
return upvalue;
|
|
|
|
}
|
2020-12-27 10:45:34 +03:00
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkClass * krk_newClass(KrkString * name) {
|
2020-12-27 10:45:34 +03:00
|
|
|
KrkClass * _class = ALLOCATE_OBJECT(KrkClass, OBJ_CLASS);
|
|
|
|
_class->name = name;
|
2020-12-28 06:16:44 +03:00
|
|
|
_class->filename = NULL;
|
2020-12-30 10:59:21 +03:00
|
|
|
_class->docstring = NULL;
|
2020-12-31 03:15:53 +03:00
|
|
|
_class->base = NULL;
|
2020-12-27 11:53:46 +03:00
|
|
|
krk_initTable(&_class->methods);
|
2021-01-11 10:31:34 +03:00
|
|
|
krk_initTable(&_class->fields);
|
2021-01-05 03:30:23 +03:00
|
|
|
|
|
|
|
_class->_getter = NULL;
|
|
|
|
_class->_setter = NULL;
|
|
|
|
_class->_slicer = NULL;
|
|
|
|
_class->_reprer = NULL;
|
|
|
|
_class->_tostr = NULL;
|
|
|
|
_class->_call = NULL;
|
|
|
|
_class->_init = NULL;
|
2021-01-07 04:39:09 +03:00
|
|
|
_class->_eq = NULL;
|
2021-01-05 03:30:23 +03:00
|
|
|
|
2020-12-27 10:45:34 +03:00
|
|
|
return _class;
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkInstance * krk_newInstance(KrkClass * _class) {
|
2020-12-27 10:45:34 +03:00
|
|
|
KrkInstance * instance = ALLOCATE_OBJECT(KrkInstance, OBJ_INSTANCE);
|
|
|
|
instance->_class = _class;
|
|
|
|
krk_initTable(&instance->fields);
|
2021-01-11 13:11:14 +03:00
|
|
|
krk_push(OBJECT_VAL(instance));
|
2021-01-11 10:31:34 +03:00
|
|
|
krk_tableAddAll(&_class->fields, &instance->fields);
|
2021-01-11 13:11:14 +03:00
|
|
|
krk_pop();
|
2021-01-05 03:30:23 +03:00
|
|
|
instance->_internal = NULL; /* To be used by C-defined types to track internal objects. */
|
2020-12-27 10:45:34 +03:00
|
|
|
return instance;
|
|
|
|
}
|
2020-12-27 11:53:46 +03:00
|
|
|
|
2020-12-29 14:25:34 +03:00
|
|
|
KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj * method) {
|
2020-12-27 11:53:46 +03:00
|
|
|
KrkBoundMethod * bound = ALLOCATE_OBJECT(KrkBoundMethod, OBJ_BOUND_METHOD);
|
|
|
|
bound->receiver = receiver;
|
|
|
|
bound->method = method;
|
|
|
|
return bound;
|
|
|
|
}
|
2021-01-05 05:38:11 +03:00
|
|
|
|
|
|
|
KrkTuple * krk_newTuple(size_t length) {
|
|
|
|
KrkTuple * tuple = ALLOCATE_OBJECT(KrkTuple, OBJ_TUPLE);
|
|
|
|
tuple->inrepr = 0;
|
|
|
|
krk_initValueArray(&tuple->values);
|
|
|
|
krk_push(OBJECT_VAL(tuple));
|
|
|
|
tuple->values.capacity = length;
|
|
|
|
tuple->values.values = GROW_ARRAY(KrkValue,NULL,0,length);
|
|
|
|
krk_pop();
|
|
|
|
return tuple;
|
|
|
|
}
|
2021-01-12 13:23:14 +03:00
|
|
|
|
|
|
|
void krk_bytesUpdateHash(KrkBytes * bytes) {
|
|
|
|
bytes->hash = hashString((char*)bytes->bytes, bytes->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
KrkBytes * krk_newBytes(size_t length, uint8_t * source) {
|
|
|
|
KrkBytes * bytes = ALLOCATE_OBJECT(KrkBytes, OBJ_BYTES);
|
|
|
|
bytes->length = length;
|
|
|
|
bytes->bytes = NULL;
|
|
|
|
krk_push(OBJECT_VAL(bytes));
|
|
|
|
bytes->bytes = ALLOCATE(uint8_t, length);
|
|
|
|
bytes->hash = -1;
|
|
|
|
if (source) {
|
|
|
|
memcpy(bytes->bytes, source, length);
|
|
|
|
krk_bytesUpdateHash(bytes);
|
|
|
|
}
|
|
|
|
krk_pop();
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|