|
| 1 | +#pragma once |
| 2 | +#include "root.h" |
| 3 | +#include "IDLTypes.h" |
| 4 | +#include "JSDOMConvertBase.h" |
| 5 | +#include "ErrorCode.h" |
| 6 | + |
| 7 | +namespace Bun { |
| 8 | + |
| 9 | +enum class BindgenCustomEnforceRangeKind { |
| 10 | + Node, |
| 11 | + Web, |
| 12 | +}; |
| 13 | + |
| 14 | +// This type implements conversion for: |
| 15 | +// - t.*.validateInteger() |
| 16 | +// - t.*.enforceRange(a, b) when A, B is not the integer's ABI size. |
| 17 | +// - t.i32.validateInt32() |
| 18 | +// - t.u32.validateUInt32() |
| 19 | +template< |
| 20 | + typename NumericType, |
| 21 | + NumericType Min, |
| 22 | + NumericType Max, |
| 23 | + BindgenCustomEnforceRangeKind Kind> |
| 24 | +struct BindgenCustomEnforceRange : WebCore::IDLType<NumericType> { |
| 25 | +}; |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +static String rangeErrorString(double value, double min, double max) |
| 30 | +{ |
| 31 | + return makeString("Value "_s, value, " is outside the range ["_s, min, ", "_s, max, ']'); |
| 32 | +} |
| 33 | + |
| 34 | +namespace WebCore { |
| 35 | + |
| 36 | +template< |
| 37 | + typename NumericType, |
| 38 | + NumericType Min, |
| 39 | + NumericType Max, |
| 40 | + Bun::BindgenCustomEnforceRangeKind Kind> |
| 41 | +struct Converter<Bun::BindgenCustomEnforceRange<NumericType, Min, Max, Kind>> |
| 42 | + : DefaultConverter<Bun::BindgenCustomEnforceRange<NumericType, Min, Max, Kind>> { |
| 43 | + template<typename ExceptionThrower = DefaultExceptionThrower> |
| 44 | + static inline NumericType convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value, ExceptionThrower&& exceptionThrower = ExceptionThrower()) |
| 45 | + { |
| 46 | + auto scope = DECLARE_THROW_SCOPE(lexicalGlobalObject.vm()); |
| 47 | + ASSERT(!scope.exception()); |
| 48 | + double unrestricted; |
| 49 | + if constexpr (Kind == Bun::BindgenCustomEnforceRangeKind::Node) { |
| 50 | + // In Node.js, `validateNumber`, `validateInt32`, `validateUint32`, |
| 51 | + // and `validateInteger` all start with the following |
| 52 | + // |
| 53 | + // if (typeof value !== 'number') |
| 54 | + // throw new ERR_INVALID_ARG_TYPE(name, 'number', value); |
| 55 | + // |
| 56 | + if (!value.isNumber()) { |
| 57 | + Bun::ERR::INVALID_ARG_TYPE(scope, &lexicalGlobalObject, exceptionThrower(), "number"_s, value); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + unrestricted = value.asNumber(); |
| 61 | + ASSERT(!scope.exception()); |
| 62 | + |
| 63 | + // Node also validates that integer types are integers |
| 64 | + if constexpr (std::is_integral_v<NumericType>) { |
| 65 | + if (unrestricted != std::round(unrestricted)) { |
| 66 | + // ERR_OUT_OF_RANGE "an integer" |
| 67 | + Bun::ERR::OUT_OF_RANGE(scope, &lexicalGlobalObject, exceptionThrower(), "an integer"_s, value); |
| 68 | + return 0; |
| 69 | + } |
| 70 | + } else { |
| 71 | + // When a range is specified (what this template is implementing), |
| 72 | + // Node also throws on NaN being out of range |
| 73 | + if (std::isnan(unrestricted)) { |
| 74 | + // ERR_OUT_OF_RANGE `>= ${min} && <= ${max}` |
| 75 | + Bun::ERR::OUT_OF_RANGE(scope, &lexicalGlobalObject, exceptionThrower(), Min, Max, value); |
| 76 | + return 0; |
| 77 | + } |
| 78 | + } |
| 79 | + } else { |
| 80 | + // WebIDL uses toNumber before applying range restrictions. This |
| 81 | + // allows something like `true` to pass for `t.f64.enforceRange(-10, 10)`, |
| 82 | + // but this behavior does not appear Node's validators. |
| 83 | + unrestricted = value.toNumber(&lexicalGlobalObject); |
| 84 | + RETURN_IF_EXCEPTION(scope, 0); |
| 85 | + |
| 86 | + if constexpr (std::is_integral_v<NumericType>) { |
| 87 | + if (std::isnan(unrestricted) || std::isinf(unrestricted)) { |
| 88 | + throwTypeError(&lexicalGlobalObject, scope, rangeErrorString(unrestricted, Min, Max)); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + // IDL uses trunc to convert the double to an integer. |
| 93 | + unrestricted = trunc(unrestricted); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + bool inRange = unrestricted >= Min && unrestricted <= Max; |
| 98 | + if (!inRange) { |
| 99 | + if constexpr (Kind == Bun::BindgenCustomEnforceRangeKind::Node) { |
| 100 | + Bun::ERR::OUT_OF_RANGE(scope, &lexicalGlobalObject, exceptionThrower(), Min, Max, value); |
| 101 | + } else { |
| 102 | + // WebKit range exception |
| 103 | + throwTypeError(&lexicalGlobalObject, scope, rangeErrorString(unrestricted, Min, Max)); |
| 104 | + } |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + return static_cast<NumericType>(unrestricted); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +} |
0 commit comments