quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

Type checks for typed arrays and array buffers.

Open xeioex opened this issue 1 year ago • 10 comments

xeioex avatar May 08 '24 22:05 xeioex

I just found myself in need of this as well, thanks! 🙌 It'd be great if the function could also take a JSTypedArrayEnum pointer and, if it's not NULL, write the type of the view when val is a typed array.

kasperisager avatar May 09 '24 07:05 kasperisager

I just found myself in need of this as well, thanks! 🙌 It'd be great if the function could also take a JSTypedArrayEnum pointer and, if it's not NULL, write the type of the view when val is a typed array.

I can do this as well, any objections from reviewers @saghul?

xeioex avatar May 09 '24 19:05 xeioex

No objections from me, but I would handle that in a separate PR.

saghul avatar May 09 '24 19:05 saghul

🙌 It'd be great if the function could also take a JSTypedArrayEnum pointer and, if it's not NULL, write the type of the view when val is a typed array.

I would favor an API that retrieves the typed array type, length and data address. A generalized version of JS_GetUint8Array.

As a matter of fact, this would make JS_IsTypedArray redundant, so maybe we should just have this:

struct JSTypedArrayDescription {
    JSTypedArrayEnum type;
    size_t length;
    void *data;
};

// Return value is -1 for proxy errors, 0 if `obj` is not a typed array, 1 if it is a typed array.
// The structure pointed to by `desc` is filled on success unless `desc` is a null pointer.
int JS_GetTypedArray(JSContext *ctx, JSValueConst obj, struct JSTypedArrayDescription *desc);

chqrlie avatar May 09 '24 21:05 chqrlie

OK, I have no objection. Will update.

xeioex avatar May 09 '24 22:05 xeioex

@chqrlie

Return value is -1 for proxy errors

Do we still want to check for proxy case here, given that JS_GetTypedArrayBuffer does not do that?

xeioex avatar May 09 '24 22:05 xeioex

The change is approved, but not merged. Any plans to align this PR with https://github.com/bellard/quickjs/pull/294?

xeioex avatar May 10 '24 18:05 xeioex

The change is approved, but not merged. Any plans to align this PR with #294?

Yes, we are discussing prototypes... Trying to cook an API in a hurry does not work. We have to let it simmer for a few days

chqrlie avatar May 10 '24 18:05 chqrlie

Agree. I have no problem with it. Just let me know of your intentions, it is enough.

xeioex avatar May 10 '24 18:05 xeioex

This would be really useful, in particular because I don't think there's currently any way to get the opaque pointer that's used when the typed array is freed. Any plans to merge this?

bendmorris avatar Nov 19 '25 18:11 bendmorris