-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathjerryscript.c
7322 lines (6290 loc) · 218 KB
/
jerryscript.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 "jerryscript.h"
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include "jerryscript-debugger-transport.h"
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-arraybuffer-object.h"
#include "ecma-bigint.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-comparison.h"
#include "ecma-container-object.h"
#include "ecma-dataview-object.h"
#include "ecma-errors.h"
#include "ecma-eval.h"
#include "ecma-exceptions.h"
#include "ecma-extended-info.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-init-finalize.h"
#include "ecma-iterator-object.h"
#include "ecma-lex-env.h"
#include "ecma-line-info.h"
#include "ecma-literal-storage.h"
#include "ecma-objects-general.h"
#include "ecma-objects.h"
#include "ecma-promise-object.h"
#include "ecma-proxy-object.h"
#include "ecma-regexp-object.h"
#include "ecma-shared-arraybuffer-object.h"
#include "ecma-symbol-object.h"
#include "ecma-typedarray-object.h"
#include "debugger.h"
#include "jcontext.h"
#include "jmem.h"
#include "jrt.h"
#include "js-parser.h"
#include "lit-char-helpers.h"
#include "opcodes.h"
#include "re-compiler.h"
JERRY_STATIC_ASSERT (sizeof (jerry_value_t) == sizeof (ecma_value_t),
size_of_jerry_value_t_must_be_equal_to_size_of_ecma_value_t);
#if JERRY_BUILTIN_REGEXP
JERRY_STATIC_ASSERT ((int) RE_FLAG_GLOBAL == (int) JERRY_REGEXP_FLAG_GLOBAL
&& (int) RE_FLAG_MULTILINE == (int) JERRY_REGEXP_FLAG_MULTILINE
&& (int) RE_FLAG_IGNORE_CASE == (int) JERRY_REGEXP_FLAG_IGNORE_CASE
&& (int) RE_FLAG_STICKY == (int) JERRY_REGEXP_FLAG_STICKY
&& (int) RE_FLAG_UNICODE == (int) JERRY_REGEXP_FLAG_UNICODE
&& (int) RE_FLAG_DOTALL == (int) JERRY_REGEXP_FLAG_DOTALL,
re_flags_t_must_be_equal_to_jerry_regexp_flags_t);
#endif /* JERRY_BUILTIN_REGEXP */
/* The internal ECMA_PROMISE_STATE_* values are "one byte away" from the API values */
JERRY_STATIC_ASSERT ((int) ECMA_PROMISE_IS_PENDING == (int) JERRY_PROMISE_STATE_PENDING
&& (int) ECMA_PROMISE_IS_FULFILLED == (int) JERRY_PROMISE_STATE_FULFILLED,
promise_internal_state_matches_external);
/**
* Offset between internal and external arithmetic operator types
*/
#define ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET (JERRY_BIN_OP_SUB - NUMBER_ARITHMETIC_SUBTRACTION)
JERRY_STATIC_ASSERT (((NUMBER_ARITHMETIC_SUBTRACTION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_SUB)
&& ((NUMBER_ARITHMETIC_MULTIPLICATION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET)
== JERRY_BIN_OP_MUL)
&& ((NUMBER_ARITHMETIC_DIVISION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_DIV)
&& ((NUMBER_ARITHMETIC_REMAINDER + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_REM),
number_arithmetics_operation_type_matches_external);
#if !JERRY_PARSER && !JERRY_SNAPSHOT_EXEC
#error "JERRY_SNAPSHOT_EXEC must be enabled if JERRY_PARSER is disabled!"
#endif /* !JERRY_PARSER && !JERRY_SNAPSHOT_EXEC */
/** \addtogroup jerry Jerry engine interface
* @{
*/
/**
* Assert that it is correct to call API in current state.
*
* Note:
* By convention, there are some states when API could not be invoked.
*
* The API can be and only be invoked when the ECMA_STATUS_API_ENABLED
* flag is set.
*
* This procedure checks whether the API is available, and terminates
* the engine if it is unavailable. Otherwise it is a no-op.
*
* Note:
* The API could not be invoked in the following cases:
* - before jerry_init and after jerry_cleanup
* - between enter to and return from a native free callback
*
* @return void
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
jerry_assert_api_enabled (void)
{
JERRY_ASSERT (JERRY_CONTEXT (status_flags) & ECMA_STATUS_API_ENABLED);
} /* jerry_assert_api_enabled */
/**
* Turn on API availability
*
* @return void
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
jerry_api_enable (void)
{
#ifndef JERRY_NDEBUG
JERRY_CONTEXT (status_flags) |= ECMA_STATUS_API_ENABLED;
#endif /* JERRY_NDEBUG */
} /* jerry_make_api_available */
/**
* Turn off API availability
*
* @return void
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
jerry_api_disable (void)
{
#ifndef JERRY_NDEBUG
JERRY_CONTEXT (status_flags) &= (uint32_t) ~ECMA_STATUS_API_ENABLED;
#endif /* JERRY_NDEBUG */
} /* jerry_make_api_unavailable */
/**
* Create an API compatible return value.
*
* @return return value for Jerry API functions
*/
static jerry_value_t
jerry_return (const jerry_value_t value) /**< return value */
{
if (ECMA_IS_VALUE_ERROR (value))
{
return ecma_create_exception_from_context ();
}
return value;
} /* jerry_return */
/**
* Jerry engine initialization
*/
void
jerry_init (jerry_init_flag_t flags) /**< combination of Jerry flags */
{
#if JERRY_EXTERNAL_CONTEXT
size_t total_size = jerry_port_context_alloc (sizeof (jerry_context_t));
JERRY_UNUSED (total_size);
#endif /* JERRY_EXTERNAL_CONTEXT */
jerry_context_t *context_p = &JERRY_CONTEXT_STRUCT;
memset (context_p, 0, sizeof (jerry_context_t));
#if JERRY_EXTERNAL_CONTEXT && !JERRY_SYSTEM_ALLOCATOR
uint32_t heap_start_offset = JERRY_ALIGNUP (sizeof (jerry_context_t), JMEM_ALIGNMENT);
uint8_t *heap_p = ((uint8_t *) context_p) + heap_start_offset;
uint32_t heap_size = JERRY_ALIGNDOWN (total_size - heap_start_offset, JMEM_ALIGNMENT);
JERRY_ASSERT (heap_p + heap_size <= ((uint8_t *) context_p) + total_size);
context_p->heap_p = (jmem_heap_t *) heap_p;
context_p->heap_size = heap_size;
#endif /* JERRY_EXTERNAL_CONTEXT && !JERRY_SYSTEM_ALLOCATOR */
JERRY_CONTEXT (jerry_init_flags) = flags;
jerry_api_enable ();
jmem_init ();
ecma_init ();
} /* jerry_init */
/**
* Terminate Jerry engine
*/
void
jerry_cleanup (void)
{
jerry_assert_api_enabled ();
#if JERRY_DEBUGGER
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
jerry_debugger_send_type (JERRY_DEBUGGER_CLOSE_CONNECTION);
jerry_debugger_transport_close ();
}
#endif /* JERRY_DEBUGGER */
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p); this_p != NULL; this_p = this_p->next_p)
{
if (this_p->manager_p->deinit_cb)
{
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
this_p->manager_p->deinit_cb (data);
}
}
ecma_free_all_enqueued_jobs ();
ecma_finalize ();
jerry_api_disable ();
for (jerry_context_data_header_t *this_p = JERRY_CONTEXT (context_data_p), *next_p = NULL; this_p != NULL;
this_p = next_p)
{
next_p = this_p->next_p;
if (this_p->manager_p->finalize_cb)
{
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
this_p->manager_p->finalize_cb (data);
}
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
}
jmem_finalize ();
#if JERRY_EXTERNAL_CONTEXT
jerry_port_context_free ();
#endif /* JERRY_EXTERNAL_CONTEXT */
} /* jerry_cleanup */
/**
* Retrieve a context data item, or create a new one.
*
* @param manager_p pointer to the manager whose context data item should be returned.
*
* @return a pointer to the user-provided context-specific data item for the given manager, creating such a pointer if
* none was found.
*/
void *
jerry_context_data (const jerry_context_data_manager_t *manager_p)
{
void *ret = NULL;
jerry_context_data_header_t *item_p;
for (item_p = JERRY_CONTEXT (context_data_p); item_p != NULL; item_p = item_p->next_p)
{
if (item_p->manager_p == manager_p)
{
return (manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p) : NULL;
}
}
item_p = jmem_heap_alloc_block (sizeof (jerry_context_data_header_t) + manager_p->bytes_needed);
item_p->manager_p = manager_p;
item_p->next_p = JERRY_CONTEXT (context_data_p);
JERRY_CONTEXT (context_data_p) = item_p;
if (manager_p->bytes_needed > 0)
{
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
memset (ret, 0, manager_p->bytes_needed);
}
if (manager_p->init_cb)
{
manager_p->init_cb (ret);
}
return ret;
} /* jerry_context_data */
/**
* Register external magic string array
*/
void
jerry_register_magic_strings (const jerry_char_t *const *ext_strings_p, /**< character arrays, representing
* external magic strings' contents */
uint32_t count, /**< number of the strings */
const jerry_length_t *str_lengths_p) /**< lengths of all strings */
{
jerry_assert_api_enabled ();
lit_magic_strings_ex_set ((const lit_utf8_byte_t *const *) ext_strings_p,
count,
(const lit_utf8_size_t *) str_lengths_p);
} /* jerry_register_magic_strings */
/**
* Run garbage collection
*/
void
jerry_heap_gc (jerry_gc_mode_t mode) /**< operational mode */
{
jerry_assert_api_enabled ();
if (mode == JERRY_GC_PRESSURE_LOW)
{
/* Call GC directly, because 'ecma_free_unused_memory' might decide it's not yet worth it. */
ecma_gc_run ();
return;
}
ecma_free_unused_memory (JMEM_PRESSURE_HIGH);
} /* jerry_heap_gc */
/**
* Get heap memory stats.
*
* @return true - get the heap stats successful
* false - otherwise. Usually it is because the MEM_STATS feature is not enabled.
*/
bool
jerry_heap_stats (jerry_heap_stats_t *out_stats_p) /**< [out] heap memory stats */
{
#if JERRY_MEM_STATS
if (out_stats_p == NULL)
{
return false;
}
jmem_heap_stats_t jmem_heap_stats;
memset (&jmem_heap_stats, 0, sizeof (jmem_heap_stats));
jmem_heap_get_stats (&jmem_heap_stats);
*out_stats_p = (jerry_heap_stats_t){ .version = 1,
.size = jmem_heap_stats.size,
.allocated_bytes = jmem_heap_stats.allocated_bytes,
.peak_allocated_bytes = jmem_heap_stats.peak_allocated_bytes };
return true;
#else /* !JERRY_MEM_STATS */
JERRY_UNUSED (out_stats_p);
return false;
#endif /* JERRY_MEM_STATS */
} /* jerry_heap_stats */
#if JERRY_PARSER
/**
* Common code for parsing a script, module, or function.
*
* @return function object value - if script was parsed successfully,
* thrown error - otherwise
*/
static jerry_value_t
jerry_parse_common (void *source_p, /**< script source */
const jerry_parse_options_t *options_p, /**< parsing options, can be NULL if not used */
uint32_t parse_opts) /**< internal parsing options */
{
jerry_assert_api_enabled ();
if (options_p != NULL)
{
const uint32_t allowed_options =
(JERRY_PARSE_STRICT_MODE | JERRY_PARSE_MODULE | JERRY_PARSE_HAS_ARGUMENT_LIST | JERRY_PARSE_HAS_SOURCE_NAME
| JERRY_PARSE_HAS_START | JERRY_PARSE_HAS_USER_VALUE);
uint32_t options = options_p->options;
if ((options & ~allowed_options) != 0
|| ((options_p->options & JERRY_PARSE_HAS_ARGUMENT_LIST)
&& ((options_p->options & JERRY_PARSE_MODULE) || !ecma_is_value_string (options_p->argument_list)))
|| ((options_p->options & JERRY_PARSE_HAS_SOURCE_NAME) && !ecma_is_value_string (options_p->source_name)))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_WRONG_ARGS_MSG));
}
}
#if JERRY_DEBUGGER
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) && options_p != NULL
&& (options_p->options & JERRY_PARSE_HAS_SOURCE_NAME) && ecma_is_value_string (options_p->source_name))
{
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name),
source_name_start_p,
source_name_size);
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME,
JERRY_DEBUGGER_NO_SUBTYPE,
source_name_start_p,
source_name_size);
ECMA_FINALIZE_UTF8_STRING (source_name_start_p, source_name_size);
}
#endif /* JERRY_DEBUGGER */
if (options_p != NULL)
{
parse_opts |= options_p->options & (JERRY_PARSE_STRICT_MODE | JERRY_PARSE_MODULE);
}
if ((parse_opts & JERRY_PARSE_MODULE) != 0)
{
#if JERRY_MODULE_SYSTEM
JERRY_CONTEXT (module_current_p) = ecma_module_create ();
#else /* !JERRY_MODULE_SYSTEM */
return jerry_throw_sz (JERRY_ERROR_SYNTAX, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
}
ecma_compiled_code_t *bytecode_data_p;
bytecode_data_p = parser_parse_script (source_p, parse_opts, options_p);
if (JERRY_UNLIKELY (bytecode_data_p == NULL))
{
#if JERRY_MODULE_SYSTEM
if ((parse_opts & JERRY_PARSE_MODULE) != 0)
{
ecma_module_cleanup_context ();
}
#endif /* JERRY_MODULE_SYSTEM */
return ecma_create_exception_from_context ();
}
#if JERRY_MODULE_SYSTEM
if (JERRY_UNLIKELY (parse_opts & JERRY_PARSE_MODULE))
{
ecma_module_t *module_p = JERRY_CONTEXT (module_current_p);
module_p->u.compiled_code_p = bytecode_data_p;
JERRY_CONTEXT (module_current_p) = NULL;
return ecma_make_object_value ((ecma_object_t *) module_p);
}
#endif /* JERRY_MODULE_SYSTEM */
if (JERRY_UNLIKELY (options_p != NULL && (options_p->options & JERRY_PARSE_HAS_ARGUMENT_LIST)))
{
ecma_object_t *global_object_p = ecma_builtin_get_global ();
#if JERRY_BUILTIN_REALMS
JERRY_ASSERT (global_object_p == (ecma_object_t *) ecma_op_function_get_realm (bytecode_data_p));
#endif /* JERRY_BUILTIN_REALMS */
ecma_object_t *lex_env_p = ecma_get_global_environment (global_object_p);
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_data_p);
ecma_bytecode_deref (bytecode_data_p);
return ecma_make_object_value (func_obj_p);
}
ecma_object_t *object_p = ecma_create_object (NULL, sizeof (ecma_extended_object_t), ECMA_OBJECT_TYPE_CLASS);
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.cls.type = ECMA_OBJECT_CLASS_SCRIPT;
ECMA_SET_INTERNAL_VALUE_POINTER (ext_object_p->u.cls.u3.value, bytecode_data_p);
return ecma_make_object_value (object_p);
} /* jerry_parse_common */
#endif /* JERRY_PARSER */
/**
* Parse a script, module, or function and create a compiled code using a character string
*
* @return function object value - if script was parsed successfully,
* thrown error - otherwise
*/
jerry_value_t
jerry_parse (const jerry_char_t *source_p, /**< script source */
size_t source_size, /**< script source size */
const jerry_parse_options_t *options_p) /**< parsing options, can be NULL if not used */
{
#if JERRY_PARSER
parser_source_char_t source_char;
source_char.source_p = source_p;
source_char.source_size = source_size;
return jerry_parse_common ((void *) &source_char, options_p, JERRY_PARSE_NO_OPTS);
#else /* !JERRY_PARSER */
JERRY_UNUSED (source_p);
JERRY_UNUSED (source_size);
JERRY_UNUSED (options_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, ecma_get_error_msg (ECMA_ERR_PARSER_NOT_SUPPORTED));
#endif /* JERRY_PARSER */
} /* jerry_parse */
/**
* Parse a script, module, or function and create a compiled code using a string value
*
* @return function object value - if script was parsed successfully,
* thrown error - otherwise
*/
jerry_value_t
jerry_parse_value (const jerry_value_t source, /**< script source */
const jerry_parse_options_t *options_p) /**< parsing options, can be NULL if not used */
{
#if JERRY_PARSER
if (!ecma_is_value_string (source))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_WRONG_ARGS_MSG));
}
return jerry_parse_common ((void *) &source, options_p, ECMA_PARSE_HAS_SOURCE_VALUE);
#else /* !JERRY_PARSER */
JERRY_UNUSED (source);
JERRY_UNUSED (options_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, ecma_get_error_msg (ECMA_ERR_PARSER_NOT_SUPPORTED));
#endif /* JERRY_PARSER */
} /* jerry_parse_value */
/**
* Run a Script or Module created by jerry_parse.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return result of bytecode - if run was successful
* thrown error - otherwise
*/
jerry_value_t
jerry_run (const jerry_value_t script) /**< script or module to run */
{
jerry_assert_api_enabled ();
if (!ecma_is_value_object (script))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_WRONG_ARGS_MSG));
}
ecma_object_t *object_p = ecma_get_object_from_value (script);
if (!ecma_object_class_is (object_p, ECMA_OBJECT_CLASS_SCRIPT))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_WRONG_ARGS_MSG));
}
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
const ecma_compiled_code_t *bytecode_data_p;
bytecode_data_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t, ext_object_p->u.cls.u3.value);
JERRY_ASSERT (CBC_FUNCTION_GET_TYPE (bytecode_data_p->status_flags) == CBC_FUNCTION_SCRIPT);
return jerry_return (vm_run_global (bytecode_data_p, object_p));
} /* jerry_run */
/**
* Perform eval
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return result of eval, may be error value.
*/
jerry_value_t
jerry_eval (const jerry_char_t *source_p, /**< source code */
size_t source_size, /**< length of source code */
uint32_t flags) /**< jerry_parse_opts_t flags */
{
jerry_assert_api_enabled ();
uint32_t allowed_parse_options = JERRY_PARSE_STRICT_MODE;
if ((flags & ~allowed_parse_options) != 0)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_WRONG_ARGS_MSG));
}
parser_source_char_t source_char;
source_char.source_p = source_p;
source_char.source_size = source_size;
return jerry_return (ecma_op_eval_chars_buffer ((void *) &source_char, flags));
} /* jerry_eval */
/**
* Link modules to their dependencies. The dependencies are resolved by a user callback.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return true - if linking is successful, error - otherwise
*/
jerry_value_t
jerry_module_link (const jerry_value_t module, /**< root module */
jerry_module_resolve_cb_t callback, /**< resolve module callback, uses
* jerry_module_resolve when NULL is passed */
void *user_p) /**< pointer passed to the resolve callback */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
if (callback == NULL)
{
callback = jerry_module_resolve;
}
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_NOT_MODULE));
}
return jerry_return (ecma_module_link (module_p, callback, user_p));
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
JERRY_UNUSED (callback);
JERRY_UNUSED (user_p);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_link */
/**
* Evaluate a module and its dependencies. The module must be in linked state.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return result of module bytecode execution - if evaluation was successful
* error - otherwise
*/
jerry_value_t
jerry_module_evaluate (const jerry_value_t module) /**< root module */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_NOT_MODULE));
}
if (module_p->header.u.cls.u1.module_state != JERRY_MODULE_STATE_LINKED)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_MUST_BE_IN_LINKED_STATE));
}
return jerry_return (ecma_module_evaluate (module_p));
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_evaluate */
/**
* Returns the current status of a module
*
* @return current status - if module is a module,
* JERRY_MODULE_STATE_INVALID - otherwise
*/
jerry_module_state_t
jerry_module_state (const jerry_value_t module) /**< module object */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return JERRY_MODULE_STATE_INVALID;
}
return (jerry_module_state_t) module_p->header.u.cls.u1.module_state;
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
return JERRY_MODULE_STATE_INVALID;
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_state */
/**
* Sets a callback which is called after a module state is changed to linked, evaluated, or error.
*/
void
jerry_module_on_state_changed (jerry_module_state_changed_cb_t callback, /**< callback */
void *user_p) /**< pointer passed to the callback */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
JERRY_CONTEXT (module_state_changed_callback_p) = callback;
JERRY_CONTEXT (module_state_changed_callback_user_p) = user_p;
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (callback);
JERRY_UNUSED (user_p);
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_on_state_changed */
/**
* Sets a callback which is called when an import.meta expression of a module is evaluated the first time.
*/
void
jerry_module_on_import_meta (jerry_module_import_meta_cb_t callback, /**< callback */
void *user_p) /**< pointer passed to the callback */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
JERRY_CONTEXT (module_import_meta_callback_p) = callback;
JERRY_CONTEXT (module_import_meta_callback_user_p) = user_p;
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (callback);
JERRY_UNUSED (user_p);
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_on_import_meta */
/**
* Returns the number of import/export requests of a module
*
* @return number of import/export requests of a module
*/
size_t
jerry_module_request_count (const jerry_value_t module) /**< module */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return 0;
}
size_t number_of_requests = 0;
ecma_module_node_t *node_p = module_p->imports_p;
while (node_p != NULL)
{
number_of_requests++;
node_p = node_p->next_p;
}
return number_of_requests;
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
return 0;
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_request_count */
/**
* Returns the module request specified by the request_index argument
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return string - if the request has not been resolved yet,
* module object - if the request has been resolved successfully,
* error - otherwise
*/
jerry_value_t
jerry_module_request (const jerry_value_t module, /**< module */
size_t request_index) /**< request index */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_NOT_MODULE));
}
ecma_module_node_t *node_p = module_p->imports_p;
while (node_p != NULL)
{
if (request_index == 0)
{
return ecma_copy_value (node_p->u.path_or_module);
}
--request_index;
node_p = node_p->next_p;
}
return jerry_throw_sz (JERRY_ERROR_RANGE, ecma_get_error_msg (ECMA_ERR_REQUEST_IS_NOT_AVAILABLE));
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
JERRY_UNUSED (request_index);
return jerry_throw_sz (JERRY_ERROR_RANGE, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_request */
/**
* Returns the namespace object of a module
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return object - if namespace object is available,
* error - otherwise
*/
jerry_value_t
jerry_module_namespace (const jerry_value_t module) /**< module */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (module);
if (module_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_NOT_MODULE));
}
if (module_p->header.u.cls.u1.module_state < JERRY_MODULE_STATE_LINKED
|| module_p->header.u.cls.u1.module_state > JERRY_MODULE_STATE_EVALUATED)
{
return jerry_throw_sz (JERRY_ERROR_RANGE, ecma_get_error_msg (ECMA_ERR_NAMESPACE_OBJECT_IS_NOT_AVAILABLE));
}
JERRY_ASSERT (module_p->namespace_object_p != NULL);
ecma_ref_object (module_p->namespace_object_p);
return ecma_make_object_value (module_p->namespace_object_p);
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (module);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_namespace */
/**
* Sets the callback which is called when dynamic imports are resolved
*/
void
jerry_module_on_import (jerry_module_import_cb_t callback_p, /**< callback which handles
* dynamic import calls */
void *user_p) /**< user pointer passed to the callback */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
JERRY_CONTEXT (module_import_callback_p) = callback_p;
JERRY_CONTEXT (module_import_callback_user_p) = user_p;
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (callback_p);
JERRY_UNUSED (user_p);
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_module_on_import */
/**
* Creates a native module with a list of exports. The initial state of the module is linked.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return native module - if the module is successfully created,
* error - otherwise
*/
jerry_value_t
jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation callback for
* native modules */
const jerry_value_t *const exports_p, /**< list of the exported bindings of the module,
* must be valid string identifiers */
size_t export_count) /**< number of exports in the exports_p list */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_object_t *global_object_p = ecma_builtin_get_global ();
ecma_object_t *scope_p = ecma_create_decl_lex_env (ecma_get_global_environment (global_object_p));
ecma_module_names_t *local_exports_p = NULL;
for (size_t i = 0; i < export_count; i++)
{
if (!ecma_is_value_string (exports_p[i]))
{
ecma_deref_object (scope_p);
ecma_module_release_module_names (local_exports_p);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_EXPORTS_MUST_BE_STRING_VALUES));
}
ecma_string_t *name_str_p = ecma_get_string_from_value (exports_p[i]);
bool valid_identifier = false;
ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start_p, name_size);
if (name_size > 0)
{
const lit_utf8_byte_t *name_p = name_start_p;
const lit_utf8_byte_t *name_end_p = name_start_p + name_size;
lit_code_point_t code_point;
lit_utf8_size_t size = lit_read_code_point_from_cesu8 (name_p, name_end_p, &code_point);
if (lit_code_point_is_identifier_start (code_point))
{
name_p += size;
valid_identifier = true;
while (name_p < name_end_p)
{
size = lit_read_code_point_from_cesu8 (name_p, name_end_p, &code_point);
if (!lit_code_point_is_identifier_part (code_point))
{
valid_identifier = false;
break;
}
name_p += size;
}
}
}
ECMA_FINALIZE_UTF8_STRING (name_start_p, name_size);
if (!valid_identifier)
{
ecma_deref_object (scope_p);
ecma_module_release_module_names (local_exports_p);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_EXPORTS_MUST_BE_VALID_IDENTIFIERS));
}
if (ecma_find_named_property (scope_p, name_str_p) != NULL)
{
continue;
}
ecma_create_named_data_property (scope_p, name_str_p, ECMA_PROPERTY_FLAG_WRITABLE, NULL);
ecma_module_names_t *new_export_p;
new_export_p = (ecma_module_names_t *) jmem_heap_alloc_block (sizeof (ecma_module_names_t));
new_export_p->next_p = local_exports_p;
local_exports_p = new_export_p;
ecma_ref_ecma_string (name_str_p);
new_export_p->imex_name_p = name_str_p;
ecma_ref_ecma_string (name_str_p);
new_export_p->local_name_p = name_str_p;
}
ecma_module_t *module_p = ecma_module_create ();
module_p->header.u.cls.u2.module_flags |= ECMA_MODULE_IS_NATIVE;
module_p->scope_p = scope_p;
module_p->local_exports_p = local_exports_p;
module_p->u.callback = callback;
ecma_deref_object (scope_p);
return ecma_make_object_value (&module_p->header.object);
#else /* !JERRY_MODULE_SYSTEM */
JERRY_UNUSED (callback);
JERRY_UNUSED (exports_p);
JERRY_UNUSED (export_count);
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_MODULE_NOT_SUPPORTED));
#endif /* JERRY_MODULE_SYSTEM */
} /* jerry_native_module */
/**
* Gets the value of an export which belongs to a native module.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return value of the export - if success
* error - otherwise
*/
jerry_value_t
jerry_native_module_get (const jerry_value_t native_module, /**< a native module object */
const jerry_value_t export_name) /**< string identifier of the export */
{
jerry_assert_api_enabled ();
#if JERRY_MODULE_SYSTEM
ecma_module_t *module_p = ecma_module_get_resolved_module (native_module);
if (module_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_NOT_MODULE));
}