object.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void printObject(Value value) {
  46. switch (OBJ_TYPE(value)) {
  47. case OBJ_FUNCTION:
  48. printFunction(AS_FUNCTION(value));
  49. break;
  50. case OBJ_STRING:
  51. printf("%s", AS_CSTRING(value));
  52. break;
  53. }
  54. }
  55. ObjFunction *newFunction() {
  56. ObjFunction *function = ALLOCATE_OBJ(ObjFunction, OBJ_FUNCTION);
  57. function->arity = 0;
  58. function->name = NULL;
  59. initChunk(&function->chunk);
  60. return function;
  61. }
  62. ObjString *takeString(char *chars, int length) {
  63. uint32_t hash = hashString(chars, length);
  64. ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
  65. if (interned != NULL) {
  66. FREE_ARRAY(char, chars, length + 1);
  67. return interned;
  68. }
  69. return allocateString(chars, length, hash);
  70. }
  71. static Obj *allocateObject(size_t size, ObjType type) {
  72. Obj *object = (Obj *) reallocate(NULL, 0, size);
  73. object->type = type;
  74. object->next = vm.objects;
  75. vm.objects = object;
  76. return object;
  77. }
  78. /// Hash 函数 -- 使用 FNV-1a 算法
  79. /// \param key
  80. /// \param length
  81. /// \return
  82. static uint32_t hashString(const char *key, int length) {
  83. uint32_t hash = 2166136261u;
  84. for (int i = 0; i < length; i++) {
  85. hash ^= (uint8_t) key[i];
  86. hash *= 16777619;
  87. }
  88. return hash;
  89. }