object.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. tableSet(&vm.strings, string, NIL_VAL);
  27. return string;
  28. }
  29. ObjString *copyString(const char *chars, int length) {
  30. uint32_t hash = hashString(chars, length);
  31. ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
  32. if (interned != NULL) return interned;
  33. char *heapChars = ALLOCATE(char, length + 1);
  34. memcpy(heapChars, chars, length);
  35. heapChars[length] = '\0';
  36. return allocateString(heapChars, length, hash);
  37. }
  38. static void printFunction(ObjFunction *function) {
  39. if (function->name == NULL) {
  40. printf("<script>");
  41. return;
  42. }
  43. printf("<fn %s>", function->name->chars);
  44. }
  45. /// 打印对象
  46. /// \param value
  47. void printObject(Value value) {
  48. switch (OBJ_TYPE(value)) {
  49. case OBJ_NATIVE:
  50. printf("<native fn>");
  51. break;
  52. case OBJ_FUNCTION:
  53. printFunction(AS_FUNCTION(value));
  54. break;
  55. case OBJ_STRING:
  56. printf("%s", AS_CSTRING(value));
  57. break;
  58. case OBJ_CLOSURE:
  59. printFunction(AS_CLOSURE(value)->function);
  60. break;
  61. case OBJ_UPVALUE:
  62. printf("upvalue");
  63. break;
  64. }
  65. }
  66. ObjFunction *newFunction() {
  67. ObjFunction *function = ALLOCATE_OBJ(ObjFunction, OBJ_FUNCTION);
  68. function->arity = 0;
  69. function->upvalueCount = 0;
  70. function->name = NULL;
  71. initChunk(&function->chunk);
  72. return function;
  73. }
  74. ObjString *takeString(char *chars, int length) {
  75. uint32_t hash = hashString(chars, length);
  76. ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
  77. if (interned != NULL) {
  78. FREE_ARRAY(char, chars, length + 1);
  79. return interned;
  80. }
  81. return allocateString(chars, length, hash);
  82. }
  83. ObjNative *newNative(NativeFn function) {
  84. ObjNative *native = ALLOCATE_OBJ(ObjNative, OBJ_NATIVE);
  85. native->function = function;
  86. return native;
  87. }
  88. ObjClosure *newClosure(ObjFunction *function) {
  89. ObjUpvalue **upvalues = ALLOCATE(ObjUpvalue *, function->upvalueCount);
  90. for (int i = 0; i < function->upvalueCount; i++) {
  91. upvalues[i] = NULL;
  92. }
  93. ObjClosure *closure = ALLOCATE_OBJ(ObjClosure, OBJ_CLOSURE);
  94. closure->function = function;
  95. closure->upvalues = upvalues;
  96. closure->upvalueCount = function->upvalueCount;
  97. return closure;
  98. }
  99. ObjUpvalue *newUpvalue(Value *slot) {
  100. ObjUpvalue *upvalue = ALLOCATE_OBJ(ObjUpvalue, OBJ_UPVALUE);
  101. upvalue->closed = NIL_VAL;
  102. upvalue->location = slot;
  103. upvalue->next = NULL;
  104. return upvalue;
  105. }
  106. /// 分配内存空间
  107. /// \param size 空间大小
  108. /// \param type 对象类型
  109. /// \return Obj*
  110. static Obj *allocateObject(size_t size, ObjType type) {
  111. Obj *object = (Obj *) reallocate(NULL, 0, size);
  112. object->type = type;
  113. object->next = vm.objects;
  114. vm.objects = object;
  115. return object;
  116. }
  117. /// Hash 函数 -- 使用 FNV-1a 算法
  118. /// \param key
  119. /// \param length
  120. /// \return
  121. static uint32_t hashString(const char *key, int length) {
  122. uint32_t hash = 2166136261u;
  123. for (int i = 0; i < length; i++) {
  124. hash ^= (uint8_t) key[i];
  125. hash *= 16777619;
  126. }
  127. return hash;
  128. }