--- parser3/src/classes/op.C 2024/12/23 18:30:55 1.272 +++ parser3/src/classes/op.C 2026/09/25 12:55:42 1.282 @@ -1,7 +1,7 @@ /** @file Parser: parser @b operators. - Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ @@ -17,8 +17,13 @@ #include "pa_vmethod_frame.h" #include "pa_vclass.h" #include "pa_charset.h" +#include "pa_varray.h" -volatile const char * IDENT_OP_C="$Id: op.C,v 1.272 2024/12/23 18:30:55 moko Exp $"; +#ifdef HAVE_SYSLOG +#include "syslog.h" +#endif + +volatile const char * IDENT_OP_C="$Id: op.C,v 1.282 2026/09/25 12:55:42 moko Exp $"; // defines @@ -182,30 +187,20 @@ static void _process(Request& r, MethodP size_t options_index=index+1; if(options_indexas_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(*value).as_bool(); - } + } else + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } - - if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } uint processe_file_no=file_alias ? r.register_file(r.full_disk_path(*file_alias)) : pseudo_file_no__process; @@ -295,28 +290,16 @@ static void _use(Request& r, MethodParam 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 == "origin") { - valid_options++; use_origin=&value->as_string(); - } - - if(key == "replace") { - valid_options++; + } else if(key == "replace") { allow_class_replace=r.process(*value).as_bool(); - } - - if(key == "main") { - valid_options++; + } else if(key == "main") { load_auto_p=r.process(*value).as_bool(); - } - - if(valid_options!=options->count()) + } else throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } } @@ -554,7 +537,6 @@ template 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) { Try_catch_result result; - // minor bug: context not restored if only finally code is present, see #1062 if(!catch_code) { result.processed_code=body_code(r, info); return result; @@ -562,38 +544,48 @@ static Try_catch_result try_catch(Reques // taking snapshot of try-context Request_context_saver try_context(r); + size_t trace_start=r.exception_trace.top_index(); + try { result.processed_code=body_code(r, info); } catch(const Exception& e) { - Request_context_saver throw_context(r); // remembering exception stack trace + size_t trace_bottom=r.exception_trace.bottom_index(); + size_t trace_top=r.exception_trace.top_index(); - Request::Exception_details details=r.get_details(e); + VException& details=*new VException(r, e); - try_context.restore(); // restoring try-context for code after try and catch-code + try_context.restore(); // restoring try-context for catch code - { - Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details.vhash); - Temp_skip temp_skip(r); - result.processed_code=r.process(*catch_code); - } - - // retriving $exception.handled - Value* vhandled=details.vhash.hash().get(exception_handled_part_name); + r.exception_trace.set_bottom_index(trace_top); // keep the original trace occupied; errors in catch start after it bool bhandled=false; - if(vhandled) { - if(vhandled->is_string()) { // not simple $exception.handled(1/0)? - if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it - result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover - } - } else - bhandled=vhandled->as_bool(); + try { + { + Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details); + Temp_skip temp_skip(r); + result.processed_code=r.process(*catch_code); + } + + Value* vhandled=details.handled(); + if(vhandled) { + if(vhandled->is_string()) { // not simple $exception.handled(1/0)? + if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it + result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover + } + } else + bhandled=vhandled->as_bool(); + } + } catch(...) { + // restore execution for finally, but keep new error trace. + try_context.restore(); + rethrow; } if(!bhandled){ - throw_context.restore(); // restoring exception stack trace creared by try_context.restore() + r.exception_trace.set_range(trace_bottom, trace_top); rethrow; } + r.exception_trace.set_range(trace_start, trace_start); } return result; @@ -664,7 +656,7 @@ 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 " + "$" EXCEPTION_VAR_NAME ".handled value must be " "either boolean or string '" CACHE_EXCEPTION_HANDLED_CACHE_NAME "'"); } else info.processed_code=&((Value &)result.processed_code).as_string(); @@ -739,7 +731,7 @@ static Cache_get_result cache_get(const static time_t as_expires(Request& r, MethodParams& params, int index, time_t now) { time_t result; if(VDate* vdate=dynamic_cast(¶ms[index])) - result=vdate->get_time(); + result=(time_t)vdate->get_time(); else result=now+(time_t)params.as_double(index, "lifespan must be date or number", r); @@ -850,8 +842,12 @@ static void _try_operator(Request& r, Me } catch(...){ // process finally code but ignore the result if(finally_code){ + size_t trace_bottom=r.exception_trace.bottom_index(); + size_t trace_top=r.exception_trace.top_index(); + r.exception_trace.set_bottom_index(trace_top); Temp_skip temp(r); /* Value &finally_result= */ r.process(*finally_code); + r.exception_trace.set_range(trace_bottom, trace_top); } rethrow; } @@ -876,13 +872,13 @@ static void _throw_operator(Request&, Me if(params.count()==1 && !params[0].is_string()) { if(HashStringValue *hash=params[0].get_hash()) { const char* type=0; - if(Value* value=hash->get(exception_type_part_name)) + if(Value* value=hash->get(Symbols::TYPE_SYMBOL)) type=value->as_string().cstr(); const String* source=0; - if(Value* value=hash->get(exception_source_part_name)) + if(Value* value=hash->get(Symbols::SOURCE_SYMBOL)) source=&value->as_string(); const char* comment=0; - if(Value* value=hash->get(exception_comment_part_name)) + if(Value* value=hash->get(Symbols::COMMENT_SYMBOL)) comment=value->as_string().cstr(); throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit @@ -902,6 +898,46 @@ static void _sleep_operator(Request& r, pa_sleep((int)trunc(seconds), (int)trunc((seconds-trunc(seconds))*1000000)); } +static void _a_operator(Request& r, MethodParams& params) { + VArray& result=*new VArray; + ArrayValue& array=result.array(); + + int count=params.count(); + for(int i=0; i2 ? log_level(params.as_string(2, "level must be string")) : LOG_INFO; + + openlog(*ident ? ident : "parser3", LOG_PID, LOG_USER); + syslog(level, "%s", message); + closelog(); + (void)r; +#else + SAPI::log(r.sapi_info, "syslog: %s", message); +#endif +} + #if defined(WIN32) && defined(_DEBUG) && !defined(_WIN64) # define PA_BPT static void _bpt(Request&, MethodParams&) { @@ -990,6 +1026,12 @@ VClassMAIN::VClassMAIN(): VClass(MAIN_CL add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3); add_native_method("sleep", Method::CT_ANY, _sleep_operator, 1, 1); + + // equivalent to ^array::create[...] + add_native_method("A", Method::CT_ANY, _a_operator, 0, 10000, Method::CO_WITHOUT_FRAME); + + // ^syslog[ident;message[;info|warning|error|debug]] + add_native_method("syslog", Method::CT_ANY, _syslog_operator, 2, 3); } // constructor & configurator