return class_id;
}
-JSClassID JS_GetClassID(JSValue v)
+JSClassID JS_GetClassID(JSValueConst v)
{
JSObject *p;
if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
TRUE);
}
+BOOL JS_IsArrayBuffer(JSValueConst obj)
+{
+ return JS_GetClassID(obj) == JS_CLASS_ARRAY_BUFFER;
+}
+
static JSValue js_array_buffer_constructor0(JSContext *ctx, JSValueConst new_target,
int argc, JSValueConst *argv,
JSClassID class_id)
js_array_buffer_update_typed_arrays(abuf);
}
+int JS_IsImmutableArrayBuffer(JSValueConst obj)
+{
+ JSArrayBuffer *abuf = JS_GetOpaque(obj, JS_CLASS_ARRAY_BUFFER);
+ if (!abuf)
+ return -1;
+ return abuf->immutable;
+}
+
+int JS_SetImmutableArrayBuffer(JSValueConst obj, BOOL immutable)
+{
+ JSArrayBuffer *abuf = JS_GetOpaque(obj, JS_CLASS_ARRAY_BUFFER);
+ if (!abuf)
+ return -1;
+ abuf->immutable = (immutable != 0);
+ return 0;
+}
+
/* get an ArrayBuffer or SharedArrayBuffer */
static JSArrayBuffer *js_get_array_buffer(JSContext *ctx, JSValueConst obj)
{
return written;
}
-static JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
+static JSValue js_new_uint8array(JSContext *ctx, JSValue buffer)
{
- JSValue buffer, obj;
+ JSValue obj;
JSArrayBuffer *abuf;
- buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL,
- JS_CLASS_ARRAY_BUFFER,
- (uint8_t *)buf,
- js_array_buffer_free, NULL,
- TRUE);
- if (JS_IsException(buffer))
- return JS_EXCEPTION;
obj = JS_CreateFromConstructor(ctx, JS_UNDEFINED, JS_CLASS_UINT8_ARRAY);
if (JS_IsException(obj)) {
JS_FreeValue(ctx, buffer);
return obj;
}
+JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
+ JSFreeArrayBufferDataFunc *free_func,
+ void *opaque, BOOL is_shared)
+{
+ JSClassID class_id =
+ is_shared ? JS_CLASS_SHARED_ARRAY_BUFFER : JS_CLASS_ARRAY_BUFFER;
+ JSValue buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL,
+ class_id, buf, free_func,
+ opaque, FALSE);
+ if (JS_IsException(buffer))
+ return JS_EXCEPTION;
+ return js_new_uint8array(ctx, buffer);
+}
+
+JSValue JS_NewUint8ArrayCopy2(JSContext *ctx, const uint8_t *buf, size_t len, BOOL is_immutable)
+{
+ JSValue buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL,
+ JS_CLASS_ARRAY_BUFFER,
+ (uint8_t *)buf,
+ js_array_buffer_free, NULL,
+ TRUE);
+ if (JS_IsException(buffer))
+ return JS_EXCEPTION;
+ if (is_immutable) {
+ JS_SetImmutableArrayBuffer(buffer, TRUE);
+ }
+ return js_new_uint8array(ctx, buffer);
+}
+
+JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
+{
+ return JS_NewUint8ArrayCopy2(ctx, buf, len, FALSE);
+}
+
+int JS_GetTypedArrayType(JSValueConst obj)
+{
+ JSClassID class_id = JS_GetClassID(obj);
+ if (class_id >= JS_CLASS_UINT8C_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY)
+ return class_id - JS_CLASS_UINT8C_ARRAY;
+ else
+ return -1;
+}
+
/* Validate that this_val is a Uint8Array (type check only, no detach check).
Returns the JSObject pointer or NULL on error (throws). */
static JSObject *check_uint8array(JSContext *ctx, JSValueConst this_val, BOOL is_write)
#define JS_INVALID_CLASS_ID 0
JSClassID JS_NewClassID(JSClassID *pclass_id);
/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
-JSClassID JS_GetClassID(JSValue v);
+JSClassID JS_GetClassID(JSValueConst v);
int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
+JS_BOOL JS_IsArrayBuffer(JSValueConst obj);
+// returns true or false if obj is an ArrayBuffer, -1 otherwise
+int JS_IsImmutableArrayBuffer(JSValueConst obj);
+// returns 0 if obj is an ArrayBuffer, -1 otherwise
+int JS_SetImmutableArrayBuffer(JSValueConst obj, JS_BOOL immutable);
typedef enum JSTypedArrayEnum {
JS_TYPED_ARRAY_UINT8C = 0,
size_t *pbyte_offset,
size_t *pbyte_length,
size_t *pbytes_per_element);
+/* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */
+int JS_GetTypedArrayType(JSValueConst obj);
+JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
+ JSFreeArrayBufferDataFunc *free_func,
+ void *opaque, JS_BOOL is_shared);
+JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
+JSValue JS_NewUint8ArrayCopy2(JSContext *ctx, const uint8_t *buf, size_t len, JS_BOOL is_immutable);
typedef struct {
void *(*sab_alloc)(void *opaque, size_t size);
void (*sab_free)(void *opaque, void *ptr);