Annotation of parser3/src/classes/op.C, revision 1.292
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
1.280 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.262 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.98 paf 6: */
1.1 paf 7:
1.13 paf 8: #include "classes.h"
1.129 paf 9: #include "pa_vmethod_frame.h"
10:
1.1 paf 11: #include "pa_common.h"
1.134 paf 12: #include "pa_os.h"
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vint.h"
15: #include "pa_sql_connection.h"
1.79 paf 16: #include "pa_vdate.h"
1.105 paf 17: #include "pa_vmethod_frame.h"
1.129 paf 18: #include "pa_vclass.h"
1.144 paf 19: #include "pa_charset.h"
1.279 moko 20: #include "pa_varray.h"
1.1 paf 21:
1.273 moko 22: #ifdef HAVE_SYSLOG
23: #include "syslog.h"
24: #endif
25:
1.292 ! moko 26: volatile const char * IDENT_OP_C="$Id: op.C,v 1.291 2026/10/31 17:03:23 moko Exp $";
1.18 parser 27:
1.10 paf 28: // defines
29:
1.82 paf 30: #define CASE_DEFAULT_VALUE "DEFAULT"
1.11 paf 31:
1.10 paf 32: // class
33:
1.113 paf 34: class VClassMAIN: public VClass {
1.10 paf 35: public:
1.129 paf 36: VClassMAIN();
1.287 moko 37: /// native methods are copied from the donor
38: VClassMAIN(VClassMAIN& donor): VClass(MAIN_CLASS_NAME, 0, &donor) {}
1.10 paf 39: };
40:
1.129 paf 41: // defines for statics
42:
43: #define SWITCH_DATA_NAME "SWITCH-DATA"
44: #define CACHE_DATA_NAME "CACHE-DATA"
1.164 paf 45:
1.129 paf 46: #define EXCEPTION_VAR_NAME "exception"
47:
48: // statics
49:
50: //^switch ^case
51: static const String switch_data_name(SWITCH_DATA_NAME);
52: //^cache
53: static const String cache_data_name(CACHE_DATA_NAME);
54:
55: static const String exception_var_name(EXCEPTION_VAR_NAME);
56:
1.164 paf 57:
1.136 paf 58: // local defines
59:
60: #define CACHE_EXCEPTION_HANDLED_CACHE_NAME "cache"
61:
1.10 paf 62: // methods
63:
1.129 paf 64: static void _if(Request& r, MethodParams& params) {
1.200 moko 65: size_t max_param=params.count()-1;
66: size_t i=0;
67: do {
68: bool condition=params.as_bool(i, "condition must be expression", r);
69: if(condition) {
1.238 moko 70: r.process_write(params[i+1]);
1.200 moko 71: return;
72: }
73: i+=2;
74: } while (i < max_param);
75:
76: if(i == max_param)
1.238 moko 77: r.process_write(params[i]);
1.1 paf 78: }
79:
1.129 paf 80: static void _untaint(Request& r, MethodParams& params) {
81: String::Language lang;
82: if(params.count()==1)
1.241 moko 83: lang=String::L_AS_IS; // mark as simply 'as-is'. useful in html from sql
1.175 misha 84: else
1.286 moko 85: lang=String::name2lang(params.as_string(0, "lang must be string"));
1.1 paf 86:
1.241 moko 87: Value& vbody=params.as_junction(params.count()-1, "body must be code");
88: Value& result=r.process(vbody);
89:
90: if(const String* string=result.get_string()){
91: String &untainted=*new String();
92: string->append_to(untainted, lang); // mark all tainted to specified language
1.243 moko 93: r.write(untainted);
1.241 moko 94: } else
1.243 moko 95: r.write(result); // this is not normal, just backward compatibility
1.1 paf 96: }
97:
1.129 paf 98: static void _taint(Request& r, MethodParams& params) {
99: String::Language lang;
100: if(params.count()==1)
1.175 misha 101: lang=String::L_TAINTED; // mark as simply 'tainted'. useful in table:create
102: else
1.286 moko 103: lang=String::name2lang(params.as_string(0, "lang must be string"));
1.1 paf 104:
105: {
1.129 paf 106: Value& vbody=params.as_no_junction(params.count()-1, "body must not be code");
1.1 paf 107:
1.191 misha 108: String result(vbody.as_string(), lang); // force result language to specified
1.243 moko 109: r.write(result);
1.1 paf 110: }
111: }
112:
1.205 moko 113: static void _apply_taint(Request& r, MethodParams& params) {
1.286 moko 114: String::Language lang=params.count()==1 ? String::L_AS_IS : String::name2lang(params.as_string(0, "lang must be string"));
1.205 moko 115: const String &sbody=params.as_string(params.count()-1, "body must be string");
1.213 moko 116: String::Body result_body=sbody.cstr_to_string_body_untaint(lang, r.connection(false), &r.charsets);
1.243 moko 117: r.write(*new String(result_body, String::L_AS_IS));
1.205 moko 118: }
119:
1.129 paf 120: static void _process(Request& r, MethodParams& params) {
121: Method* main_method;
122:
123: size_t index=0;
124: Value* target_self;
125: Value& maybe_target_self=params[index];
126: if(maybe_target_self.get_string() || maybe_target_self.get_junction())
127: target_self=&r.get_method_frame()->caller()->self();
128: else {
1.233 moko 129: target_self=&maybe_target_self;
130: if(params.count()==1)
131: throw Exception(PARSER_RUNTIME, 0, "no body specified");
132: index++;
1.129 paf 133: }
134:
1.1 paf 135: {
1.197 misha 136: VStateless_class *target_class=target_self->get_class();
1.116 paf 137: if(!target_class)
1.232 moko 138: throw Exception(PARSER_RUNTIME, 0, "no target class");
1.114 paf 139:
1.1 paf 140: // temporary zero @main so to maybe-replace it in processed code
1.129 paf 141: Temp_method temp_method_main(*target_class, main_method_name, 0);
142:
143: const String* main_alias=0;
1.146 paf 144: const String* file_alias=0;
145: int line_no_alias_offset=0;
1.216 moko 146: bool allow_class_replace=false;
1.211 misha 147:
148: size_t options_index=index+1;
149: if(options_index<params.count())
150: if(HashStringValue* options=params.as_hash(options_index)) {
1.216 moko 151: for(HashStringValue::Iterator i(*options); i; i.next() ){
152: String::Body key=i.key();
153: Value* value=i.value();
154: if(key == "main") {
155: main_alias=&value->as_string();
156: } else if(key == "file") {
157: file_alias=&value->as_string();
158: } else if(key == "lineno") {
159: line_no_alias_offset=value->as_int();
160: } else if(key == "replace") {
1.236 moko 161: allow_class_replace=r.process(*value).as_bool();
1.275 moko 162: } else
163: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.211 misha 164: }
1.146 paf 165: }
1.129 paf 166:
1.258 moko 167: uint processe_file_no=file_alias ? r.register_file(r.full_disk_path(*file_alias)) : pseudo_file_no__process;
1.146 paf 168: // process...{string}
169: Value& vjunction=params.as_junction(index, "body must be code");
170: // evaluate source to process
171: const String& source=r.process_to_string(vjunction);
1.216 moko 172:
173: Temp_class_replace class_replace(r, allow_class_replace);
174:
1.291 moko 175: const char* code=source.untaint_cstr(String::L_PARSER_CODE, r.connection(false));
176: // compiler knows only \n (files are normalized on load)
177: if(strchr(code, '\r')) {
178: size_t length=strlen(code);
179: char* normalized=pa_strdup(code, length);
180: fix_line_breaks(normalized, length);
181: code=normalized;
182: }
183:
184: r.use_buf(*target_class, code, main_alias, processe_file_no, line_no_alias_offset);
1.1 paf 185:
1.73 paf 186: // main_method
1.129 paf 187: main_method=target_class->get_method(main_method_name);
1.73 paf 188: }
189: // after restoring current-request-lang
190: // maybe-execute @main[]
191: if(main_method) {
1.244 moko 192: METHOD_FRAME_ACTION(*main_method, r.get_method_frame()->caller(), *target_self, {
193: frame.empty_params();
194: r.call(frame);
195: r.write(frame.result());
196: });
1.1 paf 197: }
198: }
199:
1.138 paf 200: static void _rem(Request&, MethodParams& params) {
1.129 paf 201: params.as_junction(0, "body must be code");
1.1 paf 202: }
203:
1.129 paf 204: static void _while(Request& r, MethodParams& params) {
1.196 misha 205: InCycle temp(r);
1.164 paf 206:
1.170 misha 207: Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");
208:
1.162 paf 209: Value& body_code=params.as_junction(1, "body must be code");
210: Value* delim_maybe_code=params.count()>2?¶ms[2]:0;
1.1 paf 211:
212: // while...
213: int endless_loop_count=0;
1.186 misha 214: if(delim_maybe_code){ // delimiter set
1.185 misha 215: bool need_delim=false;
216: while(true) {
1.235 moko 217: if(++endless_loop_count>=pa_loop_limit) // endless loop?
218: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.185 misha 219:
1.237 moko 220: if(!r.process(vcondition).as_bool())
1.185 misha 221: break;
1.1 paf 222:
1.234 moko 223: Value& sv_processed=r.process(body_code);
1.249 moko 224: TempSkip4Delimiter skip(r);
1.186 misha 225:
1.185 misha 226: const String* s_processed=sv_processed.get_string();
1.186 misha 227: if(s_processed && !s_processed->is_empty()) { // we have body
1.185 misha 228: if(need_delim) // need delim & iteration produced string?
1.243 moko 229: r.write(r.process(*delim_maybe_code));
1.185 misha 230: else
231: need_delim=true;
232: }
1.243 moko 233: r.write(sv_processed);
1.1 paf 234:
1.249 moko 235: if(skip.check_break())
1.185 misha 236: break;
1.162 paf 237: }
1.185 misha 238: } else {
239: while(true) {
1.235 moko 240: if(++endless_loop_count>=pa_loop_limit) // endless loop?
241: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.185 misha 242:
1.237 moko 243: if(!r.process(vcondition).as_bool())
1.185 misha 244: break;
1.164 paf 245:
1.185 misha 246: r.process_write(body_code);
247:
1.249 moko 248: if(r.check_skip_break())
1.185 misha 249: break;
250: }
1.1 paf 251: }
252: }
253:
1.129 paf 254: static void _use(Request& r, MethodParams& params) {
1.270 moko 255: const String& file_name=params.as_file_name(0);
1.199 misha 256:
1.256 moko 257: const String* use_origin=0;
1.216 moko 258: bool allow_class_replace=false;
1.256 moko 259: bool load_auto_p=false;
1.216 moko 260:
261: if(params.count()==2)
262: if(HashStringValue* options=params.as_hash(1)) {
263: for(HashStringValue::Iterator i(*options); i; i.next() ){
264: String::Body key=i.key();
265: Value* value=i.value();
1.256 moko 266: if(key == "origin") {
267: use_origin=&value->as_string();
1.275 moko 268: } else if(key == "replace") {
1.236 moko 269: allow_class_replace=r.process(*value).as_bool();
1.275 moko 270: } else if(key == "main") {
1.256 moko 271: load_auto_p=r.process(*value).as_bool();
1.275 moko 272: } else
1.246 moko 273: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.216 moko 274: }
275: }
276:
1.246 moko 277: if(!use_origin)
278: if(VMethodFrame* caller=r.get_method_frame()->caller())
1.247 moko 279: use_origin=r.get_method_filespec(&caller->method);
1.246 moko 280:
1.216 moko 281: Temp_class_replace class_replace(r, allow_class_replace);
282:
1.269 moko 283: r.use_file(file_name, use_origin, load_auto_p);
1.1 paf 284: }
285:
1.164 paf 286: static void set_skip(Request& r, Request::Skip askip) {
1.196 misha 287: if(!r.get_in_cycle())
1.227 moko 288: throw Exception(askip==Request::SKIP_BREAK ? "parser.break" : "parser.continue", 0, "without cycle");
1.164 paf 289: r.set_skip(askip);
290: }
291:
1.227 moko 292: static void _break(Request& r, MethodParams& params) {
293: if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_BREAK);
1.164 paf 294: }
295:
1.227 moko 296: static void _continue(Request& r, MethodParams& params) {
297: if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_CONTINUE);
1.164 paf 298: }
299:
1.249 moko 300: static void _return(Request& r, MethodParams& params) {
1.250 moko 301: VMethodFrame& caller=*r.get_method_frame()->caller();
1.249 moko 302: if(params.count())
1.255 moko 303: r.put_element(caller, Symbols::RESULT_SYMBOL, &r.process(params[0]));
1.250 moko 304: r.set_skip_return(caller);
1.249 moko 305: }
306:
1.129 paf 307: static void _for(Request& r, MethodParams& params) {
1.196 misha 308: InCycle temp(r);
1.164 paf 309:
1.129 paf 310: const String& var_name=params.as_string(0, "var name must be string");
311: int from=params.as_int(1, "from must be int", r);
312: int to=params.as_int(2, "to must be int", r);
1.186 misha 313: Value& body_code=params.as_junction(3, "body must be code");
1.129 paf 314: Value* delim_maybe_code=params.count()>4?¶ms[4]:0;
1.1 paf 315:
1.235 moko 316: if(to-from>=pa_loop_limit) // too long loop?
317: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.57 paf 318:
1.129 paf 319: VInt* vint=new VInt(0);
1.154 paf 320:
321: VMethodFrame& caller=*r.get_method_frame()->caller();
1.218 moko 322: r.put_element(caller, var_name, vint);
1.186 misha 323: if(delim_maybe_code){ // delimiter set
1.185 misha 324: bool need_delim=false;
325:
326: for(int i=from; i<=to; i++) {
327: vint->set_int(i);
328:
1.234 moko 329: Value& sv_processed=r.process(body_code);
1.249 moko 330: TempSkip4Delimiter skip(r);
1.186 misha 331:
1.185 misha 332: const String* s_processed=sv_processed.get_string();
1.186 misha 333: if(s_processed && !s_processed->is_empty()) { // we have body
1.185 misha 334: if(need_delim) // need delim & iteration produced string?
1.243 moko 335: r.write(r.process(*delim_maybe_code));
1.185 misha 336: else
337: need_delim=true;
338: }
1.243 moko 339: r.write(sv_processed);
1.1 paf 340:
1.249 moko 341: if(skip.check_break())
1.185 misha 342: break;
1.1 paf 343: }
1.185 misha 344: } else {
345: for(int i=from; i<=to; i++) {
346: vint->set_int(i);
347:
348: r.process_write(body_code);
1.164 paf 349:
1.249 moko 350: if(r.check_skip_break())
1.185 misha 351: break;
352: }
1.1 paf 353: }
354: }
355:
1.129 paf 356: static void _eval(Request& r, MethodParams& params) {
357: Value& expr=params.as_junction(0, "need expression");
1.1 paf 358: // evaluate expresion
1.237 moko 359: Value& value_result=r.process(expr).as_expr_result();
1.129 paf 360: if(params.count()>1) {
1.177 misha 361: const String& fmt=params.as_string(1, "fmt must be string").trim();
362: if(fmt.is_empty()){
1.243 moko 363: r.write(value_result);
1.177 misha 364: } else {
1.264 moko 365: r.write(String(format_double(value_result.as_double(), fmt.cstrm())));
1.177 misha 366: }
1.91 paf 367: } else
1.243 moko 368: r.write(value_result);
1.1 paf 369: }
370:
1.129 paf 371: static void _connect(Request& r, MethodParams& params) {
372: Value& url=params.as_no_junction(0, "url must not be code");
373: Value& body_code=params.as_junction(1, "body must be code");
1.1 paf 374:
1.129 paf 375: Table* protocol2driver_and_client=0;
1.197 misha 376: if(Value* sql=r.main_class.get_element(String(MAIN_SQL_NAME))) {
377: if(Value* element=sql->get_element(String(MAIN_SQL_DRIVERS_NAME))) {
1.113 paf 378: protocol2driver_and_client=element->get_table();
379: }
380: }
1.11 paf 381:
1.1 paf 382: // connect
1.142 paf 383: SQL_Connection* connection=SQL_driver_manager->get_connection(url.as_string(),
1.144 paf 384: protocol2driver_and_client,
1.180 misha 385: r.charsets.source().NAME().cstr(),
386: r.request_info.document_root);
1.1 paf 387:
1.214 moko 388: Temp_connection temp_connection(r, connection);
1.63 paf 389:
1.1 paf 390: // execute body
1.53 parser 391: try {
1.189 misha 392: r.process_write(body_code);
1.129 paf 393: connection->commit();
394: connection->close();
1.75 paf 395: } catch(...) { // process problem
1.129 paf 396: connection->rollback();
397: connection->close();
398: rethrow;
1.1 paf 399: }
400: }
401:
1.41 parser 402: #ifndef DOXYGEN
1.129 paf 403: class Switch_data: public PA_Object {
404: public:
405: Request& r;
1.181 misha 406: const String* searching_string;
407: double searching_double;
1.207 misha 408: bool searching_bool;
1.129 paf 409: Value* found;
410: Value* _default;
411: public:
412: Switch_data(Request& ar, Value& asearching):
1.181 misha 413: r(ar)
414: {
415: if(asearching.is_string() || asearching.is_void()){
416: searching_string=&asearching.as_string();
417: searching_double=0;
1.207 misha 418: searching_bool=false;
1.181 misha 419: } else {
420: searching_string=0;
421: searching_double=asearching.as_double();
1.207 misha 422: searching_bool=asearching.is_bool();
1.181 misha 423: }
424: }
1.28 parser 425: };
1.41 parser 426: #endif
1.129 paf 427: static void _switch(Request& r, MethodParams& params) {
1.236 moko 428: Switch_data* data=new Switch_data(r, r.process(params[0]));
1.215 moko 429: Temp_hash_value<HashString<void*>, void*> switch_data_setter(&r.classes_conf, switch_data_name, data);
1.28 parser 430:
1.129 paf 431: Value& cases_code=params.as_junction(1, "switch cases must be code");
1.82 paf 432: // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
433: // because of stacked WWrapper used there as wcontext
1.237 moko 434: r.process(cases_code);
1.147 paf 435: if(Value* selected_code=data->found? data->found: data->_default)
1.245 moko 436: r.process_write(*selected_code);
1.28 parser 437: }
438:
1.129 paf 439: static void _case(Request& r, MethodParams& params) {
440: Switch_data* data=static_cast<Switch_data*>(r.classes_conf.get(switch_data_name));
1.38 parser 441: if(!data)
1.166 misha 442: throw Exception(PARSER_RUNTIME,
1.129 paf 443: 0,
1.38 parser 444: "without switch");
1.28 parser 445:
1.181 misha 446: if(data->found) // matches already was found
447: return;
448:
1.129 paf 449: int count=params.count();
1.184 misha 450: Value* code=¶ms.as_expression(--count, "case result must be code");
451:
452: #ifdef USE_DESTRUCTORS
453: Junction *j=code->get_junction();
454: if (j){
455: code=new VJunction(j->self,j->method,j->method_frame,j->rcontext,j->wcontext,j->code);
456: if (j->wcontext) j->wcontext->attach_junction((VJunction *)code);
457: }
458: #endif
1.83 paf 459:
1.181 misha 460: for(int i=0; i<count; i++){
1.236 moko 461: Value& value=r.process(params[i]);
1.28 parser 462:
1.181 misha 463: if(value.is_string() && value.as_string() == CASE_DEFAULT_VALUE){
1.184 misha 464: data->_default=code;
1.181 misha 465: continue;
1.28 parser 466: }
467:
468: bool matches;
1.181 misha 469: if(data->searching_string)
470: matches=(*data->searching_string) == value.as_string();
1.207 misha 471: else if(data->searching_bool || value.is_bool())
472: matches=(data->searching_double != 0) == value.as_bool();
1.28 parser 473: else
1.181 misha 474: matches=data->searching_double == value.as_double();
1.82 paf 475:
1.181 misha 476: if(matches){
1.184 misha 477: data->found=code;
1.28 parser 478: break;
479: }
480: }
481: }
1.136 paf 482: #ifndef DOXYGEN
483: struct Try_catch_result {
1.234 moko 484: ValueRef processed_code;
1.136 paf 485: const String* exception_should_be_handled;
486:
487: Try_catch_result(): exception_should_be_handled(0) {}
488: };
1.229 moko 489:
490: /// Auto-object used for temporary changing Request::skip.
491: class Temp_skip {
492: Request& frequest;
493: Request::Skip saved_skip;
494: public:
495: Temp_skip(Request& arequest) : frequest(arequest), saved_skip(arequest.get_skip()) {
496: arequest.set_skip(Request::SKIP_NOTHING);
497: }
498: ~Temp_skip() {
1.290 moko 499: // the interruption is not restored: if it is not set now, it is already thrown
500: if(frequest.get_skip() == Request::SKIP_NOTHING && saved_skip != Request::SKIP_INTERRUPTED)
1.229 moko 501: frequest.set_skip(saved_skip);
502: }
503: };
1.136 paf 504: #endif
505:
506: /// used by ^try and ^cache, @returns $exception.handled[string] if any
507: template<class I>
1.234 moko 508: static Try_catch_result try_catch(Request& r, Value& body_code(Request&, I), I info, Value* catch_code, bool could_be_handled_by_caller=false) {
1.136 paf 509: Try_catch_result result;
1.179 misha 510:
1.136 paf 511: if(!catch_code) {
512: result.processed_code=body_code(r, info);
513: return result;
514: }
515:
516: // taking snapshot of try-context
517: Request_context_saver try_context(r);
1.282 moko 518: size_t trace_start=r.exception_trace.top_index();
519:
1.136 paf 520: try {
521: result.processed_code=body_code(r, info);
1.289 moko 522: } catch(const Uncatchable_exception&) {
1.284 moko 523: rethrow;
1.136 paf 524: } catch(const Exception& e) {
1.282 moko 525: size_t trace_bottom=r.exception_trace.bottom_index();
526: size_t trace_top=r.exception_trace.top_index();
1.230 moko 527:
1.281 moko 528: VException& details=*new VException(r, e);
1.230 moko 529:
1.282 moko 530: try_context.restore(); // restoring try-context for catch code
1.285 moko 531: r.recursion_limit_try_restore(); // here to allow "endless" recursion in catch
1.136 paf 532:
1.282 moko 533: r.exception_trace.set_bottom_index(trace_top); // keep the original trace occupied; errors in catch start after it
1.136 paf 534:
535: bool bhandled=false;
1.282 moko 536: try {
537: {
538: Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details);
539: Temp_skip temp_skip(r);
540: result.processed_code=r.process(*catch_code);
541: }
542:
543: Value* vhandled=details.handled();
544: if(vhandled) {
545: if(vhandled->is_string()) { // not simple $exception.handled(1/0)?
546: if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it
547: result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover
548: }
549: } else
550: bhandled=vhandled->as_bool();
551: }
552: } catch(...) {
1.285 moko 553: try_context.restore(); // restore execution for finally, but keep new error trace.
554: r.recursion_limit_try_restore(); // here to allow "endless" recursion in finally
1.283 moko 555: details.expire_trace();
1.282 moko 556: rethrow;
1.136 paf 557: }
558:
1.283 moko 559: details.expire_trace();
1.230 moko 560: if(!bhandled){
1.282 moko 561: r.exception_trace.set_range(trace_bottom, trace_top);
1.136 paf 562: rethrow;
1.230 moko 563: }
1.285 moko 564:
1.282 moko 565: r.exception_trace.set_range(trace_start, trace_start);
1.136 paf 566: }
1.179 misha 567:
1.136 paf 568: return result;
569: }
1.28 parser 570:
1.234 moko 571: static Value& process_try_body_code(Request& r, Value* body_code) {
1.222 moko 572: return r.process(*body_code);
573: }
574:
1.63 paf 575: // cache--
576:
577: // consts
578:
1.152 paf 579: const int DATA_STRING_SERIALIZED_VERSION=0x0006;
1.63 paf 580:
581: // helper types
582:
583: #ifndef DOXYGEN
584: struct Data_string_serialized_prolog {
585: int version;
1.79 paf 586: time_t expires;
1.63 paf 587: };
588: #endif
589:
1.68 paf 590: void cache_delete(const String& file_spec) {
1.292 ! moko 591: file_delete(file_spec, false /*fail_on_problem*/ );
1.63 paf 592: }
1.69 paf 593:
594: #ifndef DOXYGEN
1.135 paf 595: struct Cache_scope {
1.129 paf 596: public:
1.79 paf 597: time_t expires;
1.135 paf 598: const String* body_from_disk;
1.292 ! moko 599: time_t extend; // seconds the expired copy is returned during renewal
1.79 paf 600: };
1.292 ! moko 601: enum Cache_file_state {
! 602: CACHE_NOT_LOCKED, // the lock is busy: the cache is not renewed
! 603: CACHE_UNDER_LOCK, // the cache is renewed under the lock, the others wait: there is no copy, it expired more than $.extend seconds ago, or $.extend(0)
! 604: CACHE_EXTENDED, // the expired copy is extended for $.extend seconds: the code runs with the cache file not locked
! 605: CACHE_RENEWED // [or extended] by the one who has had the lock before: the copy is read under the lock
! 606: };
! 607: struct Cache_action_info {
1.69 paf 608: Request *r;
1.136 paf 609: Cache_scope *scope;
1.167 misha 610: Value* body_code;
611: Value* catch_code;
1.136 paf 612: const String* processed_code;
1.292 ! moko 613: Cache_file_state state;
1.69 paf 614: };
615: #endif
1.136 paf 616:
617:
1.234 moko 618: static Value& process_cache_body_code(Request& r, Value* body_code) {
619: return r.process(*body_code);
1.136 paf 620: }
621:
1.292 ! moko 622: /// body->process, with the cache file locked, or not locked if the expired copy is extended
! 623: static void cache_process(Cache_action_info& info) {
1.160 paf 624: const String* body_from_disk=info.scope->body_from_disk;
1.292 ! moko 625: // body->process
! 626: Try_catch_result result=try_catch(*info.r, process_cache_body_code, info.body_code, info.catch_code, body_from_disk!=0 /* we have something old=we can handle=recover later */);
1.136 paf 627:
628: if(result.exception_should_be_handled) {
629: if(*result.exception_should_be_handled==CACHE_EXCEPTION_HANDLED_CACHE_NAME) {
1.160 paf 630: assert(body_from_disk);
631: info.processed_code=body_from_disk;
1.136 paf 632: } else
1.166 misha 633: throw Exception(PARSER_RUNTIME,
1.136 paf 634: result.exception_should_be_handled,
1.281 moko 635: "$" EXCEPTION_VAR_NAME ".handled value must be "
1.219 moko 636: "either boolean or string '" CACHE_EXCEPTION_HANDLED_CACHE_NAME "'");
1.136 paf 637: } else
1.234 moko 638: info.processed_code=&((Value &)result.processed_code).as_string();
1.292 ! moko 639: }
! 640:
! 641: /* @todo maybe network order worth spending some effort?
! 642: don't bothering myself with network byte order,
! 643: am not planning to be able to move resulting file across platforms
! 644: */
! 645: static void locked_cache_put_action(int f, void *context) {
! 646: Cache_action_info& info=*static_cast<Cache_action_info *>(context);
1.69 paf 647:
1.79 paf 648: // expiration time not spoiled by ^cache(0) or something?
1.136 paf 649: if(info.scope->expires > time(0)) {
1.79 paf 650: // string -serialize> buffer
1.292 ! moko 651: String::Cm serialized=info.processed_code->serialize(sizeof(Data_string_serialized_prolog));
! 652: Data_string_serialized_prolog& prolog=*reinterpret_cast<Data_string_serialized_prolog *>(serialized.str);
1.79 paf 653: prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.136 paf 654: prolog.expires=info.scope->expires;
1.79 paf 655:
656: // buffer -write> file
1.272 moko 657: if(write(f, serialized.str, serialized.length)<0)
658: throw Exception("file.write", 0, "write failed: %s (%d)", strerror(errno), errno);
1.292 ! moko 659: }
1.69 paf 660: }
1.168 misha 661:
1.292 ! moko 662: /// the copy from the cache file content, 0 if it is broken or of the other version
! 663: static const String* cache_deserialize(char* str, size_t length, time_t& expires) {
! 664: Data_string_serialized_prolog& prolog = *reinterpret_cast<Data_string_serialized_prolog *>(str);
! 665:
! 666: if(length>=sizeof(Data_string_serialized_prolog) && prolog.version==DATA_STRING_SERIALIZED_VERSION) {
! 667: String* body=new String;
! 668: if(body->deserialize(sizeof(Data_string_serialized_prolog), str, length)) {
! 669: expires=prolog.expires;
! 670: return body;
! 671: }
! 672: }
! 673: return 0;
! 674: }
! 675:
! 676: /// the copy from the file opened by the caller under the lock, the file position is left at its end
! 677: static const String* cache_read(int f, time_t& expires) {
! 678: size_t length=check_file_size(pa_lseek(f, 0, SEEK_END), 0);
! 679: char* buf=(char*)pa_malloc_atomic(length+1);
! 680: pa_lseek(f, 0, SEEK_SET);
! 681: return (size_t)read(f, buf, length)==length ? cache_deserialize(buf, length, expires) : 0;
1.63 paf 682: }
1.229 moko 683:
1.292 ! moko 684: /// the cache is renewed under the lock, the others wait. If cache was allready renewed, read it here, under the same lock
! 685: static void locked_process_and_cache_put_action(int f, void *context) {
! 686: Cache_action_info& info=*static_cast<Cache_action_info *>(context);
! 687:
! 688: time_t expires;
! 689: const String* body=cache_read(f, expires);
! 690: if(body && expires > time(0)) {
! 691: info.processed_code=body;
! 692: info.state=CACHE_RENEWED;
! 693: return;
! 694: }
! 695: info.state=CACHE_UNDER_LOCK;
! 696: pa_lseek(f, 0, SEEK_SET);
! 697: cache_process(info);
! 698: locked_cache_put_action(f, context);
! 699: }
1.229 moko 700:
1.292 ! moko 701: /// the expired copy is extended for scope->extend seconds [its expiration time is moved forward]
! 702: static void locked_cache_extend_action(int f, void *context) {
! 703: Cache_action_info& info=*static_cast<Cache_action_info *>(context);
1.135 paf 704:
1.292 ! moko 705: Data_string_serialized_prolog prolog;
! 706: time_t now=time(0);
! 707: if((size_t)read(f, &prolog, sizeof(prolog))==sizeof(prolog) && prolog.version==DATA_STRING_SERIALIZED_VERSION && prolog.expires <= now) {
! 708: prolog.expires=now+info.scope->extend;
! 709: pa_lseek(f, 0, SEEK_SET);
! 710: if((size_t)write(f, &prolog, sizeof(prolog))==sizeof(prolog)) {
! 711: info.state=CACHE_EXTENDED;
! 712: pa_lseek(f, 0, SEEK_END); // the file is cut at the current position after the action
! 713: return;
! 714: }
! 715: }
! 716: // renewed by the other one meanwhile, or the copy has changed [cut, deleted]: as without the copy
! 717: pa_lseek(f, 0, SEEK_SET);
! 718: locked_process_and_cache_put_action(f, context);
! 719: }
! 720:
! 721: const String* process_and_cache_put(Request& r, Value& body_code, Value* catch_code, Cache_scope& scope, const String& file_spec) {
! 722: Cache_action_info info={&r, &scope, &body_code, catch_code, 0, CACHE_NOT_LOCKED};
! 723:
! 724: if(scope.body_from_disk && scope.extend > 0) {
! 725: // the copy expired no more than scope.extend seconds ago, try to extend it without waiting for a lock
! 726: file_write_action_under_lock(file_spec, "cache_extend", locked_cache_extend_action, &info, false /* as_text */, false /* do_append */, false /* block */, false /* fail_on_lock_problem */);
! 727: if(info.state==CACHE_NOT_LOCKED)
! 728: return scope.body_from_disk;
! 729: if(info.state==CACHE_EXTENDED) {
! 730: // the code runs with the cache file not locked: the others get the old copy meanwhile, as not expired yet;
! 731: // if the code throws or runs longer, the copy expires again, and the next request renews the cache
! 732: cache_process(info);
! 733: // in place, under the lock: the readers wait for it for a moment; if the lock failed, the copy stays extended
! 734: file_write_action_under_lock(file_spec, "cache_put", locked_cache_put_action, &info, false /* as_text */, false /* do_append */, true /* block */, false /* fail_on_lock_problem */);
1.135 paf 735: }
1.292 ! moko 736: } else {
! 737: // without the copy to give, the cache is renewed under the lock, the others wait for it:
! 738: // block == wait till the other one releases the lock, it may renew the cache under it
! 739: file_write_action_under_lock(file_spec, "cache_put", locked_process_and_cache_put_action, &info, false /* as_text */, false /* do_append */, true /* block */, false /* fail_on_lock_problem */);
! 740: if(info.state==CACHE_NOT_LOCKED) // the lock wait timeout: the code is run without the cache
! 741: return 0;
1.72 paf 742: }
1.63 paf 743:
1.292 ! moko 744: if(info.state==CACHE_RENEWED) // by the one who has had the lock before: the copy is not ours to delete
! 745: return info.processed_code;
! 746:
! 747: time_t now=time(0);
! 748: if(scope.expires<=now)
! 749: cache_delete(file_spec);
! 750: return info.processed_code;
! 751: }
! 752:
! 753: static const String* cache_get(const String& file_spec, time_t& expires) {
! 754: File_read_result file=file_read_binary(file_spec, false /* fail_on_read_problem */);
! 755: if(file.success && file.length /* ignore reads which are empty due to non-unary open+lockEX conflict with lockSH */)
! 756: return cache_deserialize(file.str, file.length, expires);
! 757: return 0;
! 758: }
! 759:
! 760: static time_t as_expires(Value& value, time_t now) {
! 761: if(VDate* vdate=dynamic_cast<VDate*>(&value))
! 762: return (time_t)vdate->get_time();
! 763: if(!value.is_evaluated_expr())
! 764: throw Exception(PARSER_RUNTIME, 0, "lifespan must be date or number");
! 765: return now+(time_t)value.as_double();
1.63 paf 766: }
1.252 moko 767:
768: static time_t as_expires(Request& r, MethodParams& params, int index, time_t now) {
1.263 moko 769: if(VDate* vdate=dynamic_cast<VDate*>(¶ms[index]))
1.292 ! moko 770: return (time_t)vdate->get_time();
! 771: return now+(time_t)params.as_double(index, "lifespan must be date or number", r);
1.79 paf 772: }
1.229 moko 773:
1.129 paf 774: static const String& as_file_spec(Request& r, MethodParams& params, int index) {
1.258 moko 775: return r.full_disk_path(params.as_string(index, "filespec must be string"));
1.79 paf 776: }
1.229 moko 777:
1.129 paf 778: static void _cache(Request& r, MethodParams& params) {
1.173 misha 779: if(params.count()==0) {
780: // ^cache[] -- return current expiration time
1.158 paf 781: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
782: if(!scope)
1.221 moko 783: throw Exception(PARSER_RUNTIME, 0, "expire-time get without cache");
1.243 moko 784: r.write(*new VDate((pa_time_t)scope->expires));
1.158 paf 785: return;
786: }
787:
1.79 paf 788: time_t now=time(0);
789:
1.129 paf 790: if(params.count()==1) {
1.173 misha 791: // ^cache[filename] ^cache(seconds) ^cache[expires date]
1.129 paf 792: if(params[0].is_string()) { // filename?
1.79 paf 793: cache_delete(as_file_spec(r, params, 0));
794: return;
795: }
796:
797: // secods|expires date
1.135 paf 798: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
799: if(!scope)
1.292 ! moko 800: throw Exception(PARSER_RUNTIME, 0, "expire-time change without cache");
1.79 paf 801:
1.129 paf 802: time_t expires=as_expires(r, params, 0, now);
1.135 paf 803: if(expires < scope->expires)
804: scope->expires=expires;
1.79 paf 805:
806: return;
1.149 paf 807: } else if(params.count()<3)
1.292 ! moko 808: throw Exception(PARSER_RUNTIME, 0, "invalid number of parameters");
! 809:
1.63 paf 810: // file_spec, expires, body code
1.172 misha 811: const String& file_spec=as_file_spec(r, params, 0);
1.65 paf 812:
1.292 ! moko 813: // by default the expired copy is returned for the time the others would wait for the lock
! 814: Cache_scope scope={0, 0, (time_t)PA_LOCK_ATTEMPTS_TO_TIME(pa_lock_attempts)};
! 815: if(HashStringValue* options=params[1].get_hash()) {
! 816: int valid_options=0;
! 817: Value* vexpires=options->get("expires");
! 818: if(!vexpires)
! 819: throw Exception(PARSER_RUNTIME, 0, "expires option must be specified");
! 820: scope.expires=as_expires(*vexpires, now);
! 821: valid_options++;
! 822: if(Value* vextend=options->get("extend")) {
! 823: scope.extend=(time_t)vextend->as_double();
! 824: valid_options++;
! 825: }
! 826: if(valid_options!=options->count())
! 827: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
! 828: } else
! 829: scope.expires=as_expires(r, params, 1, now);
1.135 paf 830:
1.215 moko 831: Temp_hash_value<HashString<void*>, void*> cache_scope_setter(&r.classes_conf, cache_data_name, &scope);
1.136 paf 832: Value& body_code=params.as_junction(2, "body_code must be code");
833: Value* catch_code=0;
834: if(params.count()>3)
835: catch_code=¶ms.as_junction(3, "catch_code must be code");
1.63 paf 836:
1.168 misha 837: if(scope.expires>now) {
1.292 ! moko 838: time_t cached_expires;
! 839: const String* cached=cache_get(file_spec, cached_expires);
1.69 paf 840:
1.292 ! moko 841: if(cached) { // have cached copy
! 842: if(cached_expires <= now) {
! 843: scope.body_from_disk=cached; // to return with $exception.handled[cache] and while the cache is renewed
! 844: if(cached_expires+scope.extend < now) // expired long ago [the page is requested seldom]: the copy is too old to return
! 845: scope.extend=0;
1.174 misha 846: } else {
1.292 ! moko 847: // and it's not expired yet write it out
! 848: r.write(*cached);
! 849: // happy with it
! 850: return;
! 851: }
1.168 misha 852: }
853:
854: // no cached info or it's already expired
1.173 misha 855:
1.292 ! moko 856: // trying to process code and store result in file
! 857: const String* processed_body=process_and_cache_put(r, body_code, catch_code, scope, file_spec);
1.173 misha 858: if(processed_body){
1.292 ! moko 859: // write it out
1.243 moko 860: r.write(*processed_body);
1.174 misha 861: // happy with it
862: return;
1.173 misha 863: } else {
1.292 ! moko 864: // we fail while get exclusive lock. we'll execute body_code a bit later
1.174 misha 865: }
1.292 ! moko 866: } else {
1.79 paf 867: // instructed not to cache; forget cached copy
1.68 paf 868: cache_delete(file_spec);
1.69 paf 869: }
1.168 misha 870:
1.222 moko 871: // process without caching
872: if(catch_code){
873: Try_catch_result result=try_catch(r, process_try_body_code, &body_code, catch_code);
1.243 moko 874: r.write(result.processed_code);
1.222 moko 875: } else {
1.243 moko 876: r.write(r.process_to_string(body_code));
1.222 moko 877: }
1.63 paf 878: }
879:
1.129 paf 880: static void _try_operator(Request& r, MethodParams& params) {
881: Value& body_code=params.as_junction(0, "body_code must be code");
882: Value& catch_code=params.as_junction(1, "catch_code must be code");
1.179 misha 883: Value* finally_code=(params.count()==3) ? ¶ms.as_junction(2, "finally_code must be code") : 0;
884:
885: Try_catch_result result;
1.229 moko 886:
1.179 misha 887: try{
1.229 moko 888: // process try and catch code
1.222 moko 889: result=try_catch(r, process_try_body_code, &body_code, &catch_code);
1.289 moko 890: } catch(const Uncatchable_exception&) {
1.284 moko 891: rethrow;
1.179 misha 892: } catch(...){
1.229 moko 893: // process finally code but ignore the result
894: if(finally_code){
1.282 moko 895: size_t trace_bottom=r.exception_trace.bottom_index();
896: size_t trace_top=r.exception_trace.top_index();
897: r.exception_trace.set_bottom_index(trace_top);
1.229 moko 898: Temp_skip temp(r);
1.248 moko 899: /* Value &finally_result= */ r.process(*finally_code);
1.282 moko 900: r.exception_trace.set_range(trace_bottom, trace_top);
1.229 moko 901: }
1.179 misha 902: rethrow;
903: }
1.74 paf 904:
1.229 moko 905: // process finally code
906: if(finally_code){
907: Temp_skip temp(r);
1.234 moko 908: Value& finally_result=r.process(*finally_code);
1.229 moko 909:
1.234 moko 910: // no exception in try/catch or finally, writing processed body_code or catch_code
1.243 moko 911: r.write(result.processed_code);
1.123 paf 912:
1.234 moko 913: // write out processed finally code
1.243 moko 914: r.write(finally_result);
1.234 moko 915: } else {
916: // no exception in try/catch, writing processed body_code or catch_code
1.243 moko 917: r.write(result.processed_code);
1.234 moko 918: }
1.74 paf 919: }
920:
1.138 paf 921: static void _throw_operator(Request&, MethodParams& params) {
1.178 misha 922: if(params.count()==1 && !params[0].is_string()) {
1.129 paf 923: if(HashStringValue *hash=params[0].get_hash()) {
924: const char* type=0;
1.281 moko 925: if(Value* value=hash->get(Symbols::TYPE_SYMBOL))
1.78 paf 926: type=value->as_string().cstr();
1.129 paf 927: const String* source=0;
1.281 moko 928: if(Value* value=hash->get(Symbols::SOURCE_SYMBOL))
1.74 paf 929: source=&value->as_string();
1.138 paf 930: const char* comment=0;
1.281 moko 931: if(Value* value=hash->get(Symbols::COMMENT_SYMBOL))
1.74 paf 932: comment=value->as_string().cstr();
933:
1.261 moko 934: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 935: } else
1.260 moko 936: throw Exception(PARSER_RUNTIME, 0, "one-param version has hash or string param");
1.74 paf 937: } else {
1.129 paf 938: const char* type=params.as_string(0, "type must be string").cstr();
1.261 moko 939: const String* source=params.count()>1 ? ¶ms.as_string(1, "source must be string") : 0;
940: const char* comment=params.count()>2 ? params.as_string(2, "comment must be string").cstr() : 0;
941: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 942: }
1.261 moko 943: }
1.129 paf 944:
1.150 paf 945: static void _sleep_operator(Request& r, MethodParams& params) {
946: double seconds=params.as_double(0, "seconds must be double", r);
1.288 moko 947: // sleeping by ticks to notice the interruption
948: for(double tick; seconds>0 && !r.get_skip(); seconds-=tick){
949: tick=seconds<0.1 ? seconds : 0.1;
950: pa_sleep(0, (unsigned long)(tick*1000000));
951: }
1.261 moko 952: }
1.150 paf 953:
1.279 moko 954: static void _a_operator(Request& r, MethodParams& params) {
955: VArray& result=*new VArray;
956: ArrayValue& array=result.array();
957:
958: int count=params.count();
959: for(int i=0; i<count; i++)
960: array+=&r.process(params[i]);
961: array.confirm_all_used();
962:
963: r.write(result);
964: }
965:
1.274 moko 966: #ifdef HAVE_SYSLOG
1.273 moko 967: static int log_level(const String &name){
968: if(name.is_empty()) return LOG_INFO;
969: const char *sname = str_upper(name.cstr());
970:
971: if(!strcmp(sname,"INFO")) return LOG_INFO;
972: if(!strcmp(sname,"WARNING")) return LOG_WARNING;
973: if(!strcmp(sname,"ERROR")) return LOG_ERR;
974: if(!strcmp(sname,"DEBUG")) return LOG_DEBUG;
975: throw Exception("syslog", &name, "invalid log level value");
976: }
1.274 moko 977: #endif
1.273 moko 978:
1.277 moko 979: static void _syslog_operator(Request& r, MethodParams& params) {
1.273 moko 980: const char* message=params.as_string(1, "message must be string").cstr();
1.274 moko 981: #ifdef HAVE_SYSLOG
1.278 moko 982: const char* ident=params.as_string(0, "ident must be string").cstr();
1.273 moko 983: int level=params.count()>2 ? log_level(params.as_string(2, "level must be string")) : LOG_INFO;
984:
985: openlog(*ident ? ident : "parser3", LOG_PID, LOG_USER);
986: syslog(level, "%s", message);
987: closelog();
1.277 moko 988: (void)r;
1.273 moko 989: #else
990: SAPI::log(r.sapi_info, "syslog: %s", message);
991: #endif
992: }
993:
1.254 moko 994: #if defined(WIN32) && defined(_DEBUG) && !defined(_WIN64)
1.129 paf 995: # define PA_BPT
1.138 paf 996: static void _bpt(Request&, MethodParams&) {
1.129 paf 997: _asm int 3;
998: }
999: #endif
1000:
1.10 paf 1001: // constructor
1002:
1.226 moko 1003: VClassMAIN::VClassMAIN(): VClass(MAIN_CLASS_NAME) {
1.129 paf 1004:
1005: #ifdef PA_BPT
1006: // ^bpt[]
1007: add_native_method("bpt", Method::CT_ANY, _bpt, 0, 0);
1008: #endif
1.121 paf 1009:
1.1 paf 1010: // ^if(condition){code-when-true}
1011: // ^if(condition){code-when-true}{code-when-false}
1.200 moko 1012: // ^if(condition){code-when-true} (another condition){code-when-true} ... {code-when-false}
1013: add_native_method("if", Method::CT_ANY, _if, 2, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 1014:
1.194 misha 1015: // ^untaint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
1016: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 1017:
1.194 misha 1018: // ^taint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
1019: add_native_method("taint", Method::CT_ANY, _taint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 1020:
1.205 moko 1021: // ^apply-taint[untaint lang][string]
1022: add_native_method("apply-taint", Method::CT_ANY, _apply_taint, 1, 2, Method::CO_WITHOUT_FRAME);
1023:
1.216 moko 1024: // ^process{code}
1025: // ^process[context]{code}[options hash]
1.146 paf 1026: add_native_method("process", Method::CT_ANY, _process, 1, 3);
1.1 paf 1027:
1028: // ^rem{code}
1.185 misha 1029: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 1030:
1031: // ^while(condition){code}
1.185 misha 1032: add_native_method("while", Method::CT_ANY, _while, 2, 3, Method::CO_WITHOUT_FRAME);
1.1 paf 1033:
1.216 moko 1034: // ^use[file[;options hash]]
1035: add_native_method("use", Method::CT_ANY, _use, 1, 2);
1.1 paf 1036:
1.164 paf 1037: // ^break[]
1.227 moko 1038: // ^break(condition)
1039: add_native_method("break", Method::CT_ANY, _break, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 1040:
1.164 paf 1041: // ^continue[]
1.227 moko 1042: // ^continue(condition)
1043: add_native_method("continue", Method::CT_ANY, _continue, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 1044:
1.249 moko 1045: // ^return[]
1046: // ^return[result]
1.250 moko 1047: add_native_method("return", Method::CT_ANY, _return, 0, 1);
1.249 moko 1048:
1.54 paf 1049: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.239 moko 1050: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 1051:
1052: // ^eval(expr)
1053: // ^eval(expr)[format]
1.185 misha 1054: add_native_method("eval", Method::CT_ANY, _eval, 1, 2, Method::CO_WITHOUT_FRAME);
1.52 parser 1055:
1.1 paf 1056: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 1057: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
1058:
1.63 paf 1059:
1.136 paf 1060: // ^cache[file_spec](time){code}[{catch code}] time=0 no cache
1.65 paf 1061: // ^cache[file_spec] delete cache
1.161 paf 1062: // ^cache[] get current expiration time
1063: add_native_method("cache", Method::CT_ANY, _cache, 0, 4);
1.63 paf 1064:
1.28 parser 1065: // switch
1066:
1067: // ^switch[value]{cases}
1.185 misha 1068: add_native_method("switch", Method::CT_ANY, _switch, 2, 2, Method::CO_WITHOUT_FRAME);
1.28 parser 1069:
1070: // ^case[value]{code}
1.185 misha 1071: add_native_method("case", Method::CT_ANY, _case, 2, 10000, Method::CO_WITHOUT_FRAME);
1.74 paf 1072:
1073: // try-catch
1074:
1075: // ^try{code}{catch code}
1.185 misha 1076: add_native_method("try", Method::CT_ANY, _try_operator, 2, 3, Method::CO_WITHOUT_FRAME);
1.74 paf 1077: // ^throw[$exception hash]
1078: // ^throw[type;source;comment]
1079: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
1080:
1.150 paf 1081: add_native_method("sleep", Method::CT_ANY, _sleep_operator, 1, 1);
1.273 moko 1082:
1.279 moko 1083: // equivalent to ^array::create[...]
1084: add_native_method("A", Method::CT_ANY, _a_operator, 0, 10000, Method::CO_WITHOUT_FRAME);
1085:
1.273 moko 1086: // ^syslog[ident;message[;info|warning|error|debug]]
1087: add_native_method("syslog", Method::CT_ANY, _syslog_operator, 2, 3);
1.10 paf 1088: }
1.11 paf 1089:
1090: // constructor & configurator
1.1 paf 1091:
1.129 paf 1092: VStateless_class& VClassMAIN_create() {
1.287 moko 1093: // adding native methods from a singleton
1094: static VClassMAIN* prototype=new VClassMAIN;
1095: return *new VClassMAIN(*prototype);
1.1 paf 1096: }
E-mail: