stargazing/notwa-util/kuroko.patch

177 lines
4.9 KiB
Diff

--- kuroko.orig/Makefile
+++ kuroko/Makefile
@@ -21,9 +21,6 @@
all: ${TARGET} ${MODULES} ${TOOLS} ${GENMODS}
-ifneq ($(shell tools/can-floor-without-libm.sh $(CC)),yes)
- LDLIBS += -lm
-endif
ifeq (,$(findstring mingw,$(CC)))
CFLAGS += -pthread
@@ -60,8 +57,15 @@
CFLAGS += -DKRK_DISABLE_DEBUG
endif
+ifdef KRK_DISABLE_DYNAMIC
+ BIN_OBJS += src/modules/module_math.o src/modules/module_random.o src/modules/module_socket.o src/modules/module_timeit.o src/modules/module_wcwidth.o
+ CFLAGS += -DKRK_DISABLE_DYNAMIC -DSTATIC_ONLY
+endif
+
ifdef KRK_DISABLE_THREADS
CFLAGS += -DKRK_DISABLE_THREADS
+else
+ CFLAGS += -DKRK_MEDIOCRE_TLS
endif
ifdef KRK_NO_DISASSEMBLY
--- kuroko.orig/src/kuroko.c
+++ kuroko/src/kuroko.c
@@ -10,6 +10,7 @@
#include <stdint.h>
#include <string.h>
#include <unistd.h>
+#include <getopt.h>
#include <signal.h>
#include <errno.h>
@@ -1081,7 +1082,10 @@
break;
}
- if (buf[strlen(buf)-1] != '\n') {
+ size_t buflen = strlen(buf);
+ if (buf[buflen-1] == '\n') {
+ if (buflen >= 2 && buf[buflen-2] == '\r') buf[buflen-2] = '\n', buf[buflen-1] = '0';
+ } else {
/* rline shouldn't allow this as it doesn't accept ^D to submit input
* unless the line is empty, but just in case... */
fprintf(stderr, "Expected end of line in repl input. Did you ^D early?\n");
--- kuroko.orig/src/modules/module_socket.c
+++ kuroko/src/modules/module_socket.c
@@ -13,6 +13,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include <netinet/in.h>
#endif
#ifdef AF_UNIX
#include <sys/un.h>
@@ -191,7 +192,7 @@
*sock_size = sizeof(struct sockaddr_in6);
sin->sin6_family = AF_INET6;
sin->sin6_port = htons(AS_int(addr->values.values[1]));
- sin->sin6_addr = in6addr_any;
+ sin->sin6_addr = (struct in6_addr){0};
return 0;
} else {
struct addrinfo *result;
--- kuroko.orig/src/threads.c
+++ kuroko/src/threads.c
@@ -77,7 +77,7 @@
/* Get our run function */
struct Thread * self = _threadObj;
self->threadState = &krk_currentThread;
- self->tid = gettid();
+ self->tid = pthread_getthreadid_np();
KrkValue runMethod = NONE_VAL();
KrkClass * ourType = self->inst._class;
--- kuroko.orig/src/vm.c
+++ kuroko/src/vm.c
@@ -17,6 +17,21 @@
#include "private.h"
#include "opcode_enum.h"
+#ifdef KRK_DISABLE_DYNAMIC
+KrkValue krk_module_onload_math(void);
+KrkValue krk_module_onload_random(void);
+KrkValue krk_module_onload_socket(void);
+KrkValue krk_module_onload_timeit(void);
+KrkValue krk_module_onload_wcwidth(void);
+#endif
+
+#define BUILTIN_MODULE(name, init) { \
+ KrkInstance * module = (KrkInstance*)AS_OBJECT(init()); \
+ krk_attachNamedObject(&vm.modules, name, (KrkObj*)module); \
+ krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)S(name)); \
+ krk_attachNamedValue(&module->fields, "__file__", NONE_VAL()); \
+}
+
/* Ensure we don't have a macro for this so we can reference a local version. */
#undef krk_currentThread
@@ -34,7 +49,6 @@
* can generally be allocated even with dlopen, but this is
* not guaranteed.
*/
-__attribute__((tls_model("initial-exec")))
__thread KrkThreadState krk_currentThread;
#else
/* There is only one thread, so don't store it as TLS... */
@@ -901,6 +915,16 @@
}
}
+#define CACHE_SIZE 4096
+typedef struct {
+ KrkString * name;
+ struct KrkClass * owner;
+ KrkValue value;
+ size_t index;
+} KrkClassCacheEntry;
+static KrkClassCacheEntry * cache = 0;
+static size_t nextCount = 1;
+
void krk_initVM(int flags) {
#if !defined(KRK_DISABLE_THREADS) && defined(__APPLE__) && defined(__aarch64__)
krk_forceThreadData();
@@ -909,6 +933,7 @@
vm.globalFlags = flags & 0xFF00;
vm.maximumCallDepth = KRK_CALL_FRAMES_MAX;
+ cache = calloc(CACHE_SIZE, sizeof(KrkClassCacheEntry));
/* Reset current thread */
krk_resetStack();
krk_currentThread.frames = calloc(vm.maximumCallDepth,sizeof(KrkCallFrame));
@@ -976,6 +1001,13 @@
#ifndef KRK_DISABLE_DEBUG
krk_module_init_dis();
#endif
+#ifdef KRK_DISABLE_DYNAMIC
+ BUILTIN_MODULE("math", krk_module_onload_math)
+ BUILTIN_MODULE("random", krk_module_onload_random)
+ BUILTIN_MODULE("socket", krk_module_onload_socket)
+ BUILTIN_MODULE("timeit", krk_module_onload_timeit)
+ BUILTIN_MODULE("wcwidth", krk_module_onload_wcwidth)
+#endif
#ifndef KRK_DISABLE_THREADS
krk_module_init_threading();
#endif
@@ -1014,6 +1046,7 @@
extern void krk_freeMemoryDebugger(void);
krk_freeMemoryDebugger();
+ cache = (free(cache), NULL);
}
/**
@@ -1608,16 +1641,6 @@
int krk_doRecursiveModuleLoad(KrkString * name) {
return krk_importModule(name,name);
}
-
-#define CACHE_SIZE 4096
-typedef struct {
- KrkString * name;
- struct KrkClass * owner;
- KrkValue value;
- size_t index;
-} KrkClassCacheEntry;
-static KrkClassCacheEntry cache[CACHE_SIZE] = {0};
-static size_t nextCount = 1;
static KrkClass * checkCache(KrkClass * type, KrkString * name, KrkValue * method) {
size_t index = (name->obj.hash ^ (type->obj.hash << 4)) & (CACHE_SIZE-1);