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; return NULL;
} }
clock_t start = clock(), end; clock_t start = clock(), end;
ArgonObject * registers[UINT8_MAX]; RuntimeState state = init_runtime_state(translated, path);
RuntimeState state = init_runtime_state(translated, path, registers);
Stack *main_scope = create_scope(Global_Scope, true); Stack *main_scope = create_scope(Global_Scope, true);
runtime(translated, state, main_scope, err); runtime(translated, state, main_scope, err);
if (err->exists) { if (err->exists) {

View File

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

View File

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

View File

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

View File

@@ -15,6 +15,8 @@
#define likely(x) __builtin_expect(!!(x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0) #define unlikely(x) __builtin_expect(!!(x), 0)
#define MAX_REGISTERS 256
extern ArgonObject *ARGON_METHOD_TYPE; extern ArgonObject *ARGON_METHOD_TYPE;
extern Stack *Global_Scope; 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, static inline void run_instruction(Translated *translated, RuntimeState *state,
struct Stack **stack, ArErr *err); 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); Stack *create_scope(Stack *prev, bool force);

View File

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

View File

@@ -1,5 +1,6 @@
#term.log(global) #term.log(global)
let i = 1e7 let i = 1e7
while (i) do while (true) do
i=i-1 term.log(global)
term.log(i) #i=i-1
#term.log(i)