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