diff --git a/src/execute.c b/src/execute.c index 5d25a02..76aaed5 100644 --- a/src/execute.c +++ b/src/execute.c @@ -1,4 +1,8 @@ +#ifndef _WIN32 #include +#else +#include +#endif #include #include #include @@ -524,16 +528,13 @@ void execute(Stack *stack, Stack *originstack, char *modname) // Native +#ifndef _WIN32 if (!strcmp(NAME, "native")) { - char *modulename = malloc(strlen(ARG) + 7 * sizeof(char)); + char *modulename = malloc(strlen(ARG) + 6 * 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) @@ -554,6 +555,34 @@ void execute(Stack *stack, Stack *originstack, char *modname) continue; } +#else + if (!strcmp(NAME, "native")) + { + char *modulename = malloc(strlen(ARG) + 7 * sizeof(char)); + strcpy(modulename, "./"); + strcat(modulename, ARG); + strcat(modulename, ".dll"); + void *module = LoadLibrary(TEXT(modulename)); + + 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))GetProcAddress(module, "labashka"); + + stack_init_callback(stack); + + labashka(stack_pop_callback, stack_push_callback, stack_len_callback, stack->stacksize); + + FreeLibrary(module); + + free(modulename); + + continue; + } +#endif // Self-modifying diff --git a/src/stack.c b/src/stack.c index 9c47137..d8ba880 100644 --- a/src/stack.c +++ b/src/stack.c @@ -6,6 +6,10 @@ #include "config.h" #include "stack.h" +#ifdef _WIN32 +#include "wincompat.h" +#endif + Stack *program_init(uint16_t stacksize) { Stack *stack = calloc(1,sizeof(&stack) + sizeof(Instruction) * PROGRAM_MAXSIZE + sizeof(int16_t) * MAX_LABELS + diff --git a/src/wincompat.c b/src/wincompat.c new file mode 100644 index 0000000..bfe4527 --- /dev/null +++ b/src/wincompat.c @@ -0,0 +1,57 @@ +#ifdef _WIN32 +#include +#include + +#include "wincompat.h" +size_t getline(char **lineptr, size_t *n, FILE *stream) { + char *bufptr = NULL; + char *p = bufptr; + size_t size; + int c; + + if (lineptr == NULL) { + return -1; + } + if (stream == NULL) { + return -1; + } + if (n == NULL) { + return -1; + } + bufptr = *lineptr; + size = *n; + + c = fgetc(stream); + if (c == EOF) { + return -1; + } + if (bufptr == NULL) { + bufptr = malloc(128); + if (bufptr == NULL) { + return -1; + } + size = 128; + } + p = bufptr; + while(c != EOF) { + if ((p - bufptr) > (size - 1)) { + size = size + 128; + bufptr = realloc(bufptr, size); + if (bufptr == NULL) { + return -1; + } + } + *p++ = c; + if (c == '\n') { + break; + } + c = fgetc(stream); + } + + *p++ = '\0'; + *lineptr = bufptr; + *n = size; + + return p - bufptr - 1; +} +#endif diff --git a/src/wincompat.h b/src/wincompat.h new file mode 100644 index 0000000..a6a89ee --- /dev/null +++ b/src/wincompat.h @@ -0,0 +1,6 @@ +#include +#include + +#ifdef _WIN32 +size_t getline(char **lineptr, size_t *n, FILE *stream); +#endif