Merge branch 'cleanup_thinc'

This commit is contained in:
2026-02-18 18:00:34 -06:00
6 changed files with 228 additions and 270 deletions

39
qop.c
View File

@@ -84,11 +84,11 @@ static qop_desc *js2qop(JSContext *js, JSValue v) {
static int js_qop_ensure_index(JSContext *js, qop_desc *qop) {
if (qop->hashmap != NULL) return 1;
void *buffer = js_malloc(js, qop->hashmap_size);
void *buffer = js_malloc_rt(qop->hashmap_size);
if (!buffer) return 0;
int num = qop_read_index(qop, buffer);
if (num == 0) {
js_free(js, buffer);
js_free_rt(buffer);
return 0;
}
return 1;
@@ -102,14 +102,14 @@ JSC_CCALL(qop_open,
else if (!data)
ret = JS_RaiseDisrupt(js, "Empty blob");
else {
qop_desc *qop = js_malloc(js, sizeof(qop_desc));
qop_desc *qop = js_malloc_rt(sizeof(qop_desc));
if (!qop)
ret = JS_RaiseOOM(js);
else {
int size = qop_open_data((const unsigned char *)data, len, qop);
if (size == 0) {
js_free(js, qop);
ret = JS_RaiseDisrupt(js, "Failed to open QOP archive from blob");
js_free_rt(qop);
ret = JS_ThrowReferenceError(js, "Failed to open QOP archive from blob");
} else {
JSValue obj = JS_NewObjectClass(js, js_qop_archive_class_id);
JS_SetOpaque(obj, qop);
@@ -127,7 +127,7 @@ JSC_CCALL(qop_write,
JS_FreeCString(js, path);
if (!fh) return JS_RaiseDisrupt(js, "Could not open file for writing");
qop_writer *w = js_malloc(js, sizeof(qop_writer));
qop_writer *w = js_malloc_rt(sizeof(qop_writer));
if (!w) {
fclose(fh);
return JS_RaiseOOM(js);
@@ -137,10 +137,10 @@ JSC_CCALL(qop_write,
w->capacity = 1024;
w->len = 0;
w->size = 0;
w->files = js_malloc(js, sizeof(qop_file) * w->capacity);
w->files = js_malloc_rt(sizeof(qop_file) * w->capacity);
if (!w->files) {
fclose(fh);
js_free(js, w);
js_free_rt(w);
return JS_RaiseOOM(js);
}
@@ -183,18 +183,18 @@ static JSValue js_qop_read(JSContext *js, JSValue self, int argc, JSValue *argv)
return JS_NULL;
}
unsigned char *dest = js_malloc(js, file->size);
unsigned char *dest = js_malloc_rt(file->size);
if (!dest)
return JS_RaiseOOM(js);
int bytes = qop_read(qop, file, dest);
if (bytes == 0) {
js_free(js, dest);
js_free_rt(dest);
return JS_RaiseDisrupt(js, "Failed to read file");
}
JSValue blob = js_new_blob_stoned_copy(js, dest, bytes);
js_free(js, dest);
js_free_rt(dest);
return blob;
}
@@ -223,18 +223,18 @@ static JSValue js_qop_read_ex(JSContext *js, JSValue self, int argc, JSValue *ar
if (JS_ToUint32(js, &start, argv[1]) < 0 || JS_ToUint32(js, &len, argv[2]) < 0)
return JS_RaiseDisrupt(js, "Invalid start or len");
unsigned char *dest = js_malloc(js, len);
unsigned char *dest = js_malloc_rt(len);
if (!dest)
return JS_RaiseOOM(js);
int bytes = qop_read_ex(qop, file, dest, start, len);
if (bytes == 0) {
js_free(js, dest);
js_free_rt(dest);
return JS_RaiseDisrupt(js, "Failed to read file part");
}
JSValue blob = js_new_blob_stoned_copy(js, dest, bytes);
js_free(js, dest);
js_free_rt(dest);
return blob;
}
@@ -254,19 +254,19 @@ static JSValue js_qop_list(JSContext *js, JSValue self, int argc, JSValue *argv)
qop_file *file = &qop->hashmap[i];
if (file->size == 0) continue; // empty slot
char *path = js_malloc(js, file->path_len);
char *path = js_malloc_rt(file->path_len);
if (!path) {
return JS_RaiseOOM(js);
}
int len = qop_read_path(qop, file, path);
if (len == 0) {
js_free(js, path);
js_free_rt(path);
continue; // skip on error
}
JSValue str = JS_NewStringLen(js, path, len - 1); // -1 for null terminator
js_free(js, path);
js_free_rt(path);
JS_SetPropertyNumber(js, arr, count++, str);
}
@@ -311,7 +311,7 @@ static JSValue js_qop_is_directory(JSContext *js, JSValue self, int argc, JSValu
// Check if any file starts with path + "/"
size_t path_len = strlen(path);
char *prefix = js_malloc(js, path_len + 2);
char *prefix = alloca(path_len + 2);
memcpy(prefix, path, path_len);
prefix[path_len] = '/';
prefix[path_len + 1] = '\0';
@@ -339,7 +339,6 @@ static JSValue js_qop_is_directory(JSContext *js, JSValue self, int argc, JSValu
}
}
js_free(js, prefix);
JS_FreeCString(js, path);
return JS_NewBool(js, found);
}
@@ -366,7 +365,7 @@ static JSValue js_writer_add_file(JSContext *js, JSValue self, int argc, JSValue
if (w->len >= w->capacity) {
w->capacity *= 2;
qop_file *new_files = js_realloc(js, w->files, sizeof(qop_file) * w->capacity);
qop_file *new_files = realloc(w->files, sizeof(qop_file) * w->capacity);
if (!new_files) {
JS_FreeCString(js, path);
return JS_RaiseOOM(js);