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

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

E-mail: