diff --git a/src/ffi/data.cc b/src/ffi/data.cc index 6a8d54ca0d39..308bde60cae2 100644 --- a/src/ffi/data.cc +++ b/src/ffi/data.cc @@ -16,7 +16,6 @@ using v8::ArrayBuffer; using v8::ArrayBufferView; using v8::BackingStore; using v8::BigInt; -using v8::Context; using v8::FunctionCallbackInfo; using v8::Integer; using v8::Isolate; @@ -24,7 +23,6 @@ using v8::Just; using v8::JustVoid; using v8::Local; using v8::Maybe; -using v8::MaybeLocal; using v8::NewStringType; using v8::Nothing; using v8::Number; @@ -312,7 +310,6 @@ void SetValue(const FunctionCallbackInfo& args) { } T converted; - Local context = env->context(); if constexpr (std::is_same_v) { int64_t validated; @@ -400,15 +397,16 @@ void SetValue(const FunctionCallbackInfo& args) { return; } } else if constexpr (std::is_same_v || std::is_same_v) { - MaybeLocal number = value->ToNumber(context); - Local number_local; - - if (!number.ToLocal(&number_local)) { - THROW_ERR_INVALID_ARG_VALUE(env, "Value must be a number"); + if (!value->IsNumber()) { + if constexpr (std::is_same_v) { + THROW_ERR_INVALID_ARG_VALUE(env, "Value must be a float"); + } else { + THROW_ERR_INVALID_ARG_VALUE(env, "Value must be a double"); + } return; } - converted = static_cast(number_local->Value()); + converted = static_cast(value.As()->Value()); } else { UNREACHABLE(); } diff --git a/test/ffi/test-ffi-memory.js b/test/ffi/test-ffi-memory.js index e5bf4ba07e13..72d37efacd5a 100644 --- a/test/ffi/test-ffi-memory.js +++ b/test/ffi/test-ffi-memory.js @@ -273,6 +273,12 @@ test('ffi validates memory access arguments', () => { assert.throws(() => ffi.setUint64(ptr, 0, -1n), /Value must be a uint64/); assert.throws(() => ffi.setUint64(ptr, 0, 2n ** 64n), /Value must be a uint64/); assert.throws(() => ffi.setUint64(ptr, 0, Number.MAX_SAFE_INTEGER + 1), /Value must be a uint64/); + assert.throws(() => ffi.setFloat32(ptr, 0, '1.5'), /Value must be a float/); + assert.throws(() => ffi.setFloat32(ptr, 0, null), /Value must be a float/); + assert.throws(() => ffi.setFloat64(ptr, 0, '1.5'), /Value must be a double/); + assert.throws(() => ffi.setFloat64(ptr, 0, true), /Value must be a double/); + assert.throws(() => ffi.setFloat64(ptr, 0, {}), /Value must be a double/); + assert.throws(() => ffi.setFloat64(ptr, 0, { valueOf: common.mustNotCall() }), /Value must be a double/); assert.throws(() => ffi.exportString(1, ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' }); assert.throws(() => ffi.exportString('ok', ptr, -1), { code: 'ERR_OUT_OF_RANGE' }); assert.throws(() => ffi.exportString('ok', ptr, 4, 1), { code: 'ERR_INVALID_ARG_TYPE' });