Skip to content

Commit d33227e

Browse files
committed
fixup
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
1 parent 1fee1dd commit d33227e

File tree

3 files changed

+44
-93
lines changed

3 files changed

+44
-93
lines changed

src/ffi/data.cc

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,19 @@ bool GetValidatedSize(Environment* env,
3737
const char* label,
3838
size_t* out) {
3939
if (!value->IsNumber()) {
40-
THROW_ERR_INVALID_ARG_VALUE(
41-
env, (std::string("The ") + label + " must be a number").c_str());
40+
THROW_ERR_INVALID_ARG_VALUE(env, "The %s must be a number", label);
4241
return false;
4342
}
4443

4544
double length = value.As<Number>()->Value();
4645
if (!std::isfinite(length) || length < 0 || std::floor(length) != length) {
4746
THROW_ERR_INVALID_ARG_VALUE(
48-
env,
49-
(std::string("The ") + label + " must be a non-negative integer")
50-
.c_str());
47+
env, "The %s must be a non-negative integer", label);
5148
return false;
5249
}
5350

5451
if (length > static_cast<double>(std::numeric_limits<size_t>::max())) {
55-
THROW_ERR_OUT_OF_RANGE(
56-
env, (std::string("The ") + label + " is too large").c_str());
52+
THROW_ERR_OUT_OF_RANGE(env, "The %s is too large", label);
5753
return false;
5854
}
5955

@@ -66,26 +62,21 @@ bool GetValidatedPointerAddress(Environment* env,
6662
const char* label,
6763
uintptr_t* out) {
6864
if (!value->IsBigInt()) {
69-
THROW_ERR_INVALID_ARG_VALUE(
70-
env, (std::string("The ") + label + " must be a bigint").c_str());
65+
THROW_ERR_INVALID_ARG_VALUE(env, "The %s must be a bigint", label);
7166
return false;
7267
}
7368

7469
bool lossless;
7570
uint64_t address = value.As<BigInt>()->Uint64Value(&lossless);
7671
if (!lossless) {
7772
THROW_ERR_INVALID_ARG_VALUE(
78-
env,
79-
(std::string("The ") + label + " must be a non-negative bigint")
80-
.c_str());
73+
env, "The %s must be a non-negative bigint", label);
8174
return false;
8275
}
8376

8477
if (address > static_cast<uint64_t>(std::numeric_limits<uintptr_t>::max())) {
8578
THROW_ERR_INVALID_ARG_VALUE(
86-
env,
87-
(std::string("The ") + label + " exceeds the platform pointer range")
88-
.c_str());
79+
env, "The %s exceeds the platform pointer range", label);
8980
return false;
9081
}
9182

@@ -687,10 +678,7 @@ void ExportBytes(const FunctionCallbackInfo<Value>& args) {
687678
uint8_t* source_data = nullptr;
688679
size_t source_len = 0;
689680

690-
if (Buffer::HasInstance(args[0])) {
691-
source_data = reinterpret_cast<uint8_t*>(Buffer::Data(args[0]));
692-
source_len = Buffer::Length(args[0]);
693-
} else if (args[0]->IsArrayBuffer()) {
681+
if (args[0]->IsArrayBuffer()) {
694682
Local<ArrayBuffer> array_buffer = args[0].As<ArrayBuffer>();
695683
std::shared_ptr<BackingStore> store = array_buffer->GetBackingStore();
696684
if (!store) {
@@ -700,14 +688,13 @@ void ExportBytes(const FunctionCallbackInfo<Value>& args) {
700688
source_data = static_cast<uint8_t*>(store->Data());
701689
source_len = array_buffer->ByteLength();
702690
} else if (args[0]->IsArrayBufferView()) {
703-
Local<ArrayBufferView> view = args[0].As<ArrayBufferView>();
704-
std::shared_ptr<BackingStore> store = view->Buffer()->GetBackingStore();
705-
if (!store) {
691+
ArrayBufferViewContents<uint8_t> view(args[0]);
692+
if (view.WasDetached()) {
706693
THROW_ERR_INVALID_ARG_VALUE(env, "Invalid ArrayBufferView backing store");
707694
return;
708695
}
709-
source_data = static_cast<uint8_t*>(store->Data()) + view->ByteOffset();
710-
source_len = view->ByteLength();
696+
source_data = const_cast<uint8_t*>(view.data());
697+
source_len = view.length();
711698
} else {
712699
THROW_ERR_INVALID_ARG_TYPE(
713700
env,
@@ -766,9 +753,7 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
766753

767754
uintptr_t ptr = 0;
768755

769-
if (Buffer::HasInstance(args[0])) {
770-
ptr = reinterpret_cast<uintptr_t>(Buffer::Data(args[0]));
771-
} else if (args[0]->IsArrayBuffer()) {
756+
if (args[0]->IsArrayBuffer()) {
772757
Local<ArrayBuffer> array_buffer = args[0].As<ArrayBuffer>();
773758
std::shared_ptr<BackingStore> store = array_buffer->GetBackingStore();
774759
if (!store) {
@@ -777,14 +762,12 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
777762
}
778763
ptr = reinterpret_cast<uintptr_t>(store->Data());
779764
} else if (args[0]->IsArrayBufferView()) {
780-
Local<ArrayBufferView> view = args[0].As<ArrayBufferView>();
781-
std::shared_ptr<BackingStore> store = view->Buffer()->GetBackingStore();
782-
if (!store) {
765+
ArrayBufferViewContents<uint8_t> view(args[0]);
766+
if (view.WasDetached()) {
783767
THROW_ERR_INVALID_ARG_VALUE(env, "Invalid ArrayBufferView backing store");
784768
return;
785769
}
786-
ptr = reinterpret_cast<uintptr_t>(static_cast<uint8_t*>(store->Data()) +
787-
view->ByteOffset());
770+
ptr = reinterpret_cast<uintptr_t>(view.data());
788771
} else {
789772
THROW_ERR_INVALID_ARG_TYPE(
790773
env,

src/ffi/types.cc

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool ThrowIfContainsNullBytes(Environment* env,
3939
if (value.length() != 0 &&
4040
std::memchr(*value, '\0', value.length()) != nullptr) {
4141
THROW_ERR_INVALID_ARG_VALUE(
42-
env, (label + " must not contain null bytes").c_str());
42+
env, "%s must not contain null bytes", label.c_str());
4343
return true;
4444
}
4545

@@ -122,15 +122,15 @@ bool ParseFunctionSignature(Environment* env,
122122
std::string msg = "Function signature of " + name +
123123
" must have either 'returns', 'return' or 'result' "
124124
"property";
125-
THROW_ERR_INVALID_ARG_VALUE(env, msg.c_str());
125+
THROW_ERR_INVALID_ARG_VALUE(env, msg);
126126
return false;
127127
}
128128

129129
if (has_arguments && has_parameters) {
130130
std::string msg = "Function signature of " + name +
131131
" must have either 'parameters' or 'arguments' "
132132
"property";
133-
THROW_ERR_INVALID_ARG_VALUE(env, msg.c_str());
133+
THROW_ERR_INVALID_ARG_VALUE(env, msg);
134134
return false;
135135
}
136136

@@ -156,7 +156,7 @@ bool ParseFunctionSignature(Environment* env,
156156
if (!return_type_val->IsString()) {
157157
std::string msg =
158158
"Return value type of function " + name + " must be a string";
159-
THROW_ERR_INVALID_ARG_VALUE(env, msg.c_str());
159+
THROW_ERR_INVALID_ARG_VALUE(env, msg);
160160
return false;
161161
}
162162

@@ -180,7 +180,7 @@ bool ParseFunctionSignature(Environment* env,
180180
if (!arguments_val->IsArray()) {
181181
std::string msg =
182182
"Arguments list of function " + name + " must be an array";
183-
THROW_ERR_INVALID_ARG_VALUE(env, msg.c_str());
183+
THROW_ERR_INVALID_ARG_VALUE(env, msg);
184184
return false;
185185
}
186186

@@ -273,7 +273,7 @@ bool ToFFIType(Environment* env, const std::string& type_str, ffi_type** ret) {
273273
*ret = &ffi_type_pointer;
274274
} else {
275275
std::string msg = std::string("Unsupported FFI type: ") + type_str;
276-
THROW_ERR_INVALID_ARG_VALUE(env, msg.c_str());
276+
THROW_ERR_INVALID_ARG_VALUE(env, msg);
277277
return false;
278278
}
279279

@@ -293,9 +293,7 @@ uint8_t ToFFIArgument(Environment* env,
293293
int64_t value;
294294
if (!GetValidatedSignedInt(env, arg, INT8_MIN, INT8_MAX, "int8", &value)) {
295295
if (env->isolate()->IsExecutionTerminating()) return 0;
296-
THROW_ERR_INVALID_ARG_VALUE(
297-
env,
298-
("Argument " + std::to_string(index) + " must be an int8").c_str());
296+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int8", index);
299297
return 0;
300298
}
301299

@@ -304,9 +302,7 @@ uint8_t ToFFIArgument(Environment* env,
304302
uint64_t value;
305303
if (!GetValidatedUnsignedInt(env, arg, UINT8_MAX, "uint8", &value)) {
306304
if (env->isolate()->IsExecutionTerminating()) return 0;
307-
THROW_ERR_INVALID_ARG_VALUE(
308-
env,
309-
("Argument " + std::to_string(index) + " must be a uint8").c_str());
305+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint8", index);
310306
return 0;
311307
}
312308

@@ -316,9 +312,7 @@ uint8_t ToFFIArgument(Environment* env,
316312
if (!GetValidatedSignedInt(
317313
env, arg, INT16_MIN, INT16_MAX, "int16", &value)) {
318314
if (env->isolate()->IsExecutionTerminating()) return 0;
319-
THROW_ERR_INVALID_ARG_VALUE(
320-
env,
321-
("Argument " + std::to_string(index) + " must be an int16").c_str());
315+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int16", index);
322316
return 0;
323317
}
324318

@@ -327,78 +321,60 @@ uint8_t ToFFIArgument(Environment* env,
327321
uint64_t value;
328322
if (!GetValidatedUnsignedInt(env, arg, UINT16_MAX, "uint16", &value)) {
329323
if (env->isolate()->IsExecutionTerminating()) return 0;
330-
THROW_ERR_INVALID_ARG_VALUE(
331-
env,
332-
("Argument " + std::to_string(index) + " must be a uint16").c_str());
324+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint16", index);
333325
return 0;
334326
}
335327

336328
*static_cast<uint16_t*>(ret) = static_cast<uint16_t>(value);
337329
} else if (type == &ffi_type_sint32) {
338330
if (!arg->IsInt32()) {
339-
THROW_ERR_INVALID_ARG_VALUE(
340-
env,
341-
("Argument " + std::to_string(index) + " must be an int32").c_str());
331+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int32", index);
342332
return 0;
343333
}
344334

345335
*static_cast<int32_t*>(ret) = arg->Int32Value(context).FromJust();
346336
} else if (type == &ffi_type_uint32) {
347337
if (!arg->IsUint32()) {
348-
THROW_ERR_INVALID_ARG_VALUE(
349-
env,
350-
("Argument " + std::to_string(index) + " must be a uint32").c_str());
338+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint32", index);
351339
return 0;
352340
}
353341

354342
*static_cast<uint32_t*>(ret) = arg->Uint32Value(context).FromJust();
355343
} else if (type == &ffi_type_sint64) {
356344
if (!arg->IsBigInt()) {
357-
THROW_ERR_INVALID_ARG_VALUE(
358-
env,
359-
("Argument " + std::to_string(index) + " must be an int64").c_str());
345+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int64", index);
360346
return 0;
361347
}
362348

363349
bool lossless;
364350
*static_cast<int64_t*>(ret) = arg.As<BigInt>()->Int64Value(&lossless);
365351
if (!lossless) {
366-
THROW_ERR_INVALID_ARG_VALUE(
367-
env,
368-
("Argument " + std::to_string(index) + " must be an int64").c_str());
352+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int64", index);
369353
return 0;
370354
}
371355
} else if (type == &ffi_type_uint64) {
372356
if (!arg->IsBigInt()) {
373-
THROW_ERR_INVALID_ARG_VALUE(
374-
env,
375-
("Argument " + std::to_string(index) + " must be a uint64").c_str());
357+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint64", index);
376358
return 0;
377359
}
378360

379361
bool lossless;
380362
*static_cast<uint64_t*>(ret) = arg.As<BigInt>()->Uint64Value(&lossless);
381363
if (!lossless) {
382-
THROW_ERR_INVALID_ARG_VALUE(
383-
env,
384-
("Argument " + std::to_string(index) + " must be a uint64").c_str());
364+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint64", index);
385365
return 0;
386366
}
387367
} else if (type == &ffi_type_float) {
388368
if (!arg->IsNumber()) {
389-
THROW_ERR_INVALID_ARG_VALUE(
390-
env,
391-
("Argument " + std::to_string(index) + " must be a float").c_str());
369+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a float", index);
392370
return 0;
393371
}
394372

395373
*static_cast<float*>(ret) =
396374
static_cast<float>(arg->NumberValue(context).FromJust());
397375
} else if (type == &ffi_type_double) {
398376
if (!arg->IsNumber()) {
399-
THROW_ERR_INVALID_ARG_VALUE(
400-
env,
401-
("Argument " + std::to_string(index) + " must be a double").c_str());
377+
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a double", index);
402378
return 0;
403379
}
404380

@@ -421,9 +397,8 @@ uint8_t ToFFIArgument(Environment* env,
421397
if (!store) {
422398
THROW_ERR_INVALID_ARG_VALUE(
423399
env,
424-
("Invalid ArrayBufferView backing store for argument " +
425-
std::to_string(index))
426-
.c_str());
400+
"Invalid ArrayBufferView backing store for argument %u",
401+
index);
427402
return 0;
428403
}
429404

@@ -442,10 +417,7 @@ uint8_t ToFFIArgument(Environment* env,
442417

443418
if (!store) {
444419
THROW_ERR_INVALID_ARG_VALUE(
445-
env,
446-
("Invalid ArrayBuffer backing store for argument " +
447-
std::to_string(index))
448-
.c_str());
420+
env, "Invalid ArrayBuffer backing store for argument %u", index);
449421
return 0;
450422
}
451423

@@ -455,26 +427,22 @@ uint8_t ToFFIArgument(Environment* env,
455427
uint64_t pointer = arg.As<BigInt>()->Uint64Value(&lossless);
456428
if (!lossless || pointer > static_cast<uint64_t>(
457429
std::numeric_limits<uintptr_t>::max())) {
458-
THROW_ERR_INVALID_ARG_VALUE(env,
459-
("Argument " + std::to_string(index) +
460-
" must be a non-negative pointer bigint")
461-
.c_str());
430+
THROW_ERR_INVALID_ARG_VALUE(
431+
env, "Argument %u must be a non-negative pointer bigint", index);
462432
return 0;
463433
}
464434

465435
*static_cast<uint64_t*>(ret) = pointer;
466436
} else {
467437
THROW_ERR_INVALID_ARG_VALUE(
468438
env,
469-
("Argument " + std::to_string(index) +
470-
" must be a buffer, an ArrayBuffer, a string, or a bigint")
471-
.c_str());
439+
"Argument %u must be a buffer, an ArrayBuffer, a string, or a bigint",
440+
index);
472441
return 0;
473442
}
474443
} else {
475444
THROW_ERR_INVALID_ARG_VALUE(
476-
env,
477-
("Unsupported FFI type for argument " + std::to_string(index)).c_str());
445+
env, "Unsupported FFI type for argument %u", index);
478446
return 0;
479447
}
480448

src/node_ffi.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ void DynamicLibrary::New(const FunctionCallbackInfo<Value>& args) {
250250
}
251251
#endif
252252

253-
char* library_path = nullptr;
253+
const char* library_path = nullptr;
254254
DynamicLibrary* lib = new DynamicLibrary(env, args.This());
255255

256256
if (args[0]->IsString()) {
257257
Utf8Value filename(env->isolate(), args[0]);
258258
if (ThrowIfContainsNullBytes(env, filename, "Library path")) {
259259
return;
260260
}
261-
library_path = *filename;
262-
lib->path_ = std::string(*filename);
261+
lib->path_ = filename.ToString();
262+
library_path = lib->path_.c_str();
263263
}
264264

265265
// Open the library

0 commit comments

Comments
 (0)