* gjs_string_get_char16_data: * @cx: js context * @str: a rooted JSString * @data_p: address to return allocated data buffer * @len_p: address to return length of data (number of 16-bit characters) * * Get the binary data (as a sequence of 16-bit characters) in @str. * * Returns: false if exception thrown */
| 279 | * Returns: false if exception thrown |
| 280 | */ |
| 281 | bool gjs_string_get_char16_data(JSContext* cx, JS::HandleString str, |
| 282 | char16_t** data_p, size_t* len_p) { |
| 283 | if (JS::StringHasLatin1Chars(str)) |
| 284 | return from_latin1(cx, str, data_p, len_p); |
| 285 | |
| 286 | /* From this point on, crash if a GC is triggered while we are using the |
| 287 | * string's chars */ |
| 288 | JS::AutoCheckCannotGC nogc; |
| 289 | |
| 290 | const char16_t* js_data = |
| 291 | JS_GetTwoByteStringCharsAndLength(cx, nogc, str, len_p); |
| 292 | |
| 293 | if (js_data == nullptr) |
| 294 | return false; |
| 295 | |
| 296 | mozilla::CheckedInt<size_t> len_bytes = |
| 297 | mozilla::CheckedInt<size_t>(*len_p) * sizeof(*js_data); |
| 298 | if (!len_bytes.isValid()) { |
| 299 | JS_ReportOutOfMemory(cx); // cannot call gjs_throw, it may GC |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | *data_p = static_cast<char16_t*>(g_memdup2(js_data, len_bytes.value())); |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * gjs_string_to_ucs4: |