Add #native #3

Merged
Aeris merged 1 commits from :master into master 2024-06-16 22:56:45 +00:00
3 changed files with 63 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <dlfcn.h>
#include "config.h" #include "config.h"
#include "execute.h" #include "execute.h"
@ -470,6 +471,38 @@ void execute(Stack *stack, Stack *originstack, char *modname)
continue; 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 // Self-modifying
if (!strcmp(NAME, "insert")) // arg <const> 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->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 parse_and_process(Stack *stack, FILE *file);
void repl(Stack *stack); 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);