Skip to content

Commit

Permalink
wip: refactor fromCString to use size_t for length
Browse files Browse the repository at this point in the history
  • Loading branch information
vegerot committed Oct 25, 2024
1 parent 781b264 commit 4888de8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bytecode-vm-compiler/src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void error(char const* message) {
parser.had_error = true;
}

LoxString* fromCString(char const* cString, int length) {
LoxString* fromCString(char const* cString, size_t length) {
LoxString* str = malloc(sizeof(LoxString) + (length + 1) * sizeof(char));
str->obj.type = OBJ_STRING;
str->obj.next = currentChunk()->allocatorStart;
Expand Down
2 changes: 1 addition & 1 deletion bytecode-vm-compiler/src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#include <stdbool.h>
bool compile(char const* source, Chunk* chunk);
void setCompilingChunk(Chunk* chunk);
LoxString* fromCString(char const* cString, int length);
LoxString* fromCString(char const* cString, size_t length);
5 changes: 3 additions & 2 deletions bytecode-vm-compiler/src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
void printObject(Value value) {
switch (OBJ_TYPE(value)) {
case OBJ_STRING:
printf("%.*s", AS_STRING(value)->length, AS_CSTRING(value));
printf("%.*s", (int)AS_STRING(value)->length, AS_CSTRING(value));
break;
default:
printf("\n obj type: %u\n", OBJ_TYPE(value));
Expand All @@ -28,7 +28,8 @@ static LoxObj* allocateObj(VM* vm, size_t size, ObjType type) {
return object;
}

LoxString* allocateString(VM* vm, char const* cString, int length) {
// TODO: refactor to use `allocateEmptyString`
LoxString* allocateString(VM* vm, char const* cString, size_t length) {
LoxString* str = (LoxString*)allocateObj(
vm, sizeof(LoxString) + (length + 1) * sizeof(char), OBJ_STRING);

Expand Down
4 changes: 3 additions & 1 deletion bytecode-vm-compiler/src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct LoxObj {

typedef struct {
LoxObj obj;
int length;
/** same as return value of strlen */
size_t length;
char chars[];
} LoxString;

Expand All @@ -31,3 +32,4 @@ void printObject(Value value);
static inline bool isObjType(Value value, ObjType type) {
return IS_OBJ(value) && AS_OBJ(value)->type == type;
}

2 changes: 1 addition & 1 deletion bytecode-vm-compiler/test/test-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void testStringAppend(void) {
static void appendObjectToString(char* buf, size_t maxsize, Value value) {
switch (OBJ_TYPE(value)) {
case OBJ_STRING:
snprintf(buf, maxsize, "%.*s", AS_STRING(value)->length,
snprintf(buf, maxsize, "%.*s", (int)AS_STRING(value)->length,
AS_CSTRING(value));
break;
default:
Expand Down

0 comments on commit 4888de8

Please sign in to comment.