what is the best way to determine the name of the function being run when in a c module?
I'm using
name = ID_FROM_INT(Pike_fp->current_object->prog, Pike_fp-
fun)->name;
when calling non-constant functions, but that doesn't seem to work when the function being called was added using add_function_constant().
any suggestions?
Bill
I guess you'd have to loop through the constant area of Pike_fp->current_object->prog, looking for a callable with a pointer matching your function.
Constant functions are a bit odd creatures since it's something that doesn't exist on the pike level (would be nice to have, though).
If you make a function to do this, I think it could be useful to have in the core, i.e. in program.{c,h}.
Hmm... that's what I was afraid of. In the meantime, I did an end run around the problem by making the class methods objects that implement `()(), that way I can store the method name in the object. The most obvious downsides to this are a) that it becomes more difficult to replace these in a subclass, and b) i've not yet figured out a reasonable way to do better typechecking. I suppose it's a non-issue for objective-c, but it's still a compromise i'm not completely pleased with.
Perhaps we can discuss this for a few moments in Riga.
Bill
On Tue, 10 Oct 2006, Martin Stjernholm, Roxen IS @ Pike developers forum wrote:
I guess you'd have to loop through the constant area of Pike_fp->current_object->prog, looking for a callable with a pointer matching your function.
Constant functions are a bit odd creatures since it's something that doesn't exist on the pike level (would be nice to have, though).
If you make a function to do this, I think it could be useful to have in the core, i.e. in program.{c,h}.
Sounds like a fairly heavy-handed solution just for that. It shouldn't afterall not be more difficult than a loop doing a bit of digging.
Did you resort to the object approach because a loop would be too expensive or because it was too difficult to figure out how it's stored?
Hmm, come to think about it, it actually is more difficult because there is no relevant Pike_fp->current_object in this case. Didn't realize that before.
Anyway, using non-constant functions seems simpler than objects with `().
Yes, the problem is that I wanted to preserve as much of the semantics of Objective C as possible. Like Java (actually, not really like Java at all), each Objective C class can have a number of class methods, which usually create an instance of the object. Because a user could concievably want to be able to do that, I wanted to preserve the availability of those methods in a Pike class.
so,
[ NSString stringWithCString: somestring ];
would translate to
Cocoa.NSString.stringWithCString(somestring);
Basically, for my purposes, what I needed is the class and method name. I suppose I another solution would be dynamically generated machine code, but that seems way more difficult than I want to get into for this.
I also considered a hair-brained idea of digging through the call stack to find the method name, but that seemed unreliable at best.
Consider this a vote for real java-style "static" methods in Pike :)
Bill
On Wed, 11 Oct 2006, Martin Stjernholm, Roxen IS @ Pike developers forum wrote:
Hmm, come to think about it, it actually is more difficult because there is no relevant Pike_fp->current_object in this case. Didn't realize that before.
Anyway, using non-constant functions seems simpler than objects with `().
On Wed, Oct 11, 2006 at 12:44:16PM -0400, Bill Welliver wrote:
Consider this a vote for real java-style "static" methods in Pike :)
can you explain how static methods would solve that problem?
greetings, martin. -- pike programmer travelling and working in europe open-steam.org unix system- bahai.or.at iaeste.(tuwien.ac|or).at administrator (caudium|gotpike).org is.schon.org Martin Bähr http://www.iaeste.or.at/~mbaehr/
Well, static methods (and static variables, which would be nice, too)are able to be called directly from a program without it being instantiated, much like a constant added to a program. Normally, static (as in Java, or class methods in Objective C) methods can only work with data local to the method, as they're not run from within an object.
In pike, constant functions aren't represented by a callable struct. That means that there's no way to know what the name of the function is. See function_name() for an example of this.
For a silly example that illustrates the general point, see the following article:
http://www.javaworld.com/javaworld/javaqa/2001-11/03-qa-1121-mrhappy.html
Bill
On Wed, 11 Oct 2006, Martin [iso-8859-1] Bähr wrote:
On Wed, Oct 11, 2006 at 12:44:16PM -0400, Bill Welliver wrote:
Consider this a vote for real java-style "static" methods in Pike :)
can you explain how static methods would solve that problem?
not if the semantics you need are that the method is available from a program. sure, I suppose you could make a module that in turn has a class within it that is instantiated by overriding `()() in the module, but that seems like an awful lot of work, especially if you're doing it in c, and it makes subclassing very difficult.
Class methods are something that's an integral part of a number of Object oriented languages, and there's really no equivalent in Pike.
Bill
On Wed, 11 Oct 2006, Martin [iso-8859-1] B�hr wrote:
and you can't solve that through a module?
greetings, martin.
a) because that's the way objective c works, and any workaround it would be clumsy. each class has multiple methods (often object factories) that need to be supported. it's not something that is only used occasionally, i'd be hard pressed to name a class in Objective C that doesn't have class methods. NSString, for instance, has over a dozen of them.
b) it would be difficult to impossible to allow reliable subclassing of objects if the class methods are in a separate location from the instance methods.
bill
On Wed, 11 Oct 2006, Martin [iso-8859-1] Bähr wrote:
On Wed, Oct 11, 2006 at 01:39:18PM -0400, Bill Welliver wrote:
not if the semantics you need are that the method is available from a program.
why do you need that semantic? i didn't get that.
greetings, martin.
So if you have a function added through add_function_constant in some class Foo (i.e. not a module), then you can call it using Foo.my_function without ever instantiating Foo? In that case: Cool, I didn't know that worked.
Anyway, when you add the function you should know its (complete) name, right? So all you need to do is to insert it into a reverse mapping too. I think the block_alloc.h PTR_HASH_ALLOC_FILL_PAGES macro would generate a suitable hash table for this.
pike-devel@lists.lysator.liu.se