object.h 3.0 KB

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