Add #native

This commit is contained in:
bʰedoh₂ swé 2024-06-17 03:55:38 +05:00
parent 37fb7e66b5
commit 71840d5b64
3 changed files with 63 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dlfcn.h>
#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 <const>

View File

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

View File

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