object.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. ******************************************************************************
  3. * @file : object.h
  4. * @author : simon
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2023/8/23
  8. ******************************************************************************
  9. */
  10. #ifndef CLOX_OBJECT_H
  11. #define CLOX_OBJECT_H
  12. #include "value.h"
  13. #define OBJ_TYPE(value) (AS_OBJ(value)->type)
  14. #define IS_STRING(value) isObjType(value, OBJ_STRING)
  15. #define AS_STRING(value) ((ObjString *) AS_OBJ(value))
  16. #define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
  17. typedef enum {
  18. OBJ_STRING,
  19. } ObjType;
  20. struct Obj {
  21. ObjType type;
  22. struct Obj *next;
  23. };
  24. struct ObjString {
  25. struct Obj obj;
  26. int length;
  27. char *chars;
  28. uint32_t hash;// 缓存 hash 值
  29. };
  30. ObjString *takeString(char *chars, int length);
  31. ObjString *copyString(const char *chars, int length);
  32. void printObject(Value value);
  33. static inline bool isObjType(Value value, ObjType type) {
  34. return IS_OBJ(value) && AS_OBJ(value)->type == type;
  35. }
  36. #endif//CLOX_OBJECT_H