| 33 | #include "gjs/text-encoding.h" |
| 34 | |
| 35 | GJS_JSAPI_RETURN_CONVENTION |
| 36 | static bool to_string_func(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 37 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
| 38 | JS::UniqueChars encoding; |
| 39 | JS::RootedObject byte_array(cx); |
| 40 | |
| 41 | if (!gjs_parse_call_args(cx, "toString", args, "o|s", "byteArray", |
| 42 | &byte_array, "encoding", &encoding)) |
| 43 | return false; |
| 44 | |
| 45 | const char* actual_encoding = encoding ? encoding.get() : "utf-8"; |
| 46 | JS::RootedString str{cx, gjs_decode_from_uint8array( |
| 47 | cx, byte_array, actual_encoding, |
| 48 | GjsStringTermination::ZERO_TERMINATED, true)}; |
| 49 | if (!str) |
| 50 | return false; |
| 51 | |
| 52 | args.rval().setString(str); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | /* Workaround to keep existing code compatible. This function is tacked onto any |
| 57 | * Uint8Array instances created in situations where previously a ByteArray would |
nothing calls this directly
no test coverage detected