| 245 | * type, by what the compiler believes is the most efficient method possible */ |
| 246 | template <typename T> |
| 247 | GJS_JSAPI_RETURN_CONVENTION |
| 248 | static bool from_latin1(JSContext* cx, JSString* str, T** data_p, |
| 249 | size_t* len_p) { |
| 250 | /* No garbage collection should be triggered while we are using the string's |
| 251 | * chars. Crash if that happens. */ |
| 252 | JS::AutoCheckCannotGC nogc; |
| 253 | |
| 254 | const JS::Latin1Char* js_data = |
| 255 | JS_GetLatin1StringCharsAndLength(cx, nogc, str, len_p); |
| 256 | if (js_data == nullptr) |
| 257 | return false; |
| 258 | |
| 259 | /* Unicode codepoints 0x00-0xFF are the same as Latin-1 codepoints, so we |
| 260 | * can preserve the string length and simply copy the codepoints to an array |
| 261 | * of different-sized ints */ |
| 262 | |
| 263 | *data_p = g_new(T, *len_p); |
| 264 | |
| 265 | // This will probably use a loop, unfortunately |
| 266 | std::copy(js_data, js_data + *len_p, *data_p); |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * gjs_string_get_char16_data: |
no test coverage detected