Skip to content

Commit 111f01e

Browse files
committed
Fix memory allocation and update catto
1 parent ad6eb77 commit 111f01e

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-13
lines changed

lib/atto.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,23 @@ export class Context {
143143
this.#commands[command](...args);
144144
}
145145

146+
get errorState() {
147+
return readString(exports.getErrorState(this.#getContext()));
148+
}
149+
150+
checkErrorState() {
151+
if (this.errorState != "CATTO_ERROR_STATE_NONE") {
152+
throw new Error(this.errorState);
153+
}
154+
}
155+
146156
load(code) {
147157
var codeString = internString(code);
148158

149159
try {
150160
exports.load(this.#getContext(), codeString);
161+
162+
this.checkErrorState();
151163
} finally {
152164
freeString(codeString);
153165
}
@@ -158,6 +170,10 @@ export class Context {
158170

159171
this.#steps++;
160172

173+
if (!running) {
174+
this.checkErrorState();
175+
}
176+
161177
return running;
162178
}
163179

lib/bin/atto.wasm

1.08 KB
Binary file not shown.

src/atto.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,33 @@ WASM_EXPORT_AS("freeString") void freeString(char* string) {
4141
free(string);
4242
}
4343

44+
void print(char* message) {
45+
main_log(message);
46+
main_log("\n");
47+
}
48+
4449
WASM_EXPORT_AS("init") void init(Block* memoryBase) {
50+
heapLogger = print;
51+
4552
firstFreeBlock = base = memoryBase;
53+
*firstFreeBlock = 0;
54+
}
55+
56+
WASM_EXPORT_AS("getErrorState") char* getErrorState(catto_Context* context) {
57+
switch (context->errorState) {
58+
case CATTO_ERROR_STATE_NONE: return "CATTO_ERROR_STATE_NONE";
59+
case CATTO_ERROR_STATE_UNEXPECTED_TOKEN: return "CATTO_ERROR_STATE_UNEXPECTED_TOKEN";
60+
case CATTO_ERROR_STATE_NO_RETURN: return "CATTO_ERROR_STATE_NO_RETURN";
61+
case CATTO_ERROR_STATE_MISMATCHED_OPENING_MARK: return "CATTO_ERROR_STATE_MISMATCHED_OPENING_MARK";
62+
case CATTO_ERROR_STATE_MISMATCHED_CLOSING_MARK: return "CATTO_ERROR_STATE_MISMATCHED_CLOSING_MARK";
63+
case CATTO_ERROR_STATE_LOOP_CONTROL_OUTSIDE_LOOP: return "CATTO_ERROR_STATE_LOOP_CONTROL_OUTSIDE_LOOP";
64+
case CATTO_ERROR_STATE_UNKNOWN_PROCEDURE: return "CATTO_ERROR_STATE_UNKNOWN_PROCEDURE";
65+
case CATTO_ERROR_STATE_NOT_A_FUNCTION: return "CATTO_ERROR_STATE_NOT_A_FUNCTION";
66+
case CATTO_ERROR_STATE_NOT_A_LIST: return "CATTO_ERROR_STATE_NOT_A_LIST";
67+
case CATTO_ERROR_STATE_INVALID_LIST_VALUE: return "CATTO_ERROR_STATE_INVALID_LIST_VALUE";
68+
case CATTO_ERROR_STATE_CANNOT_ASSIGN_VALUE: return "CATTO_ERROR_STATE_CANNOT_ASSIGN_VALUE";
69+
default: return "ATTOJS_ERROR_STATE_UNKNOWN";
70+
}
4671
}
4772

4873
void handleCommand(catto_Context* context) {

src/memory.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
Block* base;
1818
Block* firstFreeBlock;
1919

20+
void (*heapLogger)(char*);
21+
2022
void* memset(void* destination, int value, size_t size) {
2123
for (size_t i = 0; i < size; i++) {
2224
*((char*)destination + size) = value;
@@ -81,19 +83,16 @@ void* malloc(size_t size) {
8183

8284
Block* originalBlock = currentBlock;
8385
size_t totalUsableSize = originalBlockSize;
84-
bool encounteredUsedBlock = false;
8586
bool encounteredLastBlock = false;
8687

87-
DEBUG_HEAP_LOG("Attempt resize");
88+
DEBUG_HEAP_LOG("Attempt merge");
8889

89-
do {
90+
while (true) {
9091
currentBlock += sizeof(Block) + BLOCK_SIZE(currentBlock);
9192

9293
if (BLOCK_IS_USED(currentBlock)) {
9394
DEBUG_HEAP_LOG(" Encountered used block");
9495

95-
encounteredUsedBlock = true;
96-
9796
break;
9897
}
9998

@@ -108,20 +107,26 @@ void* malloc(size_t size) {
108107
}
109108

110109
totalUsableSize += *currentBlock + sizeof(Block);
111-
} while (totalUsableSize < size);
112-
113-
if (encounteredUsedBlock) {
114-
continue;
115110
}
116111

117-
currentBlock = originalBlock;
118-
119112
if (encounteredLastBlock) {
120113
DEBUG_HEAP_LOG(" Using last block");
121114

115+
currentBlock = originalBlock;
116+
122117
*(currentBlock + sizeof(Block) + size) = 0; // Create a new last block after this one
118+
originalBlockSize = totalUsableSize;
123119
} else {
124-
*currentBlock = totalUsableSize;
120+
*originalBlock = totalUsableSize;
121+
122+
if (totalUsableSize < size + sizeof(Block)) {
123+
continue;
124+
}
125+
126+
DEBUG_HEAP_LOG(" Use merged block");
127+
128+
currentBlock = originalBlock;
129+
originalBlockSize = totalUsableSize;
125130
}
126131
}
127132

src/memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ typedef char bool;
88
extern Block* base;
99
extern Block* firstFreeBlock;
1010

11+
extern void (*heapLogger)(char*);
12+
1113
void* memset(void* destination, int value, size_t size);
1214
void* memcpy(void* destination, const void* source, size_t size);
1315
void* malloc(size_t size);

0 commit comments

Comments
 (0)