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

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

E-mail: