object.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "chunk.h"
  13. #include "common.h"
  14. #include "table.h"
  15. #include "value.h"
  16. #define OBJ_TYPE(value) (AS_OBJ(value)->type)
  17. #define IS_CLASS(value) isObjType(value, OBJ_CLASS)
  18. #define IS_CLOSURE(value) isObjType(value, OBJ_CLOSURE)
  19. #define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
  20. #define IS_INSTANCE(value) isObjType(value, OBJ_INSTANCE)
  21. #define IS_NATIVE(value) isObjType(value, OBJ_NATIVE)
  22. #define IS_STRING(value) isObjType(value, OBJ_STRING)
  23. #define AS_CLASS(value) ((ObjClass *) AS_OBJ(value))
  24. #define AS_CLOSURE(value) ((ObjClosure *) AS_OBJ(value))
  25. #define AS_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
  26. #define AS_INSTANCE(value) ((ObjInstance *) AS_OBJ(value))
  27. #define AS_NATIVE(value) (((ObjNative *) AS_OBJ(value))->function)
  28. #define AS_STRING(value) ((ObjString *) AS_OBJ(value))
  29. #define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
  30. typedef enum {
  31. OBJ_CLASS,
  32. OBJ_CLOSURE,
  33. OBJ_NATIVE,
  34. OBJ_FUNCTION,
  35. OBJ_INSTANCE,
  36. OBJ_STRING,
  37. OBJ_UPVALUE,
  38. } ObjType;
  39. struct Obj {
  40. ObjType type;
  41. bool isMarked;
  42. struct Obj *next;
  43. };
  44. typedef struct {
  45. struct Obj obj;
  46. int arity;// stores the number of parameters the function expects
  47. int upvalueCount;
  48. Chunk chunk;
  49. ObjString *name;
  50. } ObjFunction;
  51. typedef Value (*NativeFn)(int argCount, Value *args);
  52. typedef struct {
  53. struct Obj obj;
  54. NativeFn function;
  55. } ObjNative;
  56. struct ObjString {
  57. struct Obj obj;
  58. int length;
  59. char *chars;
  60. uint32_t hash;// 缓存 hash 值
  61. };
  62. typedef struct ObjUpvalue {
  63. struct Obj obj;
  64. Value *location;
  65. Value closed;
  66. struct ObjUpvalue *next;
  67. } ObjUpvalue;
  68. typedef struct {
  69. struct Obj obj;
  70. ObjFunction *function;
  71. ObjUpvalue **upvalues;
  72. int upvalueCount;
  73. } ObjClosure;
  74. typedef struct {
  75. struct Obj obj;
  76. ObjString *name;
  77. } ObjClass;
  78. typedef struct {
  79. struct Obj obj;
  80. ObjClass *klass;
  81. Table fields;
  82. } ObjInstance;
  83. ObjClass *newClass(ObjString *name);
  84. ObjClosure *newClosure(ObjFunction *function);
  85. ObjFunction *newFunction();
  86. ObjInstance *newInstance(ObjClass *klass);
  87. ObjNative *newNative(NativeFn function);
  88. ObjString *takeString(char *chars, int length);
  89. ObjString *copyString(const char *chars, int length);
  90. ObjUpvalue *newUpvalue(Value *slot);
  91. void printObject(Value value);
  92. static inline bool isObjType(Value value, ObjType type) {
  93. return IS_OBJ(value) && AS_OBJ(value)->type == type;
  94. }
  95. #endif//CLOX_OBJECT_H