--- parser3/src/main/execute.C 2001/05/17 09:21:55 1.158 +++ parser3/src/main/execute.C 2001/07/25 08:16:21 1.182 @@ -4,17 +4,15 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: execute.C,v 1.158 2001/05/17 09:21:55 parser Exp $ */ +static const char *RCSId="$Id: execute.C,v 1.182 2001/07/25 08:16:21 parser Exp $"; -#include "pa_config_includes.h" #include "pa_opcode.h" #include "pa_array.h" #include "pa_request.h" #include "pa_vstring.h" #include "pa_vhash.h" -#include "pa_vunknown.h" +#include "pa_vvoid.h" #include "pa_vcode_frame.h" #include "pa_vmethod_frame.h" #include "pa_vobject.h" @@ -26,10 +24,7 @@ //#define DEBUG_EXECUTE - -#define PUSH(value) stack.push(value) -#define POP() static_cast(stack.pop()) -#define POP_NAME() static_cast(stack.pop())->as_string() +const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500; #ifdef DEBUG_EXECUTE char *opcode_name[]={ @@ -39,11 +34,10 @@ char *opcode_name[]={ // actions "WITH_SELF", "WITH_ROOT", "WITH_READ", "WITH_WRITE", "GET_CLASS", - "CONSTRUCT_VALUE", "CONSTRUCT_DOUBLE", + "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT", "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE", "GET_ELEMENT", "GET_ELEMENT__WRITE", "CREATE_EWPOOL", "REDUCE_EWPOOL", - "CREATE_RWPOOL", "REDUCE_RWPOOL", "CREATE_SWPOOL", "REDUCE_SWPOOL", "GET_METHOD_FRAME", "STORE_PARAM", @@ -74,7 +68,7 @@ void debug_printf(Pool& pool, const char } void debug_dump(Pool& pool, int level, const Array& ops) { - { +/* { int size=ops.quick_size(); //debug_printf(pool, "size=%d\n", size); for(int i=0; i(stack.pop()) +#define POP_NAME() static_cast(stack.pop())->as_string() + void Request::execute(const Array& ops) { #ifdef DEBUG_EXECUTE debug_printf(pool(), "source----------------------------\n"); @@ -223,6 +221,27 @@ void Request::execute(const Array& ops) value->set_name(name); break; } + case OP_CURLY_CODE__CONSTRUCT: + { + const Array *local_ops=reinterpret_cast(ops.quick_get(++i)); +#ifdef DEBUG_EXECUTE + debug_printf(pool(), " (%d)\n", local_ops->size()); + debug_dump(pool(), 1, *local_ops); +#endif + Junction& j=*NEW Junction(pool(), + *self, 0, 0, + root, + rcontext, + wcontext, + local_ops); + + Value *value=NEW VJunction(j); + const String& name=POP_NAME(); + Value *ncontext=POP(); + ncontext->put_element(name, value); + value->set_name(name); + break; + } case OP_WRITE_VALUE: { Value *value=POP(); @@ -254,6 +273,7 @@ void Request::execute(const Array& ops) // maybe they do ^object.method[] call, remember the fact wcontext->inc_somebody_entered_some_object(); + //_asm int 3; Value *value=get_element(); PUSH(value); break; @@ -284,28 +304,6 @@ void Request::execute(const Array& ops) break; } - case OP_CREATE_RWPOOL: - { - Value *ncontext=POP(); - PUSH(rcontext); - PUSH(wcontext); - rcontext=ncontext; - wcontext=NEW WWrapper(pool(), ncontext, false /*not constructing*/); - break; - } - case OP_REDUCE_RWPOOL: - { - const String *string=wcontext->get_string(); - Value *value; - if(string) - value=NEW VString(*string); - else - value=NEW VUnknown(pool()); - wcontext=static_cast(POP()); - rcontext=POP(); - PUSH(value); - break; - } case OP_CREATE_SWPOOL: { PUSH(wcontext); @@ -321,7 +319,7 @@ void Request::execute(const Array& ops) if(string) value=NEW VString(*string); else - NEW VUnknown(pool()); + NEW VVoid(pool()); wcontext=static_cast(POP()); PUSH(value); break; @@ -373,10 +371,12 @@ void Request::execute(const Array& ops) PUSH(wcontext); VStateless_class *called_class=frame->junction.self.get_class(); - // not ^name.method call and + // not ^name.method call, name:method call; and // is context object or class & is it my class or my parent's class and? VStateless_class *read_class=rcontext->get_class(); - if(!wcontext->somebody_entered_some_object() && + if( + !(wcontext->somebody_entered_some_object() && + !wcontext->somebody_entered_some_class()) && read_class && read_class->is_or_derived_from(*called_class)) // yes self=rcontext; // class dynamic call else // no, not me or relative of mine (total stranger) @@ -415,11 +415,20 @@ void Request::execute(const Array& ops) method.check_actual_numbered_params(pool(), frame->junction.self, frame->name(), frame->numbered_params()); - (*method.native_code)( + method.native_code( *this, frame->name(), frame->numbered_params()); // execute it - } else // parser code + } else { // parser code + if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) { + anti_endless_execute_recoursion=0; // give @exception a chance + THROW(0, 0, + &frame->name(), + "endless recursion detected"); + } + execute(*method.parser_code); // execute it + anti_endless_execute_recoursion--; + } else THROW(0, 0, &frame->name(), @@ -521,16 +530,44 @@ void Request::execute(const Array& ops) case OP_DIV: { Value *b=POP(); Value *a=POP(); - Value *value=NEW VDouble(pool(), a->as_double() / b->as_double()); + + double a_double=a->as_double(); + double b_double=b->as_double(); + + if(b_double == 0) { + const String *problem_source=&b->as_string(); +#ifndef NO_STRING_ORIGIN + if(!problem_source->origin().file) + problem_source=&b->name(); +#endif + THROW(0, 0, + problem_source, + "Division by zero"); + } + + Value *value=NEW VDouble(pool(), a_double / b_double); PUSH(value); break; } case OP_MOD: { Value *b=POP(); Value *a=POP(); - Value *value=NEW VDouble(pool(), - a->as_int() % - b->as_int()); + + double a_double=a->as_double(); + double b_double=b->as_double(); + + if(b_double == 0) { + const String *problem_source=&b->as_string(); +#ifndef NO_STRING_ORIGIN + if(!problem_source->origin().file) + problem_source=&b->name(); +#endif + THROW(0, 0, + problem_source, + "Modulus by zero"); + } + + Value *value=NEW VDouble(pool(), fmod(a_double, b_double)); PUSH(value); break; } @@ -685,26 +722,25 @@ void Request::execute(const Array& ops) Value *Request::get_element() { const String& name=POP_NAME(); Value *ncontext=POP(); - Value *value; - if(Method* method=OP.get_method(name)) { // operator? - // as if that method were in self and we have normal dynamic method here - Junction& junction=*NEW Junction(pool(), - *self, self->get_class(), method, 0,0,0,0); - value=NEW VJunction(junction); - } else - value=ncontext->get_element(name); + Value *value=ncontext->get_element(name); + if(!value) + if(Method* method=OP.get_method(name)) { // maybe operator? + // as if that method were in self and we have normal dynamic method here + Junction& junction=*NEW Junction(pool(), + *self, self->get_class(), method, 0,0,0,0); + value=NEW VJunction(junction); + } if(value) value=&process(*value, &name); // process possible code-junction else { - value=NEW VUnknown(pool()); + value=NEW VVoid(pool()); value->set_name(name); } return value; } -/** - intercept_string: +/** @param intercept_string - true: they want result=string value, possible object result goes to wcontext @@ -769,7 +805,8 @@ Value& Request::process(Value& value, co return *result; } -const String *Request::execute_method(Value& aself, const Method& method, bool return_cstr) { +const String *Request::execute_method(Value& aself, const Method& method, + bool return_cstr) { PUSH(self); PUSH(root); PUSH(rcontext); @@ -798,8 +835,9 @@ const String *Request::execute_method(Va return result; } -const String *Request::execute_method(Value& aself, - const String& method_name, bool return_cstr) { +const String *Request::execute_virtual_method(Value& aself, + const String& method_name, + bool return_cstr) { if(Value *value=aself.get_element(method_name)) if(Junction *junction=value->get_junction()) if(const Method *method=junction->method) @@ -807,3 +845,12 @@ const String *Request::execute_method(Va return 0; } + +const String *Request::execute_nonvirtual_method(VStateless_class& aclass, + const String& method_name, + bool return_cstr) { + if(const Method *method=aclass.get_method(method_name)) + return execute_method(aclass, *method, return_cstr); + + return 0; +}