diff --git a/include/ink.h b/include/ink.h index e7fc5d4..15e1357 100644 --- a/include/ink.h +++ b/include/ink.h @@ -232,6 +232,17 @@ int ink_push_fn(struct context *ctx, struct stack_frame value); */ struct context* ink_make_context(void*(*malloc)(struct context*, size_t), void*(*realloc)(struct context*, void*, size_t), void(*free)(struct context*, void*), int(*putchar)(struct context*, int)); +/** + * Create a context to execute routines in-place + * @param location a pointer to where the context should be built, userdata can be set in advance + * @param malloc the memory allocation function, with a signature similar to the standard malloc + * @param realloc the memory allocation function, with a signature similar to the standard realloc + * @param free the memory allocation function, with a signature similar to the standard free + * @param putchar a function to print to the output character by character + * @return a pointer to a context allocated within the malloc function itself. + */ +void ink_make_context_inplace(struct context* location, void*(*malloc)(struct context*, size_t), void*(*realloc)(struct context*, void*, size_t), void(*free)(struct context*, void*), int(*putchar)(struct context*, int)); + #ifndef NOSTDLIB /** * Creates a context that includes the standard library of ink, as well as uses the C standard library to operate diff --git a/lib.c b/lib.c index 635adf6..0c96284 100644 --- a/lib.c +++ b/lib.c @@ -298,20 +298,20 @@ void ink_pop(struct context* ctx) { struct context* ink_make_context(void*(*malloc)(struct context*, size_t), void*(*realloc)(struct context*, void*, size_t), void(*free)(struct context*, void*), int(*putchar)(struct context*, int)) { struct context* ctx; ctx = (struct context*)malloc(NULL, sizeof(struct context)); - ctx->malloc = malloc; - ctx->realloc = realloc; - ctx->free = free; - ctx->inner_malloc = malloc; - ctx->inner_realloc = realloc; - ctx->inner_free = free; - ctx->putchar = putchar; + ctx->malloc = malloc; + ctx->realloc = realloc; + ctx->free = free; + ctx->inner_malloc = malloc; + ctx->inner_realloc = realloc; + ctx->inner_free = free; + ctx->putchar = putchar; ctx->panic = 0; ctx->routines = NULL; ctx->routines_capacity = 0; ctx->routines_top = 0; - ctx->types = NULL; - ctx->types_capacity = 0; - ctx->types_top = 0; + ctx->types = NULL; + ctx->types_capacity = 0; + ctx->types_top = 0; ctx->native_words = NULL; ctx->native_words_capacity = 0; ctx->native_words_top = 0; @@ -321,11 +321,40 @@ struct context* ink_make_context(void*(*malloc)(struct context*, size_t), void*( ctx->lex_reserved_words = NULL; ctx->lex_reserved_words_capacity = 0; ctx->lex_reserved_words_top = 0; - ctx->collections = 0; - ctx->steps = 0; + ctx->collections = 0; + ctx->steps = 0; return ctx; } +void ink_make_context_inplace(struct context* location, void*(*malloc)(struct context*, size_t), void*(*realloc)(struct context*, void*, size_t), void(*free)(struct context*, void*), int(*putchar)(struct context*, int)) { + struct context* ctx = location; + ctx->malloc = malloc; + ctx->realloc = realloc; + ctx->free = free; + ctx->inner_malloc = malloc; + ctx->inner_realloc = realloc; + ctx->inner_free = free; + ctx->putchar = putchar; + ctx->panic = 0; + ctx->routines = NULL; + ctx->routines_capacity = 0; + ctx->routines_top = 0; + ctx->types = NULL; + ctx->types_capacity = 0; + ctx->types_top = 0; + ctx->native_words = NULL; + ctx->native_words_capacity = 0; + ctx->native_words_top = 0; + ctx->words = NULL; + ctx->words_capacity = 0; + ctx->words_top = 0; + ctx->lex_reserved_words = NULL; + ctx->lex_reserved_words_capacity = 0; + ctx->lex_reserved_words_top = 0; + ctx->collections = 0; + ctx->steps = 0; +} + /** * Allocates a string that contains the integer * @param _ context (used to allocate)