MCPcopy Create free account
hub / github.com/GNOME/gjs / gjs_encode_into_uint8array

Function gjs_encode_into_uint8array

gjs/text-encoding.cpp:468–522  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

466}
467
468GJS_JSAPI_RETURN_CONVENTION
469static bool gjs_encode_into_uint8array(JSContext* cx, JS::HandleString str,
470 JS::HandleObject uint8array,
471 JS::MutableHandleValue rval) {
472 if (!JS_IsUint8Array(uint8array)) {
473 gjs_throw_custom(cx, JSEXN_TYPEERR, nullptr,
474 "Argument to encodeInto() must be a Uint8Array");
475 return false;
476 }
477
478 uint32_t len = JS_GetTypedArrayByteLength(uint8array);
479 bool shared = JS_GetTypedArraySharedness(uint8array);
480
481 if (shared) {
482 gjs_throw(cx, "Cannot encode data into shared memory.");
483 return false;
484 }
485
486 mozilla::Maybe<std::tuple<size_t, size_t>> results;
487
488 {
489 JS::AutoCheckCannotGC nogc(cx);
490 uint8_t* data = JS_GetUint8ArrayData(uint8array, &shared, nogc);
491
492 // We already checked for sharedness with JS_GetTypedArraySharedness
493 g_assert(!shared);
494
495 results = JS_EncodeStringToUTF8BufferPartial(
496 cx, str, mozilla::AsWritableChars(mozilla::Span(data, len)));
497 }
498
499 if (!results) {
500 JS_ReportOutOfMemory(cx);
501 return false;
502 }
503
504 size_t read, written;
505 std::tie(read, written) = *results;
506
507 g_assert(written <= len);
508
509 JS::RootedObject result(cx, JS_NewPlainObject(cx));
510 if (!result)
511 return false;
512
513 JS::RootedValue v_read(cx, JS::NumberValue(read)),
514 v_written(cx, JS::NumberValue(written));
515
516 if (!JS_SetProperty(cx, result, "read", v_read) ||
517 !JS_SetProperty(cx, result, "written", v_written))
518 return false;
519
520 rval.setObject(*result);
521 return true;
522}
523
524GJS_JSAPI_RETURN_CONVENTION
525static bool gjs_encode(JSContext* cx, unsigned argc, JS::Value* vp) {

Callers 1

gjs_encode_intoFunction · 0.85

Calls 2

gjs_throw_customFunction · 0.85
gjs_throwFunction · 0.85

Tested by

no test coverage detected