-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjx9_api.c
1744 lines (1744 loc) · 52.8 KB
/
jx9_api.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
/*
* Symisc JX9: A Highly Efficient Embeddable Scripting Engine Based on JSON.
* Copyright (C) 2012-2013, Symisc Systems http://jx9.symisc.net/
* Version 1.7.2
* For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* or visit:
* http://jx9.symisc.net/
*/
/* $SymiscID: api.c v1.7 FreeBSD 2012-12-18 06:54 stable <[email protected]> $ */
#ifndef JX9_AMALGAMATION
#include "jx9Int.h"
#endif
/* This file implement the public interfaces presented to host-applications.
* Routines in other files are for internal use by JX9 and should not be
* accessed by users of the library.
*/
#define JX9_ENGINE_MAGIC 0xF874BCD7
#define JX9_ENGINE_MISUSE(ENGINE) (ENGINE == 0 || ENGINE->nMagic != JX9_ENGINE_MAGIC)
#define JX9_VM_MISUSE(VM) (VM == 0 || VM->nMagic == JX9_VM_STALE)
/* If another thread have released a working instance, the following macros
* evaluates to true. These macros are only used when the library
* is built with threading support enabled which is not the case in
* the default built.
*/
#define JX9_THRD_ENGINE_RELEASE(ENGINE) (ENGINE->nMagic != JX9_ENGINE_MAGIC)
#define JX9_THRD_VM_RELEASE(VM) (VM->nMagic == JX9_VM_STALE)
/* IMPLEMENTATION: jx9@embedded@symisc 311-12-32 */
/*
* All global variables are collected in the structure named "sJx9MPGlobal".
* That way it is clear in the code when we are using static variable because
* its name start with sJx9MPGlobal.
*/
static struct Jx9Global_Data
{
SyMemBackend sAllocator; /* Global low level memory allocator */
#if defined(JX9_ENABLE_THREADS)
const SyMutexMethods *pMutexMethods; /* Mutex methods */
SyMutex *pMutex; /* Global mutex */
sxu32 nThreadingLevel; /* Threading level: 0 == Single threaded/1 == Multi-Threaded
* The threading level can be set using the [jx9_lib_config()]
* interface with a configuration verb set to
* JX9_LIB_CONFIG_THREAD_LEVEL_SINGLE or
* JX9_LIB_CONFIG_THREAD_LEVEL_MULTI
*/
#endif
const jx9_vfs *pVfs; /* Underlying virtual file system */
sxi32 nEngine; /* Total number of active engines */
jx9 *pEngines; /* List of active engine */
sxu32 nMagic; /* Sanity check against library misuse */
}sJx9MPGlobal = {
{0, 0, 0, 0, 0, 0, 0, 0, {0}},
#if defined(JX9_ENABLE_THREADS)
0,
0,
0,
#endif
0,
0,
0,
0
};
#define JX9_LIB_MAGIC 0xEA1495BA
#define JX9_LIB_MISUSE (sJx9MPGlobal.nMagic != JX9_LIB_MAGIC)
/*
* Supported threading level.
* These options have meaning only when the library is compiled with multi-threading
* support.That is, the JX9_ENABLE_THREADS compile time directive must be defined
* when JX9 is built.
* JX9_THREAD_LEVEL_SINGLE:
* In this mode, mutexing is disabled and the library can only be used by a single thread.
* JX9_THREAD_LEVEL_MULTI
* In this mode, all mutexes including the recursive mutexes on [jx9] objects
* are enabled so that the application is free to share the same engine
* between different threads at the same time.
*/
#define JX9_THREAD_LEVEL_SINGLE 1
#define JX9_THREAD_LEVEL_MULTI 2
/*
* Configure a running JX9 engine instance.
* return JX9_OK on success.Any other return
* value indicates failure.
* Refer to [jx9_config()].
*/
JX9_PRIVATE sxi32 jx9EngineConfig(jx9 *pEngine, sxi32 nOp, va_list ap)
{
jx9_conf *pConf = &pEngine->xConf;
int rc = JX9_OK;
/* Perform the requested operation */
switch(nOp){
case JX9_CONFIG_ERR_LOG:{
/* Extract compile-time error log if any */
const char **pzPtr = va_arg(ap, const char **);
int *pLen = va_arg(ap, int *);
if( pzPtr == 0 ){
rc = JX9_CORRUPT;
break;
}
/* NULL terminate the error-log buffer */
SyBlobNullAppend(&pConf->sErrConsumer);
/* Point to the error-log buffer */
*pzPtr = (const char *)SyBlobData(&pConf->sErrConsumer);
if( pLen ){
if( SyBlobLength(&pConf->sErrConsumer) > 1 /* NULL '\0' terminator */ ){
*pLen = (int)SyBlobLength(&pConf->sErrConsumer);
}else{
*pLen = 0;
}
}
break;
}
case JX9_CONFIG_ERR_ABORT:
/* Reserved for future use */
break;
default:
/* Unknown configuration verb */
rc = JX9_CORRUPT;
break;
} /* Switch() */
return rc;
}
/*
* Configure the JX9 library.
* Return JX9_OK on success. Any other return value indicates failure.
* Refer to [jx9_lib_config()].
*/
static sxi32 Jx9CoreConfigure(sxi32 nOp, va_list ap)
{
int rc = JX9_OK;
switch(nOp){
case JX9_LIB_CONFIG_VFS:{
/* Install a virtual file system */
const jx9_vfs *pVfs = va_arg(ap, const jx9_vfs *);
sJx9MPGlobal.pVfs = pVfs;
break;
}
case JX9_LIB_CONFIG_USER_MALLOC: {
/* Use an alternative low-level memory allocation routines */
const SyMemMethods *pMethods = va_arg(ap, const SyMemMethods *);
/* Save the memory failure callback (if available) */
ProcMemError xMemErr = sJx9MPGlobal.sAllocator.xMemError;
void *pMemErr = sJx9MPGlobal.sAllocator.pUserData;
if( pMethods == 0 ){
/* Use the built-in memory allocation subsystem */
rc = SyMemBackendInit(&sJx9MPGlobal.sAllocator, xMemErr, pMemErr);
}else{
rc = SyMemBackendInitFromOthers(&sJx9MPGlobal.sAllocator, pMethods, xMemErr, pMemErr);
}
break;
}
case JX9_LIB_CONFIG_MEM_ERR_CALLBACK: {
/* Memory failure callback */
ProcMemError xMemErr = va_arg(ap, ProcMemError);
void *pUserData = va_arg(ap, void *);
sJx9MPGlobal.sAllocator.xMemError = xMemErr;
sJx9MPGlobal.sAllocator.pUserData = pUserData;
break;
}
case JX9_LIB_CONFIG_USER_MUTEX: {
#if defined(JX9_ENABLE_THREADS)
/* Use an alternative low-level mutex subsystem */
const SyMutexMethods *pMethods = va_arg(ap, const SyMutexMethods *);
#if defined (UNTRUST)
if( pMethods == 0 ){
rc = JX9_CORRUPT;
}
#endif
/* Sanity check */
if( pMethods->xEnter == 0 || pMethods->xLeave == 0 || pMethods->xNew == 0){
/* At least three criticial callbacks xEnter(), xLeave() and xNew() must be supplied */
rc = JX9_CORRUPT;
break;
}
if( sJx9MPGlobal.pMutexMethods ){
/* Overwrite the previous mutex subsystem */
SyMutexRelease(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex);
if( sJx9MPGlobal.pMutexMethods->xGlobalRelease ){
sJx9MPGlobal.pMutexMethods->xGlobalRelease();
}
sJx9MPGlobal.pMutex = 0;
}
/* Initialize and install the new mutex subsystem */
if( pMethods->xGlobalInit ){
rc = pMethods->xGlobalInit();
if ( rc != JX9_OK ){
break;
}
}
/* Create the global mutex */
sJx9MPGlobal.pMutex = pMethods->xNew(SXMUTEX_TYPE_FAST);
if( sJx9MPGlobal.pMutex == 0 ){
/*
* If the supplied mutex subsystem is so sick that we are unable to
* create a single mutex, there is no much we can do here.
*/
if( pMethods->xGlobalRelease ){
pMethods->xGlobalRelease();
}
rc = JX9_CORRUPT;
break;
}
sJx9MPGlobal.pMutexMethods = pMethods;
if( sJx9MPGlobal.nThreadingLevel == 0 ){
/* Set a default threading level */
sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_MULTI;
}
#endif
break;
}
case JX9_LIB_CONFIG_THREAD_LEVEL_SINGLE:
#if defined(JX9_ENABLE_THREADS)
/* Single thread mode(Only one thread is allowed to play with the library) */
sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_SINGLE;
#endif
break;
case JX9_LIB_CONFIG_THREAD_LEVEL_MULTI:
#if defined(JX9_ENABLE_THREADS)
/* Multi-threading mode (library is thread safe and JX9 engines and virtual machines
* may be shared between multiple threads).
*/
sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_MULTI;
#endif
break;
default:
/* Unknown configuration option */
rc = JX9_CORRUPT;
break;
}
return rc;
}
/*
* [CAPIREF: jx9_lib_config()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_lib_config(int nConfigOp, ...)
{
va_list ap;
int rc;
if( sJx9MPGlobal.nMagic == JX9_LIB_MAGIC ){
/* Library is already initialized, this operation is forbidden */
return JX9_LOOKED;
}
va_start(ap, nConfigOp);
rc = Jx9CoreConfigure(nConfigOp, ap);
va_end(ap);
return rc;
}
/*
* Global library initialization
* Refer to [jx9_lib_init()]
* This routine must be called to initialize the memory allocation subsystem, the mutex
* subsystem prior to doing any serious work with the library.The first thread to call
* this routine does the initialization process and set the magic number so no body later
* can re-initialize the library.If subsequent threads call this routine before the first
* thread have finished the initialization process, then the subsequent threads must block
* until the initialization process is done.
*/
static sxi32 Jx9CoreInitialize(void)
{
const jx9_vfs *pVfs; /* Built-in vfs */
#if defined(JX9_ENABLE_THREADS)
const SyMutexMethods *pMutexMethods = 0;
SyMutex *pMaster = 0;
#endif
int rc;
/*
* If the library is already initialized, then a call to this routine
* is a no-op.
*/
if( sJx9MPGlobal.nMagic == JX9_LIB_MAGIC ){
return JX9_OK; /* Already initialized */
}
/* Point to the built-in vfs */
pVfs = jx9ExportBuiltinVfs();
/* Install it */
jx9_lib_config(JX9_LIB_CONFIG_VFS, pVfs);
#if defined(JX9_ENABLE_THREADS)
if( sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_SINGLE ){
pMutexMethods = sJx9MPGlobal.pMutexMethods;
if( pMutexMethods == 0 ){
/* Use the built-in mutex subsystem */
pMutexMethods = SyMutexExportMethods();
if( pMutexMethods == 0 ){
return JX9_CORRUPT; /* Can't happen */
}
/* Install the mutex subsystem */
rc = jx9_lib_config(JX9_LIB_CONFIG_USER_MUTEX, pMutexMethods);
if( rc != JX9_OK ){
return rc;
}
}
/* Obtain a static mutex so we can initialize the library without calling malloc() */
pMaster = SyMutexNew(pMutexMethods, SXMUTEX_TYPE_STATIC_1);
if( pMaster == 0 ){
return JX9_CORRUPT; /* Can't happen */
}
}
/* Lock the master mutex */
rc = JX9_OK;
SyMutexEnter(pMutexMethods, pMaster); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
if( sJx9MPGlobal.nMagic != JX9_LIB_MAGIC ){
#endif
if( sJx9MPGlobal.sAllocator.pMethods == 0 ){
/* Install a memory subsystem */
rc = jx9_lib_config(JX9_LIB_CONFIG_USER_MALLOC, 0); /* zero mean use the built-in memory backend */
if( rc != JX9_OK ){
/* If we are unable to initialize the memory backend, there is no much we can do here.*/
goto End;
}
}
#if defined(JX9_ENABLE_THREADS)
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
/* Protect the memory allocation subsystem */
rc = SyMemBackendMakeThreadSafe(&sJx9MPGlobal.sAllocator, sJx9MPGlobal.pMutexMethods);
if( rc != JX9_OK ){
goto End;
}
}
#endif
/* Our library is initialized, set the magic number */
sJx9MPGlobal.nMagic = JX9_LIB_MAGIC;
rc = JX9_OK;
#if defined(JX9_ENABLE_THREADS)
} /* sJx9MPGlobal.nMagic != JX9_LIB_MAGIC */
#endif
End:
#if defined(JX9_ENABLE_THREADS)
/* Unlock the master mutex */
SyMutexLeave(pMutexMethods, pMaster); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
return rc;
}
/*
* Release an active JX9 engine and it's associated active virtual machines.
*/
static sxi32 EngineRelease(jx9 *pEngine)
{
jx9_vm *pVm, *pNext;
/* Release all active VM */
pVm = pEngine->pVms;
for(;;){
if( pEngine->iVm < 1 ){
break;
}
pNext = pVm->pNext;
jx9VmRelease(pVm);
pVm = pNext;
pEngine->iVm--;
}
/* Set a dummy magic number */
pEngine->nMagic = 0x7635;
/* Release the private memory subsystem */
SyMemBackendRelease(&pEngine->sAllocator);
return JX9_OK;
}
/*
* Release all resources consumed by the library.
* If JX9 is already shut when this routine is invoked then this
* routine is a harmless no-op.
* Note: This call is not thread safe. Refer to [jx9_lib_shutdown()].
*/
static void JX9CoreShutdown(void)
{
jx9 *pEngine, *pNext;
/* Release all active engines first */
pEngine = sJx9MPGlobal.pEngines;
for(;;){
if( sJx9MPGlobal.nEngine < 1 ){
break;
}
pNext = pEngine->pNext;
EngineRelease(pEngine);
pEngine = pNext;
sJx9MPGlobal.nEngine--;
}
#if defined(JX9_ENABLE_THREADS)
/* Release the mutex subsystem */
if( sJx9MPGlobal.pMutexMethods ){
if( sJx9MPGlobal.pMutex ){
SyMutexRelease(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex);
sJx9MPGlobal.pMutex = 0;
}
if( sJx9MPGlobal.pMutexMethods->xGlobalRelease ){
sJx9MPGlobal.pMutexMethods->xGlobalRelease();
}
sJx9MPGlobal.pMutexMethods = 0;
}
sJx9MPGlobal.nThreadingLevel = 0;
#endif
if( sJx9MPGlobal.sAllocator.pMethods ){
/* Release the memory backend */
SyMemBackendRelease(&sJx9MPGlobal.sAllocator);
}
sJx9MPGlobal.nMagic = 0x1928;
}
/*
* [CAPIREF: jx9_lib_shutdown()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_lib_shutdown(void)
{
if( sJx9MPGlobal.nMagic != JX9_LIB_MAGIC ){
/* Already shut */
return JX9_OK;
}
JX9CoreShutdown();
return JX9_OK;
}
/*
* [CAPIREF: jx9_lib_signature()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE const char * jx9_lib_signature(void)
{
return JX9_SIG;
}
/*
* [CAPIREF: jx9_init()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_init(jx9 **ppEngine)
{
jx9 *pEngine;
int rc;
#if defined(UNTRUST)
if( ppEngine == 0 ){
return JX9_CORRUPT;
}
#endif
*ppEngine = 0;
/* One-time automatic library initialization */
rc = Jx9CoreInitialize();
if( rc != JX9_OK ){
return rc;
}
/* Allocate a new engine */
pEngine = (jx9 *)SyMemBackendPoolAlloc(&sJx9MPGlobal.sAllocator, sizeof(jx9));
if( pEngine == 0 ){
return JX9_NOMEM;
}
/* Zero the structure */
SyZero(pEngine, sizeof(jx9));
/* Initialize engine fields */
pEngine->nMagic = JX9_ENGINE_MAGIC;
rc = SyMemBackendInitFromParent(&pEngine->sAllocator, &sJx9MPGlobal.sAllocator);
if( rc != JX9_OK ){
goto Release;
}
#if defined(JX9_ENABLE_THREADS)
SyMemBackendDisbaleMutexing(&pEngine->sAllocator);
#endif
/* Default configuration */
SyBlobInit(&pEngine->xConf.sErrConsumer, &pEngine->sAllocator);
/* Install a default compile-time error consumer routine */
pEngine->xConf.xErr = jx9VmBlobConsumer;
pEngine->xConf.pErrData = &pEngine->xConf.sErrConsumer;
/* Built-in vfs */
pEngine->pVfs = sJx9MPGlobal.pVfs;
#if defined(JX9_ENABLE_THREADS)
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
/* Associate a recursive mutex with this instance */
pEngine->pMutex = SyMutexNew(sJx9MPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
if( pEngine->pMutex == 0 ){
rc = JX9_NOMEM;
goto Release;
}
}
#endif
/* Link to the list of active engines */
#if defined(JX9_ENABLE_THREADS)
/* Enter the global mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
MACRO_LD_PUSH(sJx9MPGlobal.pEngines, pEngine);
sJx9MPGlobal.nEngine++;
#if defined(JX9_ENABLE_THREADS)
/* Leave the global mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
/* Write a pointer to the new instance */
*ppEngine = pEngine;
return JX9_OK;
Release:
SyMemBackendRelease(&pEngine->sAllocator);
SyMemBackendPoolFree(&sJx9MPGlobal.sAllocator,pEngine);
return rc;
}
/*
* [CAPIREF: jx9_release()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_release(jx9 *pEngine)
{
int rc;
if( JX9_ENGINE_MISUSE(pEngine) ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire engine mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_ENGINE_RELEASE(pEngine) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/* Release the engine */
rc = EngineRelease(&(*pEngine));
#if defined(JX9_ENABLE_THREADS)
/* Leave engine mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
/* Release engine mutex */
SyMutexRelease(sJx9MPGlobal.pMutexMethods, pEngine->pMutex) /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
#if defined(JX9_ENABLE_THREADS)
/* Enter the global mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
/* Unlink from the list of active engines */
MACRO_LD_REMOVE(sJx9MPGlobal.pEngines, pEngine);
sJx9MPGlobal.nEngine--;
#if defined(JX9_ENABLE_THREADS)
/* Leave the global mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
/* Release the memory chunk allocated to this engine */
SyMemBackendPoolFree(&sJx9MPGlobal.sAllocator, pEngine);
return rc;
}
/*
* Compile a raw JX9 script.
* To execute a JX9 code, it must first be compiled into a bytecode program using this routine.
* If something goes wrong [i.e: compile-time error], your error log [i.e: error consumer callback]
* should display the appropriate error message and this function set ppVm to null and return
* an error code that is different from JX9_OK. Otherwise when the script is successfully compiled
* ppVm should hold the JX9 bytecode and it's safe to call [jx9_vm_exec(), jx9_vm_reset(), etc.].
* This API does not actually evaluate the JX9 code. It merely compile and prepares the JX9 script
* for evaluation.
*/
static sxi32 ProcessScript(
jx9 *pEngine, /* Running JX9 engine */
jx9_vm **ppVm, /* OUT: A pointer to the virtual machine */
SyString *pScript, /* Raw JX9 script to compile */
sxi32 iFlags, /* Compile-time flags */
const char *zFilePath /* File path if script come from a file. NULL otherwise */
)
{
jx9_vm *pVm;
int rc;
/* Allocate a new virtual machine */
pVm = (jx9_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(jx9_vm));
if( pVm == 0 ){
/* If the supplied memory subsystem is so sick that we are unable to allocate
* a tiny chunk of memory, there is no much we can do here. */
if( ppVm ){
*ppVm = 0;
}
return JX9_NOMEM;
}
if( iFlags < 0 ){
/* Default compile-time flags */
iFlags = 0;
}
/* Initialize the Virtual Machine */
rc = jx9VmInit(pVm, &(*pEngine));
if( rc != JX9_OK ){
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if( ppVm ){
*ppVm = 0;
}
return JX9_VM_ERR;
}
if( zFilePath ){
/* Push processed file path */
jx9VmPushFilePath(pVm, zFilePath, -1, TRUE, 0);
}
/* Reset the error message consumer */
SyBlobReset(&pEngine->xConf.sErrConsumer);
/* Compile the script */
jx9CompileScript(pVm, &(*pScript), iFlags);
if( pVm->sCodeGen.nErr > 0 || pVm == 0){
sxu32 nErr = pVm->sCodeGen.nErr;
/* Compilation error or null ppVm pointer, release this VM */
SyMemBackendRelease(&pVm->sAllocator);
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if( ppVm ){
*ppVm = 0;
}
return nErr > 0 ? JX9_COMPILE_ERR : JX9_OK;
}
/* Prepare the virtual machine for bytecode execution */
rc = jx9VmMakeReady(pVm);
if( rc != JX9_OK ){
goto Release;
}
/* Install local import path which is the current directory */
jx9_vm_config(pVm, JX9_VM_CONFIG_IMPORT_PATH, "./");
#if defined(JX9_ENABLE_THREADS)
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
/* Associate a recursive mutex with this instance */
pVm->pMutex = SyMutexNew(sJx9MPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
if( pVm->pMutex == 0 ){
goto Release;
}
}
#endif
/* Script successfully compiled, link to the list of active virtual machines */
MACRO_LD_PUSH(pEngine->pVms, pVm);
pEngine->iVm++;
/* Point to the freshly created VM */
*ppVm = pVm;
/* Ready to execute JX9 bytecode */
return JX9_OK;
Release:
SyMemBackendRelease(&pVm->sAllocator);
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
*ppVm = 0;
return JX9_VM_ERR;
}
/*
* [CAPIREF: jx9_compile()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_compile(jx9 *pEngine, const char *zSource, int nLen, jx9_vm **ppOutVm)
{
SyString sScript;
int rc;
if( JX9_ENGINE_MISUSE(pEngine) ){
return JX9_CORRUPT;
}
if( zSource == 0 ){
/* Empty Jx9 statement ';' */
zSource = ";";
nLen = (int)sizeof(char);
}
if( nLen < 0 ){
/* Compute input length automatically */
nLen = (int)SyStrlen(zSource);
}
SyStringInitFromBuf(&sScript, zSource, nLen);
#if defined(JX9_ENABLE_THREADS)
/* Acquire engine mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_ENGINE_RELEASE(pEngine) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/* Compile the script */
rc = ProcessScript(&(*pEngine),ppOutVm,&sScript,0,0);
#if defined(JX9_ENABLE_THREADS)
/* Leave engine mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
/* Compilation result */
return rc;
}
/*
* [CAPIREF: jx9_compile_file()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_compile_file(jx9 *pEngine, const char *zFilePath, jx9_vm **ppOutVm)
{
const jx9_vfs *pVfs;
int rc;
if( ppOutVm ){
*ppOutVm = 0;
}
rc = JX9_OK; /* cc warning */
if( JX9_ENGINE_MISUSE(pEngine) || SX_EMPTY_STR(zFilePath) ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire engine mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_ENGINE_RELEASE(pEngine) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/*
* Check if the underlying vfs implement the memory map
* [i.e: mmap() under UNIX/MapViewOfFile() under windows] function.
*/
pVfs = pEngine->pVfs;
if( pVfs == 0 || pVfs->xMmap == 0 ){
/* Memory map routine not implemented */
rc = JX9_IO_ERR;
}else{
void *pMapView = 0; /* cc warning */
jx9_int64 nSize = 0; /* cc warning */
SyString sScript;
/* Try to get a memory view of the whole file */
rc = pVfs->xMmap(zFilePath, &pMapView, &nSize);
if( rc != JX9_OK ){
/* Assume an IO error */
rc = JX9_IO_ERR;
}else{
/* Compile the file */
SyStringInitFromBuf(&sScript, pMapView, nSize);
rc = ProcessScript(&(*pEngine), ppOutVm, &sScript,0,zFilePath);
/* Release the memory view of the whole file */
if( pVfs->xUnmap ){
pVfs->xUnmap(pMapView, nSize);
}
}
}
#if defined(JX9_ENABLE_THREADS)
/* Leave engine mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
/* Compilation result */
return rc;
}
/*
* [CAPIREF: jx9_vm_config()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_vm_config(jx9_vm *pVm, int iConfigOp, ...)
{
va_list ap;
int rc;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire VM mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_VM_RELEASE(pVm) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/* Confiugure the virtual machine */
va_start(ap, iConfigOp);
rc = jx9VmConfigure(&(*pVm), iConfigOp, ap);
va_end(ap);
#if defined(JX9_ENABLE_THREADS)
/* Leave VM mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
return rc;
}
/*
* [CAPIREF: jx9_vm_release()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_vm_release(jx9_vm *pVm)
{
jx9 *pEngine;
int rc;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire VM mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_VM_RELEASE(pVm) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
pEngine = pVm->pEngine;
rc = jx9VmRelease(&(*pVm));
#if defined(JX9_ENABLE_THREADS)
/* Leave VM mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
/* Release VM mutex */
SyMutexRelease(sJx9MPGlobal.pMutexMethods, pVm->pMutex) /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
if( rc == JX9_OK ){
/* Unlink from the list of active VM */
#if defined(JX9_ENABLE_THREADS)
/* Acquire engine mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_ENGINE_RELEASE(pEngine) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
MACRO_LD_REMOVE(pEngine->pVms, pVm);
pEngine->iVm--;
/* Release the memory chunk allocated to this VM */
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
#if defined(JX9_ENABLE_THREADS)
/* Leave engine mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
}
return rc;
}
/*
* [CAPIREF: jx9_create_function()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_create_function(jx9_vm *pVm, const char *zName, int (*xFunc)(jx9_context *, int, jx9_value **), void *pUserData)
{
SyString sName;
int rc;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return JX9_CORRUPT;
}
SyStringInitFromBuf(&sName, zName, SyStrlen(zName));
/* Remove leading and trailing white spaces */
SyStringFullTrim(&sName);
/* Ticket 1433-003: NULL values are not allowed */
if( sName.nByte < 1 || xFunc == 0 ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire VM mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_VM_RELEASE(pVm) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/* Install the foreign function */
rc = jx9VmInstallForeignFunction(&(*pVm), &sName, xFunc, pUserData);
#if defined(JX9_ENABLE_THREADS)
/* Leave VM mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
return rc;
}
JX9_PRIVATE int jx9DeleteFunction(jx9_vm *pVm,const char *zName)
{
jx9_user_func *pFunc = 0; /* cc warning */
int rc;
/* Perform the deletion */
rc = SyHashDeleteEntry(&pVm->hHostFunction, (const void *)zName, SyStrlen(zName), (void **)&pFunc);
if( rc == JX9_OK ){
/* Release internal fields */
SySetRelease(&pFunc->aAux);
SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pFunc->sName));
SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
}
return rc;
}
/*
* [CAPIREF: jx9_create_constant()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_create_constant(jx9_vm *pVm, const char *zName, void (*xExpand)(jx9_value *, void *), void *pUserData)
{
SyString sName;
int rc;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return JX9_CORRUPT;
}
SyStringInitFromBuf(&sName, zName, SyStrlen(zName));
/* Remove leading and trailing white spaces */
SyStringFullTrim(&sName);
if( sName.nByte < 1 ){
/* Empty constant name */
return JX9_CORRUPT;
}
/* TICKET 1433-003: NULL pointer is harmless operation */
if( xExpand == 0 ){
return JX9_CORRUPT;
}
#if defined(JX9_ENABLE_THREADS)
/* Acquire VM mutex */
SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE &&
JX9_THRD_VM_RELEASE(pVm) ){
return JX9_ABORT; /* Another thread have released this instance */
}
#endif
/* Perform the registration */
rc = jx9VmRegisterConstant(&(*pVm), &sName, xExpand, pUserData);
#if defined(JX9_ENABLE_THREADS)
/* Leave VM mutex */
SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
return rc;
}
JX9_PRIVATE int Jx9DeleteConstant(jx9_vm *pVm,const char *zName)
{
jx9_constant *pCons;
int rc;
/* Query the constant hashtable */
rc = SyHashDeleteEntry(&pVm->hConstant, (const void *)zName, SyStrlen(zName), (void **)&pCons);
if( rc == JX9_OK ){
/* Perform the deletion */
SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pCons->sName));
SyMemBackendPoolFree(&pVm->sAllocator, pCons);
}
return rc;
}
/*
* [CAPIREF: jx9_new_scalar()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE jx9_value * jx9_new_scalar(jx9_vm *pVm)
{
jx9_value *pObj;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return 0;
}
/* Allocate a new scalar variable */
pObj = (jx9_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_value));
if( pObj == 0 ){
return 0;
}
/* Nullify the new scalar */
jx9MemObjInit(pVm, pObj);
return pObj;
}
/*
* [CAPIREF: jx9_new_array()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE jx9_value * jx9_new_array(jx9_vm *pVm)
{
jx9_hashmap *pMap;
jx9_value *pObj;
/* Ticket 1433-002: NULL VM is harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return 0;
}
/* Create a new hashmap first */
pMap = jx9NewHashmap(&(*pVm), 0, 0);
if( pMap == 0 ){
return 0;
}
/* Associate a new jx9_value with this hashmap */
pObj = (jx9_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_value));
if( pObj == 0 ){
jx9HashmapRelease(pMap, TRUE);
return 0;
}
jx9MemObjInitFromArray(pVm, pObj, pMap);
return pObj;
}
/*
* [CAPIREF: jx9_release_value()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_release_value(jx9_vm *pVm, jx9_value *pValue)
{
/* Ticket 1433-002: NULL VM is a harmless operation */
if ( JX9_VM_MISUSE(pVm) ){
return JX9_CORRUPT;
}
if( pValue ){
/* Release the value */
jx9MemObjRelease(pValue);
SyMemBackendPoolFree(&pVm->sAllocator, pValue);
}
return JX9_OK;
}
/*
* [CAPIREF: jx9_value_to_int()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_value_to_int(jx9_value *pValue)
{
int rc;
rc = jx9MemObjToInteger(pValue);
if( rc != JX9_OK ){
return 0;
}
return (int)pValue->x.iVal;
}
/*
* [CAPIREF: jx9_value_to_bool()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE int jx9_value_to_bool(jx9_value *pValue)
{
int rc;
rc = jx9MemObjToBool(pValue);
if( rc != JX9_OK ){
return 0;
}
return (int)pValue->x.iVal;
}
/*
* [CAPIREF: jx9_value_to_int64()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE jx9_int64 jx9_value_to_int64(jx9_value *pValue)
{
int rc;
rc = jx9MemObjToInteger(pValue);
if( rc != JX9_OK ){
return 0;
}
return pValue->x.iVal;
}
/*
* [CAPIREF: jx9_value_to_double()]