fix memory bug occuring in debug mode and attempting to fix another

This commit is contained in:
William Bell
2025-11-11 02:30:14 +00:00
parent 434b0ed99e
commit 608fd86003
7 changed files with 47 additions and 39 deletions

View File

@@ -453,8 +453,7 @@ Stack *ar_import(char *current_directory, char *path_relative, ArErr *err) {
return NULL;
}
clock_t start = clock(), end;
ArgonObject * registers[UINT8_MAX];
RuntimeState state = init_runtime_state(translated, path, registers);
RuntimeState state = init_runtime_state(translated, path);
Stack *main_scope = create_scope(Global_Scope, true);
runtime(translated, state, main_scope, err);
if (err->exists) {

View File

@@ -134,7 +134,7 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
0);
}
if (CStackFrame) {
ArgonObject * registers[UINT8_MAX];
ArgonObject * registers[MAX_REGISTERS]; // fixed on the stack for speed purposes
StackFrame new_stackFrame = {
{object->value.argon_fn->translated.registerCount,
object->value.argon_fn->translated.registerAssignment,

View File

@@ -55,9 +55,13 @@ ArgonObject *create_ARGON_DICTIONARY_TYPE___string__(size_t argc,
string_length += length;
for (size_t i = 0; i < keys_length; i++) {
struct node_GC* node = keys[i];
if (!node) { fprintf(stderr, "NULL node at %zu\n", i); continue; }
ArgonObject *key = node->key;
ArgonObject *value = node->val;
if (!key) { fprintf(stderr, "NULL key at node %zu\n", i); continue; }
if (!value) { fprintf(stderr, "NULL value at node %zu\n", i); continue; }
ArgonObject *string_convert_method = get_builtin_field_for_class(
get_builtin_field(key, __class__), __repr__, key);

View File

@@ -826,9 +826,9 @@ static inline void load_variable(Translated *translated, RuntimeState *state,
return;
}
RuntimeState init_runtime_state(Translated translated, char *path, ArgonObject * registers[UINT8_MAX]) {
RuntimeState init_runtime_state(Translated translated, char *path) {
RuntimeState runtime = {
registers,
ar_alloc(translated.registerCount * sizeof(ArgonObject *)),
0,
path,
NULL,

View File

@@ -15,6 +15,8 @@
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define MAX_REGISTERS 256
extern ArgonObject *ARGON_METHOD_TYPE;
extern Stack *Global_Scope;
@@ -85,7 +87,7 @@ static inline uint64_t pop_bytecode(Translated *translated,
static inline void run_instruction(Translated *translated, RuntimeState *state,
struct Stack **stack, ArErr *err);
RuntimeState init_runtime_state(Translated translated, char *path, ArgonObject * registers[UINT8_MAX]);
RuntimeState init_runtime_state(Translated translated, char *path);
Stack *create_scope(Stack *prev, bool force);

View File

@@ -50,34 +50,37 @@ typedef long ssize_t;
#endif
ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
if (!lineptr || !n || !stream) return -1;
if (!lineptr || !n || !stream)
return -1;
size_t pos = 0;
int c;
size_t pos = 0;
int c;
if (*lineptr == NULL || *n == 0) {
*n = 128;
*lineptr = malloc(*n);
if (!*lineptr) return -1;
}
if (*lineptr == NULL || *n == 0) {
*n = 128;
*lineptr = malloc(*n);
if (!*lineptr)
return -1;
}
while ((c = fgetc(stream)) != EOF) {
if (pos + 1 >= *n) {
*n *= 2;
char *tmp = realloc(*lineptr, *n);
if (!tmp) return -1;
*lineptr = tmp;
}
(*lineptr)[pos++] = c;
if (c == '\n')
break;
}
if (pos == 0 && c == EOF)
while ((c = fgetc(stream)) != EOF) {
if (pos + 1 >= *n) {
*n *= 2;
char *tmp = realloc(*lineptr, *n);
if (!tmp)
return -1;
*lineptr = tmp;
}
(*lineptr)[pos++] = c;
if (c == '\n')
break;
}
(*lineptr)[pos] = '\0';
return (ssize_t)pos;
if (pos == 0 && c == EOF)
return -1;
(*lineptr)[pos] = '\0';
return (ssize_t)pos;
}
#else
#include "../external/linenoise/linenoise.h"
@@ -91,7 +94,7 @@ void handle_sigint(int sig) {
}
int execute_code(FILE *stream, char *path, Stack *scope,
ArgonObject * registers[UINT8_MAX], RuntimeState* runtime_state) {
RuntimeState *runtime_state) {
if (!stream) {
perror("fmemopen");
return 1;
@@ -150,7 +153,7 @@ int execute_code(FILE *stream, char *path, Stack *scope,
translated.constants.capacity = __translated.constants.capacity;
darray_free(&__translated.bytecode, NULL);
free(__translated.constants.data);
*runtime_state = init_runtime_state(translated, path, registers);
*runtime_state = init_runtime_state(translated, path);
runtime(translated, *runtime_state, scope, &err);
if (err.exists) {
output_err(err);
@@ -209,16 +212,14 @@ char *read_all_stdin(size_t *out_len) {
}
int shell() {
Stack *main_scope = create_scope(Global_Scope, true);
ArgonObject * registers[UINT8_MAX];
if (!isatty(STDIN_FILENO)) {
RuntimeState runtime_state;
size_t len;
char *data = read_all_stdin(&len);
FILE *file = fmemopen(data, len, "r");
int resp = execute_code(file, "<stdin>", main_scope, registers, &runtime_state);
int resp = execute_code(file, "<stdin>", main_scope, &runtime_state);
fclose(file);
free(data);
return resp;
@@ -308,12 +309,13 @@ int shell() {
totranslate[totranslatelength] = '\0';
RuntimeState runtime_state;
FILE *file = fmemopen((void *)totranslate, totranslatelength, "r");
int resp = execute_code(file, "<shell>", main_scope, registers, &runtime_state);
int resp = execute_code(file, "<shell>", main_scope, &runtime_state);
fclose(file);
if (resp) {
continue;
}
if (registers[0]&&registers[0] != ARGON_NULL) {
if (runtime_state.registers[0] &&
runtime_state.registers[0] != ARGON_NULL) {
ArErr err = no_err;
argon_call(output_object, 1,
(ArgonObject *[]){runtime_state.registers[0]}, &err,