Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.110
1.3 paf 1: /** @file
1.5 paf 2: Parser: @b method_frame write context
1.3 paf 3:
1.98 moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.22 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_VMETHOD_FRAME_H
9: #define PA_VMETHOD_FRAME_H
1.28 paf 10:
1.110 ! moko 11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.109 2016/10/06 20:16:06 moko Exp $"
1.1 paf 12:
1.101 moko 13: #include "pa_symbols.h"
1.1 paf 14: #include "pa_wcontext.h"
1.12 parser 15: #include "pa_vvoid.h"
1.1 paf 16: #include "pa_vjunction.h"
17:
1.46 paf 18: // forwards
19:
20: class Request;
21:
22: /**
23: @b method parameters passed in this array.
24: contains handy typecast ad junction/not junction ensurers
25:
26: */
1.78 misha 27: class MethodParams {
1.46 paf 28: public:
1.109 moko 29: MethodParams() : felements(0), fused(0) {}
1.78 misha 30:
1.82 misha 31: #ifdef USE_DESTRUCTORS
1.109 moko 32: ~MethodParams() {
1.82 misha 33: Value **flast=felements+count();
1.95 moko 34: for(Value **current=felements;current<flast;current++){
35: Junction* junction=(*current)->get_junction();
36: if (junction && junction->code)
1.82 misha 37: delete *current;
1.95 moko 38: }
1.82 misha 39: }
40: #endif
41:
1.109 moko 42: void store_params(Value **params, size_t count) {
1.78 misha 43: felements=params;
1.77 misha 44: fused=count;
45: }
46:
1.78 misha 47: inline size_t count() const { return fused; }
48:
1.108 moko 49: inline Value& get(size_t index) const {
1.78 misha 50: assert(index<count());
1.108 moko 51: return *felements[index];
1.78 misha 52: }
53:
1.108 moko 54: inline Value& operator[] (size_t index) { return get(index); }
1.46 paf 55:
56: /// handy is-value-a-junction ensurer
1.109 moko 57: Value& as_junction(int index, const char* msg) {
1.108 moko 58: Value& value=get(index);
1.110 ! moko 59: if(value.get_junction())
! 60: return value;
! 61: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.46 paf 62: }
1.107 moko 63:
1.46 paf 64: /// handy value-is-not-a-junction ensurer
1.109 moko 65: Value& as_no_junction(int index, const char* msg) {
1.108 moko 66: Value& value=get(index);
1.110 ! moko 67: if(!value.get_junction())
! 68: return value;
! 69: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.46 paf 70: }
1.107 moko 71:
1.62 misha 72: /// handy is-value-a-junction ensurer or can be auto-processed
1.109 moko 73: Value& as_expression(int index, const char* msg) {
1.108 moko 74: Value& value=get(index);
1.110 ! moko 75: if(value.is_evaluated_expr())
1.107 moko 76: return value;
1.110 ! moko 77: if(value.get_junction())
1.107 moko 78: return value;
1.110 ! moko 79: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.62 misha 80: }
1.107 moko 81:
1.46 paf 82: /// handy expression auto-processing to double
1.107 moko 83: double as_double(int index, const char* msg, Request& r) {
1.108 moko 84: Value& value=get(index);
1.107 moko 85: if(value.is_evaluated_expr())
86: return value.as_double();
87: return get_processed(value, msg, index, r).as_double();
1.46 paf 88: }
1.107 moko 89:
1.46 paf 90: /// handy expression auto-processing to int
1.107 moko 91: int as_int(int index, const char* msg, Request& r) {
1.108 moko 92: Value& value=get(index);
1.107 moko 93: if(value.is_evaluated_expr())
94: return value.as_int();
95: return get_processed(value, msg, index, r).as_int();
1.46 paf 96: }
1.107 moko 97:
1.46 paf 98: /// handy expression auto-processing to bool
1.107 moko 99: bool as_bool(int index, const char* msg, Request& r) {
1.108 moko 100: Value& value=get(index);
1.107 moko 101: if(value.is_evaluated_expr())
102: return value.as_bool();
103: return get_processed(value, msg, index, r).as_bool();
1.46 paf 104: }
1.107 moko 105:
1.46 paf 106: /// handy string ensurer
1.109 moko 107: const String& as_string(int index, const char* msg) {
1.46 paf 108: return as_no_junction(index, msg).as_string();
109: }
1.107 moko 110:
1.93 misha 111: /// handy hash ensurers
112: HashStringValue* as_hash(int index, const char* name=0);
1.107 moko 113:
1.93 misha 114: /// handy table ensurer
115: Table* as_table(int index, const char* name=0);
116:
1.46 paf 117: private:
118:
1.78 misha 119: Value **felements;
120: size_t fused;
121:
1.107 moko 122: Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46 paf 123:
124: };
125:
1.4 paf 126: /** Method frame write context
127: accepts values written by method code
128: also handles method parameters and local variables
1.3 paf 129: */
1.46 paf 130: class VMethodFrame: public WContext {
1.63 misha 131: protected:
1.46 paf 132: VMethodFrame *fcaller;
133:
1.83 misha 134: HashString<Value*>* my; /*OR*/ MethodParams fnumbered_params;
1.87 moko 135: Value& fself;
1.46 paf 136:
1.64 misha 137: typedef const VJunction* (VMethodFrame::*put_element_t)(const String& aname, Value* avalue);
138: put_element_t put_element_impl;
1.46 paf 139:
1.1 paf 140: public: // Value
141:
1.46 paf 142: override const char* type() const { return "method_frame"; }
1.20 paf 143:
144: /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.109 moko 145: override const String* get_string() {
1.20 paf 146: // check the $result value
1.46 paf 147: Value* result=get_result_variable();
1.20 paf 148: // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
149: return result ? result->get_string() : WContext::get_string();
150: }
151:
1.40 paf 152: /// VMethodFrame: my or self_transparent or $caller
1.109 moko 153: override Value* get_element(const String& aname) {
1.103 moko 154: if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.40 paf 155: return caller();
1.42 paf 156:
1.103 moko 157: if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67 misha 158: return &self();
159:
1.66 misha 160: Value* result;
161: if(my && (result=my->get(aname)))
162: return result;
163:
1.84 misha 164: if(result=self().get_element(aname))
1.66 misha 165: return result;
1.40 paf 166:
167: return 0;
1.1 paf 168: }
169:
1.4 paf 170: /// VMethodFrame: self_transparent
1.46 paf 171: override VStateless_class* get_class() { return self().get_class(); }
1.44 paf 172:
173: /// VMethodFrame: self_transparent
1.85 misha 174: override VStateless_class* base() { return self().base(); }
1.1 paf 175:
1.64 misha 176: /// VMethodFrame: my or self_transparent
1.94 moko 177: override const VJunction* put_element(const String& aname, Value* avalue) {
1.64 misha 178: return (this->*put_element_impl)(aname, avalue);
179: }
180:
1.69 misha 181: /// VMethodFrame: appends a fstring to result
182: override void write(const String& astring, String::Language alang) {
1.79 misha 183: #ifdef OPTIMIZE_RESULT
1.87 moko 184: switch (method.result_optimization){
1.97 moko 185: case Method::RO_USE_RESULT:
1.79 misha 186: return;
187: case Method::RO_UNKNOWN:
188: if(get_result_variable()){
1.87 moko 189: ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.79 misha 190: return;
191: }
1.97 moko 192: case Method::RO_USE_WCONTEXT: break;
1.69 misha 193: }
1.79 misha 194: #endif
195: WContext::write(astring, alang);
1.69 misha 196: }
197:
1.64 misha 198: private:
199:
200: const VJunction* put_element_local(const String& aname, Value* avalue){
201: set_my_variable(aname, *avalue);
202: return PUT_ELEMENT_REPLACED_ELEMENT;
203: }
204:
205: const VJunction* put_element_global(const String& aname, Value* avalue){
206: if(my && my->put_replaced(aname, avalue))
207: return PUT_ELEMENT_REPLACED_ELEMENT;
1.94 moko 208: return self().put_element(aname, avalue);
1.64 misha 209: }
210:
1.30 paf 211: public: // WContext
1.1 paf 212:
1.105 moko 213: override Value& result() {
1.80 misha 214: if(my){
215: // check the $result value
216: Value* result_value=get_result_variable();
217: // if we have one, return it, else return as usual: accumulated fstring or fvalue
218: if(result_value)
1.105 moko 219: return *result_value;
1.79 misha 220: #ifdef OPTIMIZE_RESULT
1.87 moko 221: if(method.result_optimization==Method::RO_USE_RESULT)
1.105 moko 222: return *VVoid::get();
1.87 moko 223: ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79 misha 224: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.87 moko 225: ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79 misha 226: #endif
227: #endif
1.80 misha 228: }
1.79 misha 229: return WContext::result();
1.1 paf 230: }
231:
1.85 misha 232: override void write(Value& avalue) {
233: WContext::write(avalue);
1.46 paf 234: }
235:
1.1 paf 236: public: // usage
237:
1.87 moko 238: VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1 paf 239:
1.40 paf 240: VMethodFrame *caller() { return fcaller; }
1.25 paf 241:
1.76 misha 242: #ifdef USE_DESTRUCTORS
1.75 misha 243: ~VMethodFrame(){
244: if(my){
245: delete my;
246: }
247: }
1.76 misha 248: #endif
1.75 misha 249:
1.87 moko 250: Value& self() { return fself; }
1.1 paf 251:
1.77 misha 252: size_t method_params_count() {
253: return method.params_names ? method.params_names->count():0;
1.27 paf 254: }
1.77 misha 255:
256: void store_params(Value **params, size_t count) {
1.92 moko 257: if(my) {
258: size_t param_count=method.params_names ? method.params_names->count():0;
1.89 moko 259: size_t i=0;
260:
261: if(count>param_count){
262: if(method.extra_params){
263: for(; i<param_count; i++) {
264: const String& fname=*(*method.params_names)[i];
265: set_my_variable(fname, *params[i]);
266: }
267:
268: VHash& vargs=*new VHash();
269: HashStringValue& args = vargs.hash();
270:
271: for(; i<count; i++) {
272: args.put(format(args.count(), 0), params[i]);
273: }
1.86 misha 274:
1.89 moko 275: set_my_variable(*method.extra_params, vargs);
276: return;
277: } else
1.109 moko 278: throw Exception(PARSER_RUNTIME, 0, "method of %s accepts maximum %d parameter(s) (%d present)", self().type(), param_count, count);
1.89 moko 279: }
1.78 misha 280:
1.86 misha 281: for(; i<count; i++) {
1.77 misha 282: const String& fname=*(*method.params_names)[i];
283: set_my_variable(fname, *params[i]);
284: }
285:
286: for(; i<param_count; i++) {
287: const String& fname=*(*method.params_names)[i];
1.68 misha 288: my->put(fname, VVoid::get());
1.1 paf 289: }
1.86 misha 290: } else
1.77 misha 291: fnumbered_params.store_params(params,count);
1.1 paf 292: }
293:
1.78 misha 294: void empty_params(){
295: if(method.params_names){
296: size_t param_count=method.params_names->count();
1.91 moko 297: if(param_count>0){
1.104 moko 298: my->put(*(*method.params_names)[0], VString::empty());
299: for(size_t i=1; i<param_count; i++)
300: my->put(*(*method.params_names)[i], VVoid::get());
1.78 misha 301: }
302: }
303: }
304:
1.72 misha 305: MethodParams* numbered_params() { return &fnumbered_params; }
1.18 paf 306:
1.63 misha 307: protected:
1.18 paf 308:
1.46 paf 309: void set_my_variable(const String& fname, Value& value) {
310: my->put(fname, &value); // remember param
1.25 paf 311: }
312:
1.46 paf 313: Value* get_result_variable();
1.1 paf 314:
315: public:
316:
1.87 moko 317: const Method& method;
1.1 paf 318:
1.41 paf 319: };
320:
1.85 misha 321: class VConstructorFrame: public VMethodFrame {
322: public:
1.87 moko 323: VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself) {
324: // prevent non-string writes for better error reporting [constructors are not expected to return anything]
325: VMethodFrame::write(aself);
326: }
1.85 misha 327:
328: override void write(const String& /*astring*/, String::Language /*alang*/) {}
329: };
330:
1.1 paf 331: #endif
E-mail: