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