diff --git a/source/qjs_blob.c b/source/qjs_blob.c index de020da2..d135b65f 100644 --- a/source/qjs_blob.c +++ b/source/qjs_blob.c @@ -195,6 +195,46 @@ static JSValue js_blob_write_number(JSContext *ctx, JSValueConst this_val, return JS_NULL; } +// blob.w16(value) - write a 16-bit value (short) +static JSValue js_blob_w16(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) { + if (argc < 1) + return JS_ThrowTypeError(ctx, "w16(value) requires 1 argument"); + + blob *bd = js2blob(ctx, this_val); + if (!bd) + return JS_ThrowTypeError(ctx, "w16: not called on a blob"); + + int32_t value; + if (JS_ToInt32(ctx, &value, argv[0]) < 0) return JS_EXCEPTION; + + int16_t short_val = (int16_t)value; + if (blob_write_bytes(bd, &short_val, sizeof(int16_t)) < 0) + return JS_ThrowTypeError(ctx, "w16: cannot write to stone blob or OOM"); + + return JS_NULL; +} + +// blob.wf(value) - write a float (32-bit) +static JSValue js_blob_wf(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) { + if (argc < 1) + return JS_ThrowTypeError(ctx, "wf(value) requires 1 argument"); + + blob *bd = js2blob(ctx, this_val); + if (!bd) + return JS_ThrowTypeError(ctx, "wf: not called on a blob"); + + double d; + if (JS_ToFloat64(ctx, &d, argv[0]) < 0) return JS_EXCEPTION; + + float f = (float)d; + if (blob_write_bytes(bd, &f, sizeof(float)) < 0) + return JS_ThrowTypeError(ctx, "wf: cannot write to stone blob or OOM"); + + return JS_NULL; +} + // blob.write_fit(value, len) static JSValue js_blob_write_fit(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -471,6 +511,8 @@ static const JSCFunctionListEntry js_blob_funcs[] = { JS_CFUNC_DEF("write_fit", 2, js_blob_write_fit), JS_CFUNC_DEF("write_text", 1, js_blob_write_text), JS_CFUNC_DEF("write_pad", 1, js_blob_write_pad), + JS_CFUNC_DEF("wf", 1, js_blob_wf), + JS_CFUNC_DEF("w16", 1, js_blob_w16), // Read methods JS_CFUNC_DEF("read_logical", 1, js_blob_read_logical), @@ -516,7 +558,7 @@ void *js_get_blob_data(JSContext *js, size_t *size, JSValue v) } if (b->length % 8 != 0) { - JS_ThrowReferenceError(js, "attempted to read data from a non-byte aligned blob"); + JS_ThrowReferenceError(js, "attempted to read data from a non-byte aligned blob [length is %d]", b->length); return -1; } diff --git a/source/quickjs.c b/source/quickjs.c index 837ffd77..584ebd30 100644 --- a/source/quickjs.c +++ b/source/quickjs.c @@ -1441,8 +1441,8 @@ void JS_FreeRuntime(JSRuntime *rt) printf("Secondary object leaks: %d\n", count); } #endif - if (!list_empty(&rt->gc_obj_list)) - printf("Object leaks!\n"); +// if (!list_empty(&rt->gc_obj_list)) +// printf("Object leaks!\n"); // assert(list_empty(&rt->gc_obj_list)); /* free the classes */