Diff for /parser3/src/main/execute.C between versions 1.201 and 1.211

version 1.201, 2001/10/31 13:07:35 version 1.211, 2002/01/31 12:40:35
Line 2 Line 2
         Parser: executor part of request class.          Parser: executor part of request class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
   
         $Id$          $Id$
 */  */
Line 25 Line 25
   
 //#define DEBUG_EXECUTE  //#define DEBUG_EXECUTE
   
 const uint ANTI_ENDLESS_EXECUTE_RECOURSION=200;  const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
   
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
 char *opcode_name[]={  char *opcode_name[]={
         // literals          // literals
         "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",          "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",
           "NESTED_CODE",
   
         // actions          // actions
         "WITH_ROOT",    "WITH_SELF",    "WITH_READ",    "WITH_WRITE",          "WITH_ROOT",    "WITH_SELF",    "WITH_READ",    "WITH_WRITE",
Line 89  void debug_dump(Pool& pool, int level, c Line 90  void debug_dump(Pool& pool, int level, c
                 case OP_CURLY_CODE__STORE_PARAM:                   case OP_CURLY_CODE__STORE_PARAM: 
                 case OP_EXPR_CODE__STORE_PARAM:                  case OP_EXPR_CODE__STORE_PARAM:
                 case OP_CURLY_CODE__CONSTRUCT:                  case OP_CURLY_CODE__CONSTRUCT:
                   case OP_NESTED_CODE:
                         const Array *local_ops=reinterpret_cast<const Array *>(i.next());                          const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                         debug_dump(pool, level+1, *local_ops);                          debug_dump(pool, level+1, *local_ops);
                 }                  }
Line 99  void debug_dump(Pool& pool, int level, c Line 101  void debug_dump(Pool& pool, int level, c
 #define PUSH(value) stack.push(value)  #define PUSH(value) stack.push(value)
 #define POP() static_cast<Value *>(stack.pop())  #define POP() static_cast<Value *>(stack.pop())
 #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()  #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
   #define POP_CODE() static_cast<Array *>(stack.pop())
   
 void Request::execute(const Array& ops) {  void Request::execute(const Array& ops) {
 //      _asm int 3;  //      _asm int 3;
Line 118  void Request::execute(const Array& ops) Line 121  void Request::execute(const Array& ops)
   
                 Value *value;                  Value *value;
                 Value *a; Value *b;                  Value *a; Value *b;
                   Array *b_code;
                 switch(op.code) {                  switch(op.code) {
                 // param in next instruction                  // param in next instruction
                 case OP_VALUE:                  case OP_VALUE:
Line 162  void Request::execute(const Array& ops) Line 166  void Request::execute(const Array& ops)
                 case OP_GET_CLASS:                  case OP_GET_CLASS:
                         {                          {
                                 // maybe they do ^class:method[] call, remember the fact                                  // maybe they do ^class:method[] call, remember the fact
                                 wcontext->set_somebody_entered_some_class();                                  wcontext->set_somebody_entered_some_class(true);
   
                                 const String& name=POP_NAME();                                  const String& name=POP_NAME();
                                 value=static_cast<Value *>(classes().get(name));                                  value=static_cast<Value *>(classes().get(name));
Line 205  void Request::execute(const Array& ops) Line 209  void Request::execute(const Array& ops)
                                 Value *ncontext=POP();                                  Value *ncontext=POP();
                                 ncontext->put_element(name, value);                                  ncontext->put_element(name, value);
                                 value->set_name(name);                                  value->set_name(name);
   
                                   // forget the fact they've entered some $class/object.xxx
                                   // see OP_GET_ELEMENT
                                   wcontext->set_somebody_entered_some_object(false);
                                   wcontext->set_somebody_entered_some_class(false);
                                 break;                                  break;
                         }                          }
                 case OP_CONSTRUCT_EXPR:                  case OP_CONSTRUCT_EXPR:
Line 237  void Request::execute(const Array& ops) Line 246  void Request::execute(const Array& ops)
                                 value->set_name(name);                                  value->set_name(name);
                                 break;                                  break;
                         }                          }
                   case OP_NESTED_CODE:
                           {
                                   Array *local_ops=static_cast<Array *>(i.next());
   #ifdef DEBUG_EXECUTE
                                   debug_printf(pool(), " (%d)\n", local_ops->size());
                                   debug_dump(pool(), 1, *local_ops);
   #endif                          
                                   PUSH(local_ops);
                                   break;
                           }
                 case OP_WRITE_VALUE:                  case OP_WRITE_VALUE:
                         {                          {
                                 value=POP();                                  value=POP();
                                 write_assign_lang(*value);                                  write_assign_lang(*value);
   
                                 // forget the fact they've entered some ^object.method[].                                  // forget the fact they've entered some ^object/class.xxx or $object/class.xxx
                                 // see OP_GET_ELEMENT                                  // see OP_GET_ELEMENT
                                 wcontext->set_somebody_entered_some_object(false);                                  wcontext->set_somebody_entered_some_object(false);
                                   wcontext->set_somebody_entered_some_class(false);
                                 break;                                  break;
                         }                          }
                 case OP_WRITE_EXPR_RESULT:                  case OP_WRITE_EXPR_RESULT:
Line 265  void Request::execute(const Array& ops) Line 285  void Request::execute(const Array& ops)
                                                   
                 case OP_GET_ELEMENT:                  case OP_GET_ELEMENT:
                         {                          {
                                   //_asm int 3;
                                   // uses entered object/class flags to plug in operators
                                   value=get_element();
   
                                 // maybe they do ^object.method[] call, remember the fact                                  // maybe they do ^object.method[] call, remember the fact
                                   // must be below get_element() call, it checks it
                                 wcontext->set_somebody_entered_some_object(true);                                  wcontext->set_somebody_entered_some_object(true);
   
                                 //_asm int 3;  
                                 value=get_element();  
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
Line 292  void Request::execute(const Array& ops) Line 315  void Request::execute(const Array& ops)
                         }                          }
                 case OP_REDUCE_EWPOOL:                  case OP_REDUCE_EWPOOL:
                         {                          {
                                 value=wcontext->result();                                  value=&wcontext->result();
                                 flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));                                  flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));
                                 wcontext=static_cast<WContext *>(POP());                                  wcontext=static_cast<WContext *>(POP());
                                 PUSH(value);                                  PUSH(value);
Line 371  void Request::execute(const Array& ops) Line 394  void Request::execute(const Array& ops)
                                                 // this is a constructor call                                                  // this is a constructor call
   
                                                 if(Value *value=called_class->create_new_value(pool())) {                                                  if(Value *value=called_class->create_new_value(pool())) {
                                                         // some stateless_object creatable derivates                                                          // some stateless_class creatable derivates
                                                         self=value;                                                          self=value;
                                                 } else {                                                  } else 
                                                         // stateful object                                                          throw Exception(0, 0,
                                                         self=NEW VObject(pool(), *called_class);                                                                  &frame->name(),
                                                 }                                                                  "is not a constructor, system class '%s' can be constructed only implicitly", 
                                                                   called_class->name().cstr());
   
                                                 frame->write(*self,                                                   frame->write(*self, 
                                                         String::UL_CLEAN  // not used, always an object, not string                                                          String::UL_CLEAN  // not used, always an object, not string
                                                 );                                                  );
Line 400  void Request::execute(const Array& ops) Line 425  void Request::execute(const Array& ops)
                                 }                                  }
   
                                 frame->set_self(*self);                                  frame->set_self(*self);
                                 root=rcontext=wcontext=frame;                                  rcontext=wcontext=frame;
                                 {                                  {
                                         // take object or class from any wrappers                                          // take object or class from any wrappers
                                         // and substitute class alias to the class they are called AS                                          // and substitute class alias to the class they are called AS
Line 414  void Request::execute(const Array& ops) Line 439  void Request::execute(const Array& ops)
                                                 method.call_type==call_type) { // allowed call type?                                                  method.call_type==call_type) { // allowed call type?
                                                 try {                                                  try {
                                                         if(method.native_code) { // native code?                                                          if(method.native_code) { // native code?
                                                                   // root unchanged, so that ^for ^foreach & co may write to locals
                                                                 method.check_actual_numbered_params(                                                                  method.check_actual_numbered_params(
                                                                         frame->junction.self,                                                                           frame->junction.self, 
                                                                         frame->name(), frame->numbered_params());                                                                          frame->name(), frame->numbered_params());
Line 421  void Request::execute(const Array& ops) Line 447  void Request::execute(const Array& ops)
                                                                         *this,                                                                           *this, 
                                                                         frame->name(), frame->numbered_params()); // execute it                                                                          frame->name(), frame->numbered_params()); // execute it
                                                         } else { // parser code                                                          } else { // parser code
                                                                 if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {                                                                  root=frame;
                                                                         anti_endless_execute_recoursion=0; // give @exception a chance                                                                  { // anti_endless_execute_recoursion
                                                                         throw Exception(0, 0,                                                                          if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                                                                                 &frame->name(),                                                                                  anti_endless_execute_recoursion=0; // give @exception a chance
                                                                                 "endless recursion detected");                                                                                  throw Exception(0, 0,
                                                                                           &frame->name(),
                                                                                           "call canceled - endless recursion detected");
                                                                           }
                                                                           execute(*method.parser_code); // execute it
                                                                           anti_endless_execute_recoursion--;
                                                                 }                                                                  }
                                                                   
                                                                 execute(*method.parser_code); // execute it  
                                                                 anti_endless_execute_recoursion--;  
                                                         }                                                          }
                                                 } catch(...) {                                                  } catch(...) {
                                                         // record it to stack trace                                                          // record it to stack trace
Line 443  void Request::execute(const Array& ops) Line 471  void Request::execute(const Array& ops)
                                                                 call_type==Method::CT_STATIC?"statically":"dynamically");                                                                  call_type==Method::CT_STATIC?"statically":"dynamically");
   
                                 }                                  }
                                 value=wcontext->result();                                  value=&wcontext->result();
   
                                 wcontext=static_cast<WContext *>(POP());                                    wcontext=static_cast<WContext *>(POP());  
                                 rcontext=POP();                                    rcontext=POP();  
Line 630  void Request::execute(const Array& ops) Line 658  void Request::execute(const Array& ops)
                         }                          }
                 case OP_LOG_AND:                  case OP_LOG_AND:
                         {                          {
                                 b=POP();  a=POP();                                  b_code=POP_CODE();  a=POP();
                                 value=NEW VBool(pool(), a->as_bool() && b->as_bool());                                  bool result;
                                   if(a->as_bool()) {
                                           execute(*b_code);
                                           b=POP();
                                           result=b->as_bool();
                                   } else
                                           result=false;
                                   value=NEW VBool(pool(), result);
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
                 case OP_LOG_OR:                  case OP_LOG_OR:
                         {                          {
                                 b=POP();  a=POP();                                  b_code=POP_CODE();  a=POP();
                                 value=NEW VBool(pool(), a->as_bool() || b->as_bool());                                  bool result;
                                   if(a->as_bool()) 
                                           result=true;
                                   else {
                                           execute(*b_code);
                                           b=POP();
                                           result=b->as_bool();
                                   }
                                   value=NEW VBool(pool(), result);
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
Line 754  Value *Request::get_element() { Line 797  Value *Request::get_element() {
         const String& name=POP_NAME();          const String& name=POP_NAME();
         Value *ncontext=POP();          Value *ncontext=POP();
         Value *value=ncontext->get_element(name);          Value *value=ncontext->get_element(name);
         if(!value)          // operators can not be in form...
           if(!value && 
                   !wcontext->get_somebody_entered_some_class () && // $class:xxx
                   !wcontext->get_somebody_entered_some_object()) // $object.xxx
                   // they can only be in form ^xxx or $xxx
                 if(Method* method=OP.get_method(name)) { // maybe operator?                  if(Method* method=OP.get_method(name)) { // maybe operator?
                         // as if that method were in self and we have normal dynamic method here                          // as if that method were in self and we have normal dynamic method here
                         Junction& junction=*NEW Junction(pool(),                           Junction& junction=*NEW Junction(pool(), 
                                 *root/*self*/, self->get_class(), method, 0,0,0,0);                                  *root, self->get_class(), method, 0,0,0,0);
                         value=NEW VJunction(junction);                          value=NEW VJunction(junction);
                 }                  }
         if(value)          if(value)
Line 812  Value& Request::process(Value& value, co Line 859  Value& Request::process(Value& value, co
                 self=&junction->self;                  self=&junction->self;
                 root=junction->root;                  root=junction->root;
                 rcontext=junction->rcontext;                  rcontext=junction->rcontext;
                 execute(*junction->code);  
                   { // anti_endless_execute_recoursion
                           if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                                   anti_endless_execute_recoursion=0; // give @exception a chance
                                   throw Exception(0, 0,
                                           name,
                                           "junction evaluation canceled - endless recursion detected");
                           }
                           execute(*junction->code);
                           anti_endless_execute_recoursion--;
                   }
                   
                 if(using_code_frame) {                  if(using_code_frame) {
                         // CodeFrame soul:                          // CodeFrame soul:
                         //   string writes were intercepted                          //   string writes were intercepted
                         //   returning them as the result of getting code-junction                          //   returning them as the result of getting code-junction
                         result=NEW VString(*frame->get_string());                          result=NEW VString(*frame->get_string());
                 } else                   } else 
                         result=frame->result();                          result=&frame->result();
                                   
                 wcontext=static_cast<WContext *>(POP());                    wcontext=static_cast<WContext *>(POP());  
                 rcontext=POP();                    rcontext=POP();  
Line 837  Value& Request::process(Value& value, co Line 895  Value& Request::process(Value& value, co
         return *result;          return *result;
 }  }
   
 const String *Request::execute_method(Value& aself, const Method& method,   const String *Request::execute_method(Value& aself, const Method& method,
                                                                           bool return_cstr) {                  bool return_cstr) {
         PUSH(self);            PUSH(self);  
         PUSH(root);            PUSH(root);  
         PUSH(rcontext);            PUSH(rcontext);  
Line 852  const String *Request::execute_method(Va Line 910  const String *Request::execute_method(Va
         execute(*method.parser_code);          execute(*method.parser_code);
                   
         // result          // result
         const String *result;          const String *result=return_cstr ? &wcontext->as_string() : 0;
         if(return_cstr)          
                 result=&wcontext->as_string();          wcontext=static_cast<WContext *>(POP());  
         else          rcontext=POP();  
                 result=0; // ignore result          root=POP();  
           self=static_cast<VAliased *>(POP());
           
           // return
           return result;
   }
   
   const String& Request::execute_method(VMethodFrame& amethodFrame, const Method& method) {
           PUSH(self);  
           PUSH(root);  
           PUSH(rcontext);  
           PUSH(wcontext);
           
           // initialize contexts
           root=rcontext=self=&amethodFrame;
           wcontext=&amethodFrame;
           
           // execute!     
           execute(*method.parser_code);
           
           // result
           const String& result=wcontext->as_string();
                   
         wcontext=static_cast<WContext *>(POP());            wcontext=static_cast<WContext *>(POP());  
         rcontext=POP();            rcontext=POP();  
Line 868  const String *Request::execute_method(Va Line 947  const String *Request::execute_method(Va
 }  }
   
 const String *Request::execute_virtual_method(Value& aself,   const String *Request::execute_virtual_method(Value& aself, 
                                                                                           const String& method_name,                                                                                             const String& method_name) {
                                                                                           bool return_cstr) {  
         if(Value *value=aself.get_element(method_name))          if(Value *value=aself.get_element(method_name))
                 if(Junction *junction=value->get_junction())                  if(Junction *junction=value->get_junction())
                         if(const Method *method=junction->method)                           if(const Method *method=junction->method) 
                                 return execute_method(aself, *method, return_cstr);                                  return execute_method(aself, *method, true /*return_cstr*/);
                                                   
         return 0;          return 0;
 }  }
   
 const String *Request::execute_nonvirtual_method(VStateless_class& aclass,   const String *Request::execute_nonvirtual_method(VStateless_class& aclass, 
                                                                                                  const String& method_name,                                                                                                    const String& method_name,
                                                                                                  bool return_cstr) {                                                                                                   bool return_cstr) {
         if(const Method *method=aclass.get_method(method_name))          if(const Method *method=aclass.get_method(method_name))
                 return execute_method(aclass, *method, return_cstr);                  return execute_method(aclass, *method, return_cstr);

Removed from v.1.201  
changed lines
  Added in v.1.211


E-mail: