Browse Source

allow making a context at a specified location, to use the userdata as wanted for allocations more easily

main
Ludovic 'Archivist' Lagouardette 1 week ago
parent
commit
403bd7efad
2 changed files with 52 additions and 12 deletions
  1. +11
    -0
      include/ink.h
  2. +41
    -12
      lib.c

+ 11
- 0
include/ink.h View File

@ -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

+ 41
- 12
lib.c View File

@ -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)

Loading…
Cancel
Save