Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.148
1.3 paf 1: /** @file
1.5 paf 2: Parser: @b method_frame write context
1.3 paf 3:
1.148 ! moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.129 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.148 ! moko 11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.147 2026/04/24 20:14:33 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"
1.147 moko 17: #include "pa_inline_hash.h"
1.1 paf 18:
1.46 paf 19: // forwards
20:
21: class Request;
22:
23: /**
24: @b method parameters passed in this array.
25: contains handy typecast ad junction/not junction ensurers
1.114 moko 26: */
1.46 paf 27:
1.78 misha 28: class MethodParams {
1.46 paf 29: public:
1.130 moko 30: MethodParams() : felements(0), fsize(0) {}
1.78 misha 31:
1.82 misha 32: #ifdef USE_DESTRUCTORS
1.109 moko 33: ~MethodParams() {
1.82 misha 34: Value **flast=felements+count();
1.95 moko 35: for(Value **current=felements;current<flast;current++){
36: Junction* junction=(*current)->get_junction();
37: if (junction && junction->code)
1.111 moko 38: delete (VJunction*)(*current);
1.95 moko 39: }
1.82 misha 40: }
41: #endif
42:
1.109 moko 43: void store_params(Value **params, size_t count) {
1.78 misha 44: felements=params;
1.130 moko 45: fsize=count;
1.77 misha 46: }
47:
1.130 moko 48: inline size_t count() const { return fsize; }
1.78 misha 49:
1.108 moko 50: inline Value& get(size_t index) const {
1.78 misha 51: assert(index<count());
1.108 moko 52: return *felements[index];
1.78 misha 53: }
54:
1.108 moko 55: inline Value& operator[] (size_t index) { return get(index); }
1.46 paf 56:
57: /// handy is-value-a-junction ensurer
1.109 moko 58: Value& as_junction(int index, const char* msg) {
1.108 moko 59: Value& value=get(index);
1.110 moko 60: if(value.get_junction())
61: return value;
1.143 moko 62: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d is '%s')", msg, 1+index, value.type());
1.46 paf 63: }
1.107 moko 64:
1.46 paf 65: /// handy value-is-not-a-junction ensurer
1.109 moko 66: Value& as_no_junction(int index, const char* msg) {
1.108 moko 67: Value& value=get(index);
1.110 moko 68: if(!value.get_junction())
69: return value;
1.143 moko 70: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d is '%s')", msg, 1+index, value.type());
1.46 paf 71: }
1.107 moko 72:
1.62 misha 73: /// handy is-value-a-junction ensurer or can be auto-processed
1.109 moko 74: Value& as_expression(int index, const char* msg) {
1.108 moko 75: Value& value=get(index);
1.110 moko 76: if(value.is_evaluated_expr())
1.107 moko 77: return value;
1.110 moko 78: if(value.get_junction())
1.107 moko 79: return value;
1.143 moko 80: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d is '%s')", msg, 1+index, value.type());
1.62 misha 81: }
1.107 moko 82:
1.46 paf 83: /// handy expression auto-processing to double
1.107 moko 84: double as_double(int index, const char* msg, Request& r) {
1.108 moko 85: Value& value=get(index);
1.107 moko 86: if(value.is_evaluated_expr())
87: return value.as_double();
88: return get_processed(value, msg, index, r).as_double();
1.46 paf 89: }
1.107 moko 90:
1.46 paf 91: /// handy expression auto-processing to int
1.107 moko 92: int as_int(int index, const char* msg, Request& r) {
1.108 moko 93: Value& value=get(index);
1.107 moko 94: if(value.is_evaluated_expr())
95: return value.as_int();
96: return get_processed(value, msg, index, r).as_int();
1.46 paf 97: }
1.107 moko 98:
1.146 moko 99: /// handy expression auto-processing to int
100: pa_wint as_wint(int index, const char* msg, Request& r) {
101: Value& value=get(index);
102: if(value.is_evaluated_expr())
103: return value.as_wint();
104: return get_processed(value, msg, index, r).as_wint();
105: }
106:
1.132 moko 107: /// handy param auto-processing to index
108: int as_index(int index, size_t count, Request& r) {
109: if(get(index).is_string()) {
110: const String& svalue=*get(index).get_string();
111: if(svalue == "last")
112: return count-1;
113: else if(svalue != "first")
114: throw Exception(PARSER_RUNTIME, &svalue, "index must be 'first', 'last' or expression");
115: return 0;
116: } else {
117: int result=as_int(index, "index must be 'first', 'last' or expression", r);
118: if(result < 0)
119: result+=count;
120: return result;
121: }
122: }
123:
1.46 paf 124: /// handy expression auto-processing to bool
1.107 moko 125: bool as_bool(int index, const char* msg, Request& r) {
1.108 moko 126: Value& value=get(index);
1.107 moko 127: if(value.is_evaluated_expr())
128: return value.as_bool();
129: return get_processed(value, msg, index, r).as_bool();
1.46 paf 130: }
1.107 moko 131:
1.46 paf 132: /// handy string ensurer
1.109 moko 133: const String& as_string(int index, const char* msg) {
1.142 moko 134: if(const String *result=get(index).get_string())
135: return *result;
1.143 moko 136: throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d is '%s')", msg, 1+index, get(index).type());
1.46 paf 137: }
1.107 moko 138:
1.134 moko 139: /// handy file name ensurer
1.141 moko 140: const String& as_file_name(int index);
141:
142: /// handy file name from string or file
143: const String& as_file_spec(int index);
1.134 moko 144:
1.93 misha 145: /// handy hash ensurers
146: HashStringValue* as_hash(int index, const char* name=0);
1.107 moko 147:
1.93 misha 148: /// handy table ensurer
149: Table* as_table(int index, const char* name=0);
150:
1.46 paf 151: private:
152:
1.78 misha 153: Value **felements;
1.130 moko 154: size_t fsize;
1.78 misha 155:
1.107 moko 156: Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46 paf 157:
158: };
159:
1.114 moko 160: /**
161: Method frame write context
1.4 paf 162: accepts values written by method code
1.3 paf 163: */
1.114 moko 164:
1.46 paf 165: class VMethodFrame: public WContext {
1.63 misha 166: protected:
1.46 paf 167: VMethodFrame *fcaller;
1.114 moko 168: Value& fself;
169:
170: public: // Value
171:
172: override const char* type() const { return "method_frame"; }
173:
174: /// VMethodFrame: self_transparent
175: override VStateless_class* get_class() { return self().get_class(); }
176:
177: /// VMethodFrame: self_transparent
178: override VStateless_class* base() { return self().base(); }
179:
180: public: // usage
181:
182: VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself):
183: WContext(0 /* no parent, junctions can be reattached only up to VMethodFrame */),
184: fcaller(acaller),
185: fself(aself),
186: method(amethod) {
187: }
188:
189: VMethodFrame *caller() { return fcaller; }
190:
191: Value& self() { return fself; }
192:
193: void check_call_type(){
1.115 moko 194: if(method.call_type==Method::CT_ANY)
195: return;
196:
1.114 moko 197: Method::Call_type call_type=self().get_class()==&self() ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.115 moko 198:
199: if(method.call_type!=call_type) // call type not allowed?
1.121 moko 200: throw Exception(PARSER_RUNTIME, method.name, "method of '%s' is not allowed to be called %s",
201: self().type(), call_type==Method::CT_STATIC ? "statically" : "dynamically");
1.114 moko 202: }
203:
204: public:
205:
206: const Method& method;
207: };
1.46 paf 208:
209:
1.114 moko 210: class VNativeMethodFrame: public VMethodFrame {
211: protected:
212: MethodParams fnumbered_params;
1.46 paf 213:
1.114 moko 214: public: // usage
215:
216: VNativeMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself){}
217:
1.122 moko 218: // params should be declared outside of *_FRAME_ACTION as MethodParams destructor will be called in ~VNativeMethodFrame
1.114 moko 219: void store_params(Value **params, size_t count) {
220: fnumbered_params.store_params(params, count);
221: method.check_actual_numbered_params(self(), &fnumbered_params);
222: }
223:
224: void empty_params() {
225: method.check_actual_numbered_params(self(), &fnumbered_params);
226: }
227:
228: void call(Request &r);
229:
230: };
231:
232:
233: /**
234: Handles named parameters and local variables
235: */
236:
237: class VParserMethodFrame: public VMethodFrame {
238: public: // Value
1.20 paf 239:
1.114 moko 240: /// VParserMethodFrame: $result | parent get_string(=accumulated fstring)
1.109 moko 241: override const String* get_string() {
1.139 moko 242: // if we have $result, return it's string value, else return as usual: accumulated fstring or fvalue
243: return my_result ? my_result->get_string() : WContext::get_string();
1.20 paf 244: }
1.114 moko 245:
246: /// VParserMethodFrame: my or self_transparent or $caller
1.109 moko 247: override Value* get_element(const String& aname) {
1.139 moko 248: if(SYMBOLS_EQ(aname,RESULT_SYMBOL))
249: return my_result ? my_result : VVoid::get();
250:
1.103 moko 251: if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.118 moko 252: return get_caller_wrapper();
1.42 paf 253:
1.103 moko 254: if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67 misha 255: return &self();
256:
1.114 moko 257: if(Value* result=my.get(aname))
1.66 misha 258: return result;
259:
1.114 moko 260: if(Value* result=self().get_element(aname))
1.66 misha 261: return result;
1.40 paf 262:
263: return 0;
1.1 paf 264: }
265:
1.114 moko 266: /// VParserMethodFrame: my or self_transparent
1.94 moko 267: override const VJunction* put_element(const String& aname, Value* avalue) {
1.139 moko 268: if(SYMBOLS_EQ(aname,RESULT_SYMBOL)){
269: my_result=avalue;
270: #ifdef OPTIMIZE_RESULT
1.140 moko 271: // This check is only for maintaining consistency. Even if we change the mode from RO_USE_WCONTEXT, it will not work because
272: // CO_WITHOUT_WCONTEXT is also active. In this mode writes go directly to the parent context and result() is never called.
273: // If we also change CO_WITHOUT_WCONTEXT, the current method call will still continue writing to the parent context and
274: // result() will not be called. Only subsequent method calls will use $result.
275: // The current behavior is more predictable: the first method call determines result_optimization, and subsequent calls never change it.
276: if(method.result_optimization==Method::RO_UNKNOWN)
277: ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.139 moko 278: #endif
279: return 0;
280: }
1.114 moko 281: if(my.put_replaced(aname, avalue))
1.126 moko 282: return 0;
1.114 moko 283: return self().put_element(aname, avalue);
1.64 misha 284: }
285:
1.114 moko 286: public: // WContext
287:
288: /// VParserMethodFrame: skip write when RO_USE_RESULT
1.112 moko 289: override void write(const String& astring) {
1.79 misha 290: #ifdef OPTIMIZE_RESULT
1.125 moko 291: if(method.result_optimization != Method::RO_USE_RESULT)
1.79 misha 292: #endif
1.125 moko 293: WContext::write(astring);
1.69 misha 294: }
295:
1.114 moko 296: override void write(Value& avalue) {
297: WContext::write(avalue);
1.64 misha 298: }
299:
1.119 moko 300: override ValueRef result() {
1.139 moko 301: // if we have $result, return it, else return as usual: accumulated fstring or fvalue
302: if(my_result)
303: return my_result;
1.79 misha 304: #ifdef OPTIMIZE_RESULT
1.114 moko 305: if(method.result_optimization==Method::RO_USE_RESULT)
1.120 moko 306: return VVoid::get();
1.140 moko 307:
308: // Due to call optimization, the following code is called only once.
309: // CO_WITHOUT_WCONTEXT means subsequent writes will be to the parent context, and the result() won't be called.
310:
1.114 moko 311: ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79 misha 312: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.114 moko 313: ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79 misha 314: #endif
315: #endif
316: return WContext::result();
1.1 paf 317: }
318:
319: public: // usage
320:
1.147 moko 321: #ifdef OPTIMIZE_MY_HASH
322: typedef InlineHashString<Value*> MyHash;
323: #else
324: typedef HashString<Value*> MyHash;
325: #endif
326: MyHash my; // public for ^stack[]
1.139 moko 327: Value* my_result;
1.123 moko 328:
1.114 moko 329: VParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1 paf 330:
1.114 moko 331: void store_params(Value **params, size_t count) {
332: size_t param_count=method.params_count;
333: size_t i=0;
1.25 paf 334:
1.114 moko 335: if(count>param_count){
1.136 moko 336:
337: for(; i<param_count; i++) {
338: const String& fname=*(*method.params_names)[i];
1.138 moko 339: set_my_variable(fname, params[i]);
1.136 moko 340: }
341:
1.114 moko 342: if(method.extra_params){
343: VHash& vargs=*new VHash();
344: HashStringValue& args = vargs.hash();
1.1 paf 345:
1.114 moko 346: for(; i<count; i++) {
1.145 moko 347: args.put(String::Body::uitoa(args.count()), params[i]);
1.114 moko 348: }
1.77 misha 349:
1.138 moko 350: set_my_variable(*method.extra_params, &vargs);
1.136 moko 351: } else if(method.named_params){
352: if(count!=param_count+1)
353: throw Exception(PARSER_RUNTIME, method.name, "method of '%s' accepts maximum %d parameter(s) (%d present)", self().type(), param_count+1, count);
354:
355: HashStringValue* named_args = params[i]->as_hash("named parameter");
356: size_t named_count=method.named_params->count();
357: for(i=0; i<named_count; i++) {
358: const String& fname=*(*method.named_params)[i];
359: Value *arg=named_args ? named_args->get(fname) : NULL;
1.138 moko 360: set_my_variable(fname, arg ? arg : VVoid::get());
1.136 moko 361: }
1.114 moko 362: } else
1.121 moko 363: throw Exception(PARSER_RUNTIME, method.name, "method of '%s' accepts maximum %d parameter(s) (%d present)", self().type(), param_count, count);
1.135 moko 364: } else {
1.136 moko 365:
366: for(; i<count; i++) {
367: const String& fname=*(*method.params_names)[i];
1.138 moko 368: set_my_variable(fname, params[i]);
1.136 moko 369: }
370:
371: for(; i<param_count; i++) {
372: const String& fname=*(*method.params_names)[i];
1.138 moko 373: set_my_variable(fname, VVoid::get());
1.136 moko 374: }
375:
1.135 moko 376: if(method.extra_params){
1.138 moko 377: set_my_variable(*method.extra_params, VVoid::get());
1.136 moko 378: } else if(method.named_params){
379: size_t named_count=method.named_params->count();
380: for(i=0; i<named_count; i++) {
381: const String& fname=*(*method.named_params)[i];
1.138 moko 382: set_my_variable(fname, VVoid::get());
1.136 moko 383: }
1.135 moko 384: }
1.114 moko 385: }
1.1 paf 386: }
387:
1.78 misha 388: void empty_params(){
1.114 moko 389: size_t param_count=method.params_count;
390: if(param_count>0){
1.138 moko 391: set_my_variable(*(*method.params_names)[0], VString::empty());
1.114 moko 392: for(size_t i=1; i<param_count; i++)
1.138 moko 393: set_my_variable(*(*method.params_names)[i], VVoid::get());
1.78 misha 394: }
1.137 moko 395: if(method.extra_params){
1.138 moko 396: set_my_variable(*method.extra_params, VVoid::get());
1.137 moko 397: } else if(method.named_params){
398: size_t named_count=method.named_params->count();
1.144 moko 399: for(size_t i=0; i<named_count; i++) {
1.137 moko 400: const String& fname=*(*method.named_params)[i];
1.138 moko 401: set_my_variable(fname, VVoid::get());
1.137 moko 402: }
403: }
1.78 misha 404: }
405:
1.114 moko 406: void call(Request &r);
1.18 paf 407:
1.63 misha 408: protected:
1.18 paf 409:
1.139 moko 410: inline void set_my_variable(const String& aname, Value* value) {
411: if(SYMBOLS_EQ(aname,RESULT_SYMBOL)){
412: my_result=value;
413: #ifdef OPTIMIZE_RESULT
414: ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
415: #endif
416: return;
417: }
418: my.put(aname, value); // remember param
1.25 paf 419: }
420:
1.118 moko 421: Value* get_caller_wrapper();
422:
1.114 moko 423: };
424:
425:
426: class VLocalParserMethodFrame: public VParserMethodFrame {
427: public: // Value
428:
429: override const VJunction* put_element(const String& aname, Value* avalue){
1.138 moko 430: set_my_variable(aname, avalue);
1.126 moko 431: return 0;
1.114 moko 432: }
433:
434: public: // usage
435:
436: VLocalParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VParserMethodFrame(amethod, acaller, aself) {}
1.1 paf 437:
1.41 paf 438: };
439:
1.114 moko 440:
441: template<typename Parent> class VExpressionFrame: public Parent {
1.113 moko 442: public:
1.114 moko 443: VExpressionFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {}
1.113 moko 444:
1.117 moko 445: /// in expressions only strings are written as strings
1.113 moko 446: override void write_as_string(Value& avalue) {
1.117 moko 447: if(avalue.is_string())
448: Parent::write(*avalue.get_string());
449: else
450: Parent::write(avalue);
1.113 moko 451: }
452: };
453:
454:
1.114 moko 455: template<typename Parent> class VConstructorFrame: public Parent {
1.85 misha 456: public:
1.114 moko 457: VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {
1.87 moko 458: // prevent non-string writes for better error reporting [constructors are not expected to return anything]
459: VMethodFrame::write(aself);
460: }
1.85 misha 461:
1.113 moko 462: override void write(const String& /*astring*/) {}
1.85 misha 463: };
464:
1.114 moko 465:
466: #define METHOD_FRAME_ACTION(method, caller, self, action) \
467: if((method).native_code){ \
468: VNativeMethodFrame frame(method, caller, self); \
469: action; \
470: } else { \
471: if((method).all_vars_local){ \
472: VLocalParserMethodFrame frame(method, caller, self); \
473: action; \
474: } else { \
475: VParserMethodFrame frame(method, caller, self); \
476: action; \
477: } \
478: }
479:
480: #define EXPRESSION_FRAME_ACTION(method, caller, self, action) \
481: if((method).native_code){ \
482: VExpressionFrame<VNativeMethodFrame> frame(method, caller, self); \
483: action; \
484: } else { \
485: if((method).all_vars_local){ \
1.128 moko 486: VLocalParserMethodFrame frame(method, caller, self); \
1.114 moko 487: action; \
488: } else { \
1.128 moko 489: VParserMethodFrame frame(method, caller, self); \
1.114 moko 490: action; \
491: } \
492: }
493:
494: #define CONSTRUCTOR_FRAME_ACTION(method, caller, self, action) \
495: if((method).native_code){ \
496: VConstructorFrame<VNativeMethodFrame> frame(method, caller, self); \
497: action; \
498: } else { \
499: if((method).all_vars_local){ \
500: VConstructorFrame<VLocalParserMethodFrame> frame(method, caller, self); \
501: action; \
502: } else { \
503: VConstructorFrame<VParserMethodFrame> frame(method, caller, self); \
504: action; \
505: } \
506: }
507:
1.1 paf 508: #endif
E-mail: