Shop OBEX P1 Docs P2 Docs Learn Events
Passing an Object — Parallax Forums

Passing an Object

EBEB Posts: 8
edited 2008-08-15 07:30 in Propeller 1
Is is possible to pass an object as an argument to a function? I can pass a pointer to the object, but it complains on the other end of about syntax. Is there some way to dereference the pointer on the other end so that it's members can be accessed?

Comments

  • hippyhippy Posts: 1,981
    edited 2008-08-12 02:49
    No, Spin doesn't support object or method pointers.

    There are a few tricks which can allow this functionality to be achieved, or something close to it. Those who have worked on such things will hopefully pass-by and point to relevant previous posts, or dive in with a forum search. It's quite complicated though, especially if new to the Propeller.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-08-12 10:50
    Hello EB,

    it's a propeller NOT a PC !

    Do you really think within an application that has to fit into 32kB of RAM needs such complicated things?

    best regards

    Stefan
  • hippyhippy Posts: 1,981
    edited 2008-08-12 13:27
    @ Stefan : Object and Method pointers would be very useful to have in Spin and it is quite a short-coming IMO. That the Propeller has limited RAM is actually an argument in favour of them given the alternatives eat up a lot of RAM.

    It's not actually that complicated in most cases, get the address of method to call from a variable rather than load it as a constant determined at compile time, however the way Spin bytecode makes method and cross-object calls makes it more difficult.
  • EBEB Posts: 8
    edited 2008-08-12 16:05
    I agree - it is a short coming. Being able to pass references to already instantiated objects does allow for making leaner (i.e. more able to fit inside 32k) programs. And for what it's worth, there's an awful lot that can be done in 32k if you take the time to build lean software.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-08-12 19:34
    Hello Hippy,

    so can you post a code-snippet that shows how it works and how it is saving RAM ?

    best regards

    Stefan
  • hippyhippy Posts: 1,981
    edited 2008-08-12 19:46
    I'm not quite sure exactly what you're asking for there. If the Propeller had bytecode which supported it, direct method calls and via a method pointer would be somewhat like the following ...
    Main:                     PUB Main
            JSR #Method         Method
            RTS
    Method:                   PRI Method
            RTS
    
    
    


    Main:                     PUB Main
            MOV R1,#Method      methodPtr := @Method
            JSR R1              *methodPtr
            RTS
    Method:                   PRI Method
            RTS
    
    



    Without that, it needs a long Case statement to call a selected method.

    A hack to the above first example to allow method pointers if not officially supported would be to overwrite the operand of the JSR #Method with the desired address at run-time, however Spin bytecode isn't quite so straight forward as that.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-08-15 07:30
    hippy said...
    will hopefully pass-by and point to relevant previous posts
    I do still keep an eye on the forums, although I've been a bit short of time recently smile.gif.
    EB said...
    Is is possible to pass an object as an argument to a function? I can pass a pointer to the object, but it complains on the other end of about syntax. Is there some way to dereference the pointer on the other end so that it's members can be accessed?
    This is possible but not all that easy. I've been working on it for awhile now (although not much for the last month or two, when I get some time I'll do some more). Here is the thread you want http://forums.parallax.com/showthread.php?p=701497.

    The latest version isn't really quite ready for any real use yet but it is getting close. The demo shows passing of objects and things. It would probably be possible to pass functions as well but I haven't tried it. If you bug me enough it may even help me get a new version out sooner smile.gif.
Sign In or Register to comment.