Exporting builtins and loading crt manually for carp imports.

This commit is contained in:
Markus Gustavsson 2016-02-25 16:38:16 +01:00
parent b63c6d7626
commit c3af14ae9b
3 changed files with 60 additions and 35 deletions

View File

@ -89,6 +89,10 @@ char* carp_get_load_library_error() {
return dlerror(); return dlerror();
} }
void carp_sleep(int millis) {
#error implement me?
}
#endif #endif
#if defined(WIN32) #if defined(WIN32)
@ -107,6 +111,7 @@ typedef struct module_list {
LARGE_INTEGER carp_perf_freq; LARGE_INTEGER carp_perf_freq;
HMODULE carp_main_module = INVALID_HANDLE_VALUE; HMODULE carp_main_module = INVALID_HANDLE_VALUE;
HMODULE carp_msvcrt_module = INVALID_HANDLE_VALUE;
module_list_t carp_loaded_modules = NULL; module_list_t carp_loaded_modules = NULL;
module_list_t new_module_list_node() { module_list_t new_module_list_node() {
@ -152,12 +157,15 @@ void free_all_modules_and_destroy_module_list(module_list_t lst) {
void carp_platform_init() { void carp_platform_init() {
QueryPerformanceFrequency(&carp_perf_freq); QueryPerformanceFrequency(&carp_perf_freq);
carp_main_module = GetModuleHandle(NULL); carp_main_module = GetModuleHandle(NULL);
carp_msvcrt_module = LoadLibrary("msvcrt.dll");
carp_loaded_modules = new_module_list_node(); carp_loaded_modules = new_module_list_node();
add_module_to_list(carp_loaded_modules, carp_msvcrt_module);
} }
void carp_platform_shutdown() { void carp_platform_shutdown() {
carp_main_module = INVALID_HANDLE_VALUE;
free_all_modules_and_destroy_module_list(carp_loaded_modules); free_all_modules_and_destroy_module_list(carp_loaded_modules);
carp_main_module = INVALID_HANDLE_VALUE;
carp_msvcrt_module = INVALID_HANDLE_VALUE;
carp_loaded_modules = NULL; carp_loaded_modules = NULL;
} }
@ -269,4 +277,8 @@ char* carp_get_load_library_error() {
return error_buf; return error_buf;
} }
void carp_sleep(int millis) {
Sleep(millis);
}
#endif #endif

View File

@ -11,71 +11,78 @@
#include "platform.h" #include "platform.h"
#endif #endif
#ifdef WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
typedef int unknown; typedef int unknown;
typedef void* typevar; typedef void* typevar;
typedef void* any; typedef void* any;
typedef char* string; typedef char* string;
int intsqrt(int x) { return (int)sqrt(x); } EXPORT int intsqrt(int x) { return (int)sqrt(x); }
float itof(int x) { return (float)x; } EXPORT float itof(int x) { return (float)x; }
#ifndef max #ifdef max
int max(int x, int y) { #undef max
EXPORT int max(int x, int y) {
return x > y ? x : y; return x > y ? x : y;
} }
#endif #endif
string itos(int x) { EXPORT string itos(int x) {
char *s = malloc(sizeof(char) * 32); char *s = malloc(sizeof(char) * 32);
snprintf(s, 32, "%d", x); snprintf(s, 32, "%d", x);
return s; return s;
} }
bool nullQMARK(void *p) { EXPORT bool nullQMARK(void *p) {
return p == NULL; return p == NULL;
} }
bool not(bool x) { EXPORT bool not(bool x) {
return !x; return !x;
} }
void panic(string msg) { EXPORT void panic(string msg) {
printf("Error: %s\n", msg); printf("Error: %s\n", msg);
exit(1); exit(1);
} }
void print(string msg) { EXPORT void print(string msg) {
printf("%s", msg); printf("%s", msg);
} }
void println(string msg) { EXPORT void println(string msg) {
printf("%s\n", msg); printf("%s\n", msg);
} }
int* fake() { EXPORT int* fake() {
return (int*)123; return (int*)123;
} }
void fake2(string *s) { EXPORT void fake2(string *s) {
} }
void eat_string(char *s) { EXPORT void eat_string(char *s) {
free(s); free(s);
} }
void eat_void(void *nothing) { EXPORT void eat_void(void *nothing) {
// nothing! // nothing!
} }
char *string_copy(char *s) { EXPORT char *string_copy(char *s) {
return strdup(s); return strdup(s);
} }
char *string_append(char *s1, char *s2) { EXPORT char *string_append(char *s1, char *s2) {
char *new_str = malloc(strlen(s1) + strlen(s2) + 1); char *new_str = malloc(strlen(s1) + strlen(s2) + 1);
new_str[0] = '\0'; new_str[0] = '\0';
strcat(new_str, s1); strcat(new_str, s1);
@ -83,14 +90,14 @@ char *string_append(char *s1, char *s2) {
return new_str; return new_str;
} }
bool file_existsQMARK(char *filename) { EXPORT bool file_existsQMARK(char *filename) {
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
return f != NULL; return f != NULL;
} }
typedef string* string_array; typedef string* string_array;
string_array string_array_new(int size) { EXPORT string_array string_array_new(int size) {
string_array a = calloc(size + 1, sizeof(string)); string_array a = calloc(size + 1, sizeof(string));
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
a[i] = strdup(""); a[i] = strdup("");
@ -98,7 +105,7 @@ string_array string_array_new(int size) {
return a; return a;
} }
int string_array_count(string_array array) { EXPORT int string_array_count(string_array array) {
int i = 0; int i = 0;
string_array p = array; string_array p = array;
while(*p) { while(*p) {
@ -108,18 +115,18 @@ int string_array_count(string_array array) {
return i; return i;
} }
string string_array_get(string_array array, int pos) { EXPORT string string_array_get(string_array array, int pos) {
return strdup(array[pos]); return strdup(array[pos]);
} }
string_array string_array_set(string_array array, int pos, string new_value) { EXPORT string_array string_array_set(string_array array, int pos, string new_value) {
array[pos] = strdup(new_value); array[pos] = strdup(new_value);
return array; return array;
} }
typedef string (*string_to_string_fn)(string); typedef string (*string_to_string_fn)(string);
string_array string_array_map(string_to_string_fn f, string_array array) { EXPORT string_array string_array_map(string_to_string_fn f, string_array array) {
string_array p = array; string_array p = array;
while(*p) { while(*p) {
string old_string = *p; string old_string = *p;
@ -130,17 +137,17 @@ string_array string_array_map(string_to_string_fn f, string_array array) {
return array; return array;
} }
int inc(x) { return x + 1; } EXPORT int inc(x) { return x + 1; }
int dec(x) { return x - 1; } EXPORT int dec(x) { return x - 1; }
void async(void *f) { EXPORT void async(void *f) {
printf("Async starting.\n"); printf("Async starting.\n");
carp_thread_t th = carp_thread_create(f, "Async"); carp_thread_t th = carp_thread_create(f, "Async");
carp_thread_destroy(th); carp_thread_destroy(th);
printf("Async done.\n"); printf("Async done.\n");
} }
int last_index_of(string s, char c) { EXPORT int last_index_of(string s, char c) {
int len = (int)strlen(s); int len = (int)strlen(s);
for(int i = len - 1; i >= 0; i--) { for(int i = len - 1; i >= 0; i--) {
if(s[i] == c) { if(s[i] == c) {
@ -150,7 +157,7 @@ int last_index_of(string s, char c) {
return -1; return -1;
} }
string substring(string s, int index) { EXPORT string substring(string s, int index) {
if(index >= strlen(s)) { if(index >= strlen(s)) {
panic("substring out of bounds"); panic("substring out of bounds");
} }
@ -158,32 +165,38 @@ string substring(string s, int index) {
return strdup(sub); return strdup(sub);
} }
string file_path_component(string s) { EXPORT string file_path_component(string s) {
int i = last_index_of(s, '/'); int i = last_index_of(s, '/');
return substring(s, i + 1); return substring(s, i + 1);
} }
string get_input() { EXPORT string get_input() {
char in[1024]; char in[1024];
fgets(in, 1024, stdin); fgets(in, 1024, stdin);
return strdup(in); return strdup(in);
} }
void call(void *f()) { EXPORT void call(void *f()) {
f(); f();
} }
void call1(void *f(int)) { EXPORT void call1(void *f(int)) {
f(1); f(1);
} }
void calls(void *f(char*)) { EXPORT void calls(void *f(char*)) {
f("hejsan"); f("hejsan");
} }
void printret(int (*f)()) { EXPORT void printret(int (*f)()) {
int x = f(); int x = f();
printf("ret = %d\n", x); printf("ret = %d\n", x);
} }
#ifdef WIN32
EXPORT void sleep(int millis) {
carp_sleep(millis);
}
#endif
#endif #endif

View File

@ -5,4 +5,4 @@ typedef void(*carp_thread_routine)(void* arg);
typedef struct carp_library* carp_library_t; typedef struct carp_library* carp_library_t;
void carp_sleep(int millis);