-
Notifications
You must be signed in to change notification settings - Fork 176
/
mjs.h
1059 lines (893 loc) · 28.7 KB
/
mjs.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
/*
* Copyright (c) 2017 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 MJS_MODULE_LINES
#line 1 "src/mjs_features.h"
#endif
#ifndef MJS_FEATURES_H_
#define MJS_FEATURES_H_
#if !defined(MJS_AGGRESSIVE_GC)
#define MJS_AGGRESSIVE_GC 0
#endif
#if !defined(MJS_MEMORY_STATS)
#define MJS_MEMORY_STATS 0
#endif
/*
* MJS_GENERATE_JSC: if enabled, and if mmapping is also enabled (CS_MMAP),
* then execution of any .js file will result in creation of a .jsc file with
* precompiled bcode, and this .jsc file will be mmapped, instead of keeping
* bcode in RAM.
*
* By default it's enabled (provided that CS_MMAP is defined)
*/
#if !defined(MJS_GENERATE_JSC)
#if defined(CS_MMAP)
#define MJS_GENERATE_JSC 1
#else
#define MJS_GENERATE_JSC 0
#endif
#endif
#endif /* MJS_FEATURES_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_core_public.h"
#endif
#ifndef MJS_CORE_PUBLIC_H_
#define MJS_CORE_PUBLIC_H_
#if !defined(_MSC_VER) || _MSC_VER >= 1700
#include <stdint.h>
#else
typedef unsigned __int64 uint64_t;
typedef int int32_t;
typedef unsigned char uint8_t;
#endif
#include <stdio.h>
#include <stddef.h>
/* Amalgamated: #include "mjs_features.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
#ifndef MJS_ENABLE_DEBUG
#define MJS_ENABLE_DEBUG 0
#endif
/*
* Double-precision floating-point number, IEEE 754
*
* 64 bit (8 bytes) in total
* 1 bit sign
* 11 bits exponent
* 52 bits mantissa
* 7 6 5 4 3 2 1 0
* seeeeeee|eeeemmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm
*
* If an exponent is all-1 and mantissa is all-0, then it is an INFINITY:
* 11111111|11110000|00000000|00000000|00000000|00000000|00000000|00000000
*
* If an exponent is all-1 and mantissa's MSB is 1, it is a quiet NaN:
* 11111111|11111000|00000000|00000000|00000000|00000000|00000000|00000000
*
* MJS NaN-packing:
* sign and exponent is 0xfff
* 4 bits specify type (tag), must be non-zero
* 48 bits specify value
*
* 11111111|1111tttt|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv
* NaN marker |type| 48-bit placeholder for values: pointers, strings
*
* On 64-bit platforms, pointers are really 48 bit only, so they can fit,
* provided they are sign extended
*/
typedef uint64_t mjs_val_t;
/* This if-0 is a dirty workaround to force etags to pick `struct mjs` */
#if 0
/* Opaque structure. MJS engine context. */
struct mjs {
/* ... */
};
#endif
struct mjs;
typedef enum mjs_err {
MJS_OK,
MJS_SYNTAX_ERROR,
MJS_REFERENCE_ERROR,
MJS_TYPE_ERROR,
MJS_OUT_OF_MEMORY,
MJS_INTERNAL_ERROR,
MJS_NOT_IMPLEMENTED_ERROR,
MJS_FILE_READ_ERROR,
MJS_BAD_ARGS_ERROR,
MJS_ERRS_CNT
} mjs_err_t;
struct mjs;
/* Create MJS instance */
struct mjs *mjs_create();
struct mjs_create_opts {
/* use non-default bytecode definition file, testing-only */
const struct bf_code *code;
};
/*
* Like `msj_create()`, but allows to customize initial MJS state, see `struct
* mjs_create_opts`.
*/
struct mjs *mjs_create_opt(struct mjs_create_opts opts);
/* Destroy MJS instance */
void mjs_destroy(struct mjs *mjs);
mjs_val_t mjs_get_global(struct mjs *mjs);
/*
* Tells the GC about an MJS value variable/field owned by C code.
*
* The user's C code should own mjs_val_t variables if the value's lifetime
* crosses any invocation of `mjs_exec()` and friends, including `mjs_call()`.
*
* The registration of the variable prevents the GC from mistakenly treat the
* object as garbage.
*
* User code should also explicitly disown the variables with `mjs_disown()`
* once it goes out of scope or the structure containing the mjs_val_t field is
* freed.
*
* Consider the following examples:
*
* Correct (owning is not necessary):
* ```c
* mjs_val_t res;
* mjs_exec(mjs, "....some script", &res);
* // ... use res somehow
*
* mjs_val_t res;
* mjs_exec(mjs, "....some script2", &res);
* // ... use new res somehow
* ```
*
* WRONG:
* ```c
* mjs_val_t res1;
* mjs_exec(mjs, "....some script", &res1);
*
* mjs_val_t res2;
* mjs_exec(mjs, "....some script2", &res2);
*
* // ... use res1 (WRONG!) and res2
* ```
*
* The code above is wrong, because after the second invocation of
* `mjs_exec()`, the value of `res1` is invalidated.
*
* Correct (res1 is owned)
* ```c
* mjs_val_t res1 = MJS_UNDEFINED;
* mjs_own(mjs, &res1);
* mjs_exec(mjs, "....some script", &res1);
*
* mjs_val_t res2 = MJS_UNDEFINED;
* mjs_exec(mjs, "....some script2", &res2);
*
* // ... use res1 and res2
* mjs_disown(mjs, &res1);
* ```
*
* NOTE that we explicly initialized `res1` to a valid value before owning it
* (in this case, the value is `MJS_UNDEFINED`). Owning an uninitialized
* variable is an undefined behaviour.
*
* Of course, it's not an error to own a variable even if it's not mandatory:
* e.g. in the last example we could own both `res1` and `res2`. Probably it
* would help us in the future, when we refactor the code so that `res2` has to
* be owned, and we could forget to do that.
*
* Also, if the user code has some C function called from MJS, and in this C
* function some MJS value (`mjs_val_t`) needs to be stored somewhere and to
* stay alive after the C function has returned, it also needs to be properly
* owned.
*/
void mjs_own(struct mjs *mjs, mjs_val_t *v);
/*
* Disowns the value previously owned by `mjs_own()`.
*
* Returns 1 if value is found, 0 otherwise.
*/
int mjs_disown(struct mjs *mjs, mjs_val_t *v);
mjs_err_t mjs_set_errorf(struct mjs *mjs, mjs_err_t err, const char *fmt, ...);
/*
* If there is no error message already set, then it's equal to
* `mjs_set_errorf()`.
*
* Otherwise, an old message gets prepended with the new one, followed by a
* colon. (the previously set error code is kept)
*/
mjs_err_t mjs_prepend_errorf(struct mjs *mjs, mjs_err_t err, const char *fmt,
...);
/*
* Print the last error details. If print_stack_trace is non-zero, also
* print stack trace. `msg` is the message which gets prepended to the actual
* error message, if it's NULL, then "MJS error" is used.
*/
void mjs_print_error(struct mjs *mjs, FILE *fp, const char *msg,
int print_stack_trace);
/*
* return a string representation of an error.
* the error string might be overwritten by calls to `mjs_set_errorf`.
*/
const char *mjs_strerror(struct mjs *mjs, enum mjs_err err);
/*
* Sets whether *.jsc files are generated when *.js file is executed. By
* default it's 0.
*
* If either `MJS_GENERATE_JSC` or `CS_MMAP` is off, then this function has no
* effect.
*/
void mjs_set_generate_jsc(struct mjs *mjs, int generate_jsc);
/*
* When invoked from a cfunction, returns number of arguments passed to the
* current JS function call.
*/
int mjs_nargs(struct mjs *mjs);
/*
* When invoked from a cfunction, returns n-th argument to the current JS
* function call.
*/
mjs_val_t mjs_arg(struct mjs *mjs, int n);
/*
* Sets return value for the current JS function call.
*/
void mjs_return(struct mjs *mjs, mjs_val_t v);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_CORE_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_ffi_public.h"
#endif
#ifndef MJS_FFI_PUBLIC_H_
#define MJS_FFI_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
enum mjs_ffi_ctype {
MJS_FFI_CTYPE_NONE,
MJS_FFI_CTYPE_USERDATA,
MJS_FFI_CTYPE_CALLBACK,
MJS_FFI_CTYPE_INT,
MJS_FFI_CTYPE_BOOL,
MJS_FFI_CTYPE_DOUBLE,
MJS_FFI_CTYPE_FLOAT,
MJS_FFI_CTYPE_CHAR_PTR,
MJS_FFI_CTYPE_VOID_PTR,
MJS_FFI_CTYPE_STRUCT_MG_STR_PTR,
MJS_FFI_CTYPE_STRUCT_MG_STR,
MJS_FFI_CTYPE_INVALID,
};
typedef void *(mjs_ffi_resolver_t)(void *handle, const char *symbol);
void mjs_set_ffi_resolver(struct mjs *mjs, mjs_ffi_resolver_t *dlsym);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_FFI_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_array_public.h"
#endif
/*
* === Arrays
*/
#ifndef MJS_ARRAY_PUBLIC_H_
#define MJS_ARRAY_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/* Make an empty array object */
mjs_val_t mjs_mk_array(struct mjs *mjs);
/* Returns length on an array. If `arr` is not an array, 0 is returned. */
unsigned long mjs_array_length(struct mjs *mjs, mjs_val_t arr);
/* Insert value `v` in array `arr` at the end of the array. */
mjs_err_t mjs_array_push(struct mjs *mjs, mjs_val_t arr, mjs_val_t v);
/*
* Return array member at index `index`. If `index` is out of bounds, undefined
* is returned.
*/
mjs_val_t mjs_array_get(struct mjs *, mjs_val_t arr, unsigned long index);
/* Insert value `v` into `arr` at index `index`. */
mjs_err_t mjs_array_set(struct mjs *mjs, mjs_val_t arr, unsigned long index,
mjs_val_t v);
/* Returns true if the given value is an array */
int mjs_is_array(mjs_val_t v);
/* Delete value in array `arr` at index `index`, if it exists. */
void mjs_array_del(struct mjs *mjs, mjs_val_t arr, unsigned long index);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_ARRAY_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_core_public.h"
#endif
#ifndef MJS_CORE_PUBLIC_H_
#define MJS_CORE_PUBLIC_H_
#if !defined(_MSC_VER) || _MSC_VER >= 1700
#include <stdint.h>
#else
typedef unsigned __int64 uint64_t;
typedef int int32_t;
typedef unsigned char uint8_t;
#endif
#include <stdio.h>
#include <stddef.h>
/* Amalgamated: #include "mjs_features.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
#ifndef MJS_ENABLE_DEBUG
#define MJS_ENABLE_DEBUG 0
#endif
/*
* Double-precision floating-point number, IEEE 754
*
* 64 bit (8 bytes) in total
* 1 bit sign
* 11 bits exponent
* 52 bits mantissa
* 7 6 5 4 3 2 1 0
* seeeeeee|eeeemmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm
*
* If an exponent is all-1 and mantissa is all-0, then it is an INFINITY:
* 11111111|11110000|00000000|00000000|00000000|00000000|00000000|00000000
*
* If an exponent is all-1 and mantissa's MSB is 1, it is a quiet NaN:
* 11111111|11111000|00000000|00000000|00000000|00000000|00000000|00000000
*
* MJS NaN-packing:
* sign and exponent is 0xfff
* 4 bits specify type (tag), must be non-zero
* 48 bits specify value
*
* 11111111|1111tttt|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv
* NaN marker |type| 48-bit placeholder for values: pointers, strings
*
* On 64-bit platforms, pointers are really 48 bit only, so they can fit,
* provided they are sign extended
*/
typedef uint64_t mjs_val_t;
/* This if-0 is a dirty workaround to force etags to pick `struct mjs` */
#if 0
/* Opaque structure. MJS engine context. */
struct mjs {
/* ... */
};
#endif
struct mjs;
typedef enum mjs_err {
MJS_OK,
MJS_SYNTAX_ERROR,
MJS_REFERENCE_ERROR,
MJS_TYPE_ERROR,
MJS_OUT_OF_MEMORY,
MJS_INTERNAL_ERROR,
MJS_NOT_IMPLEMENTED_ERROR,
MJS_FILE_READ_ERROR,
MJS_BAD_ARGS_ERROR,
MJS_ERRS_CNT
} mjs_err_t;
struct mjs;
/* Create MJS instance */
struct mjs *mjs_create();
struct mjs_create_opts {
/* use non-default bytecode definition file, testing-only */
const struct bf_code *code;
};
/*
* Like `msj_create()`, but allows to customize initial MJS state, see `struct
* mjs_create_opts`.
*/
struct mjs *mjs_create_opt(struct mjs_create_opts opts);
/* Destroy MJS instance */
void mjs_destroy(struct mjs *mjs);
mjs_val_t mjs_get_global(struct mjs *mjs);
/*
* Tells the GC about an MJS value variable/field owned by C code.
*
* The user's C code should own mjs_val_t variables if the value's lifetime
* crosses any invocation of `mjs_exec()` and friends, including `mjs_call()`.
*
* The registration of the variable prevents the GC from mistakenly treat the
* object as garbage.
*
* User code should also explicitly disown the variables with `mjs_disown()`
* once it goes out of scope or the structure containing the mjs_val_t field is
* freed.
*
* Consider the following examples:
*
* Correct (owning is not necessary):
* ```c
* mjs_val_t res;
* mjs_exec(mjs, "....some script", &res);
* // ... use res somehow
*
* mjs_val_t res;
* mjs_exec(mjs, "....some script2", &res);
* // ... use new res somehow
* ```
*
* WRONG:
* ```c
* mjs_val_t res1;
* mjs_exec(mjs, "....some script", &res1);
*
* mjs_val_t res2;
* mjs_exec(mjs, "....some script2", &res2);
*
* // ... use res1 (WRONG!) and res2
* ```
*
* The code above is wrong, because after the second invocation of
* `mjs_exec()`, the value of `res1` is invalidated.
*
* Correct (res1 is owned)
* ```c
* mjs_val_t res1 = MJS_UNDEFINED;
* mjs_own(mjs, &res1);
* mjs_exec(mjs, "....some script", &res1);
*
* mjs_val_t res2 = MJS_UNDEFINED;
* mjs_exec(mjs, "....some script2", &res2);
*
* // ... use res1 and res2
* mjs_disown(mjs, &res1);
* ```
*
* NOTE that we explicly initialized `res1` to a valid value before owning it
* (in this case, the value is `MJS_UNDEFINED`). Owning an uninitialized
* variable is an undefined behaviour.
*
* Of course, it's not an error to own a variable even if it's not mandatory:
* e.g. in the last example we could own both `res1` and `res2`. Probably it
* would help us in the future, when we refactor the code so that `res2` has to
* be owned, and we could forget to do that.
*
* Also, if the user code has some C function called from MJS, and in this C
* function some MJS value (`mjs_val_t`) needs to be stored somewhere and to
* stay alive after the C function has returned, it also needs to be properly
* owned.
*/
void mjs_own(struct mjs *mjs, mjs_val_t *v);
/*
* Disowns the value previously owned by `mjs_own()`.
*
* Returns 1 if value is found, 0 otherwise.
*/
int mjs_disown(struct mjs *mjs, mjs_val_t *v);
mjs_err_t mjs_set_errorf(struct mjs *mjs, mjs_err_t err, const char *fmt, ...);
/*
* If there is no error message already set, then it's equal to
* `mjs_set_errorf()`.
*
* Otherwise, an old message gets prepended with the new one, followed by a
* colon. (the previously set error code is kept)
*/
mjs_err_t mjs_prepend_errorf(struct mjs *mjs, mjs_err_t err, const char *fmt,
...);
/*
* Print the last error details. If print_stack_trace is non-zero, also
* print stack trace. `msg` is the message which gets prepended to the actual
* error message, if it's NULL, then "MJS error" is used.
*/
void mjs_print_error(struct mjs *mjs, FILE *fp, const char *msg,
int print_stack_trace);
/*
* return a string representation of an error.
* the error string might be overwritten by calls to `mjs_set_errorf`.
*/
const char *mjs_strerror(struct mjs *mjs, enum mjs_err err);
/*
* Sets whether *.jsc files are generated when *.js file is executed. By
* default it's 0.
*
* If either `MJS_GENERATE_JSC` or `CS_MMAP` is off, then this function has no
* effect.
*/
void mjs_set_generate_jsc(struct mjs *mjs, int generate_jsc);
/*
* When invoked from a cfunction, returns number of arguments passed to the
* current JS function call.
*/
int mjs_nargs(struct mjs *mjs);
/*
* When invoked from a cfunction, returns n-th argument to the current JS
* function call.
*/
mjs_val_t mjs_arg(struct mjs *mjs, int n);
/*
* Sets return value for the current JS function call.
*/
void mjs_return(struct mjs *mjs, mjs_val_t v);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_CORE_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_exec_public.h"
#endif
#ifndef MJS_EXEC_PUBLIC_H_
#define MJS_EXEC_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#include <stdio.h>
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
mjs_err_t mjs_exec(struct mjs *, const char *src, mjs_val_t *res);
mjs_err_t mjs_exec_buf(struct mjs *, const char *src, size_t, mjs_val_t *res);
mjs_err_t mjs_exec_file(struct mjs *mjs, const char *path, mjs_val_t *res);
mjs_err_t mjs_apply(struct mjs *mjs, mjs_val_t *res, mjs_val_t func,
mjs_val_t this_val, int nargs, mjs_val_t *args);
mjs_err_t mjs_call(struct mjs *mjs, mjs_val_t *res, mjs_val_t func,
mjs_val_t this_val, int nargs, ...);
mjs_val_t mjs_get_this(struct mjs *mjs);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_EXEC_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_ffi_public.h"
#endif
#ifndef MJS_FFI_PUBLIC_H_
#define MJS_FFI_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
enum mjs_ffi_ctype {
MJS_FFI_CTYPE_NONE,
MJS_FFI_CTYPE_USERDATA,
MJS_FFI_CTYPE_CALLBACK,
MJS_FFI_CTYPE_INT,
MJS_FFI_CTYPE_BOOL,
MJS_FFI_CTYPE_DOUBLE,
MJS_FFI_CTYPE_FLOAT,
MJS_FFI_CTYPE_CHAR_PTR,
MJS_FFI_CTYPE_VOID_PTR,
MJS_FFI_CTYPE_STRUCT_MG_STR_PTR,
MJS_FFI_CTYPE_STRUCT_MG_STR,
MJS_FFI_CTYPE_INVALID,
};
typedef void *(mjs_ffi_resolver_t)(void *handle, const char *symbol);
void mjs_set_ffi_resolver(struct mjs *mjs, mjs_ffi_resolver_t *dlsym);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_FFI_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_gc_public.h"
#endif
#ifndef MJS_GC_PUBLIC_H_
#define MJS_GC_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*
* Perform garbage collection.
* Pass true to full in order to reclaim unused heap back to the OS.
*/
void mjs_gc(struct mjs *mjs, int full);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_GC_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_object_public.h"
#endif
#ifndef MJS_OBJECT_PUBLIC_H_
#define MJS_OBJECT_PUBLIC_H_
#include <stddef.h>
/* Amalgamated: #include "mjs_core_public.h" */
/* Amalgamated: #include "mjs_ffi_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*
* Returns true if the given value is an object or array.
*/
int mjs_is_object(mjs_val_t v);
/* Make an empty object */
mjs_val_t mjs_mk_object(struct mjs *mjs);
/* Field types for struct-object conversion. */
enum mjs_struct_field_type {
MJS_STRUCT_FIELD_TYPE_INVALID,
MJS_STRUCT_FIELD_TYPE_STRUCT, /* Struct, arg points to def. */
MJS_STRUCT_FIELD_TYPE_STRUCT_PTR, /* Ptr to struct, arg points to def. */
MJS_STRUCT_FIELD_TYPE_INT,
MJS_STRUCT_FIELD_TYPE_BOOL,
MJS_STRUCT_FIELD_TYPE_DOUBLE,
MJS_STRUCT_FIELD_TYPE_FLOAT,
MJS_STRUCT_FIELD_TYPE_CHAR_PTR, /* NUL-terminated string. */
MJS_STRUCT_FIELD_TYPE_VOID_PTR, /* Converted to foreign ptr. */
MJS_STRUCT_FIELD_TYPE_MG_STR_PTR, /* Converted to string. */
MJS_STRUCT_FIELD_TYPE_MG_STR, /* Converted to string. */
MJS_STRUCT_FIELD_TYPE_DATA, /* Data, arg is length, becomes string. */
MJS_STRUCT_FIELD_TYPE_INT8,
MJS_STRUCT_FIELD_TYPE_INT16,
MJS_STRUCT_FIELD_TYPE_UINT8,
MJS_STRUCT_FIELD_TYPE_UINT16,
/*
* User-provided function. Arg is a pointer to function that takes void *
* (pointer to field within the struct) and returns mjs_val_t:
* mjs_val_t field_value(struct mjs *mjs, const void *field_ptr) { ... }
*/
MJS_STRUCT_FIELD_TYPE_CUSTOM,
};
/* C structure layout descriptor - needed by mjs_struct_to_obj */
struct mjs_c_struct_member {
const char *name;
int offset;
enum mjs_struct_field_type type;
const void *arg; /* Additional argument, used for some types. */
};
/* Create flat JS object from a C memory descriptor */
mjs_val_t mjs_struct_to_obj(struct mjs *mjs, const void *base,
const struct mjs_c_struct_member *members);
/*
* 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.
*/
mjs_val_t mjs_get(struct mjs *mjs, mjs_val_t obj, const char *name,
size_t name_len);
/*
* Like mjs_get but with a JS string.
*/
mjs_val_t mjs_get_v(struct mjs *mjs, mjs_val_t obj, mjs_val_t name);
/*
* Like mjs_get_v but lookup the prototype chain.
*/
mjs_val_t mjs_get_v_proto(struct mjs *mjs, mjs_val_t obj, mjs_val_t key);
/*
* Set object property. Behaves just like JavaScript assignment.
*/
mjs_err_t mjs_set(struct mjs *mjs, mjs_val_t obj, const char *name, size_t len,
mjs_val_t val);
/*
* Like mjs_set but the name is already a JS string.
*/
mjs_err_t mjs_set_v(struct mjs *mjs, mjs_val_t obj, mjs_val_t name,
mjs_val_t val);
/*
* 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 mjs_del(struct mjs *mjs, mjs_val_t obj, const char *name, size_t len);
/*
* Iterate over `obj` properties.
* First call should set `iterator` to MJS_UNDEFINED.
* Return object's key (a string), or MJS_UNDEFINED when no more keys left.
* Do not mutate the object during iteration.
*
* Example:
* mjs_val_t key, iter = MJS_UNDEFINED;
* while ((key = mjs_next(mjs, obj, &iter)) != MJS_UNDEFINED) {
* // Do something with the obj/key ...
* }
*/
mjs_val_t mjs_next(struct mjs *mjs, mjs_val_t obj, mjs_val_t *iterator);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_OBJECT_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_primitive_public.h"
#endif
#ifndef MJS_PRIMITIVE_PUBLIC_H_
#define MJS_PRIMITIVE_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/* JavaScript `null` value */
#define MJS_NULL MJS_TAG_NULL
/* JavaScript `undefined` value */
#define MJS_UNDEFINED MJS_TAG_UNDEFINED
/* Function pointer type used in `mjs_mk_foreign_func`. */
typedef void (*mjs_func_ptr_t)(void);
/*
* Make `null` primitive value.
*
* NOTE: this function is deprecated and will be removed in future releases.
* Use `MJS_NULL` instead.
*/
mjs_val_t mjs_mk_null(void);
/* Returns true if given value is a primitive `null` value */
int mjs_is_null(mjs_val_t v);
/*
* Make `undefined` primitive value.
*
* NOTE: this function is deprecated and will be removed in future releases.
* Use `MJS_UNDEFINED` instead.
*/
mjs_val_t mjs_mk_undefined(void);
/* Returns true if given value is a primitive `undefined` value */
int mjs_is_undefined(mjs_val_t v);
/* Make numeric primitive value */
mjs_val_t mjs_mk_number(struct mjs *mjs, double num);
/*
* Returns number value stored in `mjs_val_t` as `double`.
*
* Returns NaN for non-numbers.
*/
double mjs_get_double(struct mjs *mjs, mjs_val_t v);
/*
* Returns number value stored in `mjs_val_t` as `int`. If the number value is
* not an integer, the fraction part will be discarded.
*
* If the given value is a non-number, or NaN, the result is undefined.
*/
int mjs_get_int(struct mjs *mjs, mjs_val_t v);
/*
* Like mjs_get_int but ensures that the returned type
* is a 32-bit signed integer.
*/
int32_t mjs_get_int32(struct mjs *mjs, mjs_val_t v);
/* Returns true if given value is a primitive number value */
int mjs_is_number(mjs_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.
*/
mjs_val_t mjs_mk_foreign(struct mjs *mjs, void *ptr);
/*
* Make JavaScript value that holds C/C++ function pointer, similarly to
* `mjs_mk_foreign`.
*/
mjs_val_t mjs_mk_foreign_func(struct mjs *mjs, mjs_func_ptr_t fn);
/*
* Returns `void *` pointer stored in `mjs_val_t`.
*
* Returns NULL if the value is not a foreign pointer.
*/
void *mjs_get_ptr(struct mjs *mjs, mjs_val_t v);
/* Returns true if given value holds `void *` pointer */
int mjs_is_foreign(mjs_val_t v);
mjs_val_t mjs_mk_boolean(struct mjs *mjs, int v);
int mjs_get_bool(struct mjs *mjs, mjs_val_t v);
int mjs_is_boolean(mjs_val_t v);
mjs_val_t mjs_mk_function(struct mjs *mjs, size_t off);
int mjs_is_function(mjs_val_t v);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_PRIMITIVE_PUBLIC_H_ */
#ifdef MJS_MODULE_LINES
#line 1 "src/mjs_string_public.h"
#endif
#ifndef MJS_STRING_PUBLIC_H_
#define MJS_STRING_PUBLIC_H_
/* Amalgamated: #include "mjs_core_public.h" */
#define MJS_STRING_LITERAL_MAX_LEN 128
#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.
*/
mjs_val_t mjs_mk_string(struct mjs *mjs, const char *str, size_t len, int copy);
/* Returns true if given value is a primitive string value */
int mjs_is_string(mjs_val_t v);
/*
* Returns a pointer to the string stored in `mjs_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 `mjs_get_string()`.
*
* Short JS strings are embedded inside the `mjs_val_t` value itself. This
* is why a pointer to a `mjs_val_t` is required. It also means that the string
* data will become invalid once that `mjs_val_t` value goes out of scope.
*/
const char *mjs_get_string(struct mjs *mjs, mjs_val_t *v, size_t *len);
/*
* Returns a pointer to the string stored in `mjs_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 MJS engine (see `mjs_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 *mjs_get_cstring(struct mjs *mjs, mjs_val_t *v);
/*
* Returns the standard strcmp comparison code after comparing a JS string a
* with a possibly non null-terminated string b. NOTE: the strings are equal