object.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. ******************************************************************************
  3. * @file : object.cpp
  4. * @author : simon
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2023/8/23
  8. ******************************************************************************
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "memory.h"
  13. #include "object.h"
  14. #include "table.h"
  15. #include "vm.h"
  16. static Obj *allocateObject(size_t size, ObjType type);
  17. static uint32_t hashString(const char *key, int length);
  18. #define ALLOCATE_OBJ(type, objectType) \
  19. (type *) allocateObject(sizeof(type), objectType)
  20. static ObjString *allocateString(char *chars, int length,
  21. uint32_t hash) {
  22. ObjString *string = ALLOCATE_OBJ(ObjString, OBJ_STRING);
  23. string->length = length;
  24. string->chars = chars;
  25. string->hash = hash;
  26. push(OBJ_VAL(string));
  27. tableSet(&vm.strings, string, NIL_VAL);
  28. pop();
  29. return string;
  30. }
  31. ObjString *copyString(const char *chars, int length) {
  32. uint32_t hash = hashString(chars, length);
  33. ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
  34. if (interned != NULL) return interned;
  35. char *heapChars = ALLOCATE(char, length + 1);
  36. memcpy(heapChars, chars, length);
  37. heapChars[length] = '\0';
  38. return allocateString(heapChars, length, hash);
  39. }
  40. static void printFunction(ObjFunction *function) {
  41. if (function->name == NULL) {
  42. printf("<script>");
  43. return;
  44. }
  45. printf("<fn %s>", function->name->chars);
  46. }
  47. /// 打印对象
  48. /// \param value
  49. void printObject(Value value) {
  50. switch (OBJ_TYPE(value)) {
  51. case OBJ_NATIVE:
  52. printf("<native fn>");
  53. break;
  54. case OBJ_FUNCTION:
  55. printFunction(AS_FUNCTION(value));
  56. break;
  57. case OBJ_STRING:
  58. printf("%s", AS_CSTRING(value));
  59. break;
  60. case OBJ_CLOSURE:
  61. printFunction(AS_CLOSURE(value)->function);
  62. break;
  63. case OBJ_UPVALUE:
  64. printf("upvalue");
  65. break;
  66. case OBJ_CLASS:
  67. printf("%s", AS_CLASS(value)->name->chars);
  68. break;
  69. case OBJ_INSTANCE:
  70. printf("%s instance", AS_INSTANCE(value)->klass->name->chars);
  71. break;
  72. }
  73. }
  74. ObjFunction *newFunction() {
  75. ObjFunction *function = ALLOCATE_OBJ(ObjFunction, OBJ_FUNCTION);
  76. function->arity = 0;
  77. function->upvalueCount = 0;
  78. function->name = NULL;
  79. initChunk(&function->chunk);
  80. return function;
  81. }
  82. ObjString *takeString(char *chars, int length) {
  83. uint32_t hash = hashString(chars, length);
  84. ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
  85. if (interned != NULL) {
  86. FREE_ARRAY(char, chars, length + 1);
  87. return interned;
  88. }
  89. return allocateString(chars, length, hash);
  90. }
  91. ObjNative *newNative(NativeFn function) {
  92. ObjNative *native = ALLOCATE_OBJ(ObjNative, OBJ_NATIVE);
  93. native->function = function;
  94. return native;
  95. }
  96. ObjClosure *newClosure(ObjFunction *function) {
  97. ObjUpvalue **upvalues = ALLOCATE(ObjUpvalue *, function->upvalueCount);
  98. for (int i = 0; i < function->upvalueCount; i++) {
  99. upvalues[i] = NULL;
  100. }
  101. ObjClosure *closure = ALLOCATE_OBJ(ObjClosure, OBJ_CLOSURE);
  102. closure->function = function;
  103. closure->upvalues = upvalues;
  104. closure->upvalueCount = function->upvalueCount;
  105. return closure;
  106. }
  107. ObjUpvalue *newUpvalue(Value *slot) {
  108. ObjUpvalue *upvalue = ALLOCATE_OBJ(ObjUpvalue, OBJ_UPVALUE);
  109. upvalue->closed = NIL_VAL;
  110. upvalue->location = slot;
  111. upvalue->next = NULL;
  112. return upvalue;
  113. }
  114. ObjClass *newClass(ObjString *name) {
  115. ObjClass *klass = ALLOCATE_OBJ(ObjClass, OBJ_CLASS);
  116. klass->name = name;
  117. return klass;
  118. }
  119. ObjInstance *newInstance(ObjClass *klass) {
  120. ObjInstance *instance = ALLOCATE_OBJ(ObjInstance, OBJ_INSTANCE);
  121. instance->klass = klass;
  122. initTable(&instance->fields);
  123. return instance;
  124. }
  125. /// 分配内存空间
  126. /// \param size 空间大小
  127. /// \param type 对象类型
  128. /// \return Obj*
  129. static Obj *allocateObject(size_t size, ObjType type) {
  130. Obj *object = (Obj *) reallocate(NULL, 0, size);
  131. object->type = type;
  132. object->isMarked = false;
  133. object->next = vm.objects;
  134. vm.objects = object;
  135. #ifdef DEBUG_LOG_GC
  136. printf("%p allocate %zu for %d\n", (void *) object, size, type);
  137. #endif
  138. return object;
  139. }
  140. /// Hash 函数 -- 使用 FNV-1a 算法
  141. /// \param key
  142. /// \param length
  143. /// \return
  144. static uint32_t hashString(const char *key, int length) {
  145. uint32_t hash = 2166136261u;
  146. for (int i = 0; i < length; i++) {
  147. hash ^= (uint8_t) key[i];
  148. hash *= 16777619;
  149. }
  150. return hash;
  151. }