Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/ffi/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BigInt;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
using v8::NewStringType;
using v8::Nothing;
using v8::Number;
Expand Down Expand Up @@ -312,7 +310,6 @@ void SetValue(const FunctionCallbackInfo<Value>& args) {
}

T converted;
Local<Context> context = env->context();

if constexpr (std::is_same_v<T, int8_t>) {
int64_t validated;
Expand Down Expand Up @@ -400,15 +397,16 @@ void SetValue(const FunctionCallbackInfo<Value>& args) {
return;
}
} else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>) {
MaybeLocal<Number> number = value->ToNumber(context);
Local<Number> 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<T, float>) {
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<T>(number_local->Value());
converted = static_cast<T>(value.As<Number>()->Value());
} else {
UNREACHABLE();
}
Expand Down
6 changes: 6 additions & 0 deletions test/ffi/test-ffi-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
Loading