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

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

E-mail: