Skip to content

Commit

Permalink
utils: add helper to create a Uint8Array while copying the data
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul committed Sep 25, 2023
1 parent 3b8b2ef commit 90b8087
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

/*
* QuickJS libuv bindings
* txiki.js
*
* Copyright (c) 2019-present Saúl Ibarra Corretgé <[email protected]>
*
Expand Down Expand Up @@ -271,6 +270,17 @@ JSValue TJS_NewUint8Array(JSContext *ctx, uint8_t *data, size_t size) {
return buf;
}

JSValue TJS_NewUint8ArrayCopy(JSContext *ctx, uint8_t *data, size_t size) {
JSValue abuf = JS_NewArrayBufferCopy(ctx, data, size);
if (JS_IsException(abuf))
return abuf;
TJSRuntime *qrt = TJS_GetRuntime(ctx);
CHECK_NOT_NULL(qrt);
JSValue buf = JS_CallConstructor(ctx, qrt->builtins.u8array_ctor, 1, &abuf);
JS_FreeValue(ctx, abuf);
return buf;
}

JSValue TJS_NewDate(JSContext *ctx, double epoch_ms) {
TJSRuntime *qrt = TJS_GetRuntime(ctx);
CHECK_NOT_NULL(qrt);
Expand Down
3 changes: 2 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* QuickJS libuv bindings
* txiki.js
*
* Copyright (c) 2019-present Saúl Ibarra Corretgé <[email protected]>
*
Expand Down Expand Up @@ -122,6 +122,7 @@ JSValue TJS_NewResolvedPromise(JSContext *ctx, int argc, JSValueConst *argv);
JSValue TJS_NewRejectedPromise(JSContext *ctx, int argc, JSValueConst *argv);

JSValue TJS_NewUint8Array(JSContext *ctx, uint8_t *data, size_t size);
JSValue TJS_NewUint8ArrayCopy(JSContext *ctx, uint8_t *data, size_t size);
JSValue TJS_NewDate(JSContext *ctx, double epoch_ms);

extern const char *tjs_signal_map[];
Expand Down

0 comments on commit 90b8087

Please sign in to comment.