From dc5586e94c4470f38b96f1c11177362de4d318a8 Mon Sep 17 00:00:00 2001 From: cozis Date: Fri, 21 Jan 2022 13:49:02 +0100 Subject: [PATCH] compilation error fix and added the ability to build a runtime using an external heap object --- src/objects/o_buffer.c | 6 ++-- src/runtime/runtime.c | 66 +++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/objects/o_buffer.c b/src/objects/o_buffer.c index 92f4302..d7c18fa 100644 --- a/src/objects/o_buffer.c +++ b/src/objects/o_buffer.c @@ -111,6 +111,8 @@ Object *Object_SliceBuffer(Object *buffer, int offset, int length, Heap *heap, E return NULL; } + BufferSliceObject *slice; + if(buffer->type == &t_buffer) { BufferObject *original = (BufferObject*) buffer; @@ -118,7 +120,7 @@ Object *Object_SliceBuffer(Object *buffer, int offset, int length, Heap *heap, E if(offset == 0 && length == original->size) return buffer; - BufferSliceObject *slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error); + slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error); if(slice == NULL) return NULL; @@ -149,7 +151,7 @@ Object *Object_SliceBuffer(Object *buffer, int offset, int length, Heap *heap, E { assert(buffer->type == &t_buffer_slice); - BufferSliceObject *slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error); + slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error); if(slice == NULL) return NULL; diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index a9fb483..3567e2d 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -18,6 +18,7 @@ struct xFrame { struct xRuntime { void *callback_userp; _Bool (*callback_addr)(Runtime*, void*); + _Bool free_heap; Object *builtins; int depth; Frame *frame; @@ -84,52 +85,51 @@ Executable *Runtime_GetCurrentExecutable(Runtime *runtime) return runtime->frame->exe; } -Runtime *Runtime_New(int stack_size, int heap_size, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*)) +Runtime *Runtime_New2(int stack_size, Heap *heap, _Bool free_heap, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*)) { if(stack_size < 0) stack_size = 1024; - if(heap_size < 0) - heap_size = 65536; + Runtime *runtime = malloc(sizeof(Runtime)); - Runtime *runtime; + if(runtime != NULL) + { + runtime->heap = heap; + runtime->stack = Stack_New(stack_size); - { - runtime = malloc(sizeof(Runtime)); + if(runtime->stack == NULL) + { + Heap_Free(runtime->heap); + free(runtime); + } - if(runtime == NULL) - return NULL; - - runtime->heap = Heap_New(heap_size); - - if(runtime->heap == NULL) - { - free(runtime); - return NULL; - } - - runtime->stack = Stack_New(stack_size); - - if(runtime->stack == NULL) - { - Heap_Free(runtime->heap); - free(runtime); - } - - runtime->callback_userp = callback_userp; - runtime->callback_addr = callback_addr; - runtime->builtins = NULL; - runtime->frame = NULL; - runtime->depth = 0; - - } + runtime->free_heap = free_heap; + runtime->callback_userp = callback_userp; + runtime->callback_addr = callback_addr; + runtime->builtins = NULL; + runtime->frame = NULL; + runtime->depth = 0; + } return runtime; } +Runtime *Runtime_New(int stack_size, int heap_size, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*)) +{ + if(heap_size < 0) + heap_size = 65536; + + Heap *heap = Heap_New(heap_size); + + if(heap == NULL) + return NULL; + + return Runtime_New2(stack_size, heap, 1, callback_userp, callback_addr); +} void Runtime_Free(Runtime *runtime) { - Heap_Free(runtime->heap); + if(runtime->free_heap) + Heap_Free(runtime->heap); Stack_Free(runtime->stack); free(runtime); }