kuroko: replace crazy sed expressions with a simple patch file

This commit is contained in:
Connor Olding 2023-06-18 01:26:09 +02:00
parent aa1df1d660
commit 62eec2bdd0
2 changed files with 181 additions and 78 deletions

View File

@ -5,79 +5,7 @@ if [ ! -e /cosmo-kuroko.done ]; then
&& acquire from=github repo=kuroko-lang/kuroko dest=/root/kuroko env=KUROKO \
&& cd /root/kuroko \
&& cp -r . ../kuroko.orig \
&& shed src/os.c \
's@.*char \*\* environ.*@#ifndef COSMOPOLITAN_LIBC_RUNTIME_RUNTIME_H_\n&\n#endif@' \
\
&& shed src/vm.c \
'/static.*cache/c\\static KrkClassCacheEntry * cache = 0;' \
$'/define CACHE_SIZE/,/nextCount/{N;w /temp\nd}' \
$'/void krk_initVM/{r /temp\nN}' \
$'/Reset current thread/i\\\tcache = calloc(CACHE_SIZE, sizeof(KrkClassCacheEntry));' \
$'/krk_freeMemoryDebugger();/a\\\tcache = (free(cache), NULL);' \
\
&& shed src/threads.c 's/\bgettid();/pthread_getthreadid_np();/g' \
&& shed src/vm.c '/"initial-exec"/d' \
\
&& shed src/vm.c \
$'/Ensure we don\'t have a macro/i\\
\\#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) { \\\\\\
\tKrkInstance * module = (KrkInstance*)AS_OBJECT(init()); \\\\\\
\tkrk_attachNamedObject(&vm.modules, name, (KrkObj*)module); \\\\\\
\tkrk_attachNamedObject(&module->fields, "__name__", (KrkObj*)S(name)); \\\\\\
\tkrk_attachNamedValue(&module->fields, "__file__", NONE_VAL()); \\\\\\
}\\
' \
$'/krk_module_init_dis/a\\
\\#endif\\
\\#ifdef KRK_DISABLE_DYNAMIC\\
\t\tBUILTIN_MODULE("math", krk_module_onload_math)\\
\t\tBUILTIN_MODULE("random", krk_module_onload_random)\\
\t\tBUILTIN_MODULE("socket", krk_module_onload_socket)\\
\t\tBUILTIN_MODULE("timeit", krk_module_onload_timeit)\\
\t\tBUILTIN_MODULE("wcwidth", krk_module_onload_wcwidth)' \
\
&& shed src/modules/module_socket.c \
'/netdb\.h/a\#include <netinet/in.h>' \
's/\bin6addr_any\b/(struct in6_addr){0}/g' \
\
&& shed src/kuroko.c \
$'/strlen(buf)-1/{
s/if .*/if (buf[buflen-1] == \'\\\\n\') {/
i\\
\t\t\t\tsize_t buflen = strlen(buf);
a\\
\t\t\t\t\t\\if (buflen >= 2 && buf[buflen-2] == \'\\\\r\') buf[buflen-2] = \'\\\\n\', buf[buflen-1] = \'\\0\';\\
\t\t\t\t} else {
}' \
\
&& shed Makefile \
$'/ifdef KRK_DISABLE_THREADS/i\\
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\\
' \
$'/CFLAGS += -DKRK_DISABLE_THREADS/a\\
else\\
CFLAGS += -DKRK_MEDIOCRE_TLS' \
'/shell tools\/can-floor-without-libm\.sh/,/endif/d' \
\
&& cd /root \
&& { diff -NrU3 kuroko.orig/ kuroko/ > kuroko.patch; [ $? -le 1 ]; } \
|| exit $?
# diff exit status is normally 0 or 1, anything greater is an error.
# this isn't included in the patch because it's super specific to cosmopolitan. \
: \
&& cd /root/kuroko \
&& patch -p1 -i /vagrant/notwa-util/kuroko.patch \
&& shed src/kuroko.c 's/int main/dontinline int _main/' \
&& printf %s >>src/kuroko.c $'
STATIC_YOINK("__die");
@ -89,15 +17,10 @@ int main(int argc, char * argv[]) {
\treturn _main(argc, argv);
}
' \
|| exit $?
: \
&& cd /root/kuroko \
&& CC=cosmocc CFLAGS="-Os -g" AR=ar.com ARFLAGS=rcsD \
make.com kuroko modules/codecs/sbencs.krk modules/codecs/dbdata.krk \
-j2 KRK_DISABLE_RLINE=1 KRK_DISABLE_DYNAMIC=1 \
&& cp kuroko kuroko.com.dbg \
&& `#objcopy -S -O binary kuroko.com.dbg kuroko.com` \
&& x86_64-linux-musl-objcopy -S -O binary kuroko.com.dbg kuroko.com \
&& zipcopy.com kuroko.com.dbg kuroko.com \
\

180
notwa-util/kuroko.patch Normal file
View File

@ -0,0 +1,180 @@
--- 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
@@ -1081,7 +1081,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/os.c
+++ kuroko/src/os.c
@@ -21,7 +21,9 @@
#include <kuroko/util.h>
/* Did you know this is actually specified to not exist in a header? */
+#ifndef COSMOPOLITAN_LIBC_RUNTIME_RUNTIME_H_
extern char ** environ;
+#endif
#define DO_KEY(key) krk_attachNamedObject(AS_DICT(result), #key, (KrkObj*)krk_copyString(buf. key, strlen(buf .key)))
#define S_KEY(key,val) krk_attachNamedObject(AS_DICT(result), #key, (KrkObj*)val);
--- 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);