From 71840d5b64c82d98dee3143d3aa09882e5580a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Mon, 17 Jun 2024 03:55:38 +0500 Subject: [PATCH] Add #native --- src/execute.c | 33 +++++++++++++++++++++++++++++++++ src/stack.c | 22 ++++++++++++++++++++++ src/stack.h | 8 ++++++++ 3 files changed, 63 insertions(+) diff --git a/src/execute.c b/src/execute.c index b7ee263..8b0b84d 100644 --- a/src/execute.c +++ b/src/execute.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "config.h" #include "execute.h" @@ -470,6 +471,38 @@ void execute(Stack *stack, Stack *originstack, char *modname) continue; } + // Native + + if (!strcmp(NAME, "native")) + { + char *modulename = malloc(strlen(ARG) + 7 * sizeof(char)); + strcpy(modulename, "./"); + strcat(modulename, ARG); +#ifdef _WIN32 + strcat(modulename, ".dll"); +#else + strcat(modulename, ".so"); +#endif + void *module = dlopen(modulename, RTLD_NOW); + + if (!module) + kms(stack, "UNABLE TO OPEN DYNAMIC LIBRARY"); + + int (*labashka)(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ); + labashka = (int (*)(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ))dlsym(module, "labashka"); + + + stack_init_callback(stack); + + labashka(stack_pop_callback, stack_push_callback, stack_len_callback, stack->stacksize); + + dlclose(module); + + free(modulename); + + continue; + } + // Self-modifying if (!strcmp(NAME, "insert")) // arg diff --git a/src/stack.c b/src/stack.c index a3aa456..eddb044 100644 --- a/src/stack.c +++ b/src/stack.c @@ -98,3 +98,25 @@ void parse_and_process(Stack *stack, FILE *file) stack->program[stack->program_size++] = (Instruction){duppedname, duppedarg, inst.data}; } } + +Stack *stack_callback_pointer; + +void stack_init_callback(Stack *stack) +{ + stack_callback_pointer = stack; +} + +uint16_t stack_pop_callback(void) +{ + return stack_pop(stack_callback_pointer); +} + +void stack_push_callback(uint16_t number) +{ + stack_push(stack_callback_pointer, number); +} + +size_t stack_len_callback(void) +{ + return stack_callback_pointer->pointer; +} diff --git a/src/stack.h b/src/stack.h index f958bcf..3f9fe0f 100644 --- a/src/stack.h +++ b/src/stack.h @@ -33,3 +33,11 @@ void kms(Stack *stack, const char *err); void parse_and_process(Stack *stack, FILE *file); void repl(Stack *stack); + +void stack_init_callback(Stack *stack); + +uint16_t stack_pop_callback(void); + +void stack_push_callback(uint16_t number); + +size_t stack_len_callback(void); -- 2.39.5