-
Notifications
You must be signed in to change notification settings - Fork 680
/
Copy pathecma-helpers-string.c
2863 lines (2425 loc) · 90.6 KB
/
ecma-helpers-string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers-number.h"
#include "ecma-helpers.h"
#include "jcontext.h"
#include "jrt-libc-includes.h"
#include "jrt.h"
#include "lit-char-helpers.h"
#include "lit-magic-strings.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmahelpers Helpers for operations with ECMA data types
* @{
*/
JERRY_STATIC_ASSERT (ECMA_STRING_CONTAINER_MASK >= ECMA_STRING_CONTAINER__MAX,
ecma_string_container_types_must_be_lower_than_the_container_mask);
JERRY_STATIC_ASSERT ((ECMA_STRING_MAX_REF | ECMA_STRING_CONTAINER_MASK | ECMA_STATIC_STRING_FLAG) == UINT32_MAX,
ecma_string_ref_and_container_fields_should_fill_the_32_bit_field);
JERRY_STATIC_ASSERT (ECMA_STRING_NOT_ARRAY_INDEX == UINT32_MAX,
ecma_string_not_array_index_must_be_equal_to_uint32_max);
JERRY_STATIC_ASSERT ((ECMA_TYPE_DIRECT_STRING & 0x1) != 0, ecma_type_direct_string_must_be_odd_number);
JERRY_STATIC_ASSERT (LIT_MAGIC_STRING__COUNT <= ECMA_DIRECT_STRING_MAX_IMM,
all_magic_strings_must_be_encoded_as_direct_string);
JERRY_STATIC_ASSERT ((int) ECMA_DIRECT_STRING_UINT == (int) ECMA_STRING_CONTAINER_UINT32_IN_DESC,
ecma_direct_and_container_types_must_match);
JERRY_STATIC_ASSERT (ECMA_PROPERTY_NAME_TYPE_SHIFT > ECMA_VALUE_SHIFT,
ecma_property_name_type_shift_must_be_greater_than_ecma_value_shift);
JERRY_STATIC_ASSERT (sizeof (ecma_stringbuilder_header_t) <= ECMA_ASCII_STRING_HEADER_SIZE,
ecma_stringbuilder_header_must_not_be_larger_than_ecma_ascii_string);
/**
* Convert a string to an unsigned 32 bit value if possible
*
* @return true if the conversion is successful
* false otherwise
*/
static bool
ecma_string_to_array_index (const lit_utf8_byte_t *string_p, /**< utf-8 string */
lit_utf8_size_t string_size, /**< string size */
uint32_t *result_p) /**< [out] converted value */
{
JERRY_ASSERT (string_size > 0 && *string_p >= LIT_CHAR_0 && *string_p <= LIT_CHAR_9);
if (*string_p == LIT_CHAR_0)
{
*result_p = 0;
return (string_size == 1);
}
if (string_size > ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32)
{
return false;
}
uint32_t index = 0;
const lit_utf8_byte_t *string_end_p = string_p + string_size;
if (string_size == ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32)
{
string_end_p--;
}
do
{
if (*string_p > LIT_CHAR_9 || *string_p < LIT_CHAR_0)
{
return false;
}
index = (index * 10) + (uint32_t) (*string_p++ - LIT_CHAR_0);
} while (string_p < string_end_p);
if (string_size < ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32)
{
*result_p = index;
return true;
}
/* Overflow must be checked as well when size is
* equal to ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32. */
if (*string_p > LIT_CHAR_9 || *string_p < LIT_CHAR_0 || index > (UINT32_MAX / 10)
|| (index == (UINT32_MAX / 10) && *string_p > LIT_CHAR_5))
{
return false;
}
*result_p = (index * 10) + (uint32_t) (*string_p - LIT_CHAR_0);
return true;
} /* ecma_string_to_array_index */
/**
* Returns the characters and size of a string.
*
* Note:
* UINT type is not supported
*
* @return byte array start - if the byte array of a string is available
* NULL - otherwise
*/
static const lit_utf8_byte_t *
ecma_string_get_chars_fast (const ecma_string_t *string_p, /**< ecma-string */
lit_utf8_size_t *size_p) /**< [out] size of the ecma string */
{
if (ECMA_IS_DIRECT_STRING (string_p))
{
if (ECMA_GET_DIRECT_STRING_TYPE (string_p) == ECMA_DIRECT_STRING_MAGIC)
{
uint32_t id = (uint32_t) ECMA_GET_DIRECT_STRING_VALUE (string_p);
if (id >= LIT_MAGIC_STRING__COUNT)
{
id -= LIT_MAGIC_STRING__COUNT;
*size_p = lit_get_magic_string_ex_size (id);
return lit_get_magic_string_ex_utf8 (id);
}
*size_p = lit_get_magic_string_size (id);
return lit_get_magic_string_utf8 (id);
}
}
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);
switch (ECMA_STRING_GET_CONTAINER (string_p))
{
case ECMA_STRING_CONTAINER_HEAP_UTF8_STRING:
{
*size_p = ((ecma_short_string_t *) string_p)->size;
return ECMA_SHORT_STRING_GET_BUFFER (string_p);
}
case ECMA_STRING_CONTAINER_LONG_OR_EXTERNAL_STRING:
{
ecma_long_string_t *long_string_p = (ecma_long_string_t *) string_p;
*size_p = long_string_p->size;
return long_string_p->string_p;
}
case ECMA_STRING_CONTAINER_HEAP_ASCII_STRING:
{
*size_p = ECMA_ASCII_STRING_GET_SIZE (string_p);
return ECMA_ASCII_STRING_GET_BUFFER (string_p);
}
default:
{
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX);
lit_magic_string_ex_id_t id = LIT_MAGIC_STRING__COUNT - string_p->u.magic_string_ex_id;
*size_p = lit_get_magic_string_ex_size (id);
return lit_get_magic_string_ex_utf8 (id);
}
}
} /* ecma_string_get_chars_fast */
/**
* Allocate new ecma-string and fill it with reference to ECMA magic string
*
* @return pointer to ecma-string descriptor
*/
static ecma_string_t *
ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_string_ex_id_t id) /**< identifier of externl magic string */
{
JERRY_ASSERT (id < lit_get_magic_string_ex_count ());
uintptr_t string_id = (uintptr_t) (id + LIT_MAGIC_STRING__COUNT);
if (JERRY_LIKELY (string_id <= ECMA_DIRECT_STRING_MAX_IMM))
{
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_MAGIC, string_id);
}
ecma_string_t *string_desc_p = ecma_alloc_string ();
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_MAGIC_STRING_EX | ECMA_STRING_REF_ONE;
string_desc_p->u.magic_string_ex_id = id + LIT_MAGIC_STRING__COUNT;
return string_desc_p;
} /* ecma_new_ecma_string_from_magic_string_ex_id */
#if JERRY_ESNEXT
/**
* Allocate new ecma-string and fill it with reference to the symbol descriptor
*
* Note:
* Takes the reference to the string_desc
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_symbol_from_descriptor_string (ecma_value_t string_desc) /**< ecma-string */
{
JERRY_ASSERT (!ecma_is_value_symbol (string_desc));
ecma_extended_string_t *symbol_p = ecma_alloc_extended_string ();
symbol_p->header.refs_and_container = ECMA_STRING_REF_ONE | ECMA_STRING_CONTAINER_SYMBOL;
symbol_p->u.symbol_descriptor = string_desc;
symbol_p->header.u.hash = (lit_string_hash_t) (((uintptr_t) symbol_p) & (uintptr_t) ~ECMA_SYMBOL_FLAGS_MASK);
return (ecma_string_t *) symbol_p;
} /* ecma_new_symbol_from_descriptor_string */
/**
* Check whether an ecma-string contains an ecma-symbol
*
* @return true - if the ecma-string contains an ecma-symbol
* false - otherwise
*/
bool
ecma_prop_name_is_symbol (ecma_string_t *string_p) /**< ecma-string */
{
JERRY_ASSERT (string_p != NULL);
return (!ECMA_IS_DIRECT_STRING (string_p) && ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_SYMBOL);
} /* ecma_prop_name_is_symbol */
#endif /* JERRY_ESNEXT */
/**
* Allocate new UTF8 ecma-string and fill it with characters from the given utf8 buffer
*
* @return pointer to ecma-string descriptor
*/
static inline ecma_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_new_ecma_string_from_utf8_buffer (lit_utf8_size_t length, /**< length of the buffer */
lit_utf8_size_t size, /**< size of the buffer */
lit_utf8_byte_t **data_p) /**< [out] pointer to the start of the string buffer */
{
if (JERRY_LIKELY (size <= UINT16_MAX))
{
if (JERRY_LIKELY (length == size) && size <= (UINT8_MAX + 1))
{
ecma_string_t *string_desc_p;
string_desc_p = (ecma_string_t *) ecma_alloc_string_buffer (size + ECMA_ASCII_STRING_HEADER_SIZE);
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_HEAP_ASCII_STRING | ECMA_STRING_REF_ONE;
ECMA_ASCII_STRING_SET_SIZE (string_desc_p, size);
*data_p = ECMA_ASCII_STRING_GET_BUFFER (string_desc_p);
return (ecma_string_t *) string_desc_p;
}
ecma_short_string_t *string_desc_p;
string_desc_p = (ecma_short_string_t *) ecma_alloc_string_buffer (size + sizeof (ecma_short_string_t));
string_desc_p->header.refs_and_container = ECMA_STRING_CONTAINER_HEAP_UTF8_STRING | ECMA_STRING_REF_ONE;
string_desc_p->size = (uint16_t) size;
string_desc_p->length = (uint16_t) length;
*data_p = ECMA_SHORT_STRING_GET_BUFFER (string_desc_p);
return (ecma_string_t *) string_desc_p;
}
ecma_long_string_t *long_string_p;
long_string_p = (ecma_long_string_t *) ecma_alloc_string_buffer (size + sizeof (ecma_long_string_t));
long_string_p->header.refs_and_container = ECMA_STRING_CONTAINER_LONG_OR_EXTERNAL_STRING | ECMA_STRING_REF_ONE;
long_string_p->string_p = ECMA_LONG_STRING_BUFFER_START (long_string_p);
long_string_p->size = size;
long_string_p->length = length;
*data_p = ECMA_LONG_STRING_BUFFER_START (long_string_p);
return (ecma_string_t *) long_string_p;
} /* ecma_new_ecma_string_from_utf8_buffer */
/**
* Checks whether a string has a special representation, that is, the string is either a magic string,
* an external magic string, or an uint32 number, and creates an ecma string using the special representation,
* if available.
*
* @return pointer to ecma string with the special representation
* NULL, if there is no special representation for the string
*/
static ecma_string_t *
ecma_find_special_string (const lit_utf8_byte_t *string_p, /**< utf8 string */
lit_utf8_size_t string_size) /**< string size */
{
JERRY_ASSERT (string_p != NULL || string_size == 0);
lit_magic_string_id_t magic_string_id = lit_is_utf8_string_magic (string_p, string_size);
if (magic_string_id != LIT_MAGIC_STRING__COUNT)
{
return ecma_get_magic_string (magic_string_id);
}
JERRY_ASSERT (string_size > 0);
if (*string_p >= LIT_CHAR_0 && *string_p <= LIT_CHAR_9)
{
uint32_t array_index;
if (ecma_string_to_array_index (string_p, string_size, &array_index))
{
return ecma_new_ecma_string_from_uint32 (array_index);
}
}
if (lit_get_magic_string_ex_count () > 0)
{
lit_magic_string_ex_id_t magic_string_ex_id = lit_is_ex_utf8_string_magic (string_p, string_size);
if (magic_string_ex_id < lit_get_magic_string_ex_count ())
{
return ecma_new_ecma_string_from_magic_string_ex_id (magic_string_ex_id);
}
}
return NULL;
} /* ecma_find_special_string */
/**
* Allocate new ecma-string and fill it with characters from ascii characters
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_ascii (const lit_utf8_byte_t *string_p, /**< ascii string */
lit_utf8_size_t string_size) /**< string size */
{
JERRY_ASSERT (string_p != NULL || string_size == 0);
ecma_string_t *string_desc_p = ecma_find_special_string (string_p, string_size);
if (string_desc_p != NULL)
{
return string_desc_p;
}
lit_utf8_byte_t *data_p;
string_desc_p = ecma_new_ecma_string_from_utf8_buffer (string_size, string_size, &data_p);
string_desc_p->u.hash = lit_utf8_string_calc_hash (string_p, string_size);
memcpy (data_p, string_p, string_size);
return string_desc_p;
} /* ecma_new_ecma_string_from_ascii */
/**
* Allocate new ecma-string and fill it with characters from the utf8 string
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, /**< utf-8 string */
lit_utf8_size_t string_size) /**< string size */
{
JERRY_ASSERT (string_p != NULL || string_size == 0);
JERRY_ASSERT (lit_is_valid_cesu8_string (string_p, string_size));
ecma_string_t *string_desc_p = ecma_find_special_string (string_p, string_size);
if (string_desc_p != NULL)
{
return string_desc_p;
}
lit_utf8_byte_t *data_p;
string_desc_p =
ecma_new_ecma_string_from_utf8_buffer (lit_utf8_string_length (string_p, string_size), string_size, &data_p);
string_desc_p->u.hash = lit_utf8_string_calc_hash (string_p, string_size);
memcpy (data_p, string_p, string_size);
return string_desc_p;
} /* ecma_new_ecma_string_from_utf8 */
/**
* Allocate a new ecma-string and initialize it from the utf8 string argument.
* All 4-bytes long unicode sequences are converted into two 3-bytes long sequences.
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string_p, /**< utf-8 string */
lit_utf8_size_t string_size) /**< utf-8 string size */
{
JERRY_ASSERT (string_p != NULL || string_size == 0);
lit_utf8_size_t converted_string_length = 0;
lit_utf8_size_t converted_string_size = 0;
lit_utf8_size_t pos = 0;
/* Calculate the required length and size information of the converted cesu-8 encoded string */
while (pos < string_size)
{
if ((string_p[pos] & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER)
{
pos++;
}
else if ((string_p[pos] & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER)
{
pos += 2;
}
else if ((string_p[pos] & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER)
{
pos += 3;
}
else
{
JERRY_ASSERT ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER);
pos += 4;
converted_string_size += 2;
converted_string_length++;
}
converted_string_length++;
}
JERRY_ASSERT (pos == string_size);
if (converted_string_size == 0)
{
return ecma_new_ecma_string_from_utf8 (string_p, string_size);
}
converted_string_size += string_size;
JERRY_ASSERT (lit_is_valid_utf8_string (string_p, string_size, false));
lit_utf8_byte_t *data_p;
ecma_string_t *string_desc_p =
ecma_new_ecma_string_from_utf8_buffer (converted_string_length, converted_string_size, &data_p);
const lit_utf8_byte_t *const begin_data_p = data_p;
pos = 0;
while (pos < string_size)
{
if ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER)
{
/* Processing 4 byte unicode sequence. Always converted to two 3 byte long sequence. */
lit_four_byte_utf8_char_to_cesu8 (data_p, string_p + pos);
data_p += 3 * 2;
pos += 4;
}
else
{
*data_p++ = string_p[pos++];
}
}
JERRY_ASSERT (pos == string_size);
string_desc_p->u.hash = lit_utf8_string_calc_hash (begin_data_p, converted_string_size);
return (ecma_string_t *) string_desc_p;
} /* ecma_new_ecma_string_from_utf8_converted_to_cesu8 */
/**
* Allocate new ecma-external-string and fill it with characters from the cesu8 string
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_external_string_from_cesu8 (const lit_utf8_byte_t *string_p, /**< cesu-8 string */
lit_utf8_size_t string_size, /**< string size */
void *user_p) /**< user pointer passed to the callback
* when the string is freed */
{
JERRY_ASSERT (string_p != NULL || string_size == 0);
JERRY_ASSERT (lit_is_valid_cesu8_string (string_p, string_size));
if (string_size < (sizeof (ecma_external_string_t) - sizeof (ecma_short_string_t)))
{
/* Normal strings are created for short strings. */
ecma_string_t *string_desc_p = ecma_new_ecma_string_from_utf8 (string_p, string_size);
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
free_cb ((lit_utf8_byte_t *) string_p, string_size, user_p);
}
return string_desc_p;
}
ecma_string_t *string_desc_p = ecma_find_special_string (string_p, string_size);
if (string_desc_p != NULL)
{
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
free_cb ((lit_utf8_byte_t *) string_p, string_size, user_p);
}
return string_desc_p;
}
ecma_external_string_t *external_string_p = ecma_alloc_external_string ();
ecma_long_string_t *long_string_p = (ecma_long_string_t *) external_string_p;
long_string_p->header.refs_and_container = ECMA_STRING_CONTAINER_LONG_OR_EXTERNAL_STRING | ECMA_STRING_REF_ONE;
long_string_p->header.u.hash = lit_utf8_string_calc_hash (string_p, string_size);
long_string_p->string_p = string_p;
long_string_p->size = string_size;
long_string_p->length = lit_utf8_string_length (string_p, string_size);
external_string_p->user_p = user_p;
return (ecma_string_t *) external_string_p;
} /* ecma_new_ecma_external_string_from_cesu8 */
/**
* Allocate new ecma-string and fill it with cesu-8 character which represents specified code unit
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_code_unit (ecma_char_t code_unit) /**< code unit */
{
lit_utf8_byte_t lit_utf8_bytes[LIT_UTF8_MAX_BYTES_IN_CODE_UNIT];
lit_utf8_size_t bytes_size = lit_code_unit_to_utf8 (code_unit, lit_utf8_bytes);
return ecma_new_ecma_string_from_utf8 (lit_utf8_bytes, bytes_size);
} /* ecma_new_ecma_string_from_code_unit */
#if JERRY_ESNEXT
/**
* Allocate new ecma-string and fill it with cesu-8 character which represents specified code units
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_code_units (ecma_char_t first_code_unit, /**< code unit */
ecma_char_t second_code_unit) /**< code unit */
{
lit_utf8_byte_t lit_utf8_bytes[2 * LIT_UTF8_MAX_BYTES_IN_CODE_UNIT];
lit_utf8_size_t bytes_size = lit_code_unit_to_utf8 (first_code_unit, lit_utf8_bytes);
bytes_size += lit_code_unit_to_utf8 (second_code_unit, lit_utf8_bytes + bytes_size);
return ecma_new_ecma_string_from_utf8 (lit_utf8_bytes, bytes_size);
} /* ecma_new_ecma_string_from_code_units */
#endif /* JERRY_ESNEXT */
/**
* Allocate new ecma-string and fill it with ecma-number
*
* Note: the number cannot be represented as direct string
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_non_direct_string_from_uint32 (uint32_t uint32_number) /**< uint32 value of the string */
{
JERRY_ASSERT (uint32_number > ECMA_DIRECT_STRING_MAX_IMM);
ecma_string_t *string_p = ecma_alloc_string ();
string_p->refs_and_container = ECMA_STRING_CONTAINER_UINT32_IN_DESC | ECMA_STRING_REF_ONE;
string_p->u.uint32_number = uint32_number;
return string_p;
} /* ecma_new_non_direct_string_from_uint32 */
/**
* Allocate new ecma-string and fill it with property length number
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_length (ecma_length_t number) /**< property length */
{
if (JERRY_LIKELY (number <= ECMA_DIRECT_STRING_MAX_IMM))
{
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) number);
}
#if JERRY_ESNEXT
JERRY_ASSERT ((ecma_number_t) number <= ECMA_NUMBER_MAX_SAFE_INTEGER);
if (JERRY_UNLIKELY (number > UINT32_MAX))
{
return ecma_new_ecma_string_from_number ((ecma_number_t) number);
}
#endif /* JERRY_ESNEXT */
return ecma_new_non_direct_string_from_uint32 ((uint32_t) number);
} /* ecma_new_ecma_string_from_length */
/**
* Allocate new ecma-string and fill it with uint32 number
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_uint32 (uint32_t uint32_number) /**< uint32 value of the string */
{
if (JERRY_LIKELY (uint32_number <= ECMA_DIRECT_STRING_MAX_IMM))
{
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) uint32_number);
}
return ecma_new_non_direct_string_from_uint32 (uint32_number);
} /* ecma_new_ecma_string_from_uint32 */
/**
* Returns the constant assigned to the uint32 number.
*
* Note:
* Calling ecma_deref_ecma_string on the returned pointer is optional.
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_get_ecma_string_from_uint32 (uint32_t uint32_number) /**< input number */
{
JERRY_ASSERT (uint32_number <= ECMA_DIRECT_STRING_MAX_IMM);
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) uint32_number);
} /* ecma_get_ecma_string_from_uint32 */
/**
* Allocate new ecma-string and fill it with ecma-number
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_ecma_string_from_number (ecma_number_t num) /**< ecma-number */
{
uint32_t uint32_num = ecma_number_to_uint32 (num);
if (num == ((ecma_number_t) uint32_num))
{
return ecma_new_ecma_string_from_uint32 (uint32_num);
}
if (ecma_number_is_nan (num))
{
return ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
}
if (ecma_number_is_infinity (num))
{
lit_magic_string_id_t id =
(ecma_number_is_negative (num) ? LIT_MAGIC_STRING_NEGATIVE_INFINITY_UL : LIT_MAGIC_STRING_INFINITY_UL);
return ecma_get_magic_string (id);
}
lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
lit_utf8_size_t str_size = ecma_number_to_utf8_string (num, str_buf, sizeof (str_buf));
JERRY_ASSERT (str_size > 0);
#ifndef JERRY_NDEBUG
JERRY_ASSERT (lit_is_utf8_string_magic (str_buf, str_size) == LIT_MAGIC_STRING__COUNT
&& lit_is_ex_utf8_string_magic (str_buf, str_size) == lit_get_magic_string_ex_count ());
#endif /* !JERRY_NDEBUG */
lit_utf8_byte_t *data_p;
ecma_string_t *string_desc_p =
ecma_new_ecma_string_from_utf8_buffer (lit_utf8_string_length (str_buf, str_size), str_size, &data_p);
string_desc_p->u.hash = lit_utf8_string_calc_hash (str_buf, str_size);
memcpy (data_p, str_buf, str_size);
return string_desc_p;
} /* ecma_new_ecma_string_from_number */
/**
* Returns the constant assigned to the magic string id.
*
* Note:
* Calling ecma_deref_ecma_string on the returned pointer is optional.
*
* @return pointer to ecma-string descriptor
*/
extern inline ecma_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_get_magic_string (lit_magic_string_id_t id) /**< identifier of magic string */
{
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT);
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_MAGIC, (uintptr_t) id);
} /* ecma_get_magic_string */
/**
* Returns the constant assigned to the internal magic string id.
*
* Note:
* Calling ecma_deref_ecma_string on the returned pointer is optional.
*
* @return pointer to ecma-string descriptor
*/
extern inline ecma_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_get_internal_string (lit_magic_string_id_t id) /**< identifier of magic string */
{
JERRY_ASSERT (id >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT && id < LIT_MAGIC_STRING__COUNT);
return (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_SPECIAL, (uintptr_t) id);
} /* ecma_get_internal_string */
/**
* Append a cesu8 string after an ecma-string
*
* Note:
* The string1_p argument is freed. If it needs to be preserved,
* call ecma_ref_ecma_string with string1_p before the call.
*
* @return concatenation of an ecma-string and a cesu8 string
*/
ecma_string_t *
ecma_append_chars_to_string (ecma_string_t *string1_p, /**< base ecma-string */
const lit_utf8_byte_t *cesu8_string2_p, /**< characters to be appended */
lit_utf8_size_t cesu8_string2_size, /**< byte size of cesu8_string2_p */
lit_utf8_size_t cesu8_string2_length) /**< character length of cesu8_string2_p */
{
JERRY_ASSERT (string1_p != NULL && cesu8_string2_size > 0 && cesu8_string2_length > 0);
if (JERRY_UNLIKELY (ecma_string_is_empty (string1_p)))
{
return ecma_new_ecma_string_from_utf8 (cesu8_string2_p, cesu8_string2_size);
}
lit_utf8_size_t cesu8_string1_size;
lit_utf8_size_t cesu8_string1_length;
uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
lit_utf8_byte_t uint32_to_string_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32];
const lit_utf8_byte_t *cesu8_string1_p =
ecma_string_get_chars (string1_p, &cesu8_string1_size, &cesu8_string1_length, uint32_to_string_buffer, &flags);
JERRY_ASSERT (!(flags & ECMA_STRING_FLAG_MUST_BE_FREED));
JERRY_ASSERT (cesu8_string1_length > 0);
JERRY_ASSERT (cesu8_string1_length <= cesu8_string1_size);
lit_utf8_size_t new_size = cesu8_string1_size + cesu8_string2_size;
/* Poor man's carry flag check: it is impossible to allocate this large string. */
if (new_size < (cesu8_string1_size | cesu8_string2_size))
{
jerry_fatal (ERR_OUT_OF_MEMORY);
}
lit_magic_string_id_t magic_string_id;
magic_string_id =
lit_is_utf8_string_pair_magic (cesu8_string1_p, cesu8_string1_size, cesu8_string2_p, cesu8_string2_size);
if (magic_string_id != LIT_MAGIC_STRING__COUNT)
{
ecma_deref_ecma_string (string1_p);
return ecma_get_magic_string (magic_string_id);
}
if ((flags & ECMA_STRING_FLAG_IS_UINT32) && new_size <= ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32)
{
memcpy (uint32_to_string_buffer + cesu8_string1_size, cesu8_string2_p, cesu8_string2_size);
uint32_t array_index;
if (ecma_string_to_array_index (uint32_to_string_buffer, new_size, &array_index))
{
ecma_deref_ecma_string (string1_p);
return ecma_new_ecma_string_from_uint32 (array_index);
}
}
if (lit_get_magic_string_ex_count () > 0)
{
lit_magic_string_ex_id_t magic_string_ex_id;
magic_string_ex_id =
lit_is_ex_utf8_string_pair_magic (cesu8_string1_p, cesu8_string1_size, cesu8_string2_p, cesu8_string2_size);
if (magic_string_ex_id < lit_get_magic_string_ex_count ())
{
ecma_deref_ecma_string (string1_p);
return ecma_new_ecma_string_from_magic_string_ex_id (magic_string_ex_id);
}
}
lit_utf8_byte_t *data_p;
ecma_string_t *string_desc_p =
ecma_new_ecma_string_from_utf8_buffer (cesu8_string1_length + cesu8_string2_length, new_size, &data_p);
lit_string_hash_t hash_start;
if (JERRY_UNLIKELY (flags & ECMA_STRING_FLAG_REHASH_NEEDED))
{
hash_start = lit_utf8_string_calc_hash (cesu8_string1_p, cesu8_string1_size);
}
else
{
JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (string1_p));
hash_start = string1_p->u.hash;
}
string_desc_p->u.hash = lit_utf8_string_hash_combine (hash_start, cesu8_string2_p, cesu8_string2_size);
memcpy (data_p, cesu8_string1_p, cesu8_string1_size);
memcpy (data_p + cesu8_string1_size, cesu8_string2_p, cesu8_string2_size);
ecma_deref_ecma_string (string1_p);
return (ecma_string_t *) string_desc_p;
} /* ecma_append_chars_to_string */
/**
* Concatenate ecma-strings
*
* Note:
* The string1_p argument is freed. If it needs to be preserved,
* call ecma_ref_ecma_string with string1_p before the call.
*
* @return concatenation of two ecma-strings
*/
ecma_string_t *
ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
ecma_string_t *string2_p) /**< second ecma-string */
{
JERRY_ASSERT (string1_p != NULL && string2_p != NULL);
if (JERRY_UNLIKELY (ecma_string_is_empty (string1_p)))
{
ecma_ref_ecma_string (string2_p);
return string2_p;
}
else if (JERRY_UNLIKELY (ecma_string_is_empty (string2_p)))
{
return string1_p;
}
lit_utf8_size_t cesu8_string2_size;
lit_utf8_size_t cesu8_string2_length;
lit_utf8_byte_t uint32_to_string_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32];
uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
const lit_utf8_byte_t *cesu8_string2_p =
ecma_string_get_chars (string2_p, &cesu8_string2_size, &cesu8_string2_length, uint32_to_string_buffer, &flags);
JERRY_ASSERT (cesu8_string2_p != NULL);
ecma_string_t *result_p =
ecma_append_chars_to_string (string1_p, cesu8_string2_p, cesu8_string2_size, cesu8_string2_length);
JERRY_ASSERT (!(flags & ECMA_STRING_FLAG_MUST_BE_FREED));
return result_p;
} /* ecma_concat_ecma_strings */
/**
* Increase reference counter of non-direct ecma-string.
*/
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_ref_ecma_string_non_direct (ecma_string_t *string_p) /**< string descriptor */
{
JERRY_ASSERT (string_p != NULL);
JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (string_p));
#ifdef JERRY_NDEBUG
if (ECMA_STRING_IS_STATIC (string_p))
{
return;
}
#endif /* JERRY_NDEBUG */
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);
if (JERRY_LIKELY (string_p->refs_and_container < ECMA_STRING_MAX_REF))
{
/* Increase reference counter. */
string_p->refs_and_container += ECMA_STRING_REF_ONE;
}
else
{
jerry_fatal (ERR_REF_COUNT_LIMIT);
}
} /* ecma_ref_ecma_string_non_direct */
/**
* Increase reference counter of ecma-string.
*/
void
ecma_ref_ecma_string (ecma_string_t *string_p) /**< string descriptor */
{
JERRY_ASSERT (string_p != NULL);
if (ECMA_IS_DIRECT_STRING (string_p))
{
return;
}
ecma_ref_ecma_string_non_direct (string_p);
} /* ecma_ref_ecma_string */
/**
* Decrease reference counter and deallocate a non-direct ecma-string
* if the counter becomes zero.
*/
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_deref_ecma_string_non_direct (ecma_string_t *string_p) /**< ecma-string */
{
JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (string_p));
#ifdef JERRY_NDEBUG
if (ECMA_STRING_IS_STATIC (string_p))
{
return;
}
#endif /* JERRY_NDEBUG */
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);
/* Decrease reference counter. */
string_p->refs_and_container -= ECMA_STRING_REF_ONE;
if (string_p->refs_and_container >= ECMA_STRING_REF_ONE)
{
return;
}
ecma_destroy_ecma_string (string_p);
} /* ecma_deref_ecma_string_non_direct */
/**
* Decrease reference counter and deallocate ecma-string
* if the counter becomes zero.
*/
void
ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
{
JERRY_ASSERT (string_p != NULL);
if (ECMA_IS_DIRECT_STRING (string_p))
{
return;
}
ecma_deref_ecma_string_non_direct (string_p);
} /* ecma_deref_ecma_string */
/**
* Deallocate an ecma-string
*/
void
ecma_destroy_ecma_string (ecma_string_t *string_p) /**< ecma-string */
{
JERRY_ASSERT (string_p != NULL);
JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (string_p));
JERRY_ASSERT ((string_p->refs_and_container < ECMA_STRING_REF_ONE) || ECMA_STRING_IS_STATIC (string_p));
switch (ECMA_STRING_GET_CONTAINER (string_p))
{
case ECMA_STRING_CONTAINER_HEAP_UTF8_STRING:
{
ecma_dealloc_string_buffer (string_p, ((ecma_short_string_t *) string_p)->size + sizeof (ecma_short_string_t));
return;
}
case ECMA_STRING_CONTAINER_LONG_OR_EXTERNAL_STRING:
{
ecma_long_string_t *long_string_p = (ecma_long_string_t *) string_p;
if (long_string_p->string_p == ECMA_LONG_STRING_BUFFER_START (long_string_p))
{
ecma_dealloc_string_buffer (string_p, long_string_p->size + sizeof (ecma_long_string_t));
return;
}
ecma_external_string_t *external_string_p = (ecma_external_string_t *) string_p;
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
free_cb ((lit_utf8_byte_t *) external_string_p->header.string_p,
external_string_p->header.size,
external_string_p->user_p);
}
ecma_dealloc_external_string (external_string_p);
return;
}
case ECMA_STRING_CONTAINER_HEAP_ASCII_STRING:
{
ecma_dealloc_string_buffer (string_p, ECMA_ASCII_STRING_GET_SIZE (string_p) + ECMA_ASCII_STRING_HEADER_SIZE);
return;
}
#if JERRY_ESNEXT
case ECMA_STRING_CONTAINER_SYMBOL:
{
ecma_extended_string_t *symbol_p = (ecma_extended_string_t *) string_p;
ecma_free_value (symbol_p->u.symbol_descriptor);
ecma_dealloc_extended_string (symbol_p);
return;