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

1.1       misha       1: /** @file
                      2:        Parser: @b reflection parser class.
                      3: 
1.24    ! moko        4:        Copyright (c) 2001-2012 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.24    ! moko       12: volatile const char * IDENT_REFLECTION_C="$Id: 2011-02-20 05:32:39 $";
        !            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.2       misha      27: 
1.1       misha      28: // class
                     29: 
                     30: class MReflection: public Methoded {
                     31: public:
                     32:        MReflection();
                     33: };
                     34: 
                     35: // global variable
                     36: 
                     37: DECLARE_CLASS_VAR(reflection, new MReflection, 0);
                     38: 
                     39: // methods
                     40: 
1.2       misha      41: 
                     42: static void _create(Request& r, MethodParams& params) {
1.1       misha      43:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha      44:        Value* class_value=r.get_class(class_name);
1.1       misha      45: 
                     46:        if(!class_value)
                     47:                throw Exception(PARSER_RUNTIME,
                     48:                        &class_name,
                     49:                        "class is undefined");
                     50: 
                     51:        const String& constructor_name=params.as_string(1, "constructor_name must be string");
1.6       misha      52:        Value* constructor_value=class_value->get_element(constructor_name);
1.1       misha      53: 
1.10      misha      54:        if(!constructor_value || !constructor_value->get_junction())
1.1       misha      55:                throw Exception(PARSER_RUNTIME,
                     56:                        &constructor_name,
                     57:                        "constructor must be declared in class '%s'",
1.2       misha      58:                        class_value->get_class()->name_cstr());
1.1       misha      59: 
                     60:        Junction* junction=constructor_value->get_junction();
1.2       misha      61:        const Method* method=junction->method;
                     62: 
                     63:        int nparams=params.count()-2;
                     64:        int max_params_count;
                     65: 
                     66:        if(method->native_code){
                     67:                if(method->call_type==Method::CT_STATIC)
                     68:                        throw Exception(PARSER_RUNTIME,
                     69:                                &constructor_name,
                     70:                                "native method of class '%s' (%s) is not allowed to be called dynamically",
                     71:                                class_value->get_class()->name_cstr(),
                     72:                                class_value->type());
                     73: 
                     74:                if(nparams<method->min_numbered_params_count)
                     75:                        throw Exception(PARSER_RUNTIME,
                     76:                                &constructor_name,
                     77:                                "native method of class '%s' (%s) accepts minimum %d parameter(s) (%d passed)",
                     78:                                class_value->get_class()->name_cstr(),
                     79:                                class_value->type(),
                     80:                                method->min_numbered_params_count,
                     81:                                nparams);
                     82: 
                     83:                max_params_count=method->max_numbered_params_count;
                     84:        } else {
1.12      misha      85:                max_params_count=method->params_names?method->params_names->count():0;
1.2       misha      86:        }
                     87: 
                     88:        if(nparams>max_params_count)
                     89:                throw Exception(PARSER_RUNTIME,
                     90:                        &constructor_name,
                     91:                        "method of class '%s' (%s) accepts maximum %d parameter(s) (%d passed)",
                     92:                        class_value->get_class()->name_cstr(),
                     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())
                    144:                return &lclass->name();
                    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: 
                    160: 
1.2       misha     161: static void _base(Request& r, MethodParams& params) {
1.3       misha     162:        if(VStateless_class* lclass=params[0].get_class())
                    163:                if(Value* base=lclass->base()){
                    164:                        r.write_no_lang(*get_class(base));
                    165:                        return;
                    166:                }
                    167: 
                    168:        // classes with fields only, like env & console or without base
                    169:        r.write_no_lang(*VVoid::get());
1.2       misha     170: }
1.1       misha     171: 
                    172: 
1.2       misha     173: static void _base_name(Request& r, MethodParams& params) {
1.3       misha     174:        if(VStateless_class* lclass=params[0].get_class())
1.2       misha     175:                if(Value* base=lclass->base())
1.3       misha     176:                        r.write_no_lang(*get_class_name(base));
1.2       misha     177: }
1.1       misha     178: 
1.2       misha     179: static void store_method_info(
1.7       misha     180:                HashStringMethod::key_type key, 
                    181:                HashStringMethod::value_type method,
1.8       misha     182:                HashStringValue* result
1.2       misha     183: ) {
1.8       misha     184:        result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1       misha     185: }
                    186: 
                    187: static void _methods(Request& r, MethodParams& params) {
                    188:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha     189:        Value* class_value=r.get_class(class_name);
1.1       misha     190:        if(!class_value)
                    191:                throw Exception(PARSER_RUNTIME,
                    192:                        &class_name,
                    193:                        "class is undefined");
                    194: 
                    195:        VHash& result=*new VHash;
                    196:        if(VStateless_class* lclass=class_value->get_class()){
1.7       misha     197:                HashStringMethod methods=lclass->get_methods();
1.8       misha     198:                methods.for_each(store_method_info, result.get_hash());
1.1       misha     199:        } else {
1.3       misha     200:                // class which does not have methods (env, console, etc)
1.1       misha     201:        }
                    202:        r.write_no_lang(result);
                    203: }
                    204: 
1.13      misha     205: static void _fields(Request& r, MethodParams& params) {
                    206:        if(HashStringValue* fields=params[0].get_fields()){
                    207:                VHash& result=*new VHash(*fields);
                    208:                r.write_no_lang(result);
                    209:        } else
                    210:                r.write_no_lang(*new VHash());
                    211: }
1.2       misha     212: 
1.8       misha     213: static void _method_info(Request& r, MethodParams& params) {
1.2       misha     214:        const String& class_name=params.as_string(0, "class_name must be string");
1.11      misha     215:        Value* class_value=r.get_class(class_name);
1.2       misha     216:        if(!class_value)
                    217:                throw Exception(PARSER_RUNTIME,
                    218:                        &class_name,
                    219:                        "class is undefined");
                    220: 
                    221:        VStateless_class* lclass=class_value->get_class();
                    222:        if(!lclass)
                    223:                throw Exception(PARSER_RUNTIME,
                    224:                        &class_name,
                    225:                        "class does not have methods");
                    226: 
                    227:        const String& method_name=params.as_string(1, "method_name must be string");
                    228:        Method* method=lclass->get_method(method_name);
                    229:        if(!method)
                    230:                throw Exception(PARSER_RUNTIME,
                    231:                        &method_name,
                    232:                        "method not found in class %s",
                    233:                        class_name.cstr());
                    234: 
                    235:        VHash& result=*new VHash;
                    236:        HashStringValue* hash=result.get_hash();
1.8       misha     237: 
                    238:        VStateless_class* c=lclass;
1.23      misha     239:        Method* base_method;
                    240:        if(c->base() && (base_method=c->base()->get_method(method_name))){
1.8       misha     241:                c=c->base()->get_class();
1.23      misha     242:                while(c->base() && base_method==c->base()->get_method(method_name))
                    243:                        c=c->base()->get_class();
                    244:                hash->put((base_method==method) ? method_inherited : method_overridden, new VString(c->name()));
                    245:        }
1.8       misha     246: 
1.2       misha     247:        if(method->native_code){
                    248:                // native code
                    249:                hash->put(method_min_params, new VInt(method->min_numbered_params_count));
                    250:                hash->put(method_max_params, new VInt(method->max_numbered_params_count));
1.5       misha     251:                Value* call_type=0;
1.2       misha     252:                switch(method->call_type){
                    253:                        case Method::CT_DYNAMIC:
1.4       misha     254:                                call_type=new VString(method_call_type_dynamic);
1.2       misha     255:                                break;
                    256:                        case Method::CT_STATIC:
1.4       misha     257:                                call_type=new VString(method_call_type_static);
1.2       misha     258:                                break;
                    259:                }
1.5       misha     260:                if(call_type)
                    261:                        hash->put(method_call_type, call_type);
1.2       misha     262:        } else {
                    263:                // parser code
1.16      misha     264:                const String* filespec = r.get_method_filename(method);
                    265:                if( filespec )
                    266:                        hash->put("file", new VString(*filespec));
1.2       misha     267:                if(method->params_names)
                    268:                        for(size_t i=0; i<method->params_names->count(); i++)
                    269:                                hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
                    270:        }
                    271: 
                    272:        r.write_no_lang(result);
                    273: }
                    274: 
1.9       misha     275: static void _dynamical(Request& r, MethodParams& params) {
                    276:        if(params.count()){
                    277:                r.write_no_lang(VBool::get(params[0].get_class() != &params[0]));
                    278:        } else {
                    279:                VMethodFrame* caller=r.get_method_frame()->caller();
                    280:                r.write_no_lang(VBool::get(caller && caller->get_class() != &caller->self()));
                    281:        }
                    282: }
                    283: 
1.19      moko      284: static void _copy(Request& r, MethodParams& params) {
                    285:        HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
                    286: 
                    287:        if(src==NULL) 
                    288:                throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
                    289: 
                    290:        Value& dst=params.as_no_junction(1, "destination must not be code");
                    291: 
                    292:        for(HashStringValue::Iterator i(*src); i; i.next())
1.20      moko      293:                r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19      moko      294: }
                    295: 
1.1       misha     296: // constructor
                    297: MReflection::MReflection(): Methoded("reflection") {
1.2       misha     298:        // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
                    299:        add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1       misha     300: 
1.3       misha     301:        // ^reflection:classes[]
                    302:        add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
                    303: 
1.1       misha     304:        // ^reflection:class[object]
                    305:        add_native_method("class", Method::CT_STATIC, _class, 1, 1);
                    306: 
                    307:        // ^reflection:class_name[object]
                    308:        add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
                    309: 
1.2       misha     310:        // ^reflection:base_class[object]
                    311:        add_native_method("base", Method::CT_STATIC, _base, 1, 1);
                    312: 
                    313:        // ^reflection:base_class_name[object]
                    314:        add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
                    315: 
                    316:        // ^reflection:methods[class_name]
1.1       misha     317:        add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2       misha     318: 
1.13      misha     319:        // ^reflection:fields[object or class]
                    320:        add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
                    321: 
1.14      misha     322:        // ^reflection:method_info[class_name;method_name]
1.8       misha     323:        add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2);
1.9       misha     324: 
                    325:        // ^reflection:dynamical[[object or class, caller if absent]]
                    326:        add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19      moko      327: 
                    328:        // ^reflection:copy[src;dst]
                    329:        add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.1       misha     330: }

E-mail: