24
24
return env->ThrowTypeError (" argument should be a Buffer" ); \
25
25
} while (0 )
26
26
27
- #define ARGS_THIS (argT ) \
28
- Local<Object> obj = argT; \
29
- size_t obj_length = obj->GetIndexedPropertiesExternalArrayDataLength (); \
30
- char * obj_data = static_cast <char *>( \
31
- obj->GetIndexedPropertiesExternalArrayData ()); \
32
- if (obj_length > 0 ) \
33
- CHECK_NE (obj_data, nullptr );
27
+ #define ARGS_THIS_DEC (name ) \
28
+ size_t name##_length; \
29
+ char * name##_data;
30
+
31
+ #define ARGS_THIS (argT, name ) \
32
+ Local<Object> name = argT; \
33
+ name##_length = name->GetIndexedPropertiesExternalArrayDataLength (); \
34
+ name##_data = static_cast <char *>( \
35
+ name->GetIndexedPropertiesExternalArrayData ()); \
36
+ if (name##_length > 0 ) \
37
+ CHECK_NE (name##_data, nullptr );
34
38
35
39
#define SLICE_START_END (start_arg, end_arg, end_max ) \
36
40
size_t start; \
@@ -228,34 +232,38 @@ Local<Object> Use(Environment* env, char* data, uint32_t length) {
228
232
template <encoding encoding>
229
233
void StringSlice (const FunctionCallbackInfo<Value>& args) {
230
234
Environment* env = Environment::GetCurrent (args);
235
+ Isolate* isolate = env->isolate ();
231
236
232
237
THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
233
- ARGS_THIS (args.This ())
234
238
235
- if (obj_length == 0 )
239
+ ARGS_THIS_DEC (ts_obj);
240
+ ARGS_THIS (args.This (), ts_obj);
241
+
242
+ if (ts_obj_length == 0 )
236
243
return args.GetReturnValue ().SetEmptyString ();
237
244
238
- SLICE_START_END (args[0 ], args[1 ], obj_length )
245
+ SLICE_START_END (args[0 ], args[1 ], ts_obj_length )
239
246
240
247
args.GetReturnValue ().Set (
241
- StringBytes::Encode (env-> isolate (), obj_data + start, length, encoding));
248
+ StringBytes::Encode (isolate, ts_obj_data + start, length, encoding));
242
249
}
243
250
244
251
245
252
template <>
246
253
void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) {
247
254
Environment* env = Environment::GetCurrent (args);
255
+ ARGS_THIS_DEC (ts_obj);
248
256
249
257
THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
250
- ARGS_THIS (args.This ())
258
+ ARGS_THIS (args.This (), ts_obj);
251
259
252
- if (obj_length == 0 )
260
+ if (ts_obj_length == 0 )
253
261
return args.GetReturnValue ().SetEmptyString ();
254
262
255
- SLICE_START_END (args[0 ], args[1 ], obj_length )
263
+ SLICE_START_END (args[0 ], args[1 ], ts_obj_length )
256
264
length /= 2 ;
257
265
258
- const char * data = obj_data + start;
266
+ const char * data = ts_obj_data + start;
259
267
const uint16_t * buf;
260
268
bool release = false ;
261
269
@@ -319,13 +327,10 @@ void Base64Slice(const FunctionCallbackInfo<Value>& args) {
319
327
void Copy (const FunctionCallbackInfo<Value> &args) {
320
328
Environment* env = Environment::GetCurrent (args);
321
329
322
- if (!HasInstance (args[0 ]))
323
- return env->ThrowTypeError (" first arg should be a Buffer" );
324
-
325
- Local<Object> target = args[0 ].As <Object>();
326
-
327
330
THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
328
- ARGS_THIS (args.This ())
331
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
332
+ Local<Object> target_obj = args[0 ].As <Object>();
333
+
329
334
size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength ();
330
335
char * target_data = static_cast <char *>(
331
336
target->GetIndexedPropertiesExternalArrayData ());
@@ -335,63 +340,64 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
335
340
336
341
CHECK_NOT_OOB (ParseArrayIndex (args[1 ], 0 , &target_start));
337
342
CHECK_NOT_OOB (ParseArrayIndex (args[2 ], 0 , &source_start));
338
- CHECK_NOT_OOB (ParseArrayIndex (args[3 ], obj_length , &source_end));
343
+ CHECK_NOT_OOB (ParseArrayIndex (args[3 ], ts_obj_length , &source_end));
339
344
340
345
// Copy 0 bytes; we're done
341
346
if (target_start >= target_length || source_start >= source_end)
342
347
return args.GetReturnValue ().Set (0 );
343
348
344
- if (source_start > obj_length )
349
+ if (source_start > ts_obj_length )
345
350
return env->ThrowRangeError (" out of range index" );
346
351
347
352
if (source_end - source_start > target_length - target_start)
348
353
source_end = source_start + target_length - target_start;
349
354
350
355
uint32_t to_copy = MIN (MIN (source_end - source_start,
351
356
target_length - target_start),
352
- obj_length - source_start);
357
+ ts_obj_length - source_start);
353
358
354
- memmove (target_data + target_start, obj_data + source_start, to_copy);
359
+ memmove (target_data + target_start, ts_obj_data + source_start, to_copy);
355
360
args.GetReturnValue ().Set (to_copy);
356
361
}
357
362
358
363
359
364
void Fill (const FunctionCallbackInfo<Value>& args) {
360
365
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
361
- ARGS_THIS (args[0 ].As <Object>())
366
+ ARGS_THIS_DEC (ts_obj);
367
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
362
368
363
369
size_t start = args[2 ]->Uint32Value ();
364
370
size_t end = args[3 ]->Uint32Value ();
365
371
size_t length = end - start;
366
- CHECK (length + start <= obj_length );
372
+ CHECK (length + start <= ts_obj_length );
367
373
368
374
if (args[1 ]->IsNumber ()) {
369
375
int value = args[1 ]->Uint32Value () & 255 ;
370
- memset (obj_data + start, value, length);
376
+ memset (ts_obj_data + start, value, length);
371
377
return ;
372
378
}
373
379
374
380
node::Utf8Value str (args.GetIsolate (), args[1 ]);
375
381
size_t str_length = str.length ();
376
382
size_t in_there = str_length;
377
- char * ptr = obj_data + start + str_length;
383
+ char * ptr = ts_obj_data + start + str_length;
378
384
379
385
if (str_length == 0 )
380
386
return ;
381
387
382
- memcpy (obj_data + start, *str, MIN (str_length, length));
388
+ memcpy (ts_obj_data + start, *str, MIN (str_length, length));
383
389
384
390
if (str_length >= length)
385
391
return ;
386
392
387
393
while (in_there < length - in_there) {
388
- memcpy (ptr, obj_data + start, in_there);
394
+ memcpy (ptr, ts_obj_data + start, in_there);
389
395
ptr += in_there;
390
396
in_there *= 2 ;
391
397
}
392
398
393
399
if (in_there < length) {
394
- memcpy (ptr, obj_data + start, length - in_there);
400
+ memcpy (ptr, ts_obj_data + start, length - in_there);
395
401
in_there = length;
396
402
}
397
403
}
@@ -400,9 +406,10 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
400
406
template <encoding encoding>
401
407
void StringWrite (const FunctionCallbackInfo<Value>& args) {
402
408
Environment* env = Environment::GetCurrent (args);
409
+ ARGS_THIS_DEC (ts_obj);
403
410
404
411
THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
405
- ARGS_THIS (args.This ())
412
+ ARGS_THIS (args.This (), ts_obj);
406
413
407
414
if (!args[0 ]->IsString ())
408
415
return env->ThrowTypeError (" Argument must be a string" );
@@ -416,18 +423,18 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
416
423
size_t max_length;
417
424
418
425
CHECK_NOT_OOB (ParseArrayIndex (args[1 ], 0 , &offset));
419
- CHECK_NOT_OOB (ParseArrayIndex (args[2 ], obj_length - offset, &max_length));
426
+ CHECK_NOT_OOB (ParseArrayIndex (args[2 ], ts_obj_length - offset, &max_length));
420
427
421
- max_length = MIN (obj_length - offset, max_length);
428
+ max_length = MIN (ts_obj_length - offset, max_length);
422
429
423
430
if (max_length == 0 )
424
431
return args.GetReturnValue ().Set (0 );
425
432
426
- if (offset >= obj_length )
433
+ if (offset >= ts_obj_length )
427
434
return env->ThrowRangeError (" Offset is out of bounds" );
428
435
429
436
uint32_t written = StringBytes::Write (env->isolate (),
430
- obj_data + offset,
437
+ ts_obj_data + offset,
431
438
max_length,
432
439
str,
433
440
encoding,
@@ -479,18 +486,19 @@ static inline void Swizzle(char* start, unsigned int len) {
479
486
template <typename T, enum Endianness endianness>
480
487
void ReadFloatGeneric (const FunctionCallbackInfo<Value>& args) {
481
488
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
482
- ARGS_THIS (args[0 ].As <Object>());
489
+ ARGS_THIS_DEC (ts_obj);
490
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
483
491
484
492
uint32_t offset = args[1 ]->Uint32Value ();
485
- CHECK_LE (offset + sizeof (T), obj_length );
493
+ CHECK_LE (offset + sizeof (T), ts_obj_length );
486
494
487
495
union NoAlias {
488
496
T val;
489
497
char bytes[sizeof (T)];
490
498
};
491
499
492
500
union NoAlias na;
493
- const char * ptr = static_cast <const char *>(obj_data ) + offset;
501
+ const char * ptr = static_cast <const char *>(ts_obj_data ) + offset;
494
502
memcpy (na.bytes , ptr, sizeof (na.bytes ));
495
503
if (endianness != GetEndianness ())
496
504
Swizzle (na.bytes , sizeof (na.bytes ));
@@ -521,19 +529,20 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
521
529
522
530
template <typename T, enum Endianness endianness>
523
531
uint32_t WriteFloatGeneric (const FunctionCallbackInfo<Value>& args) {
524
- ARGS_THIS (args[0 ].As <Object>())
532
+ ARGS_THIS_DEC (ts_obj);
533
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
525
534
526
535
T val = args[1 ]->NumberValue ();
527
536
uint32_t offset = args[2 ]->Uint32Value ();
528
- CHECK_LE (offset + sizeof (T), obj_length );
537
+ CHECK_LE (offset + sizeof (T), ts_obj_length );
529
538
530
539
union NoAlias {
531
540
T val;
532
541
char bytes[sizeof (T)];
533
542
};
534
543
535
544
union NoAlias na = { val };
536
- char * ptr = static_cast <char *>(obj_data ) + offset;
545
+ char * ptr = static_cast <char *>(ts_obj_data ) + offset;
537
546
if (endianness != GetEndianness ())
538
547
Swizzle (na.bytes , sizeof (na.bytes ));
539
548
memcpy (ptr, na.bytes , sizeof (na.bytes ));
@@ -575,6 +584,7 @@ void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) {
575
584
576
585
void Compare (const FunctionCallbackInfo<Value> &args) {
577
586
Environment* env = Environment::GetCurrent (args);
587
+
578
588
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
579
589
THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
580
590
@@ -631,28 +641,30 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
631
641
ASSERT (args[2 ]->IsNumber ());
632
642
633
643
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
634
- ARGS_THIS (args[0 ].As <Object>());
644
+ ARGS_THIS_DEC (ts_obj);
645
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
646
+
635
647
node::Utf8Value str (args.GetIsolate (), args[1 ]);
636
648
int32_t offset_i32 = args[2 ]->Int32Value ();
637
649
uint32_t offset;
638
650
639
651
if (offset_i32 < 0 ) {
640
- if (offset_i32 + static_cast <int32_t >(obj_length ) < 0 )
652
+ if (offset_i32 + static_cast <int32_t >(ts_obj_length ) < 0 )
641
653
offset = 0 ;
642
654
else
643
- offset = static_cast <uint32_t >(obj_length + offset_i32);
655
+ offset = static_cast <uint32_t >(ts_obj_length + offset_i32);
644
656
} else {
645
657
offset = static_cast <uint32_t >(offset_i32);
646
658
}
647
659
648
660
if (str.length () == 0 ||
649
- obj_length == 0 ||
661
+ ts_obj_length == 0 ||
650
662
(offset != 0 && str.length () + offset <= str.length ()) ||
651
- str.length () + offset > obj_length )
663
+ str.length () + offset > ts_obj_length )
652
664
return args.GetReturnValue ().Set (-1 );
653
665
654
666
int32_t r =
655
- IndexOf (obj_data + offset, obj_length - offset, *str, str.length ());
667
+ IndexOf (ts_obj_data + offset, ts_obj_length - offset, *str, str.length ());
656
668
args.GetReturnValue ().Set (r == -1 ? -1 : static_cast <int32_t >(r + offset));
657
669
}
658
670
@@ -662,7 +674,9 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
662
674
ASSERT (args[2 ]->IsNumber ());
663
675
664
676
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
665
- ARGS_THIS (args[0 ].As <Object>());
677
+ ARGS_THIS_DEC (ts_obj);
678
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
679
+
666
680
Local<Object> buf = args[1 ].As <Object>();
667
681
int32_t offset_i32 = args[2 ]->Int32Value ();
668
682
size_t buf_length = buf->GetIndexedPropertiesExternalArrayDataLength ();
@@ -674,22 +688,22 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
674
688
CHECK_NE (buf_data, nullptr );
675
689
676
690
if (offset_i32 < 0 ) {
677
- if (offset_i32 + static_cast <int32_t >(obj_length ) < 0 )
691
+ if (offset_i32 + static_cast <int32_t >(ts_obj_length ) < 0 )
678
692
offset = 0 ;
679
693
else
680
- offset = static_cast <uint32_t >(obj_length + offset_i32);
694
+ offset = static_cast <uint32_t >(ts_obj_length + offset_i32);
681
695
} else {
682
696
offset = static_cast <uint32_t >(offset_i32);
683
697
}
684
698
685
699
if (buf_length == 0 ||
686
- obj_length == 0 ||
700
+ ts_obj_length == 0 ||
687
701
(offset != 0 && buf_length + offset <= buf_length) ||
688
- buf_length + offset > obj_length )
702
+ buf_length + offset > ts_obj_length )
689
703
return args.GetReturnValue ().Set (-1 );
690
704
691
705
int32_t r =
692
- IndexOf (obj_data + offset, obj_length - offset, buf_data, buf_length);
706
+ IndexOf (ts_obj_data + offset, ts_obj_length - offset, buf_data, buf_length);
693
707
args.GetReturnValue ().Set (r == -1 ? -1 : static_cast <int32_t >(r + offset));
694
708
}
695
709
@@ -699,27 +713,29 @@ void IndexOfNumber(const FunctionCallbackInfo<Value>& args) {
699
713
ASSERT (args[2 ]->IsNumber ());
700
714
701
715
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
702
- ARGS_THIS (args[0 ].As <Object>());
716
+ ARGS_THIS_DEC (ts_obj);
717
+ ARGS_THIS (args[0 ].As <Object>(), ts_obj);
718
+
703
719
uint32_t needle = args[1 ]->Uint32Value ();
704
720
int32_t offset_i32 = args[2 ]->Int32Value ();
705
721
uint32_t offset;
706
722
707
723
if (offset_i32 < 0 ) {
708
- if (offset_i32 + static_cast <int32_t >(obj_length ) < 0 )
724
+ if (offset_i32 + static_cast <int32_t >(ts_obj_length ) < 0 )
709
725
offset = 0 ;
710
726
else
711
- offset = static_cast <uint32_t >(obj_length + offset_i32);
727
+ offset = static_cast <uint32_t >(ts_obj_length + offset_i32);
712
728
} else {
713
729
offset = static_cast <uint32_t >(offset_i32);
714
730
}
715
731
716
- if (obj_length == 0 || offset + 1 > obj_length )
732
+ if (ts_obj_length == 0 || offset + 1 > ts_obj_length )
717
733
return args.GetReturnValue ().Set (-1 );
718
734
719
- void * ptr = memchr (obj_data + offset, needle, obj_length - offset);
735
+ void * ptr = memchr (ts_obj_data + offset, needle, ts_obj_length - offset);
720
736
char * ptr_char = static_cast <char *>(ptr);
721
737
args.GetReturnValue ().Set (
722
- ptr ? static_cast <int32_t >(ptr_char - obj_data ) : -1 );
738
+ ptr ? static_cast <int32_t >(ptr_char - ts_obj_data ) : -1 );
723
739
}
724
740
725
741
0 commit comments