| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /**
- ******************************************************************************
- * @file : object.cpp
- * @author : simon
- * @brief : None
- * @attention : None
- * @date : 2023/8/23
- ******************************************************************************
- */
- #include <stdio.h>
- #include <string.h>
- #include "memory.h"
- #include "object.h"
- #include "vm.h"
- static Obj *allocateObject(size_t size, ObjType type);
- #define ALLOCATE_OBJ(type, objectType) \
- (type *) allocateObject(sizeof(type), objectType)
- static ObjString *allocateString(char *chars, int length) {
- ObjString *string = ALLOCATE_OBJ(ObjString, OBJ_STRING);
- string->length = length;
- string->chars = chars;
- return string;
- }
- ObjString *copyString(const char *chars, int length) {
- char *heapChars = ALLOCATE(char, length + 1);
- memcpy(heapChars, chars, length);
- heapChars[length] = '\0';
- return allocateString(heapChars, length);
- }
- void printObject(Value value) {
- switch (OBJ_TYPE(value)) {
- case OBJ_STRING:
- printf("%s", AS_CSTRING(value));
- break;
- }
- }
- ObjString *takeString(char *chars, int length) {
- return allocateString(chars, length);
- }
- static Obj *allocateObject(size_t size, ObjType type) {
- Obj *object = (Obj *) reallocate(NULL, 0, size);
- object->type = type;
- object->next = vm.objects;
- vm.objects = object;
- return object;
- }
|