| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /**
- ******************************************************************************
- * @file : object.cpp
- * @author : simon
- * @brief : None
- * @attention : None
- * @date : 2023/8/23
- ******************************************************************************
- */
- #include <stdio.h>
- #include <string.h>
- #include "memory.h"
- #include "object.h"
- #include "table.h"
- #include "vm.h"
- static Obj *allocateObject(size_t size, ObjType type);
- static uint32_t hashString(const char *key, int length);
- #define ALLOCATE_OBJ(type, objectType) \
- (type *) allocateObject(sizeof(type), objectType)
- static ObjString *allocateString(char *chars, int length,
- uint32_t hash) {
- ObjString *string = ALLOCATE_OBJ(ObjString, OBJ_STRING);
- string->length = length;
- string->chars = chars;
- string->hash = hash;
- tableSet(&vm.strings, string, NIL_VAL);
- return string;
- }
- ObjString *copyString(const char *chars, int length) {
- uint32_t hash = hashString(chars, length);
- ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
- if (interned != NULL) return interned;
- char *heapChars = ALLOCATE(char, length + 1);
- memcpy(heapChars, chars, length);
- heapChars[length] = '\0';
- return allocateString(heapChars, length, hash);
- }
- static void printFunction(ObjFunction *function) {
- if (function->name == NULL) {
- printf("<script>");
- return;
- }
- printf("<fn %s>", function->name->chars);
- }
- /// 打印对象
- /// \param value
- void printObject(Value value) {
- switch (OBJ_TYPE(value)) {
- case OBJ_NATIVE:
- printf("<native fn>");
- break;
- case OBJ_FUNCTION:
- printFunction(AS_FUNCTION(value));
- break;
- case OBJ_STRING:
- printf("%s", AS_CSTRING(value));
- break;
- case OBJ_CLOSURE:
- printFunction(AS_CLOSURE(value)->function);
- break;
- case OBJ_UPVALUE:
- printf("upvalue");
- break;
- }
- }
- ObjFunction *newFunction() {
- ObjFunction *function = ALLOCATE_OBJ(ObjFunction, OBJ_FUNCTION);
- function->arity = 0;
- function->upvalueCount = 0;
- function->name = NULL;
- initChunk(&function->chunk);
- return function;
- }
- ObjString *takeString(char *chars, int length) {
- uint32_t hash = hashString(chars, length);
- ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
- if (interned != NULL) {
- FREE_ARRAY(char, chars, length + 1);
- return interned;
- }
- return allocateString(chars, length, hash);
- }
- ObjNative *newNative(NativeFn function) {
- ObjNative *native = ALLOCATE_OBJ(ObjNative, OBJ_NATIVE);
- native->function = function;
- return native;
- }
- ObjClosure *newClosure(ObjFunction *function) {
- ObjUpvalue **upvalues = ALLOCATE(ObjUpvalue *, function->upvalueCount);
- for (int i = 0; i < function->upvalueCount; i++) {
- upvalues[i] = NULL;
- }
- ObjClosure *closure = ALLOCATE_OBJ(ObjClosure, OBJ_CLOSURE);
- closure->function = function;
- closure->upvalues = upvalues;
- closure->upvalueCount = function->upvalueCount;
- return closure;
- }
- ObjUpvalue *newUpvalue(Value *slot) {
- ObjUpvalue *upvalue = ALLOCATE_OBJ(ObjUpvalue, OBJ_UPVALUE);
- upvalue->location = slot;
- return upvalue;
- }
- /// 分配内存空间
- /// \param size 空间大小
- /// \param type 对象类型
- /// \return Obj*
- static Obj *allocateObject(size_t size, ObjType type) {
- Obj *object = (Obj *) reallocate(NULL, 0, size);
- object->type = type;
- object->next = vm.objects;
- vm.objects = object;
- return object;
- }
- /// Hash 函数 -- 使用 FNV-1a 算法
- /// \param key
- /// \param length
- /// \return
- static uint32_t hashString(const char *key, int length) {
- uint32_t hash = 2166136261u;
- for (int i = 0; i < length; i++) {
- hash ^= (uint8_t) key[i];
- hash *= 16777619;
- }
- return hash;
- }
|