--- parser3/src/classes/op.C 2013/08/21 12:11:13 1.215 +++ parser3/src/classes/op.C 2015/09/22 23:37:44 1.221 @@ -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.221 2015/09/22 23:37:44 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 @@ -178,24 +175,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 +213,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,6 +297,28 @@ 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)); } @@ -326,7 +357,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 +555,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) { @@ -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; } @@ -889,7 +918,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,8 +928,8 @@ 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);