Permalink
Browse files

Further improvements

  • Loading branch information...
1 parent a025a08 commit 1bdcf6778585fddecc89223b47c3c3848e66e0a9 @txus committed Mar 3, 2013
View
@@ -31,7 +31,7 @@ kernel_files()
return entries;
}
-#define DEFPRIM(N, F) Value_set(state, primitives, (N), Closure_new(state, Function_native_new((F)), NULL))
+#define DEFPRIM(N, F) Value_set(state, primitives, (N), Closure_new(state, Function_native_new(state, (F)), NULL))
#define DEFVALUE(N, V) Value_set(state, types, (N), (V))
static inline void
@@ -115,8 +115,13 @@ State_bootstrap(STATE)
bconcat(path, filename);
debug("[BOOTSTRAP] Loading %s...", bdata(path));
Primitive_require(state, String_new(state, bdata(path)), NULL, NULL);
+
+ bdestroy(path);
+ bdestroy(filename);
}
+ DArray_destroy(filenames);
+
// Reenable debugger if needed
if(reenable_debugger) Debug = 1;
View
@@ -22,6 +22,7 @@ void
Debugger_destroy(Debugger* debugger)
{
DArray_clear_destroy(debugger->breakpoints);
+ if (debugger->current_file) bstrListDestroy(debugger->current_file);
free(debugger);
}
@@ -44,6 +45,8 @@ Debugger_load_current_file(STATE)
bstring current_filename = bfromcstr(CURR_FRAME->fn->filename);
bstring buf = readfile(current_filename);
+ if(!buf) return;
+ bdestroy(current_filename);
struct bstrList *lines = bsplit(buf, '\n');
@@ -54,13 +57,17 @@ Debugger_load_current_file(STATE)
for(int i=1; i <= lines->qty; i++) {
char linenum[8];
sprintf(linenum, "%-7i", i);
+ bstring _linenum = bfromcstr(linenum);
- binsert(*l, 0, bfromcstr(linenum), ' ');
+ binsert(*l, 0, _linenum, ' ');
+ bdestroy(_linenum);
l++;
}
DEBUGGER->current_file = lines;
};
+
+ bdestroy(buf);
}
void
@@ -176,6 +183,7 @@ Debugger_print_context(STATE)
bstring current_filename = bfromcstr(CURR_FRAME->fn->filename);
printf("\n%s:%i\n", bdata(current_filename), DEBUGGER->current_line);
+ bdestroy(current_filename);
DArray *lines = getlines(DEBUGGER->current_file, ctx_start, ctx_end);
View
@@ -22,18 +22,26 @@ void
Function_destroy(Function *fn)
{
if(fn) {
- if(fn->literals) DArray_destroy(fn->literals);
+ if(fn->c_fn) {
+ free(fn->filename);
+ } else {
+ DArray_destroy(fn->literals);
+ free(fn->code);
+ }
free(fn);
}
}
Function*
-Function_native_new(native_fn c_fn)
+Function_native_new(STATE, native_fn c_fn)
{
Function *fn = calloc(1, sizeof(Function));
char *name = malloc(9 * sizeof(char));
strcpy(name, "(native)");
+ // Keep track of native functions to deallocate them properly
+ DArray_push(state->native_fns, fn);
+
fn->filename = name;
fn->code = NULL;
fn->literals = NULL;
@@ -74,6 +82,7 @@ Function_call(
// Normal dispatch
CallFrame *new_frame = CallFrame_new(receiver, fn, ret);
+ DArray_destroy(new_frame->locals);
new_frame->locals = locals;
new_frame->name = name;
View
@@ -21,7 +21,7 @@ typedef struct function_s Function;
Function* Function_new(char*, int*, DArray*);
void Function_destroy(Function*);
-Function* Function_native_new(native_fn);
+Function* Function_native_new(struct state_s*, native_fn);
struct val_s* Function_native_call(struct state_s*, Function*, struct val_s*, DArray*);
int* Function_call(struct state_s*, Function*, struct val_s*, DArray*, char* name);
View
@@ -16,9 +16,16 @@ parse_string(STATE, bstring buf, BytecodeFile *file)
file->filename = bstrcpy(*line);
line++; cnt++;
+ bstring underscore = bfromcstr("_");
+
while(1) {
// eof
- if (bstrcmp(bmidstr(*line, 0, 1), bfromcstr("_")) != 0) break;
+ bstring prefix = bmidstr(*line, 0, 1);
+ if (bstrcmp(prefix, underscore) != 0) {
+ bdestroy(prefix);
+ break;
+ }
+ bdestroy(prefix);
// Get method name
int num_literals, num_instructions, linenum;
@@ -27,6 +34,7 @@ parse_string(STATE, bstring buf, BytecodeFile *file)
bstring *fnptr = fn_params->entry;
fnptr++; // linenum
linenum = atoi(bdata(*fnptr));
+ bstrListDestroy(fn_params);
}
bstring method = bmidstr(*line, 1, (*line)->mlen);
@@ -55,8 +63,6 @@ parse_string(STATE, bstring buf, BytecodeFile *file)
for(i=0; i < num_literals; i++) {
if (bdata(*line)[0] == '"') {
bstring strData = bmidstr(*line, 1, (*line)->mlen);
- // TODO: This causes a leak in the string literals because they are
- // never freed on destruction.
DArray_push(literals, String_new(state, bdata(strData)));
bdestroy(strData);
} else {
@@ -78,6 +84,7 @@ parse_string(STATE, bstring buf, BytecodeFile *file)
if(cnt >= lines->qty) break; // EOF
}
+ bdestroy(underscore);
bstrListDestroy(lines);
}
@@ -130,6 +137,11 @@ BytecodeFile_destroy(BytecodeFile *file)
Hashmap_traverse(file->functions, Hashmap_Function_destroy);
Hashmap_destroy(file->functions);
+ for(int i=0; i < DArray_count(file->function_names); i++) {
+ bdestroy((bstring)DArray_at(file->function_names, i));
+ }
+ DArray_destroy(file->function_names);
+
free(file);
}
View
@@ -153,6 +153,7 @@ Primitive_require(STATE, void *a, void *_, void *__)
Stack_push(FRAMES, frame);
VM_run(state); // ignore result of the require
+
Stack_pop(STACK);
return NilObject;
View
@@ -17,7 +17,7 @@ VALUE TrueObject = NULL;
VALUE FalseObject = NULL;
VALUE NilObject = NULL;
-#define DEFNATIVE(V, N, F) Value_set(state, (V), (N), Closure_new(state, Function_native_new((F)), NULL))
+#define DEFNATIVE(V, N, F) Value_set(state, (V), (N), Closure_new(state, Function_native_new(state, (F)), NULL))
void Runtime_init(STATE) {
Object_bp = Value_new(state, ObjectType);
View
@@ -76,6 +76,7 @@ State_destroy(STATE)
Function_destroy((Function*)DArray_at(state->native_fns, i));
}
DArray_destroy(state->native_fns);
+ Hashmap_destroy(state->functions);
free(state);
}
View
@@ -44,23 +44,24 @@ Hashmap_destroy_bstring_key(HashmapNode *node)
void
Value_destroy(VALUE o)
{
- o->prototype = NULL;
- switch(o->type) {
- case StringType:
- if(o->data.as_str) free(o->data.as_str);
- break;
- case VectorType:
- if(o->data.as_data) DArray_destroy((DArray*)(o->data.as_data));
- break;
- default:
- break;
- }
- if(o->fields) DArray_destroy(o->fields);
-
- Hashmap_traverse(o->table, Hashmap_destroy_bstring_key);
- Hashmap_destroy(o->table);
+ if(o) {
+ o->prototype = NULL;
+ switch(o->type) {
+ case StringType:
+ if(o->data.as_str) free(o->data.as_str);
+ break;
+ case VectorType:
+ if(o->data.as_data) DArray_destroy((DArray*)(o->data.as_data));
+ break;
+ default:
+ break;
+ }
+ DArray_destroy(o->fields);
+ Hashmap_traverse(o->table, Hashmap_destroy_bstring_key);
+ Hashmap_destroy(o->table);
- if(o) free(o);
+ free(o);
+ }
}
#define append(B, S) bconcat((B), bfromcstr(S))
@@ -188,10 +189,10 @@ VALUE
Closure_new(STATE, Function *fn, CallFrame *scope)
{
VALUE val = Value_from_prototype(state, ClosureType, Closure_bp);
- fn->scope = scope;
-
- // Keep track of native functions to deallocate them properly
- if(fn->c_fn) DArray_push(state->native_fns, fn);
+ if(scope) {
+ scope->refcount += 1;
+ fn->scope = scope;
+ }
val->data.as_data = fn;
return val;
View
@@ -36,10 +36,13 @@ VALUE NilObject;
return NULL; \
#define DEFN(N, ...) \
- int _code[] = (int[]){__VA_ARGS__}; \
+ int *__code = (int[]){__VA_ARGS__}; \
+ unsigned short __count = sizeof((int[]){__VA_ARGS__})/sizeof(int); \
+ int *__instructions = calloc(__count, sizeof(int)); \
+ memcpy(__instructions, __code, __count * sizeof(int)); \
\
Function *_fn = calloc(1, sizeof(Function)); \
- _fn->code = _code; \
+ _fn->code = __instructions; \
\
Hashmap_set( \
fns, \
@@ -48,18 +51,22 @@ VALUE NilObject;
)
#define RUN(...) \
- int code[] = (int[]){__VA_ARGS__}; \
+ int *_code = (int[]){__VA_ARGS__}; \
+ unsigned short _count = sizeof((int[]){__VA_ARGS__})/sizeof(int); \
+ int *_instructions = calloc(_count, sizeof(int)); \
+ memcpy(_instructions, _code, _count * sizeof(int)); \
\
- fn->code = code; \
+ fn->code = _instructions; \
fn->literals = literals; \
\
VALUE lobby = Lobby_new(state); \
state->lobby = lobby; \
CallFrame *top_frame = CallFrame_new(lobby, fn, NULL);\
+ DArray_destroy(top_frame->locals); \
top_frame->locals = locals; \
top_frame->name = "main"; \
Stack_push(FRAMES, top_frame); \
- /* State_bootstrap(state); \\ */ \
+ State_bootstrap(state); \
VALUE result = VM_run(state); \
char *test_pushself()

0 comments on commit 1bdcf67

Please sign in to comment.