| 2359 | } |
| 2360 | |
| 2361 | GJS_JSAPI_RETURN_CONVENTION |
| 2362 | static bool gjs_array_from_basic_c_array_internal( |
| 2363 | JSContext* cx, JS::MutableHandleValue value_out, GITypeTag element_tag, |
| 2364 | size_t length, void* contents) { |
| 2365 | g_assert(GI_TYPE_TAG_IS_BASIC(element_tag)); |
| 2366 | |
| 2367 | // Special case array(uint8) |
| 2368 | if (element_tag == GI_TYPE_TAG_UINT8) { |
| 2369 | JSObject* u8array = gjs_byte_array_from_data_copy(cx, length, contents); |
| 2370 | if (!u8array) |
| 2371 | return false; |
| 2372 | value_out.setObject(*u8array); |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
| 2376 | // Special case array(unichar) to be a string in JS |
| 2377 | if (element_tag == GI_TYPE_TAG_UNICHAR) { |
| 2378 | return gjs_string_from_ucs4(cx, static_cast<gunichar*>(contents), |
| 2379 | length, value_out); |
| 2380 | } |
| 2381 | |
| 2382 | // a null array pointer takes precedence over whatever `length` says |
| 2383 | if (!contents) { |
| 2384 | JSObject* array = JS::NewArrayObject(cx, 0); |
| 2385 | if (!array) |
| 2386 | return false; |
| 2387 | value_out.setObject(*array); |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
| 2391 | JS::RootedValueVector elems{cx}; |
| 2392 | if (!elems.resize(length)) { |
| 2393 | JS_ReportOutOfMemory(cx); |
| 2394 | return false; |
| 2395 | } |
| 2396 | |
| 2397 | GIArgument arg; |
| 2398 | switch (element_tag) { |
| 2399 | // Special cases handled above |
| 2400 | case GI_TYPE_TAG_UINT8: |
| 2401 | case GI_TYPE_TAG_UNICHAR: |
| 2402 | g_assert_not_reached(); |
| 2403 | |
| 2404 | case GI_TYPE_TAG_BOOLEAN: |
| 2405 | if (!fill_vector_from_basic_c_array<Gjs::Tag::GBoolean>( |
| 2406 | cx, &elems, element_tag, &arg, contents, length)) |
| 2407 | return false; |
| 2408 | break; |
| 2409 | case GI_TYPE_TAG_INT8: |
| 2410 | if (!fill_vector_from_basic_c_array<int8_t>(cx, &elems, element_tag, |
| 2411 | &arg, contents, length)) |
| 2412 | return false; |
| 2413 | break; |
| 2414 | case GI_TYPE_TAG_UINT16: |
| 2415 | if (!fill_vector_from_basic_c_array<uint16_t>( |
| 2416 | cx, &elems, element_tag, &arg, contents, length)) |
| 2417 | return false; |
| 2418 | break; |
no test coverage detected