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