@@ -448,12 +448,9 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
448 | 448 | return handle_scope.Escape(ret); |
449 | 449 | } |
450 | 450 |
|
451 | | -template <typename T, typename > |
452 | | -v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
453 | | - const T& number, |
454 | | - v8::Isolate* isolate) { |
455 | | - if (isolate == nullptr) isolate = context->GetIsolate(); |
456 | | - |
| 451 | +template <typename T> |
| 452 | +v8::Local<v8::Value> ConvertNumberToV8Value(v8::Isolate* isolate, |
| 453 | + const T& number) { |
457 | 454 | using Limits = std::numeric_limits<T>; |
458 | 455 | // Choose Uint32, Int32, or Double depending on range checks. |
459 | 456 | // These checks should all collapse at compile time. |
@@ -474,6 +471,43 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
474 | 471 | return v8::Number::New(isolate, static_cast<double>(number)); |
475 | 472 | } |
476 | 473 |
|
| 474 | +template <typename T, typename> |
| 475 | +v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
| 476 | + const T& number, |
| 477 | + v8::Isolate* isolate) { |
| 478 | + if (isolate == nullptr) isolate = context->GetIsolate(); |
| 479 | + return ConvertNumberToV8Value(isolate, number); |
| 480 | +} |
| 481 | + |
| 482 | +template <typename T> |
| 483 | +v8::Local<v8::Array> ToV8ValuePrimitiveArray(v8::Local<v8::Context> context, |
| 484 | + const std::vector<T>& vec, |
| 485 | + v8::Isolate* isolate) { |
| 486 | + static_assert( |
| 487 | + std::is_same_v<T, bool> || std::is_integral_v<T> || |
| 488 | + std::is_floating_point_v<T>, |
| 489 | + "Only primitive types (bool, integral, floating-point) are supported."); |
| 490 | + |
| 491 | + if (isolate == nullptr) isolate = context->GetIsolate(); |
| 492 | + v8::EscapableHandleScope handle_scope(isolate); |
| 493 | + |
| 494 | + v8::LocalVector<v8::Value> elements(isolate); |
| 495 | + elements.reserve(vec.size()); |
| 496 | + |
| 497 | + for (const auto& value : vec) { |
| 498 | + if constexpr (std::is_same_v<T, bool>) { |
| 499 | + elements.emplace_back(v8::Boolean::New(isolate, value)); |
| 500 | + } else { |
| 501 | + v8::Local<v8::Value> v = ConvertNumberToV8Value(isolate, value); |
| 502 | + elements.emplace_back(v); |
| 503 | + } |
| 504 | + } |
| 505 | + |
| 506 | + v8::Local<v8::Array> arr = |
| 507 | + v8::Array::New(isolate, elements.data(), elements.size()); |
| 508 | + return handle_scope.Escape(arr); |
| 509 | +} |
| 510 | + |
477 | 511 | SlicedArguments::SlicedArguments( |
478 | 512 | const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) { |
479 | 513 | const size_t length = static_cast<size_t>(args.Length()); |
|