compilation error fix and added the ability to build a runtime using an external heap object

This commit is contained in:
cozis
2022-01-21 13:49:02 +01:00
parent 2a0af0f9bd
commit dc5586e94c
2 changed files with 37 additions and 35 deletions
+4 -2
View File
@@ -111,6 +111,8 @@ Object *Object_SliceBuffer(Object *buffer, int offset, int length, Heap *heap, E
return NULL; return NULL;
} }
BufferSliceObject *slice;
if(buffer->type == &t_buffer) if(buffer->type == &t_buffer)
{ {
BufferObject *original = (BufferObject*) 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) if(offset == 0 && length == original->size)
return buffer; return buffer;
BufferSliceObject *slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error); slice = (BufferSliceObject*) Heap_Malloc(heap, &t_buffer_slice, error);
if(slice == NULL) if(slice == NULL)
return 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); 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) if(slice == NULL)
return NULL; return NULL;
+33 -33
View File
@@ -18,6 +18,7 @@ struct xFrame {
struct xRuntime { struct xRuntime {
void *callback_userp; void *callback_userp;
_Bool (*callback_addr)(Runtime*, void*); _Bool (*callback_addr)(Runtime*, void*);
_Bool free_heap;
Object *builtins; Object *builtins;
int depth; int depth;
Frame *frame; Frame *frame;
@@ -84,52 +85,51 @@ Executable *Runtime_GetCurrentExecutable(Runtime *runtime)
return runtime->frame->exe; 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) if(stack_size < 0)
stack_size = 1024; stack_size = 1024;
if(heap_size < 0) Runtime *runtime = malloc(sizeof(Runtime));
heap_size = 65536;
Runtime *runtime; if(runtime != NULL)
{
runtime->heap = heap;
runtime->stack = Stack_New(stack_size);
{ if(runtime->stack == NULL)
runtime = malloc(sizeof(Runtime)); {
Heap_Free(runtime->heap);
free(runtime);
}
if(runtime == NULL) runtime->free_heap = free_heap;
return NULL; runtime->callback_userp = callback_userp;
runtime->callback_addr = callback_addr;
runtime->heap = Heap_New(heap_size); runtime->builtins = NULL;
runtime->frame = NULL;
if(runtime->heap == NULL) runtime->depth = 0;
{ }
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;
}
return runtime; 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) void Runtime_Free(Runtime *runtime)
{ {
Heap_Free(runtime->heap); if(runtime->free_heap)
Heap_Free(runtime->heap);
Stack_Free(runtime->stack); Stack_Free(runtime->stack);
free(runtime); free(runtime);
} }