-
Notifications
You must be signed in to change notification settings - Fork 10
/
ql_misc.qpp
3070 lines (2468 loc) · 116 KB
/
ql_misc.qpp
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
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
ql_misc.qpp
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#include <qore/Qore.h>
#include "qore/intern/ql_misc.h"
#include "qore/intern/QoreSignal.h"
#include "qore/intern/QoreNamespaceIntern.h"
#include "qore/intern/QC_Program.h"
#include "qore/intern/ModuleInfo.h"
#include "qore/intern/qore_program_private.h"
#include "qore/intern/QoreHashNodeIntern.h"
#include <cerrno>
#include <cstring>
#include <ctime>
#ifndef WARN_MODULES
// needed so that the Qore default argument value in sinatures below will match a C++ value
#define WARN_MODULES QP_WARN_MODULES
#endif
static const QoreFunction* get_builtin_func(const QoreStringNode* str, ExceptionSink* xsink) {
const qore_ns_private* ns;
const QoreFunction* f = qore_root_ns_private::runtimeFindFunction(*(getRootNS()), str->c_str(), ns);
if (f)
return f;
xsink->raiseException("NO-FUNCTION", "cannot find any builtin function '%s()'", str->c_str());
return nullptr;
}
static const char* tlist[] = { "OPTION", "ALGORITHM", "FUNCTION" };
// signal constants - if they are not defined define them here
#ifndef SIGABRT
#define SIGABRT 0
#endif
#ifndef SIGALRM
#define SIGALRM 0
#endif
#ifndef SIGBUS
#define SIGBUS 0
#endif
#ifndef SIGCANCEL
#define SIGCANCEL 0
#endif
#ifndef SIGCHLD
#define SIGCHLD 0
#endif
#ifndef SIGCLD
#define SIGCLD 0
#endif
#ifndef SIGCONT
#define SIGCONT 0
#endif
#ifndef SIGEMT
#define SIGEMT 0
#endif
#ifndef SIGFPE
#define SIGFPE 0
#endif
#ifndef SIGFREEZE
#define SIGFREEZE 0
#endif
#ifndef SIGHUP
#define SIGHUP 0
#endif
#ifndef SIGILL
#define SIGILL 0
#endif
#ifndef SIGINFO
#define SIGINFO 0
#endif
#ifndef SIGINT
#define SIGINT 0
#endif
#ifndef SIGIO
#define SIGIO 0
#endif
#ifndef SIGIOT
#define SIGIOT 0
#endif
#ifndef SIGJVM1
#define SIGJVM1 0
#endif
#ifndef SIGJVM2
#define SIGJVM2 0
#endif
#ifndef SIGKILL
#define SIGKILL 0
#endif
#ifndef SIGLOST
#define SIGLOST 0
#endif
#ifndef SIGLWP
#define SIGLWP 0
#endif
#ifndef SIGPIPE
#define SIGPIPE 0
#endif
#ifndef SIGPOLL
#define SIGPOLL 0
#endif
#ifndef SIGPROF
#define SIGPROF 0
#endif
#ifndef SIGPWR
#define SIGPWR 0
#endif
#ifndef SIGQUIT
#define SIGQUIT 0
#endif
#ifndef SIGSEGV
#define SIGSEGV 0
#endif
#ifndef SIGSTKFLT
#define SIGSTKFLT 0
#endif
#ifndef SIGSTOP
#define SIGSTOP 0
#endif
#ifndef SIGSYS
#define SIGSYS 0
#endif
#ifndef SIGTERM
#define SIGTERM 0
#endif
#ifndef SIGTHAW
#define SIGTHAW 0
#endif
#ifndef SIGTRAP
#define SIGTRAP 0
#endif
#ifndef SIGTSTP
#define SIGTSTP 0
#endif
#ifndef SIGTTIN
#define SIGTTIN 0
#endif
#ifndef SIGTTOU
#define SIGTTOU 0
#endif
#ifndef SIGURG
#define SIGURG 0
#endif
#ifndef SIGUSR1
#define SIGUSR1 0
#endif
#ifndef SIGUSR2
#define SIGUSR2 0
#endif
#ifndef SIGVTALRM
#define SIGVTALRM 0
#endif
#ifndef SIGWAITING
#define SIGWAITING 0
#endif
#ifndef SIGWINCH
#define SIGWINCH 0
#endif
#ifndef SIGXCPU
#define SIGXCPU 0
#endif
#ifndef SIGXFSZ
#define SIGXFSZ 0
#endif
#ifndef SIGXRES
#define SIGXRES 0
#endif
/** @defgroup parse_url_options URL Parsing Options
@since %Qore 1.0.10
*/
///@{
//! If the hostname or address is enclosed in square brackets, the brackets will be included in the \c "host" key
/** Square brackets are used by some %Qore methods to denote IPv6 addresses; for example see
@ref Qore::Socket::connect() "Socket::connect()"
*/
const QURL_KEEP_BRACKETS = QURL_KEEP_BRACKETS;
//! Perform percent decoding on the \c "host", \c "username", and \c "password" fields
const QURL_DECODE = QURL_DECODE;
//! Decodes all fields like @ref QURL_DECODE plus also performs percent decoding on \c "path" and \c "query" fields
const QURL_DECODE_PATH = QURL_DECODE_PATH;
///@}
//! a hash describing a parsed URL
/** @since %Qore 0.9.3
*/
hashdecl UrlInfo {
//! the scheme or protocol of the URL, if present
string protocol;
//! the path given in the URL string, if present
string path;
//! the query part of the URL (i.e. text after any \c '?' char), if present
string query;
//! the username of the URL, if present
string username;
//! the password given in the URL, if any
string password;
//! the hostname given in the URL string, if any
string host;
//! the port number given in the URL string, if any
int port;
}
/** @defgroup StringConcatEncoding String Concatenation Encoding Codes
@see <string>::getEncoded()
@since %Qore 0.8.12
*/
///@{
//! code for encoding HTML 5 symbols as named character references
/** encodes all HTML 5 symbols as <a href="http://www.w3.org/TR/html5/syntax.html#named-character-references">named character references</a>
@note to encode all non-ascii symbols as numeric character references as well, include @ref Qore::CE_NONASCII in the bitfield
@see @ref Qore::CD_HTML
*/
const CE_HTML = CE_HTML;
//! code for encoding XML entities
/** The following symbols are encoded:
- \c '\"': as \c """
- \c "&": as \c "&"
- \c "'": as \c "'"
- \c "<": as \c "<"
- \c ">": as \c ">"
@note \c "'" / \c "'" is the only character not included in @ref Qore::CE_HTML
@see @ref Qore::CD_XML
*/
const CE_XML = CE_XML;
//! code for encoding all non-ASCII symbols as numeric character references
/** using this code ensures that the resulting string has no non-ASCII characters
@see @ref Qore::CD_NUM_REF
*/
const CE_NONASCII = CE_NONASCII;
//! code for encoding XHTML entities
/** This code is a combination of @ref Qore::CE_HTML and @ref Qore::CE_XML
@see @ref Qore::CD_XHTML
*/
const CE_XHTML = CE_XHTML;
//! code for encoding everything
/** @see @ref Qore::CD_ALL
*/
const CE_ALL = CE_ALL;
///@}
/** @defgroup StringConcatDecoding String Concatenation Decoding Codes
@see <string>::getDecoded()
@since %Qore 0.8.12
*/
///@{
//! code for decoding HTML 5 named character references to their native symbols
/** decodes all HTML 5 <a href="http://www.w3.org/TR/html5/syntax.html#named-character-references">named character references</a> to their native symbols
@note to decode all numeric character references as well, include @ref Qore::CD_NUM_REF in the bitfield
@see @ref Qore::CE_HTML
*/
const CD_HTML = CD_HTML;
//! code for decoding XML entities
/** The following symbols are decoded:
- \c '\"': as \c """
- \c "&": as \c "&"
- \c "'": as \c "'"
- \c "<": as \c "<"
- \c ">": as \c ">"
@note \c "'" / \c "'" is the only character not included in @ref Qore::CD_HTML
@see @ref Qore::CE_XML
*/
const CD_XML = CD_XML;
//! code for decoding numeric character references to symbols
/** @see @ref CE_NONASCII
*/
const CD_NUM_REF = CD_NUM_REF;
//! code for decoding XHTML named character references to symbols
/** This code is a combination of @ref Qore::CD_HTML and @ref Qore::CD_XML
@see @ref Qore::CE_XHTML
*/
const CD_XHTML = CD_XHTML;
//! code for decoding everything
/** @see @ref Qore::CE_ALL
*/
const CD_ALL = CD_ALL;
///@}
/** @defgroup signal_constants Signal Constants
Signal constants - if any of the constants in this section are not defined on the host; the constant's value will be 0
*/
///@{
//! SIGABRT
const SIGABRT = SIGABRT;
//! SIGALRM
const SIGALRM = SIGALRM;
//! SIGBUS
const SIGBUS = SIGBUS;
//! SIGCANCEL
const SIGCANCEL = SIGCANCEL;
//! SIGCHLD
const SIGCHLD = SIGCHLD;
//! SIGCLD
const SIGCLD = SIGCLD;
//! SIGCONT
const SIGCONT = SIGCONT;
//! SIGEMT
const SIGEMT = SIGEMT;
//! SIGFPE
const SIGFPE = SIGFPE;
//! SIGFREEZE
const SIGFREEZE = SIGFREEZE;
//! SIGHUP
const SIGHUP = SIGHUP;
//! SIGILL
const SIGILL = SIGILL;
//! SIGINFO
const SIGINFO = SIGINFO;
//! SIGINT
const SIGINT = SIGINT;
//! SIGIO
const SIGIO = SIGIO;
//! SIGIOT
const SIGIOT = SIGIOT;
//! SIGJVM1
const SIGJVM1 = SIGJVM1;
//! SIGJVM2
const SIGJVM2 = SIGJVM2;
//! SIGKILL
const SIGKILL = SIGKILL;
//! SIGLOST
const SIGLOST = SIGLOST;
//! SIGLWP
const SIGLWP = SIGLWP;
//! SIGPIPE
const SIGPIPE = SIGPIPE;
//! SIGPOLL
const SIGPOLL = SIGPOLL;
//! SIGPROF
const SIGPROF = SIGPROF;
//! SIGPWR
const SIGPWR = SIGPWR;
//! SIGQUIT
const SIGQUIT = SIGQUIT;
//! SIGSEGV
const SIGSEGV = SIGSEGV;
//! SIGSTKFLT
const SIGSTKFLT = SIGSTKFLT;
//! SIGSTOP
const SIGSTOP = SIGSTOP;
//! SIGSYS
const SIGSYS = SIGSYS;
//! SIGTERM
const SIGTERM = SIGTERM;
//! SIGTHAW
const SIGTHAW = SIGTHAW;
//! SIGTRAP
const SIGTRAP = SIGTRAP;
//! SIGTSTP
const SIGTSTP = SIGTSTP;
//! SIGTTIN
const SIGTTIN = SIGTTIN;
//! SIGTTOU
const SIGTTOU = SIGTTOU;
//! SIGURG
const SIGURG = SIGURG;
//! SIGUSR1
const SIGUSR1 = SIGUSR1;
//! SIGUSR2
const SIGUSR2 = SIGUSR2;
//! SIGVTALRM
const SIGVTALRM = SIGVTALRM;
//! SIGWAITING
const SIGWAITING = SIGWAITING;
//! SIGWINCH
const SIGWINCH = SIGWINCH;
//! SIGXCPU
const SIGXCPU = SIGXCPU;
//! SIGXFSZ
const SIGXFSZ = SIGXFSZ;
//! SIGXRES
const SIGXRES = SIGXRES;
//! maps signal names to signal values
const NameToSignal = (
"SIGABRT": SIGABRT,
"SIGALRM": SIGALRM,
"SIGBUS": SIGBUS,
"SIGCANCEL": SIGCANCEL,
"SIGCHLD": SIGCHLD,
"SIGCLD": SIGCLD,
"SIGCONT": SIGCONT,
"SIGEMT": SIGEMT,
"SIGFPE": SIGFPE,
"SIGFREEZE": SIGFREEZE,
"SIGHUP": SIGHUP,
"SIGILL": SIGILL,
"SIGINFO": SIGINFO,
"SIGINT": SIGINT,
"SIGIO": SIGIO,
"SIGIOT": SIGIOT,
"SIGJVM1": SIGJVM1,
"SIGJVM2": SIGJVM2,
"SIGKILL": SIGKILL,
"SIGLOST": SIGLOST,
"SIGLWP": SIGLWP,
"SIGPIPE": SIGPIPE,
"SIGPOLL": SIGPOLL,
"SIGPROF": SIGPROF,
"SIGPWR": SIGPWR,
"SIGQUIT": SIGQUIT,
"SIGSEGV": SIGSEGV,
"SIGSTKFLT": SIGSTKFLT,
"SIGSTOP": SIGSTOP,
"SIGSYS": SIGSYS,
"SIGTERM": SIGTERM,
"SIGTHAW": SIGTHAW,
"SIGTRAP": SIGTRAP,
"SIGTSTP": SIGTSTP,
"SIGTTIN": SIGTTIN,
"SIGTTOU": SIGTTOU,
"SIGURG": SIGURG,
"SIGUSR1": SIGUSR1,
"SIGUSR2": SIGUSR2,
"SIGVTALRM": SIGVTALRM,
"SIGWAITING": SIGWAITING,
"SIGWINCH": SIGWINCH,
"SIGXCPU": SIGXCPU,
"SIGXFSZ": SIGXFSZ,
"SIGXRES": SIGXRES,
);
//! maps signal numbers (as a string key) to the symbolic name for the signal
const SignalToName = (
SIGABRT: "SIGABRT",
SIGALRM: "SIGALRM",
SIGBUS: "SIGBUS",
SIGCANCEL: "SIGCANCEL",
SIGCHLD: "SIGCHLD",
SIGCONT: "SIGCONT",
SIGEMT: "SIGEMT",
SIGFPE: "SIGFPE",
SIGFREEZE: "SIGFREEZE",
SIGHUP: "SIGHUP",
SIGILL: "SIGILL",
SIGINFO: "SIGINFO",
SIGINT: "SIGINT",
SIGIO: "SIGIO",
SIGIOT: "SIGIOT",
SIGJVM1: "SIGJVM1",
SIGJVM2: "SIGJVM2",
SIGKILL: "SIGKILL",
SIGLOST: "SIGLOST",
SIGLWP: "SIGLWP",
SIGPIPE: "SIGPIPE",
SIGPOLL: "SIGPOLL",
SIGPROF: "SIGPROF",
SIGPWR: "SIGPWR",
SIGQUIT: "SIGQUIT",
SIGSEGV: "SIGSEGV",
SIGSTKSZ: "SIGSTKSZ",
SIGSTOP: "SIGSTOP",
SIGSYS: "SIGSYS",
SIGTERM: "SIGTERM",
SIGTHAW: "SIGTHAW",
SIGTRAP: "SIGTRAP",
SIGTSTP: "SIGTSTP",
SIGTTIN: "SIGTTIN",
SIGTTOU: "SIGTTOU",
SIGURG: "SIGURG",
SIGUSR1: "SIGUSR1",
SIGUSR2: "SIGUSR2",
SIGVTALRM: "SIGVTALRM",
SIGWAITING: "SIGWAITING",
SIGWINCH: "SIGWINCH",
SIGXCPU: "SIGXCPU",
SIGXFSZ: "SIGXFSZ",
SIGXRES: "SIGXRES",
);
///@}
/** @defgroup signal_handling_functions Signal Handing Functions
Signal handing functions
*/
///@{
//! Sets or replaces a signal handler according to the signal number and closure or call reference (function or object method reference) passed
/** @par Platform Availability:
@ref Qore::Option::HAVE_SIGNAL_HANDLING
By the time this function returns, changes to the signal handling thread have already been effected.
When a signal is raised and the signal handler code is called, the signal number is passed as an integer argument to the signal handling code.
@param signal The signal number to process, see @ref signal_constants for possible values
@param f The code to execute when the signal is caught; this should accept an integer argument giving the signal number
@par Example:
@code{.py}
set_signal_handler(SIGINT, \signal_handler());
@endcode
@see @ref signal_handling for more information
*/
nothing set_signal_handler(softint signal, code f) [dom=PROCESS] {
#ifdef HAVE_SIGNAL_HANDLING
if (signal <= 0 || signal > QORE_SIGNAL_MAX)
return xsink->raiseException("SET-SIGNAL-HANDLER-ERROR", "%d is not a valid signal", signal);
QSM.setHandler(signal, f, xsink);
#else
return xsink->raiseException("MISSING-FEATURE-ERROR", "this platform does not support signal handling, therefore "
"the set_signal_handler() and remove_signal_handler() functions are not available in Qore; for maximum "
"portability, use the constant Option::HAVE_SIGNAL_HANDLING to check if this function is implemented before "
"calling");
#endif
}
//! Removes a signal handler and returns the signal handling state to the default
/**
@par Platform Availability:
@ref Qore::Option::HAVE_SIGNAL_HANDLING
By the time this function returns, changes to the signal handling thread have already been effected.
@param signal The signal number to process, see @ref signal_constants for possible values
@par Example:
@code{.py}
remove_signal_handler(SIGINT);
@endcode
@see @ref signal_handling for more information
*/
nothing remove_signal_handler(softint signal) [dom=PROCESS] {
#ifdef HAVE_SIGNAL_HANDLING
if (signal <= 0 || signal > QORE_SIGNAL_MAX) {
return xsink->raiseException("REMOVE-SIGNAL-HANDLER-ERROR", "%d is not a valid signal", signal);
}
QSM.removeHandler(signal, xsink);
#else
return xsink->raiseException("MISSING-FEATURE-ERROR", "this platform does not support signal handling, therefore "
"the set_signal_handler() and remove_signal_handler() functions are not available in Qore; for maximum "
"portability, use the constant Option::HAVE_SIGNAL_HANDLING to check if this function is implemented before "
"calling");
#endif
}
///@}
/** @defgroup misc_functions Miscellaneous Functions
Miscellaneous functions
*/
///@{
//! Calls a function and returns the return value, passing the remaining arguments after the function name to the function
/**
@param name The name of the function to call
@param ... Any optional arguments to the function
@return The value returned by the called function
@par Example:
@code{.py}
auto result = call_function("func_name", arg1, arg2);
@endcode
@throw INVALID-FUNCTION-ACCESS Parse options do not allow access to the function
@throw NO-FUNCTION The function does not exist
@note The function called could also throw other exceptions
@see
- call_function_args()
- call_static_method()
- call_static_method_args()
- call_object_method()
- call_object_method_args()
*/
auto call_function(string name, ...) {
ReferenceHolder<QoreListNode> vargs(xsink);
// if there are arguments to pass, create argument list by copying current list
if (num_args(args) > 1) {
vargs = args->copyListFrom(1);
}
return getProgram()->callFunction(name->c_str(), *vargs, xsink);
}
//! Calls the given @ref call_reference "call reference" or @ref closure "closure" and returns the result, passing the remaining arguments to the @ref call_reference "call reference" or @ref closure "closure"
/**
@param f The @ref call_reference "call reference" or @ref closure "closure"
@param ... Any optional arguments to the @ref call_reference "call reference" or @ref closure "closure"
@par Example:
@code{.py}
auto result = call_function(call_ref, arg1, arg2);
@endcode
@note The @ref call_reference "call reference" or @ref closure "closure" called could also throw other exceptions
@see
- call_function_args()
- call_static_method()
- call_static_method_args()
- call_object_method()
- call_object_method_args()
*/
auto call_function(code f, ...) {
ReferenceHolder<QoreListNode> vargs(xsink);
// if there are arguments to pass, create argument list by copying current list
if (num_args(args) > 1) {
vargs = args->copyListFrom(1);
}
return f->execValue(*vargs, xsink);
}
//! Calls a function and returns the return value, using the optional second argument as a list of arguments for the function
/**
@param name The name of the function to call
@param vargs Optionally a single argument to the function or a list of arguments to the function
@return The value returned by the called function
@par Example:
@code{.py}
auto result = call_function_args("func_name", (arg1, arg2));
@endcode
@throw INVALID-FUNCTION-ACCESS Parse options do not allow access to the function
@throw NO-FUNCTION The function does not exist
@note The function called could also throw other exceptions
@see
- call_function()
- call_static_method()
- call_static_method_args()
- call_object_method()
- call_object_method_args()
*/
auto call_function_args(string name, *softlist<auto> vargs) {
return getProgram()->callFunction(name->c_str(), vargs, xsink);
}
//! Calls the given @ref call_reference "call reference" or @ref closure "closure" and returns the result, using the optional second argument as a list of arguments to the @ref call_reference "call reference" or @ref closure "closure"
/**
@param f The @ref call_reference "call reference" or @ref closure "closure"
@param vargs Optionally a single argument to the @ref call_reference "call reference" or @ref closure "closure" or a list of arguments to the @ref call_reference "call reference" or @ref closure "closure"
@par Example:
@code{.py}
auto result = call_function_args(call_ref, (arg1, arg2));
@endcode
@note The @ref call_reference "call reference" or @ref closure "closure" called could also throw other exceptions
@see
- call_function()
- call_static_method()
- call_static_method_args()
- call_object_method()
- call_object_method_args()
*/
auto call_function_args(code f, *softlist<auto> vargs) {
return f->execValue(vargs, xsink);
}
//! Calls a function and returns the return value, passing the remaining arguments after the function name to the builtin function
/**
@param name The name of the builtin function to call
@param ... Any optional arguments to the function
@return The value returned by the called function
@par Example:
@code{.py}
auto result = call_builtin_function("func_name", arg1, arg2);
@endcode
@throw INVALID-FUNCTION-ACCESS Parse options do not allow access to the function
@throw NO-FUNCTION The function does not exist
@note The function called could also throw other exceptions
@since 0.8.4 this function no longer restricts its search to builtin functions as as of Qore 0.8.4 builtin and user functions are stored identically internally; there is only one function implementation which can contain both builtin and user variants
*/
auto call_builtin_function(string name, ...) {
const QoreFunction* f = get_builtin_func(name, xsink);
if (!f)
return QoreValue();
ReferenceHolder<QoreListNode> vargs(xsink);
// if there are arguments to pass, create argument list by copying current list
if (num_args(args) > 1) {
vargs = args->copyListFrom(1);
}
return f->evalDynamic(*vargs, xsink);
}
//! Calls a function and returns the return value, using the optional second argument as a list of arguments for the function
/**
@param name The name of the builtin function to call
@param vargs Optionally a single argument to the function or a list of arguments to the function
@return The value returned by the called function
@par Example:
@code{.py}
auto result = call_builtin_function_args("func_name", (arg1, arg2));
@endcode
@throw INVALID-FUNCTION-ACCESS Parse options do not allow access to the function
@throw NO-FUNCTION The function does not exist
@note The function called could also throw other exceptions
@since 0.8.4 this function no longer restricts its search to builtin functions as as of Qore 0.8.4 builtin and user functions are stored identically internally; there is only one function implementation which can contain both builtin and user variants
*/
auto call_builtin_function_args(string name, *softlist<auto> vargs) {
const QoreFunction* f = get_builtin_func(name, xsink);
if (!f)
return QoreValue();
return f->evalDynamic(vargs, xsink);
}
//! A function performing the same role as the @ref exists "exists operator"
/** @param ... if only a single argument is passed, then this function returns @ref True if the single argument exists, @ref False if not; otherwise is multiple arguments are passed to the function, it always returns @ref True; this is to emulate the behavior of the @ref exists "exists operator"
@return if only a single argument is passed, then this function returns @ref True if the single argument exists, @ref False if not; otherwise is multiple arguments are passed to the function, it always returns @ref True; this is to emulate the behavior of the @ref exists "exists operator"
@par Example:
@code{.py}
bool b = exists(val);
@endcode
*/
bool exists(...) [flags=CONSTANT] {
// to emulate the exists operator, we must return True if more than one argument is passed
// as this will appear to be a list to the exists operator, which is different from NOTHING
return (num_args(args) <= 1) ? !get_param_value(args, 0).isNothing() : true;
}
//! Returns @ref True if the function exists in the current program's function name space
/** @param name the name of the function to check
@return @ref True if the function exists in the current program's function name space, @ref False if not
@deprecated use exists_function(); camel-case function names were deprecated in %Qore 0.8.12
*/
bool existsFunction(string name) [flags=CONSTANT,DEPRECATED] {
return getProgram()->existsFunction(name->c_str());
}
//! Always returns @ref True
/** This function variant is included for backwards-compatibility
@param c a @ref call_reference "call reference" or @ref closure "closure":
@return always returns @ref True
@deprecated use exists_function(); camel-case function names were deprecated in %Qore 0.8.12
*/
bool existsFunction(code[doc] c) [flags=NOOP,DEPRECATED] {
return true;
}
//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
*/
nothing existsFunction() [flags=RUNTIME_NOOP,DEPRECATED] {
}
//! Returns @ref True if the function exists in the current program's function name space
/** @param name the name of the function to check
@return @ref True if the function exists in the current program's function name space, @ref False if not
@par Example:
@code{.py}
bool b = exists_function("func_name");
@endcode
@since %Qore 0.8.12 as a replacement for deprecated camel-case existsFunction()
*/
bool exists_function(string name) [flags=CONSTANT] {
return getProgram()->existsFunction(name->c_str());
}
//! Always returns @ref True
/** This function variant is included for backwards-compatibility
@param c a @ref call_reference "call reference" or @ref closure "closure":
@return always returns @ref True
@since %Qore 0.8.12 as a replacement for deprecated camel-case existsFunction()
*/
bool exists_function(code[doc] c) [flags=NOOP] {
return true;
}
//! Returns \c "builtin" (for a builtin function), \c "user" (for a user function), or @ref nothing (if the function cannot be found) according to the function name passed
/**
@param name The function name to check
@return \c "builtin" (for a builtin function), \c "user" (for a user function), or @ref nothing (if the function cannot be found) according to the function name passed
@deprecated use function_type(); camel-case function names were deprecated in %Qore 0.8.12
*/
*string functionType(string name) [flags=CONSTANT,DEPRECATED] {
const qore_ns_private* ns;
const QoreFunction* f = qore_root_ns_private::runtimeFindFunction(*(getRootNS()), name->c_str(), ns);
if (!f)
return QoreValue();
return new QoreStringNode(f->hasBuiltin() ? "builtin" : "user");
}
//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
*/
nothing functionType() [flags=RUNTIME_NOOP,DEPRECATED] {
}
//! Returns \c "builtin" (for a builtin function), \c "user" (for a user function), or @ref nothing (if the function cannot be found) according to the function name passed
/**
@param name The function name to check
@return \c "builtin" (for a builtin function), \c "user" (for a user function), or @ref nothing (if the function cannot be found) according to the function name passed
@par Example:
@code{.py}
*string type = function_type("print");
@endcode
@since %Qore 0.8.12 as a replacement for deprecated camel-case functionType()
*/
*string function_type(string name) {
const qore_ns_private* ns;
const QoreFunction* f = qore_root_ns_private::runtimeFindFunction(*(getRootNS()), name->c_str(), ns);
if (!f)
return QoreValue();
return new QoreStringNode(f->hasBuiltin() ? "builtin" : "user");
}
//! Returns a string with characters needing HTML escaping translated to HTML escape codes
/** @param str the argument to process
@return the string passed as an argument with characters needing HTML escaping translated to HTML escape codes
@par Example:
@code{.py}
string str = html_encode("<hello>"); # returns "<hello>"
@endcode
@note equivalent to <string>::getEncoded() with @ref Qore::CE_HTML
@see <string>::getEncoded()
*/
string html_encode(string str) [flags=CONSTANT] {
QoreStringNodeHolder rv(new QoreStringNode(str->getEncoding()));
return rv->concatEncode(xsink, *str, CE_HTML) ? QoreValue() : rv.release();
}
//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
*/
nothing html_encode() [flags=RUNTIME_NOOP] {
}
//! Returns a string with any HTML escape codes translated to the original characters
/** @param str the argument to decode
@return the string passed as an argument with any HTML escape codes translated to the original characters
@par Example:
@code{.py}
string str = html_decode("<hello>"); # returns "<hello>"
@endcode
@note equivalent to <string>::getDecoded() with @ref Qore::CD_HTML
@see <string>::getDecoded()
*/
string html_decode(string str) [flags=CONSTANT] {
QoreStringNodeHolder rv(new QoreStringNode(str->getEncoding()));
return rv->concatDecode(xsink, *str, CD_HTML) ? QoreValue() : rv.release();
}
//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
*/
nothing html_decode() [flags=RUNTIME_NOOP] {
}
//! Returns the name of the @ref default_encoding "default character encoding"
/** @return the name of the @ref default_encoding "default character encoding"
@par Example:
@code{.py}
string encoding = get_default_encoding();
@endcode
*/
string get_default_encoding() {
return new QoreStringNode(QCS_DEFAULT->getCode());
}
//! Adds the text passed to the current program's code, tagged with the given label
/**
@param code the string of %Qore source code to parse; the parsed code will be added to the current program
@param label a label identifying the code or the code's source
@param warning_mask An optional warning mask; see @ref warning_constants for values to combine by binary-or; if this arguments is 0 or not given then no warnings will be checked or issued and the return value will always be @ref nothing
@param source An optional source file name for the code being parsed; this is useful if sections of a file are parsed
@param offset An optional line offset for use with the \a source parameter; this gives the line offset in the file to the code being parsed
@param format_label %Qore 0.9 is obsolete / ignored
@par Example:
@code{.py}
parse(code, filename);
@endcode
@note This function could throw many parse exceptions which are not enumerated here; any parse errors will result in an appropriate exception.
@see
- Qore::Program::parse()
- Qore::Program::parsePending()
@since %Qore 0.8.7 the optional \a warning_mask, \a source, \a offset, and \a format_label arguments were added; %Qore 0.9 the \a format_label is obsolete / ignored;
these changes were made to align the functionality of this function with the @ref Qore::Program::parse() "Program::parse()"
and @ref Qore::Program::parsePending() "Program::parsePending()" methods
*/
*hash<auto> parse(string code, string label, *softint warning_mask, *string source, *softint offset, softbool[doc] format_label = True) [dom=EMBEDDED_LOGIC,IN_MODULE] {
if (warning_mask) {
ExceptionSink wsink;
getProgram()->parse(code, label, xsink, &wsink, warning_mask, source, offset);
if (!wsink.isException())
return QoreValue();
QoreException* e = wsink.catchException();
return e->makeExceptionObjectAndDelete(xsink);
}
getProgram()->parse(code, label, xsink, 0, 0, source, offset);
}
//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
*/
nothing parse() [flags=RUNTIME_NOOP;dom=EMBEDDED_LOGIC,IN_MODULE] {
}
//! Returns the class name of the object passed
/**
@param obj the object to get the class name of
@return the class name of the object passed
@deprecated use get_class_name(); camel-case function names were deprecated in %Qore 0.8.12