--- parser3/src/classes/op.C 2013/08/21 12:11:13 1.215 +++ parser3/src/classes/op.C 2016/04/17 20:20:48 1.227 @@ -1,7 +1,7 @@ /** @file Parser: parser @b operators. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -18,7 +18,7 @@ #include "pa_vclass.h" #include "pa_charset.h" -volatile const char * IDENT_OP_C="$Id: op.C,v 1.215 2013/08/21 12:11:13 moko Exp $"; +volatile const char * IDENT_OP_C="$Id: op.C,v 1.227 2016/04/17 20:20:48 moko Exp $"; // limits @@ -27,9 +27,6 @@ volatile const char * IDENT_OP_C="$Id: o // defines #define CASE_DEFAULT_VALUE "DEFAULT" -#define PROCESS_MAIN_OPTION_NAME "main" -#define PROCESS_FILE_OPTION_NAME "file" -#define PROCESS_LINENO_OPTION_NAME "lineno" // class @@ -64,8 +61,7 @@ static const String exception_var_name(E class Untaint_lang_name2enum: public HashString { public: Untaint_lang_name2enum() { - #define ULN(name, LANG) \ - put(String::Body(name), (value_type)(String::L_##LANG)); + #define ULN(name, LANG) put(name, (value_type)(String::L_##LANG)); ULN("clean", CLEAN); ULN("as-is", AS_IS); ULN("optimized-as-is", AS_IS|String::L_OPTIMIZE_BIT); @@ -178,24 +174,33 @@ static void _process(Request& r, MethodP const String* main_alias=0; const String* file_alias=0; int line_no_alias_offset=0; + bool allow_class_replace=false; size_t options_index=index+1; if(options_indexget(PROCESS_MAIN_OPTION_NAME)) { - valid_options++; - main_alias=&vmain_alias->as_string(); - } - if(Value* vfile_alias=options->get(PROCESS_FILE_OPTION_NAME)) { - valid_options++; - file_alias=&vfile_alias->as_string(); - } - if(Value* vline_no_alias_offset=options->get(PROCESS_LINENO_OPTION_NAME)) { - valid_options++; - line_no_alias_offset=vline_no_alias_offset->as_int(); + for(HashStringValue::Iterator i(*options); i; i.next() ){ + + String::Body key=i.key(); + Value* value=i.value(); + + if(key == "main") { + valid_options++; + main_alias=&value->as_string(); + } else if(key == "file") { + valid_options++; + file_alias=&value->as_string(); + } else if(key == "lineno") { + valid_options++; + line_no_alias_offset=value->as_int(); + } else if(key == "replace") { + valid_options++; + allow_class_replace=r.process_to_value(*value).as_bool(); + } } - + if(valid_options!=options->count()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -207,6 +212,9 @@ static void _process(Request& r, MethodP Value& vjunction=params.as_junction(index, "body must be code"); // evaluate source to process const String& source=r.process_to_string(vjunction); + + Temp_class_replace class_replace(r, allow_class_replace); + r.use_buf(*target_class, source.untaint_cstr(String::L_AS_IS, r.connection(false)), main_alias, @@ -288,25 +296,44 @@ static void _while(Request& r, MethodPar static void _use(Request& r, MethodParams& params) { Value& vfile=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE); + bool allow_class_replace=false; + + if(params.count()==2) + if(HashStringValue* options=params.as_hash(1)) { + int valid_options=0; + for(HashStringValue::Iterator i(*options); i; i.next() ){ + + String::Body key=i.key(); + Value* value=i.value(); + + if(key == "replace") { + valid_options++; + allow_class_replace=r.process_to_value(*value).as_bool(); + } + + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + } + + Temp_class_replace class_replace(r, allow_class_replace); + // _use could be called from the parser3 method only, so caller is always defined r.use_file(r.main_class, vfile.as_string(), r.get_method_filename(&r.get_method_frame()->caller()->method)); } static void set_skip(Request& r, Request::Skip askip) { if(!r.get_in_cycle()) - throw Exception(askip==Request::SKIP_BREAK?"parser.break":"parser.continue", - 0, - "without cycle"); - + throw Exception(askip==Request::SKIP_BREAK ? "parser.break" : "parser.continue", 0, "without cycle"); r.set_skip(askip); } -static void _break(Request& r, MethodParams&) { - set_skip(r, Request::SKIP_BREAK); +static void _break(Request& r, MethodParams& params) { + if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_BREAK); } -static void _continue(Request& r, MethodParams&) { - set_skip(r, Request::SKIP_CONTINUE); +static void _continue(Request& r, MethodParams& params) { + if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_CONTINUE); } static void _for(Request& r, MethodParams& params) { @@ -326,7 +353,7 @@ static void _for(Request& r, MethodParam VInt* vint=new VInt(0); VMethodFrame& caller=*r.get_method_frame()->caller(); - caller.put_element(var_name, vint, false); + r.put_element(caller, var_name, vint); if(delim_maybe_code){ // delimiter set bool need_delim=false; @@ -524,13 +551,13 @@ static Try_catch_result try_catch(Reques Value* method_frame=junction->method_frame; Value* saved_exception_var_value=method_frame->get_element(exception_var_name); VMethodFrame& frame=*junction->method_frame; - frame.put_element(exception_var_name, &details.vhash, false); + frame.put_element(exception_var_name, &details.vhash); result.processed_code=r.process(*catch_code); // retriving $exception.handled, restoring $exception var Value* vhandled=details.vhash.hash().get(exception_handled_part_name); - frame.put_element(exception_var_name, saved_exception_var_value, false); + frame.put_element(exception_var_name, saved_exception_var_value); bool bhandled=false; if(vhandled) { @@ -554,6 +581,10 @@ static Try_catch_result try_catch(Reques return result; } +static StringOrValue process_try_body_code(Request& r, Value* body_code) { + return r.process(*body_code); +} + // cache-- // consts @@ -615,8 +646,8 @@ static void locked_process_and_cache_put } else throw Exception(PARSER_RUNTIME, result.exception_should_be_handled, - "$"EXCEPTION_VAR_NAME"."EXCEPTION_HANDLED_PART_NAME" value must be " - "either boolean or string '"CACHE_EXCEPTION_HANDLED_CACHE_NAME"'"); + "$" EXCEPTION_VAR_NAME "." EXCEPTION_HANDLED_PART_NAME " value must be " + "either boolean or string '" CACHE_EXCEPTION_HANDLED_CACHE_NAME "'"); } else info.processed_code=&result.processed_code.as_string(); @@ -694,7 +725,7 @@ static time_t as_expires(Request& r, Met int index, time_t now) { time_t result; if(Value* vdate=params[index].as(VDATE_TYPE)) - result=static_cast(vdate)->get_time(); + result=(time_t)(static_cast(vdate)->get_time()); else result=now+(time_t)params.as_double(index, "lifespan must be date or number", r); @@ -708,10 +739,8 @@ static void _cache(Request& r, MethodPar // ^cache[] -- return current expiration time Cache_scope* scope=static_cast(r.classes_conf.get(cache_data_name)); if(!scope) - throw Exception(PARSER_RUNTIME, - 0, - "expire-time get without cache"); - r.write_no_lang(*new VDate(scope->expires)); + throw Exception(PARSER_RUNTIME, 0, "expire-time get without cache"); + r.write_no_lang(*new VDate((pa_time_t)scope->expires)); return; } @@ -783,15 +812,16 @@ static void _cache(Request& r, MethodPar cache_delete(file_spec); } - // process without cacheing - const String& processed_body=r.process_to_string(body_code); - // write it out - r.write_assign_lang(processed_body); + // process without caching + if(catch_code){ + Try_catch_result result=try_catch(r, process_try_body_code, &body_code, catch_code); + r.write_assign_lang(result.processed_code); + } else { + const String& processed_body=r.process_to_string(body_code); + r.write_assign_lang(processed_body); + } } -static StringOrValue process_try_body_code(Request& r, Value* body_code) { - return r.process(*body_code); -} static void _try_operator(Request& r, MethodParams& params) { Value& body_code=params.as_junction(0, "body_code must be code"); Value& catch_code=params.as_junction(1, "catch_code must be code"); @@ -800,13 +830,7 @@ static void _try_operator(Request& r, Me Try_catch_result result; StringOrValue finally_result; try{ - result=try_catch(r, - process_try_body_code, &body_code, - &catch_code); - if(result.exception_should_be_handled) - throw Exception(PARSER_RUNTIME, - result.exception_should_be_handled, - "catch block must set $exception.handled to some boolean value, not string"); + result=try_catch(r, process_try_body_code, &body_code, &catch_code); } catch(...){ if(finally_code) finally_result=r.process(*finally_code); @@ -867,8 +891,7 @@ static void _bpt(Request&, MethodParams& // constructor -VClassMAIN::VClassMAIN(): VClass() { - set_name(*new String(MAIN_CLASS_NAME)); +VClassMAIN::VClassMAIN(): VClass(MAIN_CLASS_NAME) { #ifdef PA_BPT // ^bpt[] @@ -889,7 +912,8 @@ VClassMAIN::VClassMAIN(): VClass() { // ^apply-taint[untaint lang][string] add_native_method("apply-taint", Method::CT_ANY, _apply_taint, 1, 2, Method::CO_WITHOUT_FRAME); - // ^process[code] + // ^process{code} + // ^process[context]{code}[options hash] add_native_method("process", Method::CT_ANY, _process, 1, 3); // ^rem{code} @@ -898,14 +922,16 @@ VClassMAIN::VClassMAIN(): VClass() { // ^while(condition){code} add_native_method("while", Method::CT_ANY, _while, 2, 3, Method::CO_WITHOUT_FRAME); - // ^use[file] - add_native_method("use", Method::CT_ANY, _use, 1, 1); + // ^use[file[;options hash]] + add_native_method("use", Method::CT_ANY, _use, 1, 2); // ^break[] - add_native_method("break", Method::CT_ANY, _break, 0, 0, Method::CO_WITHOUT_FRAME); + // ^break(condition) + add_native_method("break", Method::CT_ANY, _break, 0, 1, Method::CO_WITHOUT_FRAME); // ^continue[] - add_native_method("continue", Method::CT_ANY, _continue, 0, 0, Method::CO_WITHOUT_FRAME); + // ^continue(condition) + add_native_method("continue", Method::CT_ANY, _continue, 0, 1, Method::CO_WITHOUT_FRAME); // ^for[i](from-number;to-number-inclusive){code}[delim] add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1, Method::CO_WITHOUT_WCONTEXT);