pocketlang/src/var.c

877 lines
23 KiB
C
Raw Normal View History

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"
// Public Api /////////////////////////////////////////////////////////////////
Var msVarBool(MSVM* vm, bool value) {
return VAR_BOOL(value);
}
Var msVarNumber(MSVM* vm, double value) {
return VAR_NUM(value);
}
Var msVarString(MSVM* vm, const char* value) {
return VAR_OBJ(newString(vm, value, (uint32_t)strlen(value)));
}
bool msAsBool(MSVM* vm, Var value) {
return AS_BOOL(value);
}
double msAsNumber(MSVM* vm, Var value) {
return AS_NUM(value);
}
const char* msAsString(MSVM* vm, Var value) {
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-02-08 02:30:29 +08:00
void varInitObject(Object* self, MSVM* 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-01 18:13:39 +08:00
void grayObject(Object* self, MSVM* 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-01 18:13:39 +08:00
void grayValue(Var self, MSVM* 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);
}
void grayVarBuffer(VarBuffer* self, MSVM* vm) {
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);
}
}
void grayStringBuffer(StringBuffer* self, MSVM* vm) {
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
grayObject((Object*)self->data[i], vm);
}
}
void grayFunctionBuffer(FunctionBuffer* self, MSVM* vm) {
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
grayObject((Object*)self->data[i], vm);
}
}
static void blackenObject(Object* obj, MSVM* vm) {
// 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;
for (uint32_t i = 0; i < map->count; i++) {
if (IS_UNDEF(map->entries[i].key)) continue;
grayObject((Object*)map->entries[i].key, vm);
grayObject((Object*)map->entries[i].value, vm);
}
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);
grayVarBuffer(&script->globals, vm);
vm->bytes_allocated += sizeof(Var) * script->globals.capacity;
// 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;
// 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;
grayObject((Object*)script->body, vm);
} break;
case OBJ_FUNC:
{
Function* func = (Function*)obj;
vm->bytes_allocated += sizeof(Function);
grayObject((Object*)func->owner, vm);
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);
grayObject((Object*)fiber->func, vm);
// 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++) {
grayObject((Object*)fiber->frames[i].fn, vm);
grayObject((Object*)fiber->frames[i].fn->owner, vm);
}
vm->bytes_allocated += sizeof(CallFrame) * fiber->frame_capacity;
grayObject((Object*)fiber->error, vm);
} break;
case OBJ_USER:
TODO;
break;
}
}
void blackenObjects(MSVM* vm) {
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-04 18:24:26 +08:00
static String* _allocateString(MSVM* 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-02-08 02:30:29 +08:00
String* newString(MSVM* 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-02-13 01:40:19 +08:00
List* newList(MSVM* vm, uint32_t size) {
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-03 21:09:23 +08:00
Map* newMap(MSVM* vm) {
Map* map = ALLOCATE(vm, Map);
varInitObject(&map->_super, vm, OBJ_MAP);
map->capacity = 0;
map->count = 0;
map->entries = NULL;
return map;
}
Range* newRange(MSVM* vm, double from, double to) {
Range* range = ALLOCATE(vm, Range);
varInitObject(&range->_super, vm, OBJ_RANGE);
range->from = from;
range->to = to;
return range;
}
2021-02-08 02:30:29 +08:00
Script* newScript(MSVM* vm) {
2021-02-12 01:35:43 +08:00
Script* script = ALLOCATE(vm, Script);
varInitObject(&script->_super, vm, OBJ_SCRIPT);
2021-02-16 02:51:00 +08:00
script->name = NULL;
2021-05-01 18:13:39 +08:00
// TODO: script->path = NULL;
2021-02-16 02:51:00 +08:00
2021-02-12 01:35:43 +08:00
varBufferInit(&script->globals);
uintBufferInit(&script->global_names);
2021-02-12 01:35:43 +08:00
varBufferInit(&script->literals);
functionBufferInit(&script->functions);
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);
const char* fn_name = "@(ScriptLevel)";
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-02-16 02:51:00 +08:00
Function* newFunction(MSVM* 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);
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);
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);
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-04-26 17:34:30 +08:00
Fiber* newFiber(MSVM* vm) {
Fiber* fiber = ALLOCATE(vm, Fiber);
memset(fiber, 0, sizeof(Fiber));
varInitObject(&fiber->_super, vm, OBJ_FIBER);
return fiber;
}
2021-05-03 21:09:23 +08:00
void listInsert(List* self, MSVM* vm, uint32_t index, Var value) {
// 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;
}
Var listRemoveAt(List* self, MSVM* vm, uint32_t index) {
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) {
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:
ASSERT(false, "Only immutable objects are hashable.");
break;
}
}
static uint32_t _hashVar(Var value) {
if (IS_OBJ(value)) return _hashObject(AS_OBJ(value));
#if VAR_NAN_TAGGING
return utilHashBits(value);
#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.
uint32_t start_index = _hashVar(key) % self->capacity;
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].
static void _mapResize(Map* self, MSVM* vm, uint32_t capacity) {
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++) {
self->entries->key = VAR_UNDEFINED;
self->entries->value = VAR_FALSE;
}
// 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
}
void mapSet(Map* self, MSVM* vm, Var key, Var value) {
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
}
}
void mapClear(Map* self, MSVM* 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
}
Var mapRemoveKey(Map* self, MSVM* 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-04-26 17:34:30 +08:00
void freeObject(MSVM* vm, Object* obj) {
// 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);
uintBufferClear(&scr->global_names, vm);
2021-04-26 17:34:30 +08:00
varBufferClear(&scr->literals, vm);
functionBufferClear(&scr->functions, vm);
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);
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";
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-02-13 01:40:19 +08:00
String* toString(MSVM* vm, Var v, bool recursive) {
2021-02-12 01:35:43 +08:00
if (IS_NULL(v)) {
return newString(vm, "null", 4);
} else if (IS_BOOL(v)) {
if (AS_BOOL(v)) {
return newString(vm, "true", 4);
} else {
return newString(vm, "false", 5);
}
} 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.");
return newString(vm, buff, length);
} 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-05 12:55:27 +08:00
String* string = newString(vm, ((String*)obj)->data, ((String*)obj)->length);
if (!recursive) return string;
else return (String*)AS_OBJ(stringFormat(vm, "\"@\"", string));
}
case OBJ_LIST: {
List* list = (List*)obj;
String* result = newString(vm, "[", 1);
for (uint32_t i = 0; i < list->elements.count; i++) {
const char* fmt = (i != 0) ? "@, @" : "@@";
vmPushTempRef(vm, &result->_super);
result = (String*)AS_OBJ(stringFormat(vm, fmt,
result, toString(vm, list->elements.data[i], true)));
vmPopTempRef(vm);
}
return (String*)AS_OBJ(stringFormat(vm, "@]", result));
2021-02-13 01:40:19 +08:00
}
2021-02-12 01:35:43 +08:00
case OBJ_MAP: return newString(vm, "[Map]", 5); // TODO;
case OBJ_RANGE: return newString(vm, "[Range]", 7); // TODO;
case OBJ_SCRIPT: return newString(vm, "[Script]", 8); // TODO;
case OBJ_FUNC: {
const char* name = ((Function*)obj)->name;
int length = (int)strlen(name); // TODO: Assert length.
char buff[TO_STRING_BUFF_SIZE];
2021-02-16 02:51:00 +08:00
memcpy(buff, "[Func:", 6);
memcpy(buff + 6, name, length);
buff[6 + length] = ']';
return newString(vm, buff, 6 + length + 1);
}
case OBJ_USER: return newString(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
Var stringFormat(MSVM* vm, const char* fmt, ...) {
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-04-28 21:56:56 +08:00
return VAR_OBJ(result);
}
uint32_t scriptAddName(Script* self, MSVM* vm, const char* name,
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.
String* new_name = newString(vm, name, length);
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;
}