zero copy blob

This commit is contained in:
2026-02-20 14:06:42 -06:00
parent c5ad4f0a99
commit ebfc89e072
11 changed files with 160 additions and 153 deletions

View File

@@ -10,6 +10,8 @@ extern "C" {
// blob fns
JSValue js_core_blob_use(JSContext *js);
JSValue js_new_blob_alloc(JSContext *js, size_t bytes, void **out);
void js_blob_stone(JSValue blob, size_t actual_bytes);
JSValue js_new_blob_stoned_copy(JSContext *js, void *data, size_t bytes);
void *js_get_blob_data(JSContext *js, size_t *size, JSValue v); // bytes
void *js_get_blob_data_bits(JSContext *js, size_t *bits, JSValue v); // bits

View File

@@ -10048,16 +10048,38 @@ JSValue js_core_blob_use (JSContext *js) {
return JS_GetPropertyStr (js, js->global_obj, "blob");
}
/* Create a new blob from raw data, stone it, and return as JSValue */
JSValue js_new_blob_stoned_copy (JSContext *js, void *data, size_t bytes) {
/* Allocate a mutable blob. *out receives writable pointer to data area.
WARNING: *out is invalidated by ANY GC-triggering operation.
Caller fills data, then calls js_blob_stone(). */
JSValue js_new_blob_alloc (JSContext *js, size_t bytes, void **out) {
size_t bits = bytes * 8;
JSValue bv = js_new_heap_blob (js, bits);
if (JS_IsException (bv)) return bv;
if (JS_IsException (bv)) {
*out = NULL;
return bv;
}
JSBlob *bd = (JSBlob *)chase (bv);
if (bytes > 0)
memcpy (bd->bits, data, bytes);
bd->length = bits;
*out = bd->bits;
return bv;
}
/* Set actual length and stone the blob. actual_bytes <= allocated bytes.
Does NOT allocate — cannot trigger GC. */
void js_blob_stone (JSValue blob, size_t actual_bytes) {
JSBlob *bd = (JSBlob *)chase (blob);
bd->length = actual_bytes * 8;
bd->mist_hdr = objhdr_set_s (bd->mist_hdr, true);
}
/* Create a new blob from raw data, stone it, and return as JSValue */
JSValue js_new_blob_stoned_copy (JSContext *js, void *data, size_t bytes) {
void *out;
JSValue bv = js_new_blob_alloc (js, bytes, &out);
if (JS_IsException (bv)) return bv;
if (bytes > 0)
memcpy (out, data, bytes);
js_blob_stone (bv, bytes);
return bv;
}