Annotation of parser3/src/classes/reflection.C, revision 1.41

1.1       misha       1: /** @file
                      2:        Parser: @b reflection parser class.
                      3: 
1.38      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       misha       5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
                      8: #include "pa_vmethod_frame.h"
                      9: #include "pa_request.h"
                     10: #include "pa_vbool.h"
                     11: 
1.41    ! moko       12: volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.40 2016/03/31 21:46:20 moko Exp $";
1.24      moko       13: 
1.3       misha      14: static const String class_type_methoded("methoded");
1.2       misha      15: 
1.3       misha      16: static const String method_type_native("native");
                     17: static const String method_type_parser("parser");
1.2       misha      18: 
1.5       misha      19: static const String method_call_type("call_type");
1.8       misha      20: static const String method_inherited("inherited");
1.23      misha      21: static const String method_overridden("overridden");
1.3       misha      22: static const String method_call_type_static("static");
                     23: static const String method_call_type_dynamic("dynamic");
1.2       misha      24: 
1.3       misha      25: static const String method_min_params("min_params");
                     26: static const String method_max_params("max_params");
1.29      misha      27: static const String method_extra_param("extra_param");
1.2       misha      28: 
1.30      misha      29: static const String def_class("class");
                     30: 
1.1       misha      31: // class
                     32: 
                     33: class MReflection: public Methoded {
                     34: public:
                     35:        MReflection();
                     36: };
                     37: 
                     38: // global variable
                     39: 
1.40      moko       40: DECLARE_CLASS_VAR(reflection, new MReflection);
1.1       misha      41: 
                     42: // methods
                     43: 
1.2       misha      44: 
                     45: static void _create(Request& r, MethodParams& params) {
1.1       misha      46:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha      47:        Value* class_value=r.get_class(class_name);
1.1       misha      48: 
                     49:        if(!class_value)
                     50:                throw Exception(PARSER_RUNTIME,
                     51:                        &class_name,
                     52:                        "class is undefined");
                     53: 
                     54:        const String& constructor_name=params.as_string(1, "constructor_name must be string");
1.6       misha      55:        Value* constructor_value=class_value->get_element(constructor_name);
1.1       misha      56: 
1.10      misha      57:        if(!constructor_value || !constructor_value->get_junction())
1.1       misha      58:                throw Exception(PARSER_RUNTIME,
                     59:                        &constructor_name,
                     60:                        "constructor must be declared in class '%s'",
1.41    ! moko       61:                        class_value->type());
1.1       misha      62: 
                     63:        Junction* junction=constructor_value->get_junction();
1.2       misha      64:        const Method* method=junction->method;
                     65: 
                     66:        int nparams=params.count()-2;
                     67:        int max_params_count;
                     68: 
                     69:        if(method->native_code){
                     70:                if(method->call_type==Method::CT_STATIC)
                     71:                        throw Exception(PARSER_RUNTIME,
                     72:                                &constructor_name,
1.41    ! moko       73:                                "native method of class '%s' is not allowed to be called dynamically",
1.2       misha      74:                                class_value->type());
                     75: 
                     76:                if(nparams<method->min_numbered_params_count)
                     77:                        throw Exception(PARSER_RUNTIME,
                     78:                                &constructor_name,
1.41    ! moko       79:                                "native method of class '%s' accepts minimum %d parameter(s) (%d passed)",
1.2       misha      80:                                class_value->type(),
                     81:                                method->min_numbered_params_count,
                     82:                                nparams);
                     83: 
                     84:                max_params_count=method->max_numbered_params_count;
                     85:        } else {
1.12      misha      86:                max_params_count=method->params_names?method->params_names->count():0;
1.2       misha      87:        }
                     88: 
                     89:        if(nparams>max_params_count)
                     90:                throw Exception(PARSER_RUNTIME,
                     91:                        &constructor_name,
1.41    ! moko       92:                        "method of class '%s' accepts maximum %d parameter(s) (%d passed)",
1.2       misha      93:                        class_value->type(),
                     94:                        max_params_count,
                     95:                        nparams);
1.1       misha      96: 
1.17      moko       97:        Value &object = r.construct(*class_value, *method);
                     98:        VConstructorFrame frame(*method, r.get_method_frame(), object);
1.1       misha      99: 
1.3       misha     100:        Value* v[100];
1.1       misha     101:        if(nparams>0){
1.2       misha     102:                for(int i=0; i<nparams; i++)
1.1       misha     103:                        v[i]=&r.process_to_value(params[i+2]);
                    104:                frame.store_params((Value**)&v, nparams);
                    105:        } else {
                    106:                frame.empty_params();
                    107:        }
1.17      moko      108:        r.op_call(frame);
1.18      moko      109:        object.enable_default_setter();
1.21      moko      110:        r.write_pass_lang(frame.result());
1.1       misha     111: }
                    112: 
1.2       misha     113: 
1.3       misha     114: static void store_vlass_info(
                    115:                HashStringValue::key_type key, 
                    116:                HashStringValue::value_type value,
                    117:                HashStringValue* result
                    118: ){
                    119:        Value* v;
                    120:        if(value->get_class())
                    121:                v=new VString(class_type_methoded);
                    122:        else
                    123:                v=VVoid::get();
                    124:        result->put(key, v);
                    125: }
                    126: 
                    127: static void _classes(Request& r, MethodParams&) {
                    128:        VHash& result=*new VHash;
                    129:        r.classes().for_each(store_vlass_info, result.get_hash());
                    130:        r.write_no_lang(result);
                    131: }
                    132: 
                    133: 
                    134: static Value* get_class(Value* value){
                    135:        if(VStateless_class* result=value->get_class())
                    136:                return result;
                    137:        else
                    138:                // classes with fields only, like env & console
                    139:                return value;
                    140: }
                    141: 
                    142: static const String* get_class_name(Value* value){
                    143:        if(VStateless_class* lclass=value->get_class())
1.41    ! moko      144:                return new String(lclass->type());
1.3       misha     145:        else
                    146:                // classes with fields only, like env & console
                    147:                return new String(value->type());
                    148: }
                    149: 
                    150: 
1.1       misha     151: static void _class(Request& r, MethodParams& params) {
1.3       misha     152:        r.write_no_lang(*get_class(&params[0]));
1.1       misha     153: }
                    154: 
1.2       misha     155: 
1.1       misha     156: static void _class_name(Request& r, MethodParams& params) {
1.3       misha     157:        r.write_no_lang(*get_class_name(&params[0]));
1.1       misha     158: }
                    159: 
1.34      moko      160: static void _class_by_name(Request& r, MethodParams& params) {
                    161:        const String& class_name=params.as_string(0, "class_name must be string");
                    162:        Value* class_value=r.get_class(class_name);
                    163:        if(!class_value)
                    164:                throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
                    165:        r.write_no_lang(*class_value);
                    166: }
1.1       misha     167: 
1.2       misha     168: static void _base(Request& r, MethodParams& params) {
1.3       misha     169:        if(VStateless_class* lclass=params[0].get_class())
                    170:                if(Value* base=lclass->base()){
                    171:                        r.write_no_lang(*get_class(base));
                    172:                        return;
                    173:                }
                    174: 
                    175:        // classes with fields only, like env & console or without base
                    176:        r.write_no_lang(*VVoid::get());
1.2       misha     177: }
1.1       misha     178: 
                    179: 
1.2       misha     180: static void _base_name(Request& r, MethodParams& params) {
1.3       misha     181:        if(VStateless_class* lclass=params[0].get_class())
1.2       misha     182:                if(Value* base=lclass->base())
1.3       misha     183:                        r.write_no_lang(*get_class_name(base));
1.2       misha     184: }
1.1       misha     185: 
1.2       misha     186: static void store_method_info(
1.7       misha     187:                HashStringMethod::key_type key, 
                    188:                HashStringMethod::value_type method,
1.8       misha     189:                HashStringValue* result
1.2       misha     190: ) {
1.8       misha     191:        result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1       misha     192: }
                    193: 
1.30      misha     194: static void _def(Request& r, MethodParams& params) {
                    195:        const String& type=params.as_string(0, "type must be string");
                    196:        if(type == def_class) {
                    197:                const String& name=params.as_string(1, "name must be string");
                    198:                r.write_no_lang(VBool::get(r.get_class(name)!=0));
                    199:        } else {
1.31      misha     200:                throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr());
1.30      misha     201:        }
                    202: }
                    203: 
1.1       misha     204: static void _methods(Request& r, MethodParams& params) {
                    205:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha     206:        Value* class_value=r.get_class(class_name);
1.1       misha     207:        if(!class_value)
                    208:                throw Exception(PARSER_RUNTIME,
                    209:                        &class_name,
                    210:                        "class is undefined");
                    211: 
                    212:        VHash& result=*new VHash;
1.28      misha     213:        if(VStateless_class* lclass=class_value->get_class()) {
1.7       misha     214:                HashStringMethod methods=lclass->get_methods();
1.8       misha     215:                methods.for_each(store_method_info, result.get_hash());
1.1       misha     216:        } else {
1.3       misha     217:                // class which does not have methods (env, console, etc)
1.1       misha     218:        }
                    219:        r.write_no_lang(result);
                    220: }
                    221: 
1.28      misha     222: static void _method(Request& r, MethodParams& params) {
                    223:        Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
                    224:        const String& name=params.as_string(1, "method name must be string");
                    225: 
                    226:        if(VStateless_class* lclass=o.get_class()) {
                    227:                if(Method* method=lclass->get_method(name))
                    228:                        r.write_no_lang(*method->get_vjunction(o));
                    229:        } else {
                    230:                // class which does not have methods (env, console, etc)
                    231:        }
                    232: }
                    233: 
1.13      misha     234: static void _fields(Request& r, MethodParams& params) {
1.28      misha     235:        Value& o=params.as_no_junction(0, "param must be object or class, not junction");
                    236: 
                    237:        if(HashStringValue* fields=o.get_fields()) {
1.13      misha     238:                VHash& result=*new VHash(*fields);
                    239:                r.write_no_lang(result);
                    240:        } else
                    241:                r.write_no_lang(*new VHash());
                    242: }
1.2       misha     243: 
1.28      misha     244: static void _field(Request& r, MethodParams& params) {
                    245:        Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
                    246:        const String& name=params.as_string(1, "field name must be string");
                    247: 
                    248:        if(HashStringValue* fields=o.get_fields())
                    249:                if(Value* value=fields->get(name))
                    250:                        r.write_no_lang(*value);
                    251: }
                    252: 
1.8       misha     253: static void _method_info(Request& r, MethodParams& params) {
1.2       misha     254:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha     255:        Value* class_value=r.get_class(class_name);
1.2       misha     256:        if(!class_value)
                    257:                throw Exception(PARSER_RUNTIME,
                    258:                        &class_name,
                    259:                        "class is undefined");
                    260: 
                    261:        VStateless_class* lclass=class_value->get_class();
                    262:        if(!lclass)
                    263:                throw Exception(PARSER_RUNTIME,
                    264:                        &class_name,
                    265:                        "class does not have methods");
                    266: 
                    267:        const String& method_name=params.as_string(1, "method_name must be string");
                    268:        Method* method=lclass->get_method(method_name);
                    269:        if(!method)
                    270:                throw Exception(PARSER_RUNTIME,
                    271:                        &method_name,
                    272:                        "method not found in class %s",
                    273:                        class_name.cstr());
                    274: 
                    275:        VHash& result=*new VHash;
                    276:        HashStringValue* hash=result.get_hash();
1.8       misha     277: 
                    278:        VStateless_class* c=lclass;
1.23      misha     279:        Method* base_method;
                    280:        if(c->base() && (base_method=c->base()->get_method(method_name))){
1.8       misha     281:                c=c->base()->get_class();
1.23      misha     282:                while(c->base() && base_method==c->base()->get_method(method_name))
                    283:                        c=c->base()->get_class();
1.41    ! moko      284:                hash->put((base_method==method) ? method_inherited : method_overridden, new VString(*new String(c->type())));
1.23      misha     285:        }
1.8       misha     286: 
1.29      misha     287:        Value* call_type=0;
                    288:        switch(method->call_type){
                    289:                case Method::CT_DYNAMIC:
                    290:                        call_type=new VString(method_call_type_dynamic);
                    291:                        break;
                    292:                case Method::CT_STATIC:
                    293:                        call_type=new VString(method_call_type_static);
                    294:                        break;
1.32      moko      295:                case Method::CT_ANY:
                    296:                        break;
1.29      misha     297:        }
                    298:        if(call_type)
                    299:                hash->put(method_call_type, call_type);
                    300: 
1.2       misha     301:        if(method->native_code){
                    302:                // native code
                    303:                hash->put(method_min_params, new VInt(method->min_numbered_params_count));
                    304:                hash->put(method_max_params, new VInt(method->max_numbered_params_count));
                    305:        } else {
                    306:                // parser code
1.16      misha     307:                const String* filespec = r.get_method_filename(method);
                    308:                if( filespec )
                    309:                        hash->put("file", new VString(*filespec));
1.29      misha     310: 
                    311:                hash->put(method_max_params, new VInt(method->params_names ? method->params_names->count() : 0));
                    312: 
1.2       misha     313:                if(method->params_names)
                    314:                        for(size_t i=0; i<method->params_names->count(); i++)
                    315:                                hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
1.29      misha     316: 
                    317:                if(method->extra_params)
                    318:                        hash->put(method_extra_param, new VString(*method->extra_params));
1.2       misha     319:        }
                    320: 
                    321:        r.write_no_lang(result);
                    322: }
                    323: 
1.9       misha     324: static void _dynamical(Request& r, MethodParams& params) {
                    325:        if(params.count()){
                    326:                r.write_no_lang(VBool::get(params[0].get_class() != &params[0]));
                    327:        } else {
                    328:                VMethodFrame* caller=r.get_method_frame()->caller();
                    329:                r.write_no_lang(VBool::get(caller && caller->get_class() != &caller->self()));
                    330:        }
                    331: }
                    332: 
1.35      moko      333: static void _is(Request& r, MethodParams& params) {
1.39      moko      334:        const String& name=params.as_string(0, "element name must be string");
                    335:        const String& type=params.as_string(1, "class name must be string");
                    336:        Value *context=params.count()==3 ? &(params.as_no_junction(2, "context must not be code")) : r.get_method_frame()->caller();
1.37      moko      337:        Value *value=context ? context->get_element(name) : 0;
1.35      moko      338: 
                    339:        if(value) {
                    340:                if(type == "code" || type == "method") {
                    341:                        Junction *junction=value->get_junction();
                    342:                        r.write_no_lang(VBool::get(junction && ((junction->code==0) ^ (type == "code"))) );
                    343:                } else {
                    344:                        r.write_no_lang(VBool::get( value->is(type.cstr()) ));
                    345:                }
                    346:        } else
1.36      moko      347:                r.write_no_lang(VBool::get(type == "void"));
1.35      moko      348: }
                    349: 
1.19      moko      350: static void _copy(Request& r, MethodParams& params) {
                    351:        HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
                    352: 
                    353:        if(src==NULL) 
                    354:                throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
                    355: 
                    356:        Value& dst=params.as_no_junction(1, "destination must not be code");
                    357: 
                    358:        for(HashStringValue::Iterator i(*src); i; i.next())
1.20      moko      359:                r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19      moko      360: }
                    361: 
1.25      moko      362: static void _uid(Request& r, MethodParams& params) {
                    363:        Value& obj=params.as_no_junction(0, "object must not be code");
                    364: 
                    365:        char local_buf[MAX_NUMBER];
                    366:        int size=snprintf(local_buf, sizeof(local_buf), "%p", &obj);
                    367: 
1.33      moko      368:        r.write_pass_lang(*new String(String::C(pa_strdup(local_buf, (size_t)size), size)));
1.25      moko      369: }
                    370: 
1.27      moko      371: static void _delete(Request&, MethodParams& params) {
1.26      misha     372:        const String& key=params.as_string(1, "field name must be string");
                    373:        if(HashStringValue* fields=params[0].get_fields()){
                    374:                fields->remove(key);
                    375:        }
                    376: }
                    377: 
                    378: 
1.1       misha     379: // constructor
                    380: MReflection::MReflection(): Methoded("reflection") {
1.2       misha     381:        // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
                    382:        add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1       misha     383: 
1.3       misha     384:        // ^reflection:classes[]
                    385:        add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
                    386: 
1.1       misha     387:        // ^reflection:class[object]
                    388:        add_native_method("class", Method::CT_STATIC, _class, 1, 1);
                    389: 
                    390:        // ^reflection:class_name[object]
                    391:        add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
                    392: 
1.34      moko      393:        // ^reflection:class_by_name[class_name]
                    394:        add_native_method("class_by_name", Method::CT_STATIC, _class_by_name, 1, 1);
                    395: 
1.2       misha     396:        // ^reflection:base_class[object]
                    397:        add_native_method("base", Method::CT_STATIC, _base, 1, 1);
                    398: 
                    399:        // ^reflection:base_class_name[object]
                    400:        add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
                    401: 
1.30      misha     402:        // ^reflection:def[class|...;name]
                    403:        add_native_method("def", Method::CT_STATIC, _def, 2, 2);
                    404: 
1.2       misha     405:        // ^reflection:methods[class_name]
1.1       misha     406:        add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2       misha     407: 
1.28      misha     408:        // ^reflection:method[object or class;method_name]
                    409:        add_native_method("method", Method::CT_STATIC, _method, 2, 2);
                    410: 
                    411:        // ^reflection:method_info[class_name;method_name]
                    412:        add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2);
                    413: 
1.13      misha     414:        // ^reflection:fields[object or class]
                    415:        add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
                    416: 
1.28      misha     417:        // ^reflection:field[object or class;field_name]
                    418:        add_native_method("field", Method::CT_STATIC, _field, 2, 2);
1.9       misha     419: 
                    420:        // ^reflection:dynamical[[object or class, caller if absent]]
                    421:        add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19      moko      422: 
1.39      moko      423:        // ^reflection:is[element_name;class_name|code|method[;context]]
1.35      moko      424:        add_native_method("is", Method::CT_STATIC, _is, 2, 3);
                    425: 
1.19      moko      426:        // ^reflection:copy[src;dst]
                    427:        add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.25      moko      428: 
                    429:        // ^reflection:uid[object or class]
                    430:        add_native_method("uid", Method::CT_STATIC, _uid, 1, 1);
1.26      misha     431: 
                    432:        // ^reflection:delete[object or class;field_name]
                    433:        add_native_method("delete", Method::CT_STATIC, _delete, 2, 2);
1.1       misha     434: }

E-mail: