forked from cesanta/v7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v7.h
1572 lines (1335 loc) · 43.4 KB
/
v7.h
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
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/license.h"
#endif
/*
* Copyright (c) 2013-2014 Cesanta Software Limited
* All rights reserved
*
* This software is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively, you can license this software under a commercial
* license, as set out in <https://www.cesanta.com/license>.
*/
#ifdef V7_EXPOSE_PRIVATE
#define V7_PRIVATE
#define V7_EXTERN extern
#else
#define V7_PRIVATE static
#define V7_EXTERN static
#endif /* CS_V7_SRC_LICENSE_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/features_profiles.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_V7_SRC_FEATURES_PROFILES_H_
#define CS_V7_SRC_FEATURES_PROFILES_H_
#define V7_BUILD_PROFILE_MINIMAL 1
#define V7_BUILD_PROFILE_MEDIUM 2
#define V7_BUILD_PROFILE_FULL 3
#ifndef V7_BUILD_PROFILE
#define V7_BUILD_PROFILE V7_BUILD_PROFILE_FULL
#endif
#endif /* CS_V7_SRC_FEATURES_PROFILES_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/features_minimal.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/* Amalgamated: #include "v7/src/features_profiles.h" */
#if V7_BUILD_PROFILE == V7_BUILD_PROFILE_MINIMAL
/* This space is intentionally left blank. */
#endif /* CS_V7_SRC_FEATURES_MINIMAL_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/features_medium.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/* Amalgamated: #include "v7/src/features_profiles.h" */
#if V7_BUILD_PROFILE == V7_BUILD_PROFILE_MEDIUM
#define V7_ENABLE__Date 1
#define V7_ENABLE__Date__now 1
#define V7_ENABLE__Date__UTC 1
#define V7_ENABLE__Math 1
#define V7_ENABLE__Math__atan2 1
#define V7_ENABLE__RegExp 1
#endif /* CS_V7_SRC_FEATURES_MEDIUM_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/features_full.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/* Amalgamated: #include "v7/src/features_profiles.h" */
#if V7_BUILD_PROFILE == V7_BUILD_PROFILE_FULL
/*
* DO NOT EDIT.
* This file is generated by scripts/gen-features-full.pl.
*/
#ifndef CS_ENABLE_UTF8
#define CS_ENABLE_UTF8 1
#endif
#define V7_ENABLE__Array__reduce 1
#define V7_ENABLE__Blob 1
#define V7_ENABLE__Date 1
#define V7_ENABLE__Date__UTC 1
#define V7_ENABLE__Date__getters 1
#define V7_ENABLE__Date__now 1
#define V7_ENABLE__Date__parse 1
#define V7_ENABLE__Date__setters 1
#define V7_ENABLE__Date__toJSON 1
#define V7_ENABLE__Date__toLocaleString 1
#define V7_ENABLE__Date__toString 1
#define V7_ENABLE__File__list 1
#define V7_ENABLE__File__require 1
#define V7_ENABLE__Function__bind 1
#define V7_ENABLE__Function__call 1
#define V7_ENABLE__Math 1
#define V7_ENABLE__Math__abs 1
#define V7_ENABLE__Math__acos 1
#define V7_ENABLE__Math__asin 1
#define V7_ENABLE__Math__atan 1
#define V7_ENABLE__Math__atan2 1
#define V7_ENABLE__Math__ceil 1
#define V7_ENABLE__Math__constants 1
#define V7_ENABLE__Math__cos 1
#define V7_ENABLE__Math__exp 1
#define V7_ENABLE__Math__floor 1
#define V7_ENABLE__Math__log 1
#define V7_ENABLE__Math__max 1
#define V7_ENABLE__Math__min 1
#define V7_ENABLE__Math__pow 1
#define V7_ENABLE__Math__random 1
#define V7_ENABLE__Math__round 1
#define V7_ENABLE__Math__sin 1
#define V7_ENABLE__Math__sqrt 1
#define V7_ENABLE__Math__tan 1
#define V7_ENABLE__Memory__stats 1
#define V7_ENABLE__NUMBER__NEGATIVE_INFINITY 1
#define V7_ENABLE__NUMBER__POSITIVE_INFINITY 1
#define V7_ENABLE__Object__create 1
#define V7_ENABLE__Object__defineProperties 1
#define V7_ENABLE__Object__getOwnPropertyDescriptor 1
#define V7_ENABLE__Object__getOwnPropertyNames 1
#define V7_ENABLE__Object__getPrototypeOf 1
#define V7_ENABLE__Object__hasOwnProperty 1
#define V7_ENABLE__Object__isExtensible 1
#define V7_ENABLE__Object__isFrozen 1
#define V7_ENABLE__Object__isPrototypeOf 1
#define V7_ENABLE__Object__isSealed 1
#define V7_ENABLE__Object__keys 1
#define V7_ENABLE__Object__preventExtensions 1
#define V7_ENABLE__Object__propertyIsEnumerable 1
#define V7_ENABLE__RegExp 1
#define V7_ENABLE__StackTrace 1
#define V7_ENABLE__String__localeCompare 1
#define V7_ENABLE__String__localeLowerCase 1
#define V7_ENABLE__String__localeUpperCase 1
#endif /* V7_BUILD_PROFILE == V7_BUILD_PROFILE_FULL */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/v7_features.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_V7_SRC_V7_FEATURES_H_
#define CS_V7_SRC_V7_FEATURES_H_
/* Only one will actually be used based on V7_BUILD_PROFILE. */
/* Amalgamated: #include "v7/src/features_minimal.h" */
/* Amalgamated: #include "v7/src/features_medium.h" */
/* Amalgamated: #include "v7/src/features_full.h" */
#endif /* CS_V7_SRC_V7_FEATURES_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./v7/src/core_public.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === Core
*/
#ifndef CS_V7_SRC_CORE_PUBLIC_H_
#define CS_V7_SRC_CORE_PUBLIC_H_
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
/* Amalgamated: #include "v7/src/license.h" */
/* Amalgamated: #include "v7/src/v7_features.h" */
#include <stddef.h> /* For size_t */
#include <stdio.h> /* For FILE */
/*
* TODO(dfrank) : improve amalgamation, so that we'll be able to include
* files here, and include common/platform.h
*
* For now, copy-pasting `WARN_UNUSED_RESULT` here
*/
#ifdef __GNUC__
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WARN_UNUSED_RESULT
#endif
#define V7_VERSION "1.0"
#if (defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)) || \
(defined(_MSC_VER) && _MSC_VER <= 1200)
#define V7_WINDOWS
#endif
#ifdef V7_WINDOWS
typedef unsigned __int64 uint64_t;
#else
#include <inttypes.h>
#endif
/* 64-bit value, used to store JS values */
typedef uint64_t v7_val_t;
/* This if-0 is a dirty workaround to force etags to pick `struct v7` */
#if 0
/* Opaque structure. V7 engine context. */
struct v7 {
/* ... */
};
#endif
struct v7;
/*
* Code which is returned by some of the v7 functions. If something other than
* `V7_OK` is returned from some function, the caller function typically should
* either immediately cleanup and return the code further, or handle the error.
*/
enum v7_err {
V7_OK,
V7_SYNTAX_ERROR,
V7_EXEC_EXCEPTION,
V7_AST_TOO_LARGE,
V7_INTERNAL_ERROR,
};
/* JavaScript -> C call interface */
WARN_UNUSED_RESULT
typedef enum v7_err(v7_cfunction_t)(struct v7 *v7, v7_val_t *res);
/* Create V7 instance */
struct v7 *v7_create(void);
/*
* Customizations of initial V7 state; used by `v7_create_opt()`.
*/
struct v7_create_opts {
size_t object_arena_size;
size_t function_arena_size;
size_t property_arena_size;
#ifdef V7_STACK_SIZE
void *c_stack_base;
#endif
#ifdef V7_FREEZE
/* if not NULL, dump JS heap after init */
char *freeze_file;
#endif
};
/*
* Like `v7_create()`, but allows to customize initial v7 state, see `struct
* v7_create_opts`.
*/
struct v7 *v7_create_opt(struct v7_create_opts opts);
/* Destroy V7 instance */
void v7_destroy(struct v7 *v7);
/* Return root level (`global`) object of the given V7 instance. */
v7_val_t v7_get_global(struct v7 *v);
/* Return current `this` object. */
v7_val_t v7_get_this(struct v7 *v);
/* Return current `arguments` array */
v7_val_t v7_get_arguments(struct v7 *v);
/* Return i-th argument */
v7_val_t v7_arg(struct v7 *v, unsigned long i);
/* Return the length of `arguments` */
unsigned long v7_argc(struct v7 *v7);
/*
* Tells the GC about a JS value variable/field owned
* by C code.
*
* User C code should own v7_val_t variables
* if the value's lifetime crosses any invocation
* to the v7 runtime that creates new objects or new
* properties and thus can potentially trigger GC.
*
* The registration of the variable prevents the GC from mistakenly treat
* the object as garbage. The GC might be triggered potentially
* allows the GC to update pointers
*
* User code should also explicitly disown the variables with v7_disown once
* it goes out of scope or the structure containing the v7_val_t field is freed.
*
* Example:
*
* ```
* struct v7_val cb;
* v7_own(v7, &cb);
* cb = v7_array_get(v7, args, 0);
* // do something with cb
* v7_disown(v7, &cb);
* ```
*/
void v7_own(struct v7 *v7, v7_val_t *v);
/*
* Returns 1 if value is found, 0 otherwise
*/
int v7_disown(struct v7 *v7, v7_val_t *v);
/*
* Enable or disable GC.
*
* Must be called before invoking v7_exec or v7_apply
* from within a cfunction unless you know what you're doing.
*
* GC is disabled during execution of cfunctions in order to simplify
* memory management of simple cfunctions.
* However executing even small snippets of JS code causes a lot of memory
* pressure. Enabling GC solves that but forces you to take care of the
* reachability of your temporary V7 v7_val_t variables, as the GC needs
* to know where they are since objects and strings can be either reclaimed
* or relocated during a GC pass.
*/
void v7_set_gc_enabled(struct v7 *v7, int enabled);
/*
* Set an optional C stack limit.
*
* It sets a flag that will cause the interpreter
* to throw an InterruptedError.
* It's safe to call it from signal handlers and ISRs
* on single threaded environments.
*/
void v7_interrupt(struct v7 *v7);
/* Returns last parser error message. TODO: rename it to `v7_get_error()` */
const char *v7_get_parser_error(struct v7 *v7);
#if defined(V7_ENABLE_STACK_TRACKING)
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Stack metric id. See `v7_stack_stat()`
*/
enum v7_stack_stat_what {
/* max stack size consumed by `i_exec()` */
V7_STACK_STAT_EXEC,
/* max stack size consumed by `parse()` (which is called from `i_exec()`) */
V7_STACK_STAT_PARSER,
V7_STACK_STATS_CNT
};
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Returns stack metric specified by the metric id `what`. See
* `v7_stack_stat_clean()`
*/
int v7_stack_stat(struct v7 *v7, enum v7_stack_stat_what what);
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Clean all stack statistics gathered so far. See `v7_stack_stat()`
*/
void v7_stack_stat_clean(struct v7 *v7);
#endif
#endif /* CS_V7_SRC_CORE_PUBLIC_H_ */
#ifndef V7_EXPORT_INTERNAL_HEADERS
#ifdef V7_MODULE_LINES
#line 1 "./src/core_public.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === Core
*/
#ifndef CS_V7_SRC_CORE_PUBLIC_H_
#define CS_V7_SRC_CORE_PUBLIC_H_
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
/* Amalgamated: #include "v7/src/license.h" */
/* Amalgamated: #include "v7/src/v7_features.h" */
#include <stddef.h> /* For size_t */
#include <stdio.h> /* For FILE */
/*
* TODO(dfrank) : improve amalgamation, so that we'll be able to include
* files here, and include common/platform.h
*
* For now, copy-pasting `WARN_UNUSED_RESULT` here
*/
#ifdef __GNUC__
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WARN_UNUSED_RESULT
#endif
#define V7_VERSION "1.0"
#if (defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)) || \
(defined(_MSC_VER) && _MSC_VER <= 1200)
#define V7_WINDOWS
#endif
#ifdef V7_WINDOWS
typedef unsigned __int64 uint64_t;
#else
#include <inttypes.h>
#endif
/* 64-bit value, used to store JS values */
typedef uint64_t v7_val_t;
/* This if-0 is a dirty workaround to force etags to pick `struct v7` */
#if 0
/* Opaque structure. V7 engine context. */
struct v7 {
/* ... */
};
#endif
struct v7;
/*
* Code which is returned by some of the v7 functions. If something other than
* `V7_OK` is returned from some function, the caller function typically should
* either immediately cleanup and return the code further, or handle the error.
*/
enum v7_err {
V7_OK,
V7_SYNTAX_ERROR,
V7_EXEC_EXCEPTION,
V7_AST_TOO_LARGE,
V7_INTERNAL_ERROR,
};
/* JavaScript -> C call interface */
WARN_UNUSED_RESULT
typedef enum v7_err(v7_cfunction_t)(struct v7 *v7, v7_val_t *res);
/* Create V7 instance */
struct v7 *v7_create(void);
/*
* Customizations of initial V7 state; used by `v7_create_opt()`.
*/
struct v7_create_opts {
size_t object_arena_size;
size_t function_arena_size;
size_t property_arena_size;
#ifdef V7_STACK_SIZE
void *c_stack_base;
#endif
#ifdef V7_FREEZE
/* if not NULL, dump JS heap after init */
char *freeze_file;
#endif
};
/*
* Like `v7_create()`, but allows to customize initial v7 state, see `struct
* v7_create_opts`.
*/
struct v7 *v7_create_opt(struct v7_create_opts opts);
/* Destroy V7 instance */
void v7_destroy(struct v7 *v7);
/* Return root level (`global`) object of the given V7 instance. */
v7_val_t v7_get_global(struct v7 *v);
/* Return current `this` object. */
v7_val_t v7_get_this(struct v7 *v);
/* Return current `arguments` array */
v7_val_t v7_get_arguments(struct v7 *v);
/* Return i-th argument */
v7_val_t v7_arg(struct v7 *v, unsigned long i);
/* Return the length of `arguments` */
unsigned long v7_argc(struct v7 *v7);
/*
* Tells the GC about a JS value variable/field owned
* by C code.
*
* User C code should own v7_val_t variables
* if the value's lifetime crosses any invocation
* to the v7 runtime that creates new objects or new
* properties and thus can potentially trigger GC.
*
* The registration of the variable prevents the GC from mistakenly treat
* the object as garbage. The GC might be triggered potentially
* allows the GC to update pointers
*
* User code should also explicitly disown the variables with v7_disown once
* it goes out of scope or the structure containing the v7_val_t field is freed.
*
* Example:
*
* ```
* struct v7_val cb;
* v7_own(v7, &cb);
* cb = v7_array_get(v7, args, 0);
* // do something with cb
* v7_disown(v7, &cb);
* ```
*/
void v7_own(struct v7 *v7, v7_val_t *v);
/*
* Returns 1 if value is found, 0 otherwise
*/
int v7_disown(struct v7 *v7, v7_val_t *v);
/*
* Enable or disable GC.
*
* Must be called before invoking v7_exec or v7_apply
* from within a cfunction unless you know what you're doing.
*
* GC is disabled during execution of cfunctions in order to simplify
* memory management of simple cfunctions.
* However executing even small snippets of JS code causes a lot of memory
* pressure. Enabling GC solves that but forces you to take care of the
* reachability of your temporary V7 v7_val_t variables, as the GC needs
* to know where they are since objects and strings can be either reclaimed
* or relocated during a GC pass.
*/
void v7_set_gc_enabled(struct v7 *v7, int enabled);
/*
* Set an optional C stack limit.
*
* It sets a flag that will cause the interpreter
* to throw an InterruptedError.
* It's safe to call it from signal handlers and ISRs
* on single threaded environments.
*/
void v7_interrupt(struct v7 *v7);
/* Returns last parser error message. TODO: rename it to `v7_get_error()` */
const char *v7_get_parser_error(struct v7 *v7);
#if defined(V7_ENABLE_STACK_TRACKING)
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Stack metric id. See `v7_stack_stat()`
*/
enum v7_stack_stat_what {
/* max stack size consumed by `i_exec()` */
V7_STACK_STAT_EXEC,
/* max stack size consumed by `parse()` (which is called from `i_exec()`) */
V7_STACK_STAT_PARSER,
V7_STACK_STATS_CNT
};
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Returns stack metric specified by the metric id `what`. See
* `v7_stack_stat_clean()`
*/
int v7_stack_stat(struct v7 *v7, enum v7_stack_stat_what what);
/*
* Available if only `V7_ENABLE_STACK_TRACKING` is defined.
*
* Clean all stack statistics gathered so far. See `v7_stack_stat()`
*/
void v7_stack_stat_clean(struct v7 *v7);
#endif
#endif /* CS_V7_SRC_CORE_PUBLIC_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./src/primitive_public.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === Primitives
*
* All primitive values but strings.
*
* "foreign" values are also here, see `v7_mk_foreign()`.
*/
#ifndef CS_V7_SRC_PRIMITIVE_PUBLIC_H_
#define CS_V7_SRC_PRIMITIVE_PUBLIC_H_
/* Amalgamated: #include "v7/src/core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/* Make numeric primitive value */
v7_val_t v7_mk_number(double num);
/*
* Returns `double` value stored in `v7_val_t`.
*
* Returns NaN for non-numbers.
*/
double v7_get_double(v7_val_t v);
/* Returns true if given value is a primitive number value */
int v7_is_number(v7_val_t v);
/* Make boolean primitive value (either `true` or `false`) */
v7_val_t v7_mk_boolean(int is_true);
/*
* Returns boolean stored in `v7_val_t`:
* 0 for `false` or non-boolean, non-0 for `true`
*/
int v7_get_bool(v7_val_t v);
/* Returns true if given value is a primitive boolean value */
int v7_is_boolean(v7_val_t v);
/* Make `null` primitive value */
v7_val_t v7_mk_null(void);
/* Returns true if given value is a primitive `null` value */
int v7_is_null(v7_val_t v);
/* Make `undefined` primitive value */
v7_val_t v7_mk_undefined(void);
/* Returns true if given value is a primitive `undefined` value */
int v7_is_undefined(v7_val_t v);
/*
* Make JavaScript value that holds C/C++ `void *` pointer.
*
* A foreign value is completely opaque and JS code cannot do anything useful
* with it except holding it in properties and passing it around.
* It behaves like a sealed object with no properties.
*
* NOTE:
* Only valid pointers (as defined by each supported architecture) will fully
* preserved. In particular, all supported 64-bit architectures (x86_64, ARM-64)
* actually define a 48-bit virtual address space.
* Foreign values will be sign-extended as required, i.e creating a foreign
* value of something like `(void *) -1` will work as expected. This is
* important because in some 64-bit OSs (e.g. Solaris) the user stack grows
* downwards from the end of the address space.
*
* If you need to store exactly sizeof(void*) bytes of raw data where
* `sizeof(void*)` >= 8, please use byte arrays instead.
*/
v7_val_t v7_mk_foreign(void *ptr);
/*
* Returns `void *` pointer stored in `v7_val_t`.
*
* Returns NULL if the value is not a foreign pointer.
*/
void *v7_get_ptr(v7_val_t v);
/* Returns true if given value holds `void *` pointer */
int v7_is_foreign(v7_val_t v);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* CS_V7_SRC_PRIMITIVE_PUBLIC_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./src/string_public.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === Strings
*/
#ifndef CS_V7_SRC_STRING_PUBLIC_H_
#define CS_V7_SRC_STRING_PUBLIC_H_
/* Amalgamated: #include "v7/src/core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*
* Creates a string primitive value.
* `str` must point to the utf8 string of length `len`.
* If `len` is ~0, `str` is assumed to be NUL-terminated and `strlen(str)` is
* used.
*
* If `copy` is non-zero, the string data is copied and owned by the GC. The
* caller can free the string data afterwards. Otherwise (`copy` is zero), the
* caller owns the string data, and is responsible for not freeing it while it
* is used.
*/
v7_val_t v7_mk_string(struct v7 *v7, const char *str, size_t len, int copy);
/* Returns true if given value is a primitive string value */
int v7_is_string(v7_val_t v);
/*
* Returns a pointer to the string stored in `v7_val_t`.
*
* String length returned in `len`, which is allowed to be NULL. Returns NULL
* if the value is not a string.
*
* JS strings can contain embedded NUL chars and may or may not be NUL
* terminated.
*
* CAUTION: creating new JavaScript object, array, or string may kick in a
* garbage collector, which in turn may relocate string data and invalidate
* pointer returned by `v7_get_string()`.
*
* Short JS strings are embedded inside the `v7_val_t` value itself. This is why
* a pointer to a `v7_val_t` is required. It also means that the string data
* will become invalid once that `v7_val_t` value goes out of scope.
*/
const char *v7_get_string(struct v7 *v7, v7_val_t *v, size_t *len);
/*
* Returns a pointer to the string stored in `v7_val_t`.
*
* Returns NULL if the value is not a string or if the string is not compatible
* with a C string.
*
* C compatible strings contain exactly one NUL char, in terminal position.
*
* All strings owned by the V7 engine (see `v7_mk_string()`) are guaranteed to
* be NUL terminated. Out of these, those that don't include embedded NUL chars
* are guaranteed to be C compatible.
*/
const char *v7_get_cstring(struct v7 *v7, v7_val_t *v);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* CS_V7_SRC_STRING_PUBLIC_H_ */
#ifdef V7_MODULE_LINES
#line 1 "./src/object_public.h"
#endif
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === Objects
*/
#ifndef CS_V7_SRC_OBJECT_PUBLIC_H_
#define CS_V7_SRC_OBJECT_PUBLIC_H_
/* Amalgamated: #include "v7/src/core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*
* Property attributes bitmask
*/
typedef unsigned short v7_prop_attr_t;
#define V7_PROPERTY_NON_WRITABLE (1 << 0)
#define V7_PROPERTY_NON_ENUMERABLE (1 << 1)
#define V7_PROPERTY_NON_CONFIGURABLE (1 << 2)
#define V7_PROPERTY_GETTER (1 << 3)
#define V7_PROPERTY_SETTER (1 << 4)
#define _V7_PROPERTY_HIDDEN (1 << 5)
/* property not managed by V7 HEAP */
#define _V7_PROPERTY_OFF_HEAP (1 << 6)
/* special property holding user data and destructor cb */
#define _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR (1 << 7)
/*
* not a property attribute, but a flag for `v7_def()`. It's here in order to
* keep all offsets in one place
*/
#define _V7_DESC_PRESERVE_VALUE (1 << 8)
/*
* Internal helpers for `V7_DESC_...` macros
*/
#define _V7_DESC_SHIFT 16
#define _V7_DESC_MASK ((1 << _V7_DESC_SHIFT) - 1)
#define _V7_MK_DESC(v, n) \
(((v7_prop_attr_desc_t)(n)) << _V7_DESC_SHIFT | ((v) ? (n) : 0))
#define _V7_MK_DESC_INV(v, n) _V7_MK_DESC(!(v), (n))
/*
* Property attribute descriptors that may be given to `v7_def()`: for each
* attribute (`v7_prop_attr_t`), there is a corresponding macro, which takes
* param: either 1 (set attribute) or 0 (clear attribute). If some particular
* attribute isn't mentioned at all, it's left unchanged (or default, if the
* property is being created)
*
* There is additional flag: `V7_DESC_PRESERVE_VALUE`. If it is set, the
* property value isn't changed (or set to `undefined` if the property is being
* created)
*/
typedef unsigned long v7_prop_attr_desc_t;
#define V7_DESC_WRITABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_WRITABLE)
#define V7_DESC_ENUMERABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_ENUMERABLE)
#define V7_DESC_CONFIGURABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_CONFIGURABLE)
#define V7_DESC_GETTER(v) _V7_MK_DESC(v, V7_PROPERTY_GETTER)
#define V7_DESC_SETTER(v) _V7_MK_DESC(v, V7_PROPERTY_SETTER)
#define V7_DESC_PRESERVE_VALUE _V7_DESC_PRESERVE_VALUE
#define _V7_DESC_HIDDEN(v) _V7_MK_DESC(v, _V7_PROPERTY_HIDDEN)
#define _V7_DESC_OFF_HEAP(v) _V7_MK_DESC(v, _V7_PROPERTY_OFF_HEAP)
/* See `v7_set_destructor_cb` */
typedef void(v7_destructor_cb_t)(void *ud);
/* Make an empty object */
v7_val_t v7_mk_object(struct v7 *v7);
/*
* Returns true if the given value is an object or function.
* i.e. it returns true if the value holds properties and can be
* used as argument to `v7_get`, `v7_set` and `v7_def`.
*/
int v7_is_object(v7_val_t v);
/* Set object's prototype. Return old prototype or undefined on error. */
v7_val_t v7_set_proto(struct v7 *v7, v7_val_t obj, v7_val_t proto);
/*
* Lookup property `name` in object `obj`. If `obj` holds no such property,
* an `undefined` value is returned.
*
* If `name_len` is ~0, `name` is assumed to be NUL-terminated and
* `strlen(name)` is used.
*/
v7_val_t v7_get(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len);
/*
* Like `v7_get()`, but "returns" value through `res` pointer argument.
* `res` must not be `NULL`.
*
* Caller should check the error code returned, and if it's something other
* than `V7_OK`, perform cleanup and return this code further.
*/
WARN_UNUSED_RESULT
enum v7_err v7_get_throwing(struct v7 *v7, v7_val_t obj, const char *name,
size_t name_len, v7_val_t *res);
/*
* Define object property, similar to JavaScript `Object.defineProperty()`.
*
* `name`, `name_len` specify property name, `val` is a property value.
* `attrs_desc` is a set of flags which can affect property's attributes,
* see comment of `v7_prop_attr_desc_t` for details.
*
* If `name_len` is ~0, `name` is assumed to be NUL-terminated and
* `strlen(name)` is used.
*
* Returns non-zero on success, 0 on error (e.g. out of memory).
*
* See also `v7_set()`.
*/
int v7_def(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len,
v7_prop_attr_desc_t attrs_desc, v7_val_t v);
/*
* Set object property. Behaves just like JavaScript assignment.
*
* See also `v7_def()`.
*/
int v7_set(struct v7 *v7, v7_val_t obj, const char *name, size_t len,
v7_val_t val);
/*
* A helper function to define object's method backed by a C function `func`.
* `name` must be NUL-terminated.
*
* Return value is the same as for `v7_set()`.
*/
int v7_set_method(struct v7 *, v7_val_t obj, const char *name,
v7_cfunction_t *func);
/*
* Delete own property `name` of the object `obj`. Does not follow the
* prototype chain.
*
* If `name_len` is ~0, `name` is assumed to be NUL-terminated and
* `strlen(name)` is used.
*
* Returns 0 on success, -1 on error.
*/
int v7_del(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len);
/*
* Iterate over the `obj`'s properties.
*
* Usage example:
*
* void *h = NULL;
* v7_val_t name, val;
* v7_prop_attr_t attrs;
* while ((h = v7_next_prop(h, obj, &name, &val, &attrs)) != NULL) {
* ...
* }
*/
void *v7_next_prop(void *handle, v7_val_t obj, v7_val_t *name, v7_val_t *value,
v7_prop_attr_t *attrs);
/* Returns true if the object is an instance of a given constructor. */
int v7_is_instanceof(struct v7 *v7, v7_val_t o, const char *c);
/* Returns true if the object is an instance of a given constructor. */
int v7_is_instanceof_v(struct v7 *v7, v7_val_t o, v7_val_t c);
/*
* Associates an opaque C value (anything that can be casted to a `void * )
* with an object.
*
* You can achieve a similar effect by just setting a special property with
* a foreign value (see `v7_mk_foreign`), except user data offers the following
* advantages:
*
* 1. You don't have to come up with some arbitrary "special" property name.
* 2. JS scripts cannot access user data by mistake via property lookup.
* 3. The user data is available to the destructor. When the desctructor is
* invoked you cannot access any of its properties.
* 4. Allows the implementation to use a more compact encoding
*
* Does nothing if `obj` is not a mutable object.
*/
void v7_set_user_data(struct v7 *v7, v7_val_t obj, void *ud);
/*
* Get the opaque user data set with `v7_set_user_data`.
*
* Returns NULL if there is no user data set or if `obj` is not an object.
*/
void *v7_get_user_data(struct v7 *v7, v7_val_t obj);
/*
* Register a callback which will be invoked when a given object gets
* reclaimed by the garbage collector.
*
* The callback will be invoked while garbage collection is still in progress
* and hence the internal state of the JS heap is in an undefined state.
* The callback thus cannot perform any calls to the V7 API and will receive
* only the user data associated with the destructed object.
*
* The intended use case is to reclaim resources allocated by C code.
*/
void v7_set_destructor_cb(struct v7 *v7, v7_val_t obj, v7_destructor_cb_t *d);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* CS_V7_SRC_OBJECT_PUBLIC_H_ */