--- parser3/src/main/execute.C 2003/04/02 16:05:20 1.295.2.27.2.15 +++ parser3/src/main/execute.C 2005/07/28 10:02:26 1.309.2.4 @@ -1,11 +1,11 @@ /** @file Parser: executor part of request class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_EXECUTE_C="$Date: 2003/04/02 16:05:20 $"; +static const char * const IDENT_EXECUTE_C="$Date: 2005/07/28 10:02:26 $"; #include "pa_opcode.h" #include "pa_array.h" @@ -24,7 +24,6 @@ static const char* IDENT_EXECUTE_C="$Dat #include "pa_wwrapper.h" //#define DEBUG_EXECUTE -#define LATER_STACK_TRACE #ifdef DEBUG_EXECUTE char *opcode_name[]={ @@ -71,20 +70,21 @@ void debug_printf(SAPI_Info& sapi_info, void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) { Array_iterator i(ops); while(i.has_next()) { - OPCODE opinfo.code()=i.next().code(); + OPCODE opcode=i.next().code; - if(opinfo.code()==OP_VALUE || opinfo.code()==OP_STRING__WRITE) { + if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) { + Operation::Origin origin=i.next().origin; Value& value=*i.next().value; debug_printf(sapi_info, "%*s%s" " \"%s\" %s", - level*4, "", opcode_name[opinfo.code()], + level*4, "", opcode_name[opcode], value.get_string()->cstr(), value.type()); continue; } - debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opinfo.code()]); + debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]); - switch(opinfo.code()) { + switch(opcode) { case OP_CURLY_CODE__STORE_PARAM: case OP_EXPR_CODE__STORE_PARAM: case OP_CURLY_CODE__CONSTRUCT: @@ -105,7 +105,7 @@ void debug_dump(SAPI_Info& sapi_info, in void Request::execute(ArrayOperation& ops) { register Stack& stack=this->stack; // helps a lot on MSVC: 'esi' - const String* debug_name=0; Operation::Info debug_info; + const String* debug_name=0; Operation::Origin debug_origin={0, 0, 0}; try{ #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "source----------------------------\n"); @@ -119,17 +119,17 @@ void Request::execute(ArrayOperation& op 0, "execution stopped"); } - Operation::Info opinfo=i.next().info; + OPCODE opcode=i.next().code; #ifdef DEBUG_EXECUTE - debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opinfo.code()]); + debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]); #endif - switch(opinfo.code()) { + switch(opcode) { // param in next instruction case OP_VALUE: { - debug_info=opinfo; + debug_origin=i.next().origin; Value& value=*i.next().value; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type()); @@ -186,7 +186,8 @@ void Request::execute(ArrayOperation& op Value& value=stack.pop().value(); const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); - ncontext.put_element(name, &value, false); + put_element(ncontext, name, value); + break; } case OP_CONSTRUCT_EXPR: @@ -194,10 +195,11 @@ void Request::execute(ArrayOperation& op // see OP_PREPARE_TO_EXPRESSION wcontext->set_in_expression(false); - Value& value=stack.pop().value(); + Value& expr=stack.pop().value(); const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); - ncontext.put_element(name, &value.as_expr_result(), false); + Value& value=expr.as_expr_result(); + put_element(ncontext, name, value); break; } case OP_CURLY_CODE__CONSTRUCT: @@ -216,7 +218,12 @@ void Request::execute(ArrayOperation& op const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); - ncontext.put_element(name, &value, false); + if(const Junction* junction=ncontext.put_element(ncontext, name, &value, false)) + if(junction!=PUT_ELEMENT_REPLACED_ELEMENT) + throw Exception("parser.runtime", + 0, + "property value can not be code, use [] or () brackets"); + break; } case OP_NESTED_CODE: @@ -247,6 +254,7 @@ void Request::execute(ArrayOperation& op } case OP_STRING__WRITE: { + i.next(); // ignore origin Value* value=i.next().value; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr()); @@ -353,7 +361,7 @@ void Request::execute(ArrayOperation& op get_self(), 0, method_frame, rcontext, - opinfo.code()==OP_EXPR_CODE__STORE_PARAM?0:wcontext, + opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext, &local_ops)); // store param // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0 @@ -376,6 +384,7 @@ void Request::execute(ArrayOperation& op case OP_CALL: case OP_CALL__WRITE: { + //is_debug_junction=true; ArrayOperation* local_ops=i.next().ops; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0); @@ -387,11 +396,17 @@ void Request::execute(ArrayOperation& op Value& value=stack.pop().value(); Junction* junction=value.get_junction(); - if(!junction) - throw Exception("parser.runtime", - 0, - "(%s) not a method or junction, can not call it", - value.type()); + if(!junction) { + if(value.is("void")) + throw Exception("parser.runtime", + 0, + "undefined method"); + else + throw Exception("parser.runtime", + 0, + "is '%s', not a method or junction, can not call it", + value.type()); + } /* no check needed, code compiled the way that that's impossible // check: // that this is method-junction, not a code-junction @@ -401,7 +416,7 @@ void Request::execute(ArrayOperation& op if(!junction->method) throw Exception("parser.runtime", - "(%s) is code junction, can not call it", + "is '%s', it is code junction, can not call it", value.type()); */ @@ -417,13 +432,15 @@ void Request::execute(ArrayOperation& op WContext *saved_wcontext=wcontext; VStateless_class& called_class=*frame.junction.self.get_class(); - Value* new_self=&get_self(); + Value* new_self; if(wcontext->get_constructing()) { wcontext->set_constructing(false); + new_self=&get_self(); if(frame.junction.method->call_type!=Method::CT_STATIC) { // this is a constructor call - if(Value* value=called_class.create_new_value()) { + HashStringValue& new_object_fields=*new HashStringValue(); + if(Value* value=called_class.create_new_value(fpool, new_object_fields)) { // some stateless_class creatable derivates new_self=value; } else @@ -462,9 +479,9 @@ void Request::execute(ArrayOperation& op /*frame.name(), */frame.numbered_params()); method.native_code( *this, - /*frame.name(), */frame.numbered_params()); // execute it + *frame.numbered_params()); // execute it } else // parser code, execute it - recoursion_checked_execute(/*frame.name(), */*method.parser_code); + recoursion_checked_execute(*method.parser_code); } else throw Exception("parser.runtime", 0, //&frame.name(), @@ -478,7 +495,7 @@ void Request::execute(ArrayOperation& op rcontext=saved_rcontext; method_frame=saved_method_frame; - if(opinfo.code()==OP_CALL__WRITE) { + if(opcode==OP_CALL__WRITE) { write_assign_lang(result); } else { // OP_CALL stack.push(result.as_value()); @@ -487,6 +504,7 @@ void Request::execute(ArrayOperation& op #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "<-returned"); #endif + //is_debug_junction=false; break; } @@ -701,49 +719,49 @@ void Request::execute(ArrayOperation& op } case OP_NUM_LT: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result < 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double>b_double); stack.push(value); break; } case OP_NUM_LE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result <= 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double<=b_double); stack.push(value); break; } case OP_NUM_GE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result >= 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double>=b_double); stack.push(value); break; } case OP_NUM_EQ: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result == 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double==b_double); stack.push(value); break; } case OP_NUM_NE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result != 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double!=b_double); stack.push(value); break; } @@ -800,12 +818,13 @@ void Request::execute(ArrayOperation& op default: throw Exception(0, 0, - "invalid opinfo.code() 0x%X", opinfo.code()); + "invalid opcode %d", opcode); } } - } catch(...) { + } catch(const Exception&) { // record it to stack trace - exception_trace.push(Trace(debug_name, debug_info)); + if(debug_name) + exception_trace.push(Trace(debug_name, debug_origin)); rethrow; } } @@ -818,8 +837,7 @@ Value& Request::get_element(Value& ncont Value* value=0; if(can_call_operator) { if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST - value=new VJunction(new Junction( - main_class, method, 0,0,0, 0)); + value=new VJunction(new Junction(main_class, method)); } if(!value) { if(!wcontext->get_constructing() // not constructing @@ -854,6 +872,42 @@ value_ready: return *value; } +void Request::put_element(Value& ncontext, const String& name, Value& value) { + // put_element can return property-setting-junction + if(const Junction* junction=ncontext.put_element(ncontext, name, &value, false)) + if(junction!=PUT_ELEMENT_REPLACED_ELEMENT) { + // process it + ArrayString* params_names=junction->method->params_names; + int param_count=params_names? params_names->count(): 0; + if(param_count!=1) + throw Exception("parser.runtime", + 0, + "setter method must have ONE parameter (has %d parameters)", param_count); + + VMethodFrame frame(*junction, method_frame/*caller*/); + frame.store_param(value); + + frame.set_self(frame.junction.self); + + VMethodFrame *saved_method_frame=method_frame; + Value* saved_rcontext=rcontext; + WContext *saved_wcontext=wcontext; + + rcontext=wcontext=&frame; + method_frame=&frame; + + // prevent non-string writes for better error reporting + wcontext->write(*method_frame); + + recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it + // we don't need it StringOrValue result=wcontext->result(); + + method_frame=saved_method_frame; + wcontext=saved_wcontext; + rcontext=saved_rcontext; + } +} + /** @param intercept_string - true: they want result=string value, @@ -866,68 +920,101 @@ value_ready: using the fact it's either string_ or value_ result requested to speed up checkes */ StringOrValue Request::process(Value& input_value, bool intercept_string) { - StringOrValue result; Junction* junction=input_value.get_junction(); - if(junction && junction->code) { // is it a code-junction? - // process it + if(junction) { + if(junction->is_getter) { // is it a getter-junction? + // process it + + VMethodFrame frame(*junction, method_frame/*caller*/); + if(junction->method->params_names) + throw Exception("parser.runtime", + 0, + "getter method must have no parameters"); + + frame.set_self(frame.junction.self); + + VMethodFrame *saved_method_frame=method_frame; + Value* saved_rcontext=rcontext; + WContext *saved_wcontext=wcontext; + + rcontext=wcontext=&frame; + method_frame=&frame; + + recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it + StringOrValue result=wcontext->result(); + + method_frame=saved_method_frame; + wcontext=saved_wcontext; + rcontext=saved_rcontext; + + return result; + } + + if(junction->code) { // is it a code-junction? + // process it + StringOrValue result; #ifdef DEBUG_EXECUTE - debug_printf(sapi_info, "ja->\n"); + debug_printf(sapi_info, "ja->\n"); #endif - if(!junction->method_frame) - throw Exception("parser.runtime", + if(!junction->method_frame) + throw Exception("parser.runtime", 0, "junction used outside of context"); - VMethodFrame *saved_method_frame=method_frame; - Value* saved_rcontext=rcontext; - WContext *saved_wcontext=wcontext; - - method_frame=junction->method_frame; - rcontext=junction->rcontext; - - // for expression method params - // wcontext is set 0 - // using the fact in decision "which wwrapper to use" - bool using_code_frame=intercept_string && junction->wcontext; - if(using_code_frame) { - // almost plain wwrapper about junction wcontext, - // BUT intercepts string writes - VCodeFrame local(*junction->wcontext, junction->wcontext); - wcontext=&local; + VMethodFrame *saved_method_frame=method_frame; + Value* saved_rcontext=rcontext; + WContext *saved_wcontext=wcontext; + + method_frame=junction->method_frame; + rcontext=junction->rcontext; + + // for expression method params + // wcontext is set 0 + // using the fact in decision "which wwrapper to use" + bool using_code_frame=intercept_string && junction->wcontext; + if(using_code_frame) { + // almost plain wwrapper about junction wcontext, + // BUT intercepts string writes + VCodeFrame local(*junction->wcontext, junction->wcontext); + wcontext=&local; + + // execute it + recoursion_checked_execute(*junction->code); + + // CodeFrame soul: + // string writes were intercepted + // returning them as the result of getting code-junction + result.set_string(*wcontext->get_string()); + } else { + // plain wwrapper + WWrapper local(0/*empty*/, wcontext); + wcontext=&local; + + // execute it + recoursion_checked_execute(*junction->code); + + result=wcontext->result(); + } + + wcontext=saved_wcontext; + rcontext=saved_rcontext; + method_frame=saved_method_frame; - // execute it - recoursion_checked_execute(/*0/*result_name* /, */ *junction->code); - - // CodeFrame soul: - // string writes were intercepted - // returning them as the result of getting code-junction - result.set_string(*wcontext->get_string()); - } else { - // plain wwrapper - WWrapper local(0/*empty*/, wcontext); - wcontext=&local; - - // execute it - recoursion_checked_execute(/*0/*result_name* /, */ *junction->code); - - result=wcontext->result(); - } - - wcontext=saved_wcontext; - rcontext=saved_rcontext; - method_frame=saved_method_frame; - #ifdef DEBUG_EXECUTE - debug_printf(sapi_info, "<-ja returned"); + debug_printf(sapi_info, "<-ja returned"); #endif - } else { - result.set_value(input_value); - } - return result; + return result; + } + + // it is then method-junction, do not explode it + // just return it as we do for usual objects + } + + return input_value; } -const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) { +StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) { VMethodFrame *saved_method_frame=method_frame; Value* saved_rcontext=rcontext; WContext *saved_wcontext=wcontext; @@ -939,7 +1026,7 @@ const String& Request::execute_method(VM execute(*method.parser_code); // result - const String& result=wcontext->result().as_string(); + StringOrValue result=wcontext->result(); wcontext=saved_wcontext; rcontext=saved_rcontext; @@ -950,14 +1037,14 @@ const String& Request::execute_method(VM } const String* Request::execute_method(Value& aself, - const Method& method, VString* optional_param, - bool do_return_string) { + const Method& method, VString* optional_param, + bool do_return_string) { VMethodFrame *saved_method_frame=method_frame; Value* saved_rcontext=rcontext; WContext *saved_wcontext=wcontext; - Junction local_junction(aself, &method, 0,0,0, 0); - VMethodFrame local_frame(/*method.name, */local_junction, method_frame/*caller*/); + Junction local_junction(aself, &method); + VMethodFrame local_frame(local_junction, method_frame/*caller*/); if(optional_param && local_frame.can_store_param()) { local_frame.store_param(*optional_param); local_frame.fill_unspecified_params();