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

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

E-mail: