2021-02-07 15:40:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021 Thakee Nathees
|
|
|
|
* Licensed under: MIT License
|
|
|
|
*/
|
|
|
|
|
2021-02-15 20:49:19 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-05-03 21:09:23 +08:00
|
|
|
#include "utils.h"
|
2021-02-07 15:40:00 +08:00
|
|
|
#include "var.h"
|
|
|
|
#include "vm.h"
|
|
|
|
|
2021-02-25 17:03:06 +08:00
|
|
|
// Public Api /////////////////////////////////////////////////////////////////
|
2021-05-09 18:28:00 +08:00
|
|
|
Var pkVarBool(PKVM* vm, bool value) {
|
2021-02-25 17:03:06 +08:00
|
|
|
return VAR_BOOL(value);
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var pkVarNumber(PKVM* vm, double value) {
|
2021-02-25 17:03:06 +08:00
|
|
|
return VAR_NUM(value);
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var pkVarString(PKVM* vm, const char* value) {
|
2021-05-06 22:19:30 +08:00
|
|
|
return VAR_OBJ(newStringLength(vm, value, (uint32_t)strlen(value)));
|
2021-02-25 17:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
bool pkAsBool(PKVM* vm, Var value) {
|
2021-02-25 17:03:06 +08:00
|
|
|
return AS_BOOL(value);
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
double pkAsNumber(PKVM* vm, Var value) {
|
2021-02-25 17:03:06 +08:00
|
|
|
return AS_NUM(value);
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
const char* pkAsString(PKVM* vm, Var value) {
|
2021-02-25 17:03:06 +08:00
|
|
|
return AS_STRING(value)->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Number of maximum digits for to_string buffer.
|
2021-02-12 01:35:43 +08:00
|
|
|
#define TO_STRING_BUFF_SIZE 128
|
|
|
|
|
2021-05-03 21:09:23 +08:00
|
|
|
// The maximum percentage of the map entries that can be filled before the map
|
|
|
|
// is grown. A lower percentage reduce collision which makes looks up faster
|
|
|
|
// but take more memory.
|
2021-05-05 12:55:27 +08:00
|
|
|
#define MAP_LOAD_PERCENT 75
|
2021-05-03 21:09:23 +08:00
|
|
|
|
|
|
|
// The factor a collection would grow by when it's exceeds the current capacity.
|
|
|
|
// The new capacity will be calculated by multiplying it's old capacity by the
|
|
|
|
// GROW_FACTOR.
|
|
|
|
#define GROW_FACTOR 2
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void varInitObject(Object* self, PKVM* vm, ObjectType type) {
|
2021-02-12 01:35:43 +08:00
|
|
|
self->type = type;
|
2021-04-26 17:34:30 +08:00
|
|
|
self->is_marked = false;
|
2021-02-12 01:35:43 +08:00
|
|
|
self->next = vm->first;
|
|
|
|
vm->first = self;
|
2021-04-26 17:34:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void grayObject(Object* self, PKVM* vm) {
|
2021-04-26 17:34:30 +08:00
|
|
|
if (self == NULL || self->is_marked) return;
|
|
|
|
self->is_marked = true;
|
|
|
|
|
|
|
|
// Add the object to the VM's gray_list so that we can recursively mark
|
|
|
|
// it's referenced objects later.
|
2021-05-01 18:13:39 +08:00
|
|
|
if (vm->gray_list_count >= vm->gray_list_capacity) {
|
|
|
|
vm->gray_list_capacity *= 2;
|
|
|
|
vm->gray_list = (Object**)vm->config.realloc_fn(
|
|
|
|
vm->gray_list,
|
|
|
|
vm->gray_list_capacity * sizeof(Object*),
|
2021-04-26 17:34:30 +08:00
|
|
|
vm->config.user_data);
|
|
|
|
}
|
|
|
|
|
2021-05-01 18:13:39 +08:00
|
|
|
vm->gray_list[vm->gray_list_count++] = self;
|
2021-04-26 17:34:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void grayValue(Var self, PKVM* vm) {
|
2021-04-26 17:34:30 +08:00
|
|
|
if (!IS_OBJ(self)) return;
|
2021-05-01 18:13:39 +08:00
|
|
|
grayObject(AS_OBJ(self), vm);
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void grayVarBuffer(VarBuffer* self, PKVM* vm) {
|
2021-05-16 17:37:52 +08:00
|
|
|
if (self == NULL) return;
|
2021-05-05 12:55:27 +08:00
|
|
|
for (uint32_t i = 0; i < self->count; i++) {
|
2021-05-01 18:13:39 +08:00
|
|
|
grayValue(self->data[i], vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:37:52 +08:00
|
|
|
#define GRAY_OBJ_BUFFER(m_name) \
|
|
|
|
void gray##m_name##Buffer(m_name##Buffer* self, PKVM* vm) { \
|
|
|
|
if (self == NULL) return; \
|
|
|
|
for (uint32_t i = 0; i < self->count; i++) { \
|
|
|
|
grayObject(&self->data[i]->_super, vm); \
|
|
|
|
} \
|
2021-05-01 18:13:39 +08:00
|
|
|
}
|
|
|
|
|
2021-05-16 17:37:52 +08:00
|
|
|
GRAY_OBJ_BUFFER(String)
|
|
|
|
GRAY_OBJ_BUFFER(Function)
|
|
|
|
|
2021-05-01 18:13:39 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
static void blackenObject(Object* obj, PKVM* vm) {
|
2021-05-01 18:13:39 +08:00
|
|
|
// TODO: trace here.
|
|
|
|
|
|
|
|
switch (obj->type) {
|
|
|
|
case OBJ_STRING: {
|
|
|
|
vm->bytes_allocated += sizeof(String);
|
|
|
|
vm->bytes_allocated += (size_t)(((String*)obj)->length + 1);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_LIST: {
|
|
|
|
List* list = (List*)obj;
|
|
|
|
grayVarBuffer(&list->elements, vm);
|
|
|
|
vm->bytes_allocated += sizeof(List);
|
|
|
|
vm->bytes_allocated += sizeof(Var) * list->elements.capacity;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_MAP: {
|
2021-05-05 12:55:27 +08:00
|
|
|
Map* map = (Map*)obj;
|
2021-05-07 17:41:19 +08:00
|
|
|
for (uint32_t i = 0; i < map->capacity; i++) {
|
2021-05-05 12:55:27 +08:00
|
|
|
if (IS_UNDEF(map->entries[i].key)) continue;
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(AS_OBJ(map->entries[i].key), vm);
|
|
|
|
grayObject(AS_OBJ(map->entries[i].value), vm);
|
2021-05-05 12:55:27 +08:00
|
|
|
}
|
|
|
|
vm->bytes_allocated += sizeof(Map);
|
|
|
|
vm->bytes_allocated += sizeof(MapEntry) * map->capacity;
|
2021-05-01 18:13:39 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_RANGE: {
|
|
|
|
vm->bytes_allocated += sizeof(Range);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_SCRIPT:
|
|
|
|
{
|
|
|
|
Script* script = (Script*)obj;
|
|
|
|
vm->bytes_allocated += sizeof(Script);
|
|
|
|
|
2021-05-18 19:21:38 +08:00
|
|
|
grayObject(&script->path->_super, vm);
|
|
|
|
grayObject(&script->moudle->_super, vm);
|
2021-05-07 17:41:19 +08:00
|
|
|
|
2021-05-01 18:13:39 +08:00
|
|
|
grayVarBuffer(&script->globals, vm);
|
|
|
|
vm->bytes_allocated += sizeof(Var) * script->globals.capacity;
|
|
|
|
|
2021-05-05 13:55:34 +08:00
|
|
|
// Integer buffer have no gray call.
|
|
|
|
vm->bytes_allocated += sizeof(uint32_t) * script->global_names.capacity;
|
2021-05-01 18:13:39 +08:00
|
|
|
|
|
|
|
grayVarBuffer(&script->literals, vm);
|
|
|
|
vm->bytes_allocated += sizeof(Var) * script->literals.capacity;
|
|
|
|
|
|
|
|
grayFunctionBuffer(&script->functions, vm);
|
|
|
|
vm->bytes_allocated += sizeof(Function*) * script->functions.capacity;
|
|
|
|
|
2021-05-05 13:55:34 +08:00
|
|
|
// Integer buffer have no gray call.
|
|
|
|
vm->bytes_allocated += sizeof(uint32_t) * script->function_names.capacity;
|
2021-05-01 18:13:39 +08:00
|
|
|
|
|
|
|
grayStringBuffer(&script->names, vm);
|
|
|
|
vm->bytes_allocated += sizeof(String*) * script->names.capacity;
|
|
|
|
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(&script->body->_super, vm);
|
2021-05-01 18:13:39 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_FUNC:
|
|
|
|
{
|
|
|
|
Function* func = (Function*)obj;
|
|
|
|
vm->bytes_allocated += sizeof(Function);
|
|
|
|
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(&func->owner->_super, vm);
|
2021-05-01 18:13:39 +08:00
|
|
|
|
|
|
|
if (!func->is_native) {
|
|
|
|
Fn* fn = func->fn;
|
|
|
|
vm->bytes_allocated += sizeof(uint8_t)* fn->opcodes.capacity;
|
|
|
|
vm->bytes_allocated += sizeof(int) * fn->oplines.capacity;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_FIBER:
|
|
|
|
{
|
|
|
|
Fiber* fiber = (Fiber*)obj;
|
|
|
|
vm->bytes_allocated += sizeof(Fiber);
|
|
|
|
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(&fiber->func->_super, vm);
|
2021-05-01 18:13:39 +08:00
|
|
|
|
|
|
|
// Blacken the stack.
|
|
|
|
for (Var* local = fiber->stack; local < fiber->sp; local++) {
|
|
|
|
grayValue(*local, vm);
|
|
|
|
}
|
|
|
|
vm->bytes_allocated += sizeof(Var) * fiber->stack_size;
|
|
|
|
|
|
|
|
// Blacken call frames.
|
|
|
|
for (int i = 0; i < fiber->frame_count; i++) {
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(&fiber->frames[i].fn->_super, vm);
|
|
|
|
grayObject(&fiber->frames[i].fn->owner->_super, vm);
|
2021-05-01 18:13:39 +08:00
|
|
|
}
|
|
|
|
vm->bytes_allocated += sizeof(CallFrame) * fiber->frame_capacity;
|
|
|
|
|
2021-05-14 18:44:57 +08:00
|
|
|
grayObject(&fiber->caller->_super, vm);
|
2021-05-07 17:41:19 +08:00
|
|
|
grayObject(&fiber->error->_super, vm);
|
2021-05-01 18:13:39 +08:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_USER:
|
|
|
|
TODO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void blackenObjects(PKVM* vm) {
|
2021-05-01 18:13:39 +08:00
|
|
|
while (vm->gray_list_count > 0) {
|
|
|
|
// Pop the gray object from the list.
|
|
|
|
Object* gray = vm->gray_list[--vm->gray_list_count];
|
|
|
|
blackenObject(gray, vm);
|
|
|
|
}
|
2021-02-07 15:40:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Var doubleToVar(double value) {
|
|
|
|
#if VAR_NAN_TAGGING
|
2021-05-03 21:09:23 +08:00
|
|
|
return utilDoubleToBits(value);
|
2021-02-07 15:40:00 +08:00
|
|
|
#else
|
2021-05-03 21:09:23 +08:00
|
|
|
#error TODO:
|
2021-02-07 15:40:00 +08:00
|
|
|
#endif // VAR_NAN_TAGGING
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:23:48 +08:00
|
|
|
double varToDouble(Var value) {
|
2021-02-07 15:40:00 +08:00
|
|
|
#if VAR_NAN_TAGGING
|
2021-05-03 21:09:23 +08:00
|
|
|
return utilDoubleFromBits(value);
|
2021-02-07 15:40:00 +08:00
|
|
|
#else
|
2021-05-03 21:09:23 +08:00
|
|
|
#error TODO:
|
2021-02-07 15:40:00 +08:00
|
|
|
#endif // VAR_NAN_TAGGING
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
static String* _allocateString(PKVM* vm, size_t length) {
|
2021-04-28 21:56:56 +08:00
|
|
|
String* string = ALLOCATE_DYNAMIC(vm, String, length + 1, char);
|
|
|
|
varInitObject(&string->_super, vm, OBJ_STRING);
|
2021-05-01 18:13:39 +08:00
|
|
|
string->length = (uint32_t)length;
|
2021-04-28 21:56:56 +08:00
|
|
|
string->data[length] = '\0';
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
String* newStringLength(PKVM* vm, const char* text, uint32_t length) {
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
ASSERT(length == 0 || text != NULL, "Unexpected NULL string.");
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
String* string = _allocateString(vm, length);
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-04-28 21:56:56 +08:00
|
|
|
if (length != 0 && text != NULL) memcpy(string->data, text, length);
|
2021-05-04 18:24:26 +08:00
|
|
|
string->hash = utilHashString(string->data);
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
return string;
|
2021-02-07 15:40:00 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
List* newList(PKVM* vm, uint32_t size) {
|
2021-02-13 01:40:19 +08:00
|
|
|
List* list = ALLOCATE(vm, List);
|
|
|
|
varInitObject(&list->_super, vm, OBJ_LIST);
|
|
|
|
varBufferInit(&list->elements);
|
|
|
|
if (size > 0) {
|
|
|
|
varBufferFill(&list->elements, vm, VAR_NULL, size);
|
|
|
|
list->elements.count = 0;
|
|
|
|
}
|
2021-02-13 21:57:59 +08:00
|
|
|
return list;
|
2021-02-13 01:40:19 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Map* newMap(PKVM* vm) {
|
2021-05-03 21:09:23 +08:00
|
|
|
Map* map = ALLOCATE(vm, Map);
|
|
|
|
varInitObject(&map->_super, vm, OBJ_MAP);
|
|
|
|
map->capacity = 0;
|
|
|
|
map->count = 0;
|
|
|
|
map->entries = NULL;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Range* newRange(PKVM* vm, double from, double to) {
|
2021-02-12 14:53:52 +08:00
|
|
|
Range* range = ALLOCATE(vm, Range);
|
|
|
|
varInitObject(&range->_super, vm, OBJ_RANGE);
|
|
|
|
range->from = from;
|
|
|
|
range->to = to;
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:21:38 +08:00
|
|
|
Script* newScript(PKVM* vm, String* path) {
|
2021-02-12 01:35:43 +08:00
|
|
|
Script* script = ALLOCATE(vm, Script);
|
|
|
|
varInitObject(&script->_super, vm, OBJ_SCRIPT);
|
|
|
|
|
2021-05-18 19:21:38 +08:00
|
|
|
script->path = path;
|
|
|
|
script->moudle = NULL;
|
2021-02-16 02:51:00 +08:00
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
varBufferInit(&script->globals);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferInit(&script->global_names);
|
2021-02-12 01:35:43 +08:00
|
|
|
varBufferInit(&script->literals);
|
|
|
|
functionBufferInit(&script->functions);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferInit(&script->function_names);
|
2021-02-12 01:35:43 +08:00
|
|
|
stringBufferInit(&script->names);
|
2021-02-11 01:23:48 +08:00
|
|
|
|
2021-02-16 02:51:00 +08:00
|
|
|
vmPushTempRef(vm, &script->_super);
|
2021-05-14 18:44:57 +08:00
|
|
|
const char* fn_name = "$(SourceBody)";
|
2021-02-16 02:51:00 +08:00
|
|
|
script->body = newFunction(vm, fn_name, (int)strlen(fn_name), script, false);
|
|
|
|
vmPopTempRef(vm);
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
return script;
|
2021-02-07 15:40:00 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Function* newFunction(PKVM* vm, const char* name, int length, Script* owner,
|
2021-02-07 15:40:00 +08:00
|
|
|
bool is_native) {
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
Function* func = ALLOCATE(vm, Function);
|
|
|
|
varInitObject(&func->_super, vm, OBJ_FUNC);
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-04-26 17:34:30 +08:00
|
|
|
if (owner == NULL) {
|
|
|
|
ASSERT(is_native, OOPS);
|
|
|
|
func->name = name;
|
|
|
|
func->owner = NULL;
|
|
|
|
func->is_native = is_native;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Add the name in the script's function buffer.
|
|
|
|
vmPushTempRef(vm, &func->_super);
|
|
|
|
functionBufferWrite(&owner->functions, vm, func);
|
2021-05-05 13:55:34 +08:00
|
|
|
uint32_t name_index = scriptAddName(owner, vm, name, length);
|
|
|
|
uintBufferWrite(&owner->function_names, vm, name_index);
|
2021-04-26 17:34:30 +08:00
|
|
|
vmPopTempRef(vm);
|
|
|
|
|
2021-05-05 13:55:34 +08:00
|
|
|
func->name = owner->names.data[name_index]->data;
|
2021-04-26 17:34:30 +08:00
|
|
|
func->owner = owner;
|
|
|
|
func->arity = -2; // -1 means variadic args.
|
|
|
|
func->is_native = is_native;
|
|
|
|
}
|
2021-02-16 02:51:00 +08:00
|
|
|
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
if (is_native) {
|
|
|
|
func->native = NULL;
|
|
|
|
} else {
|
|
|
|
Fn* fn = ALLOCATE(vm, Fn);
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
byteBufferInit(&fn->opcodes);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferInit(&fn->oplines);
|
2021-02-12 01:35:43 +08:00
|
|
|
fn->stack_size = 0;
|
|
|
|
func->fn = fn;
|
|
|
|
}
|
|
|
|
return func;
|
2021-02-07 15:40:00 +08:00
|
|
|
}
|
2021-02-09 16:21:10 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Fiber* newFiber(PKVM* vm) {
|
2021-04-26 17:34:30 +08:00
|
|
|
Fiber* fiber = ALLOCATE(vm, Fiber);
|
|
|
|
memset(fiber, 0, sizeof(Fiber));
|
|
|
|
varInitObject(&fiber->_super, vm, OBJ_FIBER);
|
|
|
|
return fiber;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void listInsert(List* self, PKVM* vm, uint32_t index, Var value) {
|
2021-05-03 21:09:23 +08:00
|
|
|
|
|
|
|
// Add an empty slot at the end of the buffer.
|
|
|
|
if (IS_OBJ(value)) vmPushTempRef(vm, AS_OBJ(value));
|
|
|
|
varBufferWrite(&self->elements, vm, VAR_NULL);
|
|
|
|
if (IS_OBJ(value)) vmPopTempRef(vm);
|
|
|
|
|
|
|
|
// Shift the existing elements down.
|
2021-05-04 18:24:26 +08:00
|
|
|
for (uint32_t i = self->elements.count - 1; i > index; i--) {
|
2021-05-03 21:09:23 +08:00
|
|
|
self->elements.data[i] = self->elements.data[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the new element.
|
|
|
|
self->elements.data[index] = value;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var listRemoveAt(List* self, PKVM* vm, uint32_t index) {
|
2021-05-03 21:09:23 +08:00
|
|
|
Var removed = self->elements.data[index];
|
|
|
|
if (IS_OBJ(removed)) vmPushTempRef(vm, AS_OBJ(removed));
|
|
|
|
|
|
|
|
// Shift the rest of the elements up.
|
2021-05-04 18:24:26 +08:00
|
|
|
for (uint32_t i = index; i < self->elements.count - 1; i++) {
|
2021-05-03 21:09:23 +08:00
|
|
|
self->elements.data[i] = self->elements.data[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink the size if it's too much excess.
|
|
|
|
if (self->elements.capacity / GROW_FACTOR >= self->elements.count) {
|
|
|
|
self->elements.data = (Var*)vmRealloc(vm, self->elements.data,
|
|
|
|
sizeof(Var) * self->elements.capacity,
|
|
|
|
sizeof(Var) * self->elements.capacity / GROW_FACTOR);
|
|
|
|
self->elements.capacity /= GROW_FACTOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_OBJ(removed)) vmPopTempRef(vm);
|
|
|
|
|
|
|
|
self->elements.count--;
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a has value for the object.
|
|
|
|
static uint32_t _hashObject(Object* obj) {
|
2021-05-06 13:00:02 +08:00
|
|
|
|
|
|
|
ASSERT(isObjectHashable(obj->type),
|
|
|
|
"Check if it's hashable before calling this method.");
|
|
|
|
|
2021-05-03 21:09:23 +08:00
|
|
|
switch (obj->type) {
|
|
|
|
|
|
|
|
case OBJ_STRING:
|
2021-05-05 12:55:27 +08:00
|
|
|
return ((String*)obj)->hash;
|
2021-05-03 21:09:23 +08:00
|
|
|
|
|
|
|
case OBJ_LIST:
|
|
|
|
case OBJ_MAP:
|
|
|
|
goto L_unhashable;
|
|
|
|
|
|
|
|
case OBJ_RANGE:
|
|
|
|
{
|
|
|
|
Range* range = (Range*)obj;
|
|
|
|
return utilHashNumber(range->from) ^ utilHashNumber(range->to);
|
|
|
|
}
|
|
|
|
|
|
|
|
case OBJ_SCRIPT:
|
|
|
|
case OBJ_FUNC:
|
|
|
|
case OBJ_FIBER:
|
|
|
|
case OBJ_USER:
|
|
|
|
TODO;
|
|
|
|
|
|
|
|
default:
|
|
|
|
L_unhashable:
|
2021-05-06 13:00:02 +08:00
|
|
|
UNREACHABLE();
|
2021-05-03 21:09:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-05-12 18:57:35 +08:00
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
return -1;
|
2021-05-03 21:09:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:38:23 +08:00
|
|
|
uint32_t varHashValue(Var v) {
|
|
|
|
if (IS_OBJ(v)) return _hashObject(AS_OBJ(v));
|
2021-05-03 21:09:23 +08:00
|
|
|
|
|
|
|
#if VAR_NAN_TAGGING
|
2021-05-11 14:38:23 +08:00
|
|
|
return utilHashBits(v);
|
2021-05-03 21:09:23 +08:00
|
|
|
#else
|
|
|
|
#error TODO:
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
// Find the entry with the [key]. Returns true if found and set [result] to
|
|
|
|
// point to the entry, return false otherwise and points [result] to where
|
|
|
|
// the entry should be inserted.
|
|
|
|
static bool _mapFindEntry(Map* self, Var key, MapEntry** result) {
|
|
|
|
|
|
|
|
// An empty map won't contain the key.
|
|
|
|
if (self->capacity == 0) return false;
|
|
|
|
|
|
|
|
// The [start_index] is where the entry supposed to be if there wasn't any
|
|
|
|
// collision occured. It'll be the start index for the linear probing.
|
2021-05-11 14:38:23 +08:00
|
|
|
uint32_t start_index = varHashValue(key) % self->capacity;
|
2021-05-04 18:24:26 +08:00
|
|
|
uint32_t index = start_index;
|
|
|
|
|
|
|
|
// Keep track of the first tombstone after the [start_index] if we don't find
|
|
|
|
// the key anywhere. The tombstone would be the entry at where we will have
|
|
|
|
// to insert the key/value pair.
|
|
|
|
MapEntry* tombstone = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
MapEntry* entry = &self->entries[index];
|
|
|
|
|
|
|
|
if (IS_UNDEF(entry->key)) {
|
|
|
|
ASSERT(IS_BOOL(entry->value), OOPS);
|
|
|
|
|
|
|
|
if (IS_TRUE(entry->value)) {
|
|
|
|
|
|
|
|
// We've found a tombstone, if we haven't found one [tombstone] should
|
|
|
|
// be updated. We still need to keep search for if the key exists.
|
|
|
|
if (tombstone == NULL) tombstone = entry;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// We've found a new empty slot and the key isn't found. If we've
|
|
|
|
// found a tombstone along the sequence we could use that entry
|
|
|
|
// otherwise the entry at the current index.
|
|
|
|
|
|
|
|
*result = (tombstone != NULL) ? tombstone : entry;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (isValuesEqual(entry->key, key)) {
|
|
|
|
// We've found the key.
|
|
|
|
*result = entry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % self->capacity;
|
|
|
|
|
|
|
|
} while (index != start_index);
|
|
|
|
|
|
|
|
// If we reach here means the map is filled with tombstone. Set the first
|
|
|
|
// tombstone as result for the next insertion and return false.
|
|
|
|
ASSERT(tombstone != NULL, OOPS);
|
|
|
|
*result = tombstone;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the key, value pair to the entries array of the map. Returns true if
|
|
|
|
// the entry added for the first time and false for replaced vlaue.
|
|
|
|
static bool _mapInsertEntry(Map* self, Var key, Var value) {
|
|
|
|
|
|
|
|
ASSERT(self->capacity != 0, "Should ensure the capacity before inserting.");
|
|
|
|
|
|
|
|
MapEntry* result;
|
|
|
|
if (_mapFindEntry(self, key, &result)) {
|
|
|
|
// Key already found, just replace the value.
|
|
|
|
result->value = value;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
result->key = key;
|
|
|
|
result->value = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize the map's size to the given [capacity].
|
2021-05-09 18:28:00 +08:00
|
|
|
static void _mapResize(Map* self, PKVM* vm, uint32_t capacity) {
|
2021-05-04 18:24:26 +08:00
|
|
|
|
|
|
|
MapEntry* old_entries = self->entries;
|
|
|
|
uint32_t old_capacity = self->capacity;
|
|
|
|
|
|
|
|
self->entries = ALLOCATE_ARRAY(vm, MapEntry, capacity);
|
|
|
|
self->capacity = capacity;
|
|
|
|
for (uint32_t i = 0; i < capacity; i++) {
|
2021-05-06 13:00:02 +08:00
|
|
|
self->entries[i].key = VAR_UNDEFINED;
|
|
|
|
self->entries[i].value = VAR_FALSE;
|
2021-05-04 18:24:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the old entries to the new entries.
|
|
|
|
for (uint32_t i = 0; i < old_capacity; i++) {
|
|
|
|
// Skip the empty entries or tombstones.
|
|
|
|
if (IS_UNDEF(old_entries[i].key)) continue;
|
|
|
|
|
|
|
|
_mapInsertEntry(self, old_entries[i].key, old_entries[i].value);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEALLOCATE(vm, old_entries);
|
|
|
|
}
|
|
|
|
|
2021-05-03 21:09:23 +08:00
|
|
|
Var mapGet(Map* self, Var key) {
|
2021-05-04 18:24:26 +08:00
|
|
|
MapEntry* entry;
|
|
|
|
if (_mapFindEntry(self, key, &entry)) return entry->value;
|
|
|
|
return VAR_UNDEFINED;
|
2021-05-03 21:09:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void mapSet(Map* self, PKVM* vm, Var key, Var value) {
|
2021-05-03 21:09:23 +08:00
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
// If map is about to fill, resize it first.
|
2021-05-05 12:55:27 +08:00
|
|
|
if (self->count + 1 > self->capacity * MAP_LOAD_PERCENT / 100) {
|
2021-05-04 18:24:26 +08:00
|
|
|
uint32_t capacity = self->capacity * GROW_FACTOR;
|
|
|
|
if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY;
|
|
|
|
_mapResize(self, vm, capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mapInsertEntry(self, key, value)) {
|
|
|
|
self->count++; //< A new key added.
|
2021-05-03 21:09:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void mapClear(Map* self, PKVM* vm) {
|
2021-05-04 18:24:26 +08:00
|
|
|
DEALLOCATE(vm, self->entries);
|
|
|
|
self->entries = NULL;
|
|
|
|
self->capacity = 0;
|
|
|
|
self->count = 0;
|
2021-05-03 21:09:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var mapRemoveKey(Map* self, PKVM* vm, Var key) {
|
2021-05-04 18:24:26 +08:00
|
|
|
MapEntry* entry;
|
|
|
|
if (!_mapFindEntry(self, key, &entry)) return VAR_NULL;
|
|
|
|
|
|
|
|
// Set the key as VAR_UNDEFINED to mark is as an available slow and set it's
|
|
|
|
// value to VAR_TRUE for tombstone.
|
|
|
|
Var value = entry->value;
|
|
|
|
entry->key = VAR_UNDEFINED;
|
|
|
|
entry->value = VAR_TRUE;
|
|
|
|
|
|
|
|
self->count--;
|
|
|
|
|
|
|
|
if (IS_OBJ(value)) vmPushTempRef(vm, AS_OBJ(value));
|
|
|
|
|
|
|
|
if (self->count == 0) {
|
|
|
|
// Clear the map if it's empty.
|
|
|
|
mapClear(self, vm);
|
|
|
|
|
|
|
|
} else if (self->capacity > MIN_CAPACITY &&
|
2021-05-05 12:55:27 +08:00
|
|
|
self->capacity / GROW_FACTOR > self->count / MAP_LOAD_PERCENT * 100) {
|
2021-05-04 18:24:26 +08:00
|
|
|
uint32_t capacity = self->capacity / GROW_FACTOR;
|
|
|
|
if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY;
|
|
|
|
|
|
|
|
_mapResize(self, vm, capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_OBJ(value)) vmPopTempRef(vm);
|
|
|
|
|
|
|
|
return value;
|
2021-05-03 21:09:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
void freeObject(PKVM* vm, Object* obj) {
|
2021-04-26 17:34:30 +08:00
|
|
|
// TODO: Debug trace memory here.
|
|
|
|
|
|
|
|
// First clean the object's referencs, but we're not recursively doallocating
|
|
|
|
// them because they're not marked and will be cleaned later.
|
|
|
|
// Example: List's `elements` is VarBuffer that contain a heap allocated
|
|
|
|
// array of `var*` which will be cleaned below but the actual `var` elements
|
|
|
|
// will won't be freed here instead they havent marked at all, and will be
|
|
|
|
// removed at the sweeping phase of the garbage collection.
|
|
|
|
switch (obj->type) {
|
|
|
|
case OBJ_STRING:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBJ_LIST:
|
|
|
|
varBufferClear(&(((List*)obj)->elements), vm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBJ_MAP:
|
2021-05-05 12:55:27 +08:00
|
|
|
DEALLOCATE(vm, ((Map*)obj)->entries);
|
2021-04-26 17:34:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OBJ_RANGE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBJ_SCRIPT: {
|
|
|
|
Script* scr = (Script*)obj;
|
|
|
|
varBufferClear(&scr->globals, vm);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferClear(&scr->global_names, vm);
|
2021-04-26 17:34:30 +08:00
|
|
|
varBufferClear(&scr->literals, vm);
|
|
|
|
functionBufferClear(&scr->functions, vm);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferClear(&scr->function_names, vm);
|
2021-04-26 17:34:30 +08:00
|
|
|
stringBufferClear(&scr->names, vm);
|
|
|
|
} break;
|
|
|
|
|
2021-05-05 12:55:27 +08:00
|
|
|
case OBJ_FUNC: {
|
2021-04-26 17:34:30 +08:00
|
|
|
Function* func = (Function*)obj;
|
|
|
|
if (!func->is_native) {
|
|
|
|
byteBufferClear(&func->fn->opcodes, vm);
|
2021-05-05 13:55:34 +08:00
|
|
|
uintBufferClear(&func->fn->oplines, vm);
|
2021-04-26 17:34:30 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2021-05-05 12:55:27 +08:00
|
|
|
case OBJ_FIBER: {
|
2021-04-26 17:34:30 +08:00
|
|
|
Fiber* fiber = (Fiber*)obj;
|
|
|
|
DEALLOCATE(vm, fiber->stack);
|
|
|
|
DEALLOCATE(vm, fiber->frames);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OBJ_USER:
|
2021-05-05 12:55:27 +08:00
|
|
|
TODO; // Remove OBJ_USER.
|
2021-04-26 17:34:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEALLOCATE(vm, obj);
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:23:48 +08:00
|
|
|
// Utility functions //////////////////////////////////////////////////////////
|
2021-02-09 16:21:10 +08:00
|
|
|
|
2021-02-16 02:51:00 +08:00
|
|
|
const char* varTypeName(Var v) {
|
|
|
|
if (IS_NULL(v)) return "null";
|
|
|
|
if (IS_BOOL(v)) return "bool";
|
2021-05-04 18:24:26 +08:00
|
|
|
if (IS_NUM(v)) return "number";
|
2021-02-16 02:51:00 +08:00
|
|
|
|
|
|
|
ASSERT(IS_OBJ(v), OOPS);
|
|
|
|
Object* obj = AS_OBJ(v);
|
|
|
|
switch (obj->type) {
|
|
|
|
case OBJ_STRING: return "String";
|
|
|
|
case OBJ_LIST: return "List";
|
|
|
|
case OBJ_MAP: return "Map";
|
|
|
|
case OBJ_RANGE: return "Range";
|
|
|
|
case OBJ_SCRIPT: return "Script";
|
|
|
|
case OBJ_FUNC: return "Func";
|
2021-05-12 18:57:35 +08:00
|
|
|
case OBJ_FIBER: return "Fiber";
|
2021-02-16 02:51:00 +08:00
|
|
|
case OBJ_USER: return "UserObj";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
bool isValuesSame(Var v1, Var v2) {
|
2021-02-09 16:21:10 +08:00
|
|
|
#if VAR_NAN_TAGGING
|
2021-02-12 01:35:43 +08:00
|
|
|
// Bit representation of each values are unique so just compare the bits.
|
|
|
|
return v1 == v2;
|
2021-02-09 16:21:10 +08:00
|
|
|
#else
|
2021-05-03 21:09:23 +08:00
|
|
|
#error TODO:
|
2021-02-09 16:21:10 +08:00
|
|
|
#endif
|
2021-02-11 01:23:48 +08:00
|
|
|
}
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
bool isValuesEqual(Var v1, Var v2) {
|
|
|
|
if (isValuesSame(v1, v2)) return true;
|
|
|
|
|
|
|
|
// If we reach here only heap allocated objects could be compared.
|
|
|
|
if (!IS_OBJ(v1) || !IS_OBJ(v2)) return false;
|
|
|
|
|
|
|
|
Object* o1 = AS_OBJ(v1), *o2 = AS_OBJ(v2);
|
|
|
|
if (o1->type != o2->type) return false;
|
|
|
|
|
|
|
|
switch (o1->type) {
|
|
|
|
case OBJ_RANGE:
|
|
|
|
return ((Range*)o1)->from == ((Range*)o2)->from &&
|
|
|
|
((Range*)o1)->to == ((Range*)o2)->to;
|
|
|
|
|
|
|
|
case OBJ_STRING: {
|
|
|
|
String* s1 = (String*)o1, *s2 = (String*)o2;
|
|
|
|
return s1->hash == s2->hash &&
|
|
|
|
s1->length == s2->length &&
|
|
|
|
memcmp(s1->data, s2->data, s1->length) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:00:02 +08:00
|
|
|
bool isObjectHashable(ObjectType type) {
|
|
|
|
// Only list and map are un-hashable.
|
|
|
|
return type != OBJ_LIST && type != OBJ_MAP;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
String* toString(PKVM* vm, Var v, bool recursive) {
|
2021-02-12 01:35:43 +08:00
|
|
|
|
|
|
|
if (IS_NULL(v)) {
|
2021-05-06 22:19:30 +08:00
|
|
|
return newStringLength(vm, "null", 4);
|
2021-02-12 01:35:43 +08:00
|
|
|
|
|
|
|
} else if (IS_BOOL(v)) {
|
|
|
|
if (AS_BOOL(v)) {
|
2021-05-06 22:19:30 +08:00
|
|
|
return newStringLength(vm, "true", 4);
|
2021-02-12 01:35:43 +08:00
|
|
|
} else {
|
2021-05-06 22:19:30 +08:00
|
|
|
return newStringLength(vm, "false", 5);
|
2021-02-12 01:35:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (IS_NUM(v)) {
|
|
|
|
char buff[TO_STRING_BUFF_SIZE];
|
|
|
|
int length = sprintf(buff, "%.14g", AS_NUM(v));
|
|
|
|
ASSERT(length < TO_STRING_BUFF_SIZE, "Buffer overflowed.");
|
2021-05-06 22:19:30 +08:00
|
|
|
return newStringLength(vm, buff, length);
|
2021-02-12 01:35:43 +08:00
|
|
|
|
|
|
|
} else if (IS_OBJ(v)) {
|
|
|
|
Object* obj = AS_OBJ(v);
|
|
|
|
switch (obj->type) {
|
|
|
|
case OBJ_STRING:
|
2021-02-13 01:40:19 +08:00
|
|
|
{
|
2021-05-04 18:24:26 +08:00
|
|
|
// If recursive return with quotes (ex: [42, "hello", 0..10]).
|
2021-05-06 22:19:30 +08:00
|
|
|
String* string = newStringLength(vm, ((String*)obj)->data, ((String*)obj)->length);
|
2021-05-05 12:55:27 +08:00
|
|
|
if (!recursive) return string;
|
2021-05-06 13:00:02 +08:00
|
|
|
else return stringFormat(vm, "\"@\"", string);
|
2021-05-05 12:55:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case OBJ_LIST: {
|
|
|
|
List* list = (List*)obj;
|
2021-05-06 22:19:30 +08:00
|
|
|
String* result = newStringLength(vm, "[", 1);
|
2021-05-05 12:55:27 +08:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < list->elements.count; i++) {
|
|
|
|
const char* fmt = (i != 0) ? "@, @" : "@@";
|
|
|
|
|
|
|
|
vmPushTempRef(vm, &result->_super);
|
2021-05-06 13:00:02 +08:00
|
|
|
result = stringFormat(vm, fmt, result,
|
|
|
|
toString(vm, list->elements.data[i], true));
|
2021-05-05 12:55:27 +08:00
|
|
|
vmPopTempRef(vm);
|
|
|
|
}
|
2021-05-06 13:00:02 +08:00
|
|
|
return stringFormat(vm, "@]", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
case OBJ_MAP:
|
|
|
|
{
|
|
|
|
Map* map = (Map*)obj;
|
2021-05-06 22:19:30 +08:00
|
|
|
String* result = newStringLength(vm, "{", 1);
|
2021-05-06 13:00:02 +08:00
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
bool _first = true; // For first element no ',' required.
|
|
|
|
do {
|
|
|
|
|
|
|
|
// Get the next valid key index.
|
|
|
|
bool _done = false; //< To break from inner loop.
|
|
|
|
while (IS_UNDEF(map->entries[i].key)) {
|
|
|
|
i++;
|
|
|
|
if (i >= map->capacity) {
|
|
|
|
_done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_done) break;
|
|
|
|
|
|
|
|
const char* fmt = (!_first) ? "@, @:@" : "@@:@";
|
|
|
|
vmPushTempRef(vm, &result->_super);
|
|
|
|
result = stringFormat(vm, fmt, result,
|
|
|
|
toString(vm, map->entries[i].key, true),
|
|
|
|
toString(vm, map->entries[i].value, true));
|
|
|
|
vmPopTempRef(vm);
|
|
|
|
|
|
|
|
_first = false;
|
|
|
|
i++;
|
|
|
|
} while (i < map->capacity);
|
|
|
|
|
|
|
|
return stringFormat(vm, "@}", result);
|
2021-02-13 01:40:19 +08:00
|
|
|
}
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-05-11 14:38:23 +08:00
|
|
|
case OBJ_RANGE:
|
|
|
|
{
|
|
|
|
Range* range = (Range*)obj;
|
|
|
|
|
|
|
|
String* from = toString(vm, VAR_NUM(range->from), false);
|
|
|
|
vmPushTempRef(vm, &from->_super);
|
|
|
|
|
|
|
|
String* to = toString(vm, VAR_NUM(range->to), false);
|
|
|
|
vmPushTempRef(vm, &from->_super);
|
|
|
|
|
|
|
|
String* str = stringFormat(vm, "[Range:@..@]", from, to);
|
|
|
|
vmPopTempRef(vm); // to.
|
|
|
|
vmPopTempRef(vm); // from.
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2021-05-18 19:21:38 +08:00
|
|
|
|
|
|
|
case OBJ_SCRIPT: {
|
|
|
|
String* name = (((Script*)obj)->moudle != NULL)
|
|
|
|
? ((Script*)obj)->moudle
|
|
|
|
: ((Script*)obj)->path;
|
|
|
|
return stringFormat(vm, "[Lib:@]", name);
|
|
|
|
}
|
2021-05-09 20:31:36 +08:00
|
|
|
case OBJ_FUNC: return stringFormat(vm, "[Func:$]", ((Function*)obj)->name);
|
2021-05-12 18:57:35 +08:00
|
|
|
case OBJ_FIBER: return newStringLength(vm, "[Fiber]", 7); // TODO;
|
2021-05-06 22:19:30 +08:00
|
|
|
case OBJ_USER: return newStringLength(vm, "[UserObj]", 9); // TODO;
|
2021-02-12 01:35:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-02-15 20:49:19 +08:00
|
|
|
|
|
|
|
bool toBool(Var v) {
|
2021-05-05 12:55:27 +08:00
|
|
|
|
2021-02-15 20:49:19 +08:00
|
|
|
if (IS_BOOL(v)) return AS_BOOL(v);
|
|
|
|
if (IS_NULL(v)) return false;
|
2021-05-05 12:55:27 +08:00
|
|
|
if (IS_NUM(v)) return AS_NUM(v) != 0;
|
2021-02-15 20:49:19 +08:00
|
|
|
|
|
|
|
ASSERT(IS_OBJ(v), OOPS);
|
|
|
|
Object* o = AS_OBJ(v);
|
|
|
|
switch (o->type) {
|
|
|
|
case OBJ_STRING: return ((String*)o)->length != 0;
|
|
|
|
case OBJ_LIST: return ((List*)o)->elements.count != 0;
|
2021-05-05 12:55:27 +08:00
|
|
|
case OBJ_MAP: return ((Map*)o)->count != 0;
|
|
|
|
case OBJ_RANGE: // [[FALLTHROUGH]]
|
|
|
|
case OBJ_SCRIPT:
|
2021-02-15 20:49:19 +08:00
|
|
|
case OBJ_FUNC:
|
|
|
|
case OBJ_USER:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-28 21:56:56 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
String* stringFormat(PKVM* vm, const char* fmt, ...) {
|
2021-04-28 21:56:56 +08:00
|
|
|
va_list arg_list;
|
|
|
|
|
|
|
|
// Calculate the total length of the resulting string. This is required to
|
|
|
|
// determine the final string size to allocate.
|
|
|
|
va_start(arg_list, fmt);
|
|
|
|
size_t total_length = 0;
|
|
|
|
for (const char* c = fmt; *c != '\0'; c++) {
|
|
|
|
switch (*c) {
|
|
|
|
case '$':
|
|
|
|
total_length += strlen(va_arg(arg_list, const char*));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '@':
|
|
|
|
total_length += AS_STRING(va_arg(arg_list, Var))->length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
total_length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(arg_list);
|
|
|
|
|
|
|
|
// Now build the new string.
|
2021-05-04 18:24:26 +08:00
|
|
|
String* result = _allocateString(vm, total_length);
|
2021-04-28 21:56:56 +08:00
|
|
|
va_start(arg_list, fmt);
|
|
|
|
char* buff = result->data;
|
|
|
|
for (const char* c = fmt; *c != '\0'; c++) {
|
|
|
|
switch (*c) {
|
|
|
|
case '$':
|
|
|
|
{
|
|
|
|
const char* string = va_arg(arg_list, const char*);
|
|
|
|
size_t length = strlen(string);
|
|
|
|
memcpy(buff, string, length);
|
|
|
|
buff += length;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case '@':
|
|
|
|
{
|
|
|
|
String* string = AS_STRING(va_arg(arg_list, Var));
|
|
|
|
memcpy(buff, string->data, string->length);
|
|
|
|
buff += string->length;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
*buff++ = *c;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(arg_list);
|
|
|
|
|
2021-05-04 18:24:26 +08:00
|
|
|
result->hash = utilHashString(result->data);
|
2021-05-06 13:00:02 +08:00
|
|
|
return result;
|
2021-05-05 13:55:34 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
uint32_t scriptAddName(Script* self, PKVM* vm, const char* name,
|
2021-05-05 13:55:34 +08:00
|
|
|
uint32_t length) {
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < self->names.count; i++) {
|
|
|
|
String* _name = self->names.data[i];
|
|
|
|
if (_name->length == length && strncmp(_name->data, name, length) == 0) {
|
|
|
|
// Name already exists in the buffer.
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here the name doesn't exists in the buffer, so add it and
|
|
|
|
// return the index.
|
2021-05-06 22:19:30 +08:00
|
|
|
String* new_name = newStringLength(vm, name, length);
|
2021-05-05 13:55:34 +08:00
|
|
|
vmPushTempRef(vm, &new_name->_super);
|
|
|
|
stringBufferWrite(&self->names, vm, new_name);
|
|
|
|
vmPopTempRef(vm);
|
|
|
|
return self->names.count - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int scriptSearchFunc(Script* script, const char* name, uint32_t length) {
|
|
|
|
for (uint32_t i = 0; i < script->function_names.count; i++) {
|
|
|
|
uint32_t name_index = script->function_names.data[i];
|
|
|
|
String* fn_name = script->names.data[name_index];
|
|
|
|
if (fn_name->length == length &&
|
|
|
|
strncmp(fn_name->data, name, length) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int scriptSearchGlobals(Script* script, const char* name, uint32_t length) {
|
|
|
|
for (uint32_t i = 0; i < script->global_names.count; i++) {
|
|
|
|
uint32_t name_index = script->global_names.data[i];
|
|
|
|
String* g_name = script->names.data[name_index];
|
|
|
|
if (g_name->length == length && strncmp(g_name->data, name, length) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|