www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The dynamic method call

reply "Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> writes:
A function or method with a signature known at runtime shall be called.

What I need is code, that pushes the arguments on the stack, calls the
method and gets the return value.
Is asm needed?

Code:
union value{
 // some primite types bool, byte, .... long
}
//...
int CallIntMethod (void* obj, void* method, ...);
int CallIntMethodV(void* obj, void* method, va_list args);
int CallIntMethodA(void* obj, void* method, value *args);

long CallLongMethod(void* obj, void* method, ...);
long CallLongMethodV(void* obj, void* method, va_list args);
long CallLongMethodA(void* obj, void* method, value *args);

// ...
// more methods with the other return types


* obj is a void* ptr to the object
* method is the function ptr to the method

I am able to retrieve the methods signature with obj and method.
In this case the return type is known for each prototype.

Any hint for a implementation?
Feb 21 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Frank Benoit (keinfarbton) wrote:
 A function or method with a signature known at runtime shall be called.
 
 What I need is code, that pushes the arguments on the stack, calls the
 method and gets the return value.
 Is asm needed?
 
 Code:
 union value{
  // some primite types bool, byte, .... long
 }
 //...
 int CallIntMethod (void* obj, void* method, ...);
 int CallIntMethodV(void* obj, void* method, va_list args);
 int CallIntMethodA(void* obj, void* method, value *args);
 
 long CallLongMethod(void* obj, void* method, ...);
 long CallLongMethodV(void* obj, void* method, va_list args);
 long CallLongMethodA(void* obj, void* method, value *args);
 
 // ...
 // more methods with the other return types
 
 
 * obj is a void* ptr to the object
 * method is the function ptr to the method
 
 I am able to retrieve the methods signature with obj and method.
 In this case the return type is known for each prototype.
 
 Any hint for a implementation?
I've thought about this sort of thing before, back when I was tinkering with D<->Python shims (but then Kirk came along, and well, there wasn't much point :P). First up, I'm assuming that by "CallIntMethod", you mean that the *return type* is an int. It wouldn't make much sense to go through all this trouble for a function that took ints :P Secondly, if you haven't seen it, check out std.boxer -- basically, it can hold anything, which makes a great variant type. As for pushing the arguments on to the stack, you probably *will* need ASM. Look at it like this: if you write a function that pushes an argument on to the stack, then when THAT function returns, you'll pop off that argument you just pushed on. Thankfully, there should be enough information at http://www.digitalmars.com/d/abi.html to explain how it all works. Hope that's of some help :) -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Feb 21 2007