Annotation of parser3/src/classes/op.C, revision 1.105
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
1.70 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.71 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.98 paf 6: */
1.1 paf 7:
1.105 ! paf 8: static const char* IDENT_OP_C="$Date: 2002/08/15 09:07:48 $";
1.1 paf 9:
1.13 paf 10: #include "classes.h"
1.1 paf 11: #include "pa_common.h"
12: #include "pa_request.h"
13: #include "pa_vint.h"
14: #include "pa_sql_connection.h"
1.79 paf 15: #include "pa_vdate.h"
1.105 ! paf 16: #include "pa_vmethod_frame.h"
1.1 paf 17:
1.18 parser 18: // limits
19:
20: #define MAX_LOOPS 10000
21:
1.10 paf 22: // defines
23:
1.82 paf 24: #define CASE_DEFAULT_VALUE "DEFAULT"
1.11 paf 25:
1.10 paf 26: // class
27:
28: class MOP : public Methoded {
29: public:
30: MOP(Pool& pool);
1.11 paf 31: public: // Methoded
1.10 paf 32: bool used_directly() { return true; }
1.11 paf 33: void configure_user(Request& r);
1.36 parser 34:
1.11 paf 35: private:
36: String main_sql_name;
37: String main_sql_drivers_name;
1.10 paf 38: };
39:
40: // methods
41:
1.5 paf 42: static void _if(Request& r, const String&, MethodParams *params) {
1.48 parser 43: Value& condition_code=params->as_junction(0, "condition must be expression");
1.1 paf 44:
1.81 paf 45: bool condition=r.process_to_value(condition_code,
1.83 paf 46: /*0/*no name* /,*/
1.1 paf 47: false/*don't intercept string*/).as_bool();
1.6 paf 48: if(condition)
1.85 paf 49: r.write_pass_lang(r.process(params->as_junction(1, "'then' parameter must be code")));
1.50 parser 50: else if(params->size()>2)
1.85 paf 51: r.write_pass_lang(r.process(params->as_junction(2, "'else' parameter must be code")));
1.1 paf 52: }
53:
1.5 paf 54: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 55: Pool& pool=r.pool();
56:
1.60 paf 57: uchar lang;
1.59 paf 58: if(params->size()==1)
59: lang=String::UL_AS_IS; // mark as simply 'tainted'. useful in html from sql
60: else {
61: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 62: lang=untaint_lang_name2enum->get_int(lang_name);
1.59 paf 63: if(!lang)
1.78 paf 64: throw Exception(0,
1.59 paf 65: &lang_name,
66: "invalid taint language");
67: }
1.1 paf 68:
69: {
1.59 paf 70: Value& vbody=params->as_junction(params->size()-1, "body must be code");
1.1 paf 71:
72: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.86 paf 73: r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1 paf 74: }
75: }
76:
1.5 paf 77: static void _taint(Request& r, const String&, MethodParams *params) {
1.1 paf 78: Pool& pool=r.pool();
79:
1.60 paf 80: uchar lang;
1.1 paf 81: if(params->size()==1)
82: lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
83: else {
1.48 parser 84: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 85: lang=untaint_lang_name2enum->get_int(lang_name);
1.1 paf 86: if(!lang)
1.78 paf 87: throw Exception(0,
1.3 paf 88: &lang_name,
89: "invalid taint language");
1.1 paf 90: }
91:
92: {
1.34 parser 93: Value& vbody=params->as_no_junction(params->size()-1, "body must not be code");
1.1 paf 94:
1.90 paf 95: String& result=*new(pool) String(pool);
1.1 paf 96: result.append(
97: vbody.as_string(), // process marking tainted with that lang
98: lang, true); // force result language to specified
99: r.write_pass_lang(result);
100: }
101: }
102:
1.5 paf 103: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.97 paf 104: Pool& pool=r.pool();
1.1 paf 105: VStateless_class& self_class=*r.self->get_class();
1.73 paf 106: const Method *main_method;
1.1 paf 107: {
1.73 paf 108: // temporary remove language change
109: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.1 paf 110: // temporary zero @main so to maybe-replace it in processed code
1.95 paf 111: Temp_method temp_method_main(self_class, r.main_method_name, 0);
1.1 paf 112: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
113: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
114:
115: // evaluate source to process
1.86 paf 116: const String& source=r.process_to_string(params->as_junction(0, "body must be code"));
1.101 paf 117: // calculate pseudo file name of processed chars
118: // would be something like "/some/file(4) process"
119: char local_place[MAX_STRING];
120: #ifndef NO_STRING_ORIGIN
121: const Origin& source_origin=source.origin();
122: const Origin& method_origin=method_name.origin();
123: size_t place_size;
124: if(source_origin.file==method_origin.file)
125: place_size=snprintf(local_place, MAX_STRING, "%s(%d) %s", // same file
126: source_origin.file?source_origin.file:"unknown_file", 1+source_origin.line,
127: method_name.cstr())+1;
128: else // different files ^process{external__file_text__or__sql}
129: place_size=snprintf(local_place, MAX_STRING, "%s",
130: source_origin.file?source_origin.file:"unknown_file")+1;
131: #else
132: strncpy(local_place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
133: size_t place_size=strlen(local_place)+1;
134: #endif
135: char *heap_place=(char *)r.malloc(place_size);
136: memcpy(heap_place, local_place, place_size);
1.1 paf 137:
138: // process source code, append processed methods to 'self' class
139: // maybe-define new @main
1.67 paf 140: r.use_buf(
141: source.cstr(String::UL_UNSPECIFIED, r.connection(0)),
1.97 paf 142: *new(pool) String(pool, heap_place, place_size, true /*tainted*/),
1.88 paf 143: heap_place,
1.67 paf 144: &self_class);
1.1 paf 145:
1.73 paf 146: // main_method
1.95 paf 147: main_method=self_class.get_method(r.main_method_name);
1.73 paf 148: }
149: // after restoring current-request-lang
150: // maybe-execute @main[]
151: if(main_method) {
152: // execute!
153: r.execute(*main_method->parser_code);
1.1 paf 154: }
155: }
156:
1.5 paf 157: static void _rem(Request& r, const String&, MethodParams *params) {
1.34 parser 158: params->as_junction(0, "body must be code");
1.1 paf 159: }
160:
1.5 paf 161: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 162: Pool& pool=r.pool();
163:
1.34 parser 164: Value& vcondition=params->as_junction(0, "condition must be expression");
165: Value& body=params->as_junction(1, "body must be code");
1.1 paf 166:
167: // while...
168: int endless_loop_count=0;
169: while(true) {
1.18 parser 170: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.78 paf 171: throw Exception("parser.runtime",
1.1 paf 172: &method_name,
173: "endless loop detected");
174:
1.86 paf 175: bool condition=r.process_to_value(vcondition,
1.83 paf 176: /*0/*no name* /,*/
1.1 paf 177: false/*don't intercept string*/).as_bool();
178: if(!condition) // ...condition is true
179: break;
180:
181: // write processed body
1.86 paf 182: r.write_pass_lang(r.process(body));
1.1 paf 183: }
184: }
185:
1.5 paf 186: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 187: Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37 parser 188: r.use_file(vfile.as_string());
1.1 paf 189: }
190:
1.5 paf 191: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 192: Pool& pool=r.pool();
1.48 parser 193: const String& var_name=params->as_string(0, "var name must be string");
194: int from=params->as_int(1, "from must be int", r);
195: int to=params->as_int(2, "to must be int", r);
1.34 parser 196: Value& body_code=params->as_junction(3, "body must be code");
1.50 parser 197: Value *delim_maybe_code=params->size()>4?¶ms->get(4):0;
1.1 paf 198:
1.57 paf 199: if(to-from>=MAX_LOOPS) // too long loop?
1.78 paf 200: throw Exception("parser.runtime",
1.57 paf 201: &method_name,
202: "endless loop detected");
203:
1.1 paf 204: bool need_delim=false;
205: VInt *vint=new(pool) VInt(pool, 0);
1.105 ! paf 206: r.method_frame->put_element(var_name, vint, false);
1.1 paf 207: for(int i=from; i<=to; i++) {
208: vint->set_int(i);
209:
1.86 paf 210: StringOrValue processed_body=r.process(body_code);
1.49 parser 211: if(delim_maybe_code) { // delimiter set?
1.1 paf 212: const String *string=processed_body.get_string();
213: if(need_delim && string && string->size()) // need delim & iteration produced string?
1.86 paf 214: r.write_pass_lang(r.process(*delim_maybe_code));
1.1 paf 215: need_delim=true;
216: }
217: r.write_pass_lang(processed_body);
218: }
219: }
220:
1.15 paf 221: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 222: Value& expr=params->as_junction(0, "need expression");
1.1 paf 223: // evaluate expresion
1.91 paf 224: Value *value_result=r.process_to_value(expr,
1.83 paf 225: /*0/*no name YET* /,*/
1.1 paf 226: true/*don't intercept string*/).as_expr_result();
1.50 parser 227: if(params->size()>1) {
1.34 parser 228: Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1 paf 229:
230: Pool& pool=r.pool();
231: String& string=*new(pool) String(pool);
1.91 paf 232: string.APPEND_CONST(format(pool, value_result->as_double(), fmt.as_string().cstr()));
233: r.write_no_lang(string);
234: } else
235: r.write_no_lang(*value_result);
1.1 paf 236: }
237:
1.42 parser 238: static void _connect(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 239: Pool& pool=r.pool();
1.63 paf 240: #ifdef RESOURCES_DEBUG
241: struct timeval mt[2];
242: #endif
1.34 parser 243: Value& url=params->as_no_junction(0, "url must not be code");
244: Value& body_code=params->as_junction(1, "body must be code");
1.1 paf 245:
1.19 parser 246: Table *protocol2driver_and_client=
1.35 parser 247: static_cast<Table *>(r.classes_conf.get(r.OP.name()));
1.11 paf 248:
1.63 paf 249: #ifdef RESOURCES_DEBUG
250: //measure:before
251: gettimeofday(&mt[0],NULL);
252: #endif
1.1 paf 253: // connect
1.67 paf 254: SQL_Connection_ptr connection=SQL_driver_manager->get_connection(
1.42 parser 255: url.as_string(), method_name, protocol2driver_and_client);
1.1 paf 256:
1.63 paf 257: #ifdef RESOURCES_DEBUG
258: //measure:after connect
259: gettimeofday(&mt[1],NULL);
260:
261: double t[2];
262: for(int i=0;i<2;i++)
263: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
264:
265: r.sql_connect_time+=t[1]-t[0];
266: #endif
1.67 paf 267: Temp_connection temp_connection(r, connection.get());
1.1 paf 268: // execute body
1.53 parser 269: try {
1.86 paf 270: r.write_assign_lang(r.process(body_code));
1.75 paf 271: } catch(...) { // process problem
1.67 paf 272: connection->mark_to_rollback();
273: /*re*/throw;
1.1 paf 274: }
275: }
276:
1.41 parser 277: #ifndef DOXYGEN
1.28 parser 278: struct Switch_data {
1.82 paf 279: Request *r;
1.28 parser 280: Value *searching;
281: Value *found;
282: Value *_default;
283: };
1.41 parser 284: #endif
1.28 parser 285: static void _switch(Request& r, const String&, MethodParams *params) {
1.82 paf 286: Switch_data data={&r, &r.process_to_value(params->get(0))};
1.79 paf 287: Temp_hash_value switch_data_setter(r.classes_conf, *switch_data_name, &data);
1.28 parser 288:
1.83 paf 289: Value& cases_code=params->as_junction(1, "switch cases must be code");
1.82 paf 290: // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
291: // because of stacked WWrapper used there as wcontext
1.84 paf 292: r.process(cases_code, true/*intercept_string*/);
1.83 paf 293: if(Value *selected_code=data.found ? data.found : data._default) {
294: // setting code context, would execute in ^switch[...]{>>context<<}
1.105 ! paf 295: //selected_code->get_junction()->change_context(cases_code.get_junction());
1.86 paf 296: r.write_pass_lang(r.process(*selected_code));
1.83 paf 297: }
1.28 parser 298: }
299:
1.38 parser 300: static void _case(Request& r, const String& method_name, MethodParams *params) {
301: Pool& pool=r.pool();
302:
303: Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
304: if(!data)
1.78 paf 305: throw Exception("parser.runtime",
1.38 parser 306: &method_name,
307: "without switch");
1.28 parser 308:
309: int count=params->size();
1.34 parser 310: Value *code=¶ms->as_junction(--count, "case result must be code");
1.83 paf 311:
312: // killing context for safety, would execute in ^switch[...]{>>context<<}
313: // reason: context is stacked, and it would become invalid afterwards
1.105 ! paf 314: //code->get_junction()->change_context(0);
1.83 paf 315:
1.28 parser 316: for(int i=0; i<count; i++) {
1.81 paf 317: Value& value=r.process_to_value(params->get(i));
1.28 parser 318:
1.82 paf 319: if(value.as_string() == CASE_DEFAULT_VALUE) {
1.38 parser 320: data->_default=code;
1.28 parser 321: break;
322: }
323:
324: bool matches;
1.38 parser 325: if(data->searching->is_string())
326: matches=data->searching->as_string() == value.as_string();
1.28 parser 327: else
1.38 parser 328: matches=data->searching->as_double() == value.as_double();
1.28 parser 329:
330: if(matches) {
1.82 paf 331: if(data->found)
332: throw Exception("parser.runtime",
333: &method_name,
334: "duplicate found");
335:
1.38 parser 336: data->found=code;
1.28 parser 337: break;
338: }
339: }
340: }
341:
1.63 paf 342: // cache--
343:
344: // consts
345:
1.79 paf 346: const int DATA_STRING_SERIALIZED_VERSION=0x0002;
1.63 paf 347:
348: // helper types
349:
350: #ifndef DOXYGEN
351: struct Data_string_serialized_prolog {
352: int version;
1.79 paf 353: time_t expires;
1.63 paf 354: };
355: #endif
356:
1.68 paf 357: void cache_delete(const String& file_spec) {
358: file_delete(file_spec, false/*fail_on_read_problem*/);
1.63 paf 359: }
1.69 paf 360:
361: #ifndef DOXYGEN
1.79 paf 362: struct Cache_data {
363: time_t expires;
364: };
1.69 paf 365: struct Locked_process_and_cache_put_action_info {
366: Request *r;
1.79 paf 367: Cache_data *data;
1.81 paf 368: Value *body_code; const String *evaluated_body;
1.69 paf 369: };
370: #endif
371: static void locked_process_and_cache_put_action(int f, void *context) {
372: Locked_process_and_cache_put_action_info& info=
373: *static_cast<Locked_process_and_cache_put_action_info *>(context);
374:
375: // body->process
1.81 paf 376: info.evaluated_body=&info.r->process_to_string(*info.body_code);
1.69 paf 377:
1.79 paf 378: // expiration time not spoiled by ^cache(0) or something?
379: if(info.data->expires > time(0)) {
380: // string -serialize> buffer
381: void *data; size_t data_size;
1.81 paf 382: info.evaluated_body->serialize(
1.79 paf 383: sizeof(Data_string_serialized_prolog),
384: data, data_size);
385: Data_string_serialized_prolog& prolog=
386: *static_cast<Data_string_serialized_prolog *>(data);
387: prolog.version=DATA_STRING_SERIALIZED_VERSION;
388: prolog.expires=info.data->expires;
389:
390: // buffer -write> file
391: write(f, data, data_size);
392: } else // expired!
393: info.data->expires=0; // flag it so that could be easily checked by caller
1.69 paf 394: }
1.81 paf 395: const String *locked_process_and_cache_put(Request& r,
1.69 paf 396: Value& body_code,
1.79 paf 397: Cache_data& data,
1.69 paf 398: const String& file_spec) {
399: Locked_process_and_cache_put_action_info info={
400: &r,
1.81 paf 401: &data,
402: &body_code
1.69 paf 403: };
404:
1.81 paf 405: const String *result=file_write_action_under_lock(
1.69 paf 406: file_spec,
407: "cache_put", locked_process_and_cache_put_action, &info,
408: false/*as_text*/,
409: false/*do_append*/,
1.96 paf 410: false/*block*/,
411: false/*fail on lock problem*/) ? info.evaluated_body: 0;
1.100 paf 412: time_t now=time(0);
413: if(data.expires<=now)
1.79 paf 414: cache_delete(file_spec);
415: return result;
1.63 paf 416: }
1.79 paf 417: String *cache_get(Pool& pool, const String& file_spec, time_t now) {
1.63 paf 418: void* data; size_t data_size;
1.72 paf 419: if(file_read(pool, file_spec,
1.63 paf 420: data, data_size,
421: false/*as_text*/,
1.69 paf 422: false/*fail_on_read_problem*/)
1.72 paf 423: && data_size/* ignore reads which are empty due to
424: non-unary open+lockEX conflict with lockSH */) {
1.63 paf 425:
1.72 paf 426: Data_string_serialized_prolog& prolog=
427: *static_cast<Data_string_serialized_prolog *>(data);
1.63 paf 428:
1.72 paf 429: String *result=new(pool) String(pool);
430: if(
431: data_size>=sizeof(Data_string_serialized_prolog)
432: && prolog.version==DATA_STRING_SERIALIZED_VERSION
1.79 paf 433: && prolog.expires > now
1.72 paf 434: && result->deserialize(
435: sizeof(Data_string_serialized_prolog), data, data_size, file_spec.cstr()))
436: return result;
437: }
1.63 paf 438:
1.72 paf 439: return 0;
1.63 paf 440: }
1.79 paf 441: static time_t as_expires(Request& r, const String& method_name, MethodParams *params,
442: int index, time_t now) {
443: time_t result;
444: Value& vlifespan_or_expires=params->get(index);
1.104 paf 445: if(Value *vdate=vlifespan_or_expires.as(VDATE_TYPE, false))
446: result=static_cast<VDate *>(vdate)->get_time();
1.79 paf 447: else
448: result=now+(time_t)params->as_double(index, "lifespan must be date or number", r);
449:
450: return result;
451: }
452: static const String as_file_spec(Request&r, MethodParams *params, int index) {
453: return r.absolute(params->as_string(index, "filespec must be string"));
454: }
1.63 paf 455: static void _cache(Request& r, const String& method_name, MethodParams *params) {
456: Pool& pool=r.pool();
1.79 paf 457: time_t now=time(0);
458:
459: // ^cache[filename] ^cache(seconds) ^cache[expires date]
460: if(params->size()==1) {
461: if(params->get(0).is_string()) { // filename?
462: cache_delete(as_file_spec(r, params, 0));
463: return;
464: }
465:
466: // secods|expires date
467: Cache_data *data=static_cast<Cache_data *>(r.classes_conf.get(*cache_data_name));
468: if(!data)
469: throw Exception("parser.runtime",
470: &method_name,
471: "expire-time reducing instruction without cache");
472:
473: time_t expires=as_expires(r, method_name, params, 0, now);
474: if(expires < data->expires)
475: data->expires=expires;
476:
477: return;
478: }
1.63 paf 479:
480: // file_spec, expires, body code
1.65 paf 481: const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
482:
1.79 paf 483: Cache_data data;
484: Temp_hash_value cache_data_setter(r.classes_conf, *cache_data_name, &data);
485: data.expires=as_expires(r, method_name, params, 1, now);
1.63 paf 486: Value& body_code=params->as_junction(2, "body must be code");
487:
1.79 paf 488: if(data.expires>now) { // valid 'expires' specified? try cached copy...
1.69 paf 489: // hence we don't hope to have unary create/lockEX
490: // we need some plan to live in a life like that, so...
491: // worst races plan:
492: // A B
493: // open
494: // |open
495: // lockSH
496: // |nonblocking-lockEX fails
497: // unlockSH
498: // close, cache_get returns 0
499: // open
500: // nonblocking-lockEX succeeds; process, write, close
501: // |retry1: open
502: // ...
503: // |lockSH succeeds; ...
504:
505: for(int retry=0; retry<2; retry++) {
1.79 paf 506: if(String *cached_body=cache_get(pool, file_spec, now)) { // have cached copy?
507: // write it out
508: r.write_assign_lang(*cached_body);
509: // happy with it
510: return;
511: }
1.69 paf 512:
513: // non-blocked lock; process; cache it
1.81 paf 514: if(const String*processed_body=
515: locked_process_and_cache_put(r, body_code, data, file_spec)) {
1.63 paf 516: // write it out
1.69 paf 517: r.write_assign_lang(*processed_body);
1.63 paf 518: // happy with it
519: return;
1.69 paf 520: } else { // somebody writing result right now
521: pa_sleep(0, 500000); // waiting half a second
522: retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63 paf 523: }
1.69 paf 524: }
1.78 paf 525: throw Exception(0,
1.69 paf 526: &file_spec,
527: "locking problem");
528: } else {
1.79 paf 529: // instructed not to cache; forget cached copy
1.68 paf 530: cache_delete(file_spec);
1.69 paf 531: // process
1.81 paf 532: const String& processed_body=r.process_to_string(body_code);
1.69 paf 533: // write it out
534: r.write_assign_lang(processed_body);
535: // happy with it
536: return;
537: }
538: // never reached
1.63 paf 539: }
540:
1.74 paf 541: // also used in pa_request.C to pass param to @unhandled_exception
542: VHash& exception2vhash(Pool& pool, const Exception& e) {
543: VHash& result=*new(pool) VHash(pool);
544: Hash& hash=result.hash(0);
1.94 paf 545: if(const char *type=e.type(true))
1.78 paf 546: hash.put(*exception_type_part_name, new(pool) VString(*new(pool) String(pool, type)));
1.76 paf 547: if(const String *asource=e.problem_source()) {
548: String& source=*new(pool) String(pool);
549: source.append(*asource, String::UL_TAINTED, true/*forced*/);
1.74 paf 550:
1.76 paf 551: hash.put(*exception_source_part_name, new(pool) VString(source));
1.74 paf 552: #ifndef NO_STRING_ORIGIN
1.76 paf 553: const Origin& origin=source.origin();
1.89 paf 554: hash.put(*new(pool) String(pool, "file", 0, true),
1.74 paf 555: new(pool) VString(*new(pool) String(pool, origin.file)));
556: hash.put(*new(pool) String(pool, "lineno"),
557: new(pool) VInt(pool, 1+origin.line));
558: #endif
559: }
1.94 paf 560: if(const char *ecomment=e.comment(true)) {
1.74 paf 561: int comment_size=strlen(ecomment);
562: char *pcomment=(char *)pool.malloc(comment_size);
563: memcpy(pcomment, ecomment, comment_size);
564: hash.put(*exception_comment_part_name,
1.76 paf 565: new(pool) VString(*new(pool) String(pool, pcomment, comment_size, true/*tainted*/)));
1.74 paf 566: }
567: hash.put(*exception_handled_part_name,
568: new(pool) VBool(pool, false));
569:
570: return result;
571: }
572:
573: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
574: Pool& pool=r.pool();
575:
576: Value& body_code=params->as_junction(0, "body_code must be code");
577: Value& catch_code=params->as_junction(1, "catch_code must be code");
578:
1.86 paf 579: StringOrValue result;
1.74 paf 580:
581: // taking snapshot of request processing status
1.77 paf 582: //int ssexception_trace=r.exception_trace.top_index();
583: int sstack=r.stack.top_index();
1.105 ! paf 584: Value *sself=r.self;
! 585: VMethodFrame *smethod_frame=r.method_frame;
! 586: Value *srcontext=r.rcontext;
1.74 paf 587: WContext *swcontext=r.wcontext;
588: try {
1.86 paf 589: result=r.process(body_code);
1.74 paf 590: } catch(const Exception& e) {
591: // restoring request processing status
1.77 paf 592: //r.exception_trace.top_index(ssexception_trace);
593: r.stack.top_index(sstack);
1.105 ! paf 594: r.self=sself; r.method_frame=smethod_frame, r.rcontext=srcontext; r.wcontext=swcontext;
1.77 paf 595:
1.74 paf 596:
597: VHash& vhash=exception2vhash(pool, e);
598:
599: Junction *junction=catch_code.get_junction();
1.105 ! paf 600: Value *method_frame=junction->method_frame;
! 601: Value *saved_exception_var_value=method_frame->get_element(*exception_var_name, method_frame, false);
! 602: junction->method_frame->put_element(*exception_var_name, &vhash, false);
1.86 paf 603: result=r.process(catch_code);
1.74 paf 604: bool handled=false;
605: if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
606: handled=value->as_bool();
1.105 ! paf 607: junction->method_frame->put_element(*exception_var_name, saved_exception_var_value, false);
1.74 paf 608:
609: if(!handled)
610: throw(e); // rethrow
611: }
612: // write it out
1.86 paf 613: r.write_pass_lang(result);
1.74 paf 614: }
615:
616: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
617: Pool& pool=r.pool();
618:
619: if(params->size()==1) {
620: Value& param0=params->get(0);
621: if(Hash *hash=param0.get_hash(&method_name)) {
1.78 paf 622: const char *type=0;
1.74 paf 623: if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
1.78 paf 624: type=value->as_string().cstr();
1.74 paf 625: const String *source=0;
626: if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
627: source=&value->as_string();
628: const char *comment=0;
629: if(Value *value=
630: static_cast<Value *>(hash->get(*exception_comment_part_name)))
631: comment=value->as_string().cstr();
632:
1.78 paf 633: throw Exception(type,
1.74 paf 634: source?source:&method_name,
635: comment);
636: } else
1.78 paf 637: throw Exception("parser.runtime",
1.74 paf 638: &method_name,
639: "one-param version has hash param");
640: } else {
1.78 paf 641: const char *type=params->as_string(0, "type must be string").cstr();
1.74 paf 642: const String& source=params->as_string(1, "source must be string");
1.92 paf 643: const char *comment=params->size()>2?params->as_string(2, "comment must be string").cstr():0;
1.97 paf 644: throw Exception(type, &source, "%s", comment?comment:"");
1.74 paf 645: }
646: }
647:
1.10 paf 648: // constructor
649:
1.91 paf 650: MOP::MOP(Pool& apool) : Methoded(apool, "OP"),
1.11 paf 651: main_sql_name(apool, MAIN_SQL_NAME),
652: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
653: {
1.1 paf 654: // ^if(condition){code-when-true}
655: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 656: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 657:
658: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 659: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 660:
661: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 662: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 663:
664: // ^process[code]
1.10 paf 665: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 666:
667: // ^rem{code}
1.51 parser 668: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 669:
670: // ^while(condition){code}
1.10 paf 671: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 672:
673: // ^use[file]
1.10 paf 674: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 675:
1.54 paf 676: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 677: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 678:
679: // ^eval(expr)
680: // ^eval(expr)[format]
1.10 paf 681: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 682:
1.1 paf 683: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 684: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
685:
1.63 paf 686:
1.65 paf 687: // ^cache[file_spec] delete cache
1.63 paf 688: // ^cache[file_spec](time){code} time=0 no cache
1.65 paf 689: add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63 paf 690:
1.28 parser 691: // switch
692:
693: // ^switch[value]{cases}
694: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
695:
696: // ^case[value]{code}
1.51 parser 697: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74 paf 698:
699: // try-catch
700:
701: // ^try{code}{catch code}
702: add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
703: // ^throw[$exception hash]
704: // ^throw[type;source;comment]
705: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
706:
1.10 paf 707: }
1.11 paf 708:
709: // constructor & configurator
1.1 paf 710:
1.10 paf 711: Methoded *MOP_create(Pool& pool) {
1.35 parser 712: return new(pool) MOP(pool);
1.11 paf 713: }
714:
715: void MOP::configure_user(Request& r) {
716: Pool& pool=r.pool();
717:
718: // $MAIN:SQL.drivers
1.103 paf 719: if(Value *sql=r.main_class->get_element(main_sql_name, r.main_class, false))
720: if(Value *element=sql->get_element(main_sql_drivers_name, sql, false))
1.11 paf 721: if(Table *protocol2library=element->get_table())
722: r.classes_conf.put(name(), protocol2library);
1.1 paf 723: }
E-mail: