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

1.1       misha       1: /** @file
                      2:        Parser: @b reflection parser class.
                      3: 
1.89    ! moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
        !             5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       misha       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.89    ! moko       13: volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.88 2020/12/31 12:08:34 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.59      moko       20: static const String method_name("name");
1.71      moko       21: static const String method_class_name("class");
1.5       misha      22: static const String method_call_type("call_type");
1.8       misha      23: static const String method_inherited("inherited");
1.23      misha      24: static const String method_overridden("overridden");
1.2       misha      25: 
1.3       misha      26: static const String method_min_params("min_params");
                     27: static const String method_max_params("max_params");
1.29      misha      28: static const String method_extra_param("extra_param");
1.2       misha      29: 
1.30      misha      30: static const String def_class("class");
                     31: 
1.1       misha      32: // class
                     33: 
                     34: class MReflection: public Methoded {
                     35: public:
                     36:        MReflection();
                     37: };
                     38: 
                     39: // global variable
                     40: 
1.40      moko       41: DECLARE_CLASS_VAR(reflection, new MReflection);
1.1       misha      42: 
                     43: // methods
                     44: 
1.73      moko       45: const int MAX_CREATE_PARAMS = 100;
1.2       misha      46: 
                     47: static void _create(Request& r, MethodParams& params) {
1.73      moko       48:        int params_offset;
                     49:        HashStringValue* params_hash=0;
                     50: 
                     51:        const String* class_name=0;
                     52:        const String* constructor_name=0;
                     53: 
1.75      moko       54:        Value& voptions=params.as_no_junction(0, "param must not be code");
1.73      moko       55:        if(HashStringValue* options=voptions.get_hash()) {
                     56:                int valid_options=0;
                     57:                if(Value* vclass_name=options->get("class")) {
                     58:                        valid_options++;
                     59:                        class_name=&vclass_name->as_string();
                     60:                }
                     61:                if(Value* vconstructor_name=options->get("constructor")) {
                     62:                        valid_options++;
                     63:                        constructor_name=&vconstructor_name->as_string();
                     64:                }
                     65:                if(Value* vparams_hash=options->get("arguments")) {
                     66:                        valid_options++;
                     67:                        params_hash=vparams_hash->as_hash("arguments");
                     68:                        if(params.count()>1)
1.86      moko       69:                                throw Exception(PARSER_RUNTIME, 0, "arguments should not be specified as hash and as create params");
1.73      moko       70:                }
                     71:                if(valid_options!=options->count())
                     72:                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                     73: 
                     74:                if(!class_name)
                     75:                        throw Exception(PARSER_RUNTIME, 0, "class name must be specified");
                     76:                if(!constructor_name)
                     77:                        throw Exception(PARSER_RUNTIME, 0, "constructor name must be specified");
                     78: 
                     79:                params_offset=1;
                     80:        } else {
1.75      moko       81:                class_name=&params.as_string(0, "param must not be code");
1.73      moko       82: 
                     83:                if(params.count()==1)
                     84:                        throw Exception(PARSER_RUNTIME, 0, "constructor name must be specified");
                     85: 
                     86:                constructor_name=&params.as_string(1, "constructor name must be string");
                     87: 
                     88:                params_offset=2;
                     89:        }
1.1       misha      90: 
1.88      moko       91:        VStateless_class& vclass=r.get_class_ref(*class_name);
1.1       misha      92: 
1.88      moko       93:        const Method* method=vclass.get_method(*constructor_name);
1.73      moko       94:        if(!method)
1.88      moko       95:                throw Exception(PARSER_RUNTIME, constructor_name, "constructor not found in class '%s'", vclass.type());
1.1       misha      96: 
1.88      moko       97:        Value &object = r.construct(vclass, *method);
1.2       misha      98: 
1.73      moko       99:        int nparams=params_hash ? params_hash->count() : (params.count()-params_offset);
                    100:        if(nparams>MAX_CREATE_PARAMS)
                    101:                throw Exception(PARSER_RUNTIME, 0, "arguments count should not exceed %d", MAX_CREATE_PARAMS);
1.1       misha     102: 
1.74      moko      103:        Value* args[MAX_CREATE_PARAMS];
1.65      moko      104:        CONSTRUCTOR_FRAME_ACTION(*method, r.get_method_frame(), object, {
                    105:                if(nparams>0){
1.73      moko      106:                        if(params_hash){
1.74      moko      107:                                int i=0;
                    108:                                for(HashStringValue::Iterator h(*params_hash); h; h.next())
                    109:                                        args[i++]=h.value();
1.73      moko      110:                        } else {
                    111:                                for(int i=0; i<nparams; i++)
1.74      moko      112:                                        args[i]=&r.process(params[i+params_offset]);
1.73      moko      113:                        }
1.74      moko      114:                        frame.store_params((Value**)&args, nparams);
1.65      moko      115:                } else {
                    116:                        frame.empty_params();
                    117:                }
                    118:                r.call(frame);
                    119:                object.enable_default_setter();
                    120:                r.write(frame.result());
                    121:        });
1.1       misha     122: }
                    123: 
1.2       misha     124: 
1.3       misha     125: static void _classes(Request& r, MethodParams&) {
                    126:        VHash& result=*new VHash;
1.47      moko      127:        for(HashString<VStateless_class*>::Iterator i(r.classes()); i; i.next()){
                    128:                result.hash().put(i.key(), i.value()->get_methods().count()>0 ? new VString(class_type_methoded) : VVoid::get() );
                    129:        }
1.64      moko      130:        r.write(result);
1.3       misha     131: }
                    132: 
                    133: 
1.53      moko      134: static Value& get_class(Value& value){
                    135:        if(VStateless_class* result=value.get_class())
                    136:                return *result;
                    137:        else {
                    138:                // we can't return code junction to outside as it's stack value
                    139:                if(Junction *j=value.get_junction())
                    140:                        if(j->code)
                    141:                                throw Exception(PARSER_RUNTIME, 0, "param must not be code junction");
                    142:                // method junction
1.3       misha     143:                return value;
1.53      moko      144:        }
1.3       misha     145: }
                    146: 
1.53      moko      147: static const String& get_class_name(Value& value){
                    148:        return *new String(get_class(value).type());
1.3       misha     149: }
                    150: 
                    151: 
1.1       misha     152: static void _class(Request& r, MethodParams& params) {
1.64      moko      153:        r.write(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.64      moko      158:        r.write(get_class_name(params[0]));
1.1       misha     159: }
                    160: 
1.34      moko      161: static void _class_by_name(Request& r, MethodParams& params) {
                    162:        const String& class_name=params.as_string(0, "class_name must be string");
1.88      moko      163:        r.write(r.get_class_ref(class_name));
1.34      moko      164: }
1.1       misha     165: 
1.2       misha     166: static void _base(Request& r, MethodParams& params) {
1.49      moko      167:        if(VStateless_class* vclass=params[0].get_class())
                    168:                if(Value* base=vclass->base()){
1.64      moko      169:                        r.write(get_class(*base));
1.3       misha     170:                        return;
                    171:                }
                    172: 
                    173:        // classes with fields only, like env & console or without base
1.52      moko      174:        r.write_value(*VVoid::get());
1.2       misha     175: }
1.1       misha     176: 
                    177: 
1.2       misha     178: static void _base_name(Request& r, MethodParams& params) {
1.49      moko      179:        if(VStateless_class* vclass=params[0].get_class())
                    180:                if(Value* base=vclass->base())
1.64      moko      181:                        r.write(get_class_name(*base));
1.2       misha     182: }
1.1       misha     183: 
1.30      misha     184: static void _def(Request& r, MethodParams& params) {
                    185:        const String& type=params.as_string(0, "type must be string");
                    186:        if(type == def_class) {
                    187:                const String& name=params.as_string(1, "name must be string");
1.47      moko      188:                // can't use get_class because it will call @autouse[] if the class wasn't loaded
1.64      moko      189:                r.write(VBool::get(r.classes().get(name)!=0));
1.30      misha     190:        } else {
1.31      misha     191:                throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr());
1.30      misha     192:        }
                    193: }
                    194: 
1.1       misha     195: static void _methods(Request& r, MethodParams& params) {
                    196:        const String& class_name=params.as_string(0, "class_name must be string");
1.88      moko      197:        VStateless_class& vclass=r.get_class_ref(class_name);
1.1       misha     198: 
1.84      moko      199:        bool reverse=true;
                    200: 
                    201:        if(params.count()>1)
                    202:                if(HashStringValue* options=params.as_hash(1, "methods options")) {
                    203:                        int valid_options=0;
                    204:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    205:                                String::Body key=i.key();
                    206:                                Value* value=i.value();
                    207:                                if(key == "reverse") {
                    208:                                        reverse=r.process(*value).as_bool();
                    209:                                        valid_options++;
                    210:                                }
                    211:                        }
                    212:                        if(valid_options!=options->count())
                    213:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    214:                }
                    215: 
1.1       misha     216:        VHash& result=*new VHash;
1.84      moko      217: 
1.85      moko      218: #ifdef HASH_ORDER
1.84      moko      219:        if(reverse){
1.88      moko      220:                for(HashStringMethod::ReverseIterator i(vclass.get_methods()); i; i.prev()){
1.84      moko      221:                        result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser));
                    222:                }
                    223:        } else {
1.85      moko      224: #else
                    225:        {
                    226: #endif
1.88      moko      227:                for(HashStringMethod::Iterator i(vclass.get_methods()); i; i.next()){
1.84      moko      228:                        result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser));
                    229:                }
1.1       misha     230:        }
1.49      moko      231: 
1.64      moko      232:        r.write(result);
1.1       misha     233: }
                    234: 
1.57      moko      235: static VJunction &method_junction(Value &self, Method &method){
1.50      moko      236:        if(method.native_code)
1.57      moko      237:                throw Exception(PARSER_RUNTIME, method.name, "method must not be native");
1.50      moko      238: 
1.51      moko      239:        if(!(dynamic_cast<VObject*>(&self) || dynamic_cast<VClass*>(&self)))
                    240:                throw Exception(PARSER_RUNTIME, 0, "self must be parser object or class");
1.50      moko      241: 
                    242:        return *method.get_vjunction(self);
                    243: }
                    244: 
1.28      misha     245: static void _method(Request& r, MethodParams& params) {
1.62      moko      246:        Value &source=params[0];
1.50      moko      247: 
                    248:        if(Junction *j=source.get_junction()){
                    249:                if(Method* method=const_cast<Method*>(j->method)){
                    250:                        Value& self=params.count()>1 ? params.as_no_junction(1, "self must be object, not junction") : r.get_method_frame()->caller()->self();
1.64      moko      251:                        r.write(method_junction(self, *method));
1.50      moko      252:                        return;
                    253:                }
1.51      moko      254:                throw Exception(PARSER_RUNTIME, 0, "param must be method junction");
1.50      moko      255:        }
                    256: 
                    257:        if(params.count()==1)
1.51      moko      258:                throw Exception(PARSER_RUNTIME, 0, "method name must be specified");
1.50      moko      259: 
1.28      misha     260:        const String& name=params.as_string(1, "method name must be string");
                    261: 
1.50      moko      262:        if(VStateless_class* vclass=source.get_class()) {
                    263:                if(Method* method=vclass->get_method(name)){
1.64      moko      264:                        r.write( params.count()>2 ? method_junction(params.as_no_junction(2, "self must be object, not junction"), *method) : *method->get_vjunction(source) );
1.50      moko      265:                        return;
                    266:                }
1.28      misha     267:        }
1.52      moko      268:        r.write_value(*VVoid::get());
1.28      misha     269: }
                    270: 
1.13      misha     271: static void _fields(Request& r, MethodParams& params) {
1.28      misha     272:        Value& o=params.as_no_junction(0, "param must be object or class, not junction");
                    273: 
1.44      moko      274:        if(HashStringValue* fields=o.get_fields())
1.64      moko      275:                r.write(*new VHash(*fields));
1.44      moko      276:        else
1.64      moko      277:                r.write(*new VHash());
1.13      misha     278: }
1.2       misha     279: 
1.44      moko      280: static void _fields_reference(Request& r, MethodParams& params) {
1.46      moko      281:        Value& o=params.as_no_junction(0, "param must be object or hash, not junction");
1.44      moko      282: 
1.45      moko      283:        if(HashStringValue* fields=o.get_fields_reference())
1.64      moko      284:                r.write(*new VHashReference(*fields));
1.44      moko      285:        else
1.46      moko      286:                throw Exception(PARSER_RUNTIME, 0, "param must be object or hash");
1.44      moko      287: }
                    288: 
1.28      misha     289: static void _field(Request& r, MethodParams& params) {
                    290:        Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
                    291:        const String& name=params.as_string(1, "field name must be string");
                    292: 
                    293:        if(HashStringValue* fields=o.get_fields())
                    294:                if(Value* value=fields->get(name))
1.64      moko      295:                        r.write(*value);
1.28      misha     296: }
                    297: 
1.8       misha     298: static void _method_info(Request& r, MethodParams& params) {
1.59      moko      299:        const Method* method;
1.66      moko      300: 
                    301:        VHash& result=*new VHash;
                    302:        HashStringValue* hash=result.get_hash();
1.2       misha     303: 
1.62      moko      304:        if(Junction *j=params[0].get_junction()){
1.59      moko      305:                if(!(method=j->method))
                    306:                        throw Exception(PARSER_RUNTIME, 0, "param must be class name or method junction");
1.66      moko      307: 
                    308:                hash->put(method_name, new VString(*method->name));
1.71      moko      309:                hash->put(method_class_name, new VString(*new String(j->self.type())));
                    310: 
1.59      moko      311:        } else {
                    312:                const String& class_name=params.as_string(0, "param must be class name or method junction");
1.88      moko      313:                VStateless_class& vclass=r.get_class_ref(class_name);
1.59      moko      314: 
                    315:                if(params.count()==1)
                    316:                        throw Exception(PARSER_RUNTIME, 0, "method name must be specified");
                    317: 
                    318:                const String& method_name=params.as_string(1, "method name must be string");
1.88      moko      319:                if(!(method=vclass.get_method(method_name)))
                    320:                        throw Exception(PARSER_RUNTIME, &method_name, "method not found in class '%s'", vclass.type());
1.2       misha     321: 
1.66      moko      322:                Method* base_method;
1.88      moko      323:                if(vclass.base() && (base_method=vclass.base()->get_method(*method->name))){
                    324:                        VStateless_class* c=vclass.base()->get_class();
1.66      moko      325:                        while(c->base() && base_method==c->base()->get_method(*method->name))
                    326:                                c=c->base()->get_class();
                    327:                        hash->put((base_method==method) ? method_inherited : method_overridden, new VString(*new String(c->type())));
                    328:                }
1.23      misha     329:        }
1.8       misha     330: 
1.29      misha     331:        Value* call_type=0;
                    332:        switch(method->call_type){
                    333:                case Method::CT_DYNAMIC:
1.42      moko      334:                        call_type=new VString(Symbols::DYNAMIC_SYMBOL);
1.29      misha     335:                        break;
                    336:                case Method::CT_STATIC:
1.42      moko      337:                        call_type=new VString(Symbols::STATIC_SYMBOL);
1.29      misha     338:                        break;
1.32      moko      339:                case Method::CT_ANY:
                    340:                        break;
1.29      misha     341:        }
                    342:        if(call_type)
                    343:                hash->put(method_call_type, call_type);
                    344: 
1.2       misha     345:        if(method->native_code){
                    346:                // native code
                    347:                hash->put(method_min_params, new VInt(method->min_numbered_params_count));
                    348:                hash->put(method_max_params, new VInt(method->max_numbered_params_count));
                    349:        } else {
                    350:                // parser code
1.68      moko      351:                const String* filespec = r.get_method_filespec(method);
1.16      misha     352:                if( filespec )
                    353:                        hash->put("file", new VString(*filespec));
1.29      misha     354: 
                    355:                hash->put(method_max_params, new VInt(method->params_names ? method->params_names->count() : 0));
                    356: 
1.2       misha     357:                if(method->params_names)
                    358:                        for(size_t i=0; i<method->params_names->count(); i++)
                    359:                                hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
1.29      misha     360: 
                    361:                if(method->extra_params)
                    362:                        hash->put(method_extra_param, new VString(*method->extra_params));
1.2       misha     363:        }
                    364: 
1.64      moko      365:        r.write(result);
1.2       misha     366: }
                    367: 
1.67      moko      368: static void _filename(Request& r, MethodParams& params) {
1.68      moko      369:        if(Junction *j=params[0].get_junction()){
                    370:                if(const Method* method=j->method){
                    371:                        if(!method->native_code)
                    372:                                if(const String* filespec = r.get_method_filespec(method))
                    373:                                        r.write(*new VString(*filespec));
                    374:                        return;
                    375:                }
                    376:                throw Exception(PARSER_RUNTIME, 0, "param must be object, class or method junction");
                    377:        }
1.67      moko      378: 
1.68      moko      379:        if(VClass* vclass = dynamic_cast<VClass*>(params[0].get_class())){
                    380:                r.write(*new VString(vclass->get_filespec()));
1.67      moko      381:        }
                    382: }
                    383: 
1.9       misha     384: static void _dynamical(Request& r, MethodParams& params) {
                    385:        if(params.count()){
1.64      moko      386:                r.write(VBool::get(params[0].get_class() != &params[0]));
1.9       misha     387:        } else {
                    388:                VMethodFrame* caller=r.get_method_frame()->caller();
1.64      moko      389:                r.write(VBool::get(caller && caller->get_class() != &caller->self()));
1.9       misha     390:        }
                    391: }
                    392: 
1.35      moko      393: static void _is(Request& r, MethodParams& params) {
1.39      moko      394:        const String& name=params.as_string(0, "element name must be string");
                    395:        const String& type=params.as_string(1, "class name must be string");
                    396:        Value *context=params.count()==3 ? &(params.as_no_junction(2, "context must not be code")) : r.get_method_frame()->caller();
1.37      moko      397:        Value *value=context ? context->get_element(name) : 0;
1.35      moko      398: 
                    399:        if(value) {
                    400:                if(type == "code" || type == "method") {
                    401:                        Junction *junction=value->get_junction();
1.64      moko      402:                        r.write(VBool::get(junction && ((junction->code==0) ^ (type == "code"))) );
1.35      moko      403:                } else {
1.64      moko      404:                        r.write(VBool::get( value->is(type.cstr()) ));
1.35      moko      405:                }
                    406:        } else
1.64      moko      407:                r.write(VBool::get(type == "void"));
1.35      moko      408: }
                    409: 
1.19      moko      410: static void _copy(Request& r, MethodParams& params) {
                    411:        HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
                    412: 
1.44      moko      413:        if(src==NULL)
1.19      moko      414:                throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
                    415: 
                    416:        Value& dst=params.as_no_junction(1, "destination must not be code");
                    417: 
                    418:        for(HashStringValue::Iterator i(*src); i; i.next())
1.20      moko      419:                r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19      moko      420: }
                    421: 
1.25      moko      422: static void _uid(Request& r, MethodParams& params) {
                    423:        Value& obj=params.as_no_junction(0, "object must not be code");
                    424: 
                    425:        char local_buf[MAX_NUMBER];
                    426:        int size=snprintf(local_buf, sizeof(local_buf), "%p", &obj);
                    427: 
1.64      moko      428:        r.write(*new String(String::C(pa_strdup(local_buf, (size_t)size), size)));
1.25      moko      429: }
                    430: 
1.27      moko      431: static void _delete(Request&, MethodParams& params) {
1.58      moko      432:        Value* v=&params.as_no_junction(0, "param must be object or class, not junction");
1.26      misha     433:        const String& key=params.as_string(1, "field name must be string");
1.58      moko      434: 
                    435:        if(VObject* o=dynamic_cast<VObject*>(v)){
                    436:                o->get_fields()->remove(key);
                    437:        } else if(VClass* c=dynamic_cast<VClass*>(v)){
                    438:                HashStringProperty &p=*c->get_properties();
                    439:                if(Property* property=p.get(key))
                    440:                        if(property->value)
                    441:                                p.remove(key);
1.26      misha     442:        }
                    443: }
                    444: 
1.55      moko      445: static void _mixin(Request& r, MethodParams& params) {
                    446:        Value& vsource=params.as_no_junction(0, "source must not be code");
                    447: 
                    448:        Value* vtarget=0;
                    449:        const String *name=0;
                    450:        bool copy_methods=true;
                    451:        bool copy_fields=true;
1.60      moko      452:        bool overwrite=false;
1.55      moko      453: 
                    454:        if(params.count()>1)
                    455:                if(HashStringValue* options=params.as_hash(1, "mixin options")) {
                    456:                        int valid_options=0;
1.80      moko      457:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    458:                                String::Body key=i.key();
                    459:                                Value* value=i.value();
                    460:                                if(key == "to") {
                    461:                                        vtarget=value;
                    462:                                        valid_options++;
                    463:                                } else if(key == "name") {
                    464:                                        name=&value->as_string();
                    465:                                        valid_options++;
                    466:                                } else if(key == "methods") {
                    467:                                        copy_methods=r.process(*value).as_bool();
                    468:                                        valid_options++;
                    469:                                } else if(key == "fields") {
                    470:                                        copy_fields=r.process(*value).as_bool();
                    471:                                        valid_options++;
                    472:                                } else if(key == "overwrite") {
                    473:                                        overwrite=r.process(*value).as_bool();
                    474:                                        valid_options++;
                    475:                                }
1.55      moko      476:                        }
                    477:                        if(valid_options!=options->count())
                    478:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    479:                }
                    480: 
                    481:        if(!vtarget)
                    482:                vtarget=&r.get_method_frame()->caller()->self();
                    483: 
                    484:        VClass* source=dynamic_cast<VClass*>(vsource.get_class());
                    485:        VClass* target=dynamic_cast<VClass*>(vtarget->get_class());
                    486: 
                    487:        if(!source)
                    488:                throw Exception(PARSER_RUNTIME, 0, "source must be parser object or class");
                    489:        if(!target)
                    490:                throw Exception(PARSER_RUNTIME, 0, "destination must be parser object or class");
                    491: 
                    492:        if(name){
                    493:                if(copy_methods)
                    494:                        if(Method* method=source->get_method(*name))
                    495:                                if(overwrite || !target->get_method(*name)){
1.59      moko      496:                                        target->set_method(*name, new Method(*method));
1.55      moko      497:                                }
                    498: 
                    499:                if(copy_fields)
                    500:                        if(Property* property=source->get_properties()->get(*name))
                    501:                                if(property->value && (overwrite || !target->get_properties()->get(*name))){
                    502:                                        target->put_element(*target, *name, property->value);
                    503:                                }
                    504: 
                    505:        } else {
                    506:                if(copy_methods)
                    507:                        for(HashStringMethod::Iterator i(source->get_methods()); i; i.next()){
                    508:                                if(overwrite || !target->get_method(i.key()))
1.59      moko      509:                                        target->set_method(*i.value()->name, new Method(*i.value()));
1.55      moko      510:                        }
                    511:                if(copy_fields)
                    512:                        for(HashStringProperty::Iterator i(*source->get_properties()); i; i.next()){
                    513:                                if(i.value()->value && ( overwrite || !target->get_properties()->get(i.key()) ))
                    514:                                        target->put_element(*target, *new String(i.key(), String::L_TAINTED), i.value()->value);
                    515:                        }
                    516:        }
                    517: }
                    518: 
1.76      moko      519: String::Language get_untaint_lang(const String& lang_name); // op.C
                    520: 
                    521: static void _tainting(Request& r, MethodParams& params) {
1.78      moko      522:        const String& str=params.as_string(params.count()-1, "param must be string");
1.76      moko      523:        String::Language lang = String::L_UNSPECIFIED;
                    524:        bool optimized=false;
                    525: 
                    526:        if(params.count()==2){
1.78      moko      527:                const String& slang=params.as_string(0, "language name must be string");
1.76      moko      528:                if(slang == "optimized")
                    529:                        optimized=true;
                    530:                else if(slang == "tainted")
                    531:                        lang=String::L_TAINTED;
                    532:                else lang=get_untaint_lang(slang);
                    533:        }
                    534: 
                    535:        if(!str.is_empty()){
                    536:                char *visual=str.visualize_langs();
                    537: 
                    538:                if(optimized){
                    539:                        for(char *c=visual; *c; c++)
                    540:                                *c = *c<0 ? '+':'-';
                    541:                } else if(lang != String::L_UNSPECIFIED){
                    542:                        for(char *c=visual; *c; c++)
                    543:                                *c = *c==lang ? '+':'-';
                    544:                } else {
                    545:                        for(char *c=visual; *c; c++)
                    546:                                *c = *c & 0x7F;
                    547:                }
                    548: 
                    549:                r.write(*new String(visual));
                    550:        }
                    551: }
1.26      misha     552: 
1.81      moko      553: static void _stack(Request& r, MethodParams& params) {
                    554:        bool show_args=false;
                    555:        bool show_locals=false;
                    556: 
                    557:        int limit=1000000;
                    558:        int offset=0;
                    559: 
                    560:        if(params.count()>0)
                    561:                if(HashStringValue* options=params.as_hash(0, "stack options")) {
                    562:                        int valid_options=0;
                    563:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    564:                                String::Body key=i.key();
                    565:                                Value* value=i.value();
                    566: 
                    567:                                if(key == "args") {
                    568:                                        show_args=r.process(*value).as_bool();
                    569:                                        valid_options++;
1.82      moko      570:                                } else if(key == "locals") {
1.81      moko      571:                                        show_locals=r.process(*value).as_bool();
                    572:                                        valid_options++;
1.82      moko      573:                                } else if(key == "limit") {
1.81      moko      574:                                        limit=r.process(*value).as_int();
                    575:                                        valid_options++;
1.82      moko      576:                                } else if(key == "offset") {
1.81      moko      577:                                        offset=r.process(*value).as_int();
                    578:                                        valid_options++;
                    579:                                }
                    580:                        }
                    581: 
                    582:                        if(valid_options!=options->count())
                    583:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    584:                }
                    585: 
                    586:        limit+=offset;
                    587: 
                    588:        VHash& vresult=*new VHash;
                    589:        HashStringValue* result=vresult.get_hash();
                    590:        int index=1;
                    591:        VMethodFrame* caller=r.get_method_frame()->caller();
                    592:        while(caller && index <= limit){
                    593:                if(index>offset){
                    594:                        VHash& vcurrent=*new VHash;
                    595:                        HashStringValue* current=vcurrent.get_hash();
                    596: 
                    597:                        current->put(Symbols::SELF_SYMBOL, &caller->self());
                    598: 
                    599:                        const Method& method=caller->method;
                    600: 
                    601:                        current->put(method_name, new VString(*method.name));
                    602: 
                    603:                        if(!method.native_code){
                    604:                                Operation::Origin origin=r.get_method_origin(&method);
                    605:                                if(origin.file_no){
                    606:                                        current->put("file", new VString(*r.get_used_filespec(origin.file_no)));
                    607:                                        current->put("line", new VInt(origin.line)); // no +1 as declaration before first command
                    608:                                }
                    609: 
                    610:                                if(show_args || show_locals){
                    611: 
                    612:                                        VHash& vargs=*new VHash;
                    613:                                        HashStringValue* args=vargs.get_hash();
                    614: 
                    615:                                        if(method.params_names){
                    616:                                                for(size_t i=0; i<method.params_names->count(); i++){
                    617:                                                        const String& pname=*(*method.params_names)[i];
                    618:                                                        Value* value=caller->get_element(pname);
                    619:                                                        args->put(pname, value->get_junction() ? VVoid::get() : value);
                    620:                                                }
                    621:                                        }
                    622:                                        if(method.extra_params)
                    623:                                                args->put(*method.extra_params, caller->get_element(*method.extra_params));
                    624: 
                    625:                                        if(show_args)
                    626:                                                current->put("args", &vargs);
                    627: 
                    628:                                        if(show_locals){
                    629:                                                VHash& vlocals=*new VHash;
                    630:                                                HashStringValue* locals=vlocals.get_hash();
                    631: 
                    632:                                                if(VParserMethodFrame* frame=dynamic_cast<VParserMethodFrame*>(caller))
                    633:                                                        for(HashString<Value*>::Iterator h(frame->my); h; h.next()){
                    634:                                                                String::Body key=h.key();
                    635:                                                                Value* value=h.value();
                    636:                                                                if(!args->contains(key) && (key != "result")){
                    637:                                                                        locals->put(key, value->get_junction() ? VVoid::get() : value);
                    638:                                                                }
                    639:                                                        }
                    640: 
                    641:                                                current->put("locals", &vlocals);
                    642:                                        }
                    643:                                }
                    644:                        }
                    645: 
                    646:                        result->put(format(index, 0), &vcurrent);
                    647:                }
                    648:                caller=caller->caller();
                    649:                index++;
                    650:        }
                    651: 
                    652:        r.write(vresult);
                    653: }
                    654: 
1.1       misha     655: // constructor
                    656: MReflection::MReflection(): Methoded("reflection") {
1.2       misha     657:        // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
1.73      moko      658:        // ^reflection:create[ $.class[name] $.constructor[name] $.arguments[ $.1[param1] $.2[param2] ...] ]
                    659:        add_native_method("create", Method::CT_STATIC, _create, 1, MAX_CREATE_PARAMS + 2);
1.1       misha     660: 
1.3       misha     661:        // ^reflection:classes[]
                    662:        add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
                    663: 
1.1       misha     664:        // ^reflection:class[object]
                    665:        add_native_method("class", Method::CT_STATIC, _class, 1, 1);
                    666: 
                    667:        // ^reflection:class_name[object]
                    668:        add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
                    669: 
1.34      moko      670:        // ^reflection:class_by_name[class_name]
                    671:        add_native_method("class_by_name", Method::CT_STATIC, _class_by_name, 1, 1);
                    672: 
1.2       misha     673:        // ^reflection:base_class[object]
                    674:        add_native_method("base", Method::CT_STATIC, _base, 1, 1);
                    675: 
                    676:        // ^reflection:base_class_name[object]
                    677:        add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
                    678: 
1.30      misha     679:        // ^reflection:def[class|...;name]
                    680:        add_native_method("def", Method::CT_STATIC, _def, 2, 2);
                    681: 
1.2       misha     682:        // ^reflection:methods[class_name]
1.84      moko      683:        add_native_method("methods", Method::CT_STATIC, _methods, 1, 2);
1.2       misha     684: 
1.54      moko      685:        // ^reflection:method[object or class;method_name[;self]]
                    686:        // ^reflection:method[junction[;self]]
1.50      moko      687:        add_native_method("method", Method::CT_STATIC, _method, 1, 3);
1.28      misha     688: 
                    689:        // ^reflection:method_info[class_name;method_name]
1.59      moko      690:        // ^reflection:method_info[junction]
                    691:        add_native_method("method_info", Method::CT_STATIC, _method_info, 1, 2);
1.28      misha     692: 
1.79      moko      693:        // ^reflection:filename[object or class or method]
1.67      moko      694:        add_native_method("filename", Method::CT_STATIC, _filename, 1, 1);
                    695: 
1.13      misha     696:        // ^reflection:fields[object or class]
                    697:        add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
                    698: 
1.44      moko      699:        // ^reflection:fields_reference[object]
                    700:        add_native_method("fields_reference", Method::CT_STATIC, _fields_reference, 1, 1);
                    701: 
1.28      misha     702:        // ^reflection:field[object or class;field_name]
                    703:        add_native_method("field", Method::CT_STATIC, _field, 2, 2);
1.9       misha     704: 
                    705:        // ^reflection:dynamical[[object or class, caller if absent]]
                    706:        add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19      moko      707: 
1.39      moko      708:        // ^reflection:is[element_name;class_name|code|method[;context]]
1.35      moko      709:        add_native_method("is", Method::CT_STATIC, _is, 2, 3);
                    710: 
1.19      moko      711:        // ^reflection:copy[src;dst]
                    712:        add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.25      moko      713: 
                    714:        // ^reflection:uid[object or class]
                    715:        add_native_method("uid", Method::CT_STATIC, _uid, 1, 1);
1.26      misha     716: 
                    717:        // ^reflection:delete[object or class;field_name]
                    718:        add_native_method("delete", Method::CT_STATIC, _delete, 2, 2);
1.54      moko      719: 
                    720:        // ^reflection:mixin[object or class or junction;options]
1.55      moko      721:        add_native_method("mixin", Method::CT_STATIC, _mixin, 1, 2);
1.54      moko      722: 
1.78      moko      723:        // ^reflection:tainting[[language or 'tainted' or 'optimized';]string]
1.76      moko      724:        add_native_method("tainting", Method::CT_STATIC, _tainting, 1, 2);
                    725: 
1.81      moko      726:        // ^reflection:stack[options]
                    727:        add_native_method("stack", Method::CT_STATIC, _stack, 0, 1);
                    728: 
1.1       misha     729: }

E-mail: