Shop OBEX P1 Docs P2 Docs Learn Events
spin dynamic calls possible, in theory? — Parallax Forums

spin dynamic calls possible, in theory?

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2008-11-15 19:51 in Propeller 1
I was wondering if there is·a way to make the following happening:

OBJ
  jb1[noparse][[/noparse]2]:  "jb1"   'object with PUB main
  jb2:     "jb2"   'another object with PUB main
  
PUB main | c
{{ application starts here }}
  c := 0
  repeat 2
    jb1[noparse][[/noparse]c].main  'call main for selected object
    jb2.main
    c++
  repeat

·The above compiles, but I would like jb1[noparse][[/noparse]0] to be "jb1", and jb1[noparse][[/noparse]1] to be "jb2"
If it were possible to do
OBJ
· jb1[noparse][[/noparse]2]: "jb1","jb2"· 'object array filled from list
then jb1[noparse][[/noparse]c].main calls different type of objects without the need for a (large) CASE statement.
If jb1 specifies more entries than the list supplies, the remaining entries are set to the
last object in the list. This ensures compatibility with current behaviour.

Any chance of updating the compiler to allow this? Is there currently a way to make it·happen
manually (people have been poking around in the object tables)?

Edit: it should be allowed to continue the list on the next line by using a comma at the end of a line
eg.
· jb1[noparse][[/noparse]4]: "jb1","jb2",
············ "jb3","jb4"·· 'no comma so list ends here

regards peter


Post Edited (Peter Verkaik) : 11/11/2008 2:59:35 PM GMT

Comments

  • mparkmpark Posts: 1,305
    edited 2008-11-11 15:36
    You can just say
    OBJ
    jb1: "jb1"
    jb2: "jb2"
    jb3: "jb3"
    jb4: "jb4"

    and then
    jb1[noparse][[/noparse] 3 ].main '<== same as jb4.main

    as long as the various jb* objects are compatible.

    You don't get the "filling out the rest of the array" behavior you want, though.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-11-11 15:44
    What do you mean by jb objects must be compatible?
    Does that mean the objects PUB main must be at identical offsets
    (I can make PUB·main the first method in the objects).
    Other than a shared name (eg. main) the objects are independant
    with different content.

    I like the implied array access via the first object, that will do for me.

    regards peter
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-11 16:44
    The names aren't matched at runtime. So the PUBlic methods' order of appearance in the various objects is important and is what determines "compatibility".

    mpark, that's a pretty neat trick, BTW!

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-11-11 17:47
    It is just the order of PUB methods? The size or presence of VAR and DAT is
    irrelevant, as are included OBJ in the objects that I want in the list?
    Also, the presence of local variables in some of the PUB 'main' methods
    is irrelevant?

    If that is all true, I can just make my PUB 'main' the first method in
    the objects and use the implied array for calling different object types.

    Thanks.

    regards peter
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-11 18:14
    The parameter count is relevant, but I'm not sure about the local variable count. I suspect that it does not matter. To the best of my recollection, a method call indexes a table in the object that contains the actual method addresses. So the size of each method should not matter. If Hippy were lurking here, I'm sure he could provide a definitive answer, since he's intimately familiar with Spin internals.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • mparkmpark Posts: 1,305
    edited 2008-11-11 20:33
    Phil Pilgrim (PhiPi) said...

    mpark, that's a pretty neat trick, BTW!
    Thanks, but I could have sworn that I got the idea from one of your threads.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-12 00:36
    mpark,

    No, it wasn't me. (Or if it was, I can testify that one advantage of early-onset senility is the perpetual joy of rediscovery!) Since Hippy doesn't seem to be lurking about, maybe we can credit (blame) him!

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!

    Post Edited (Phil Pilgrim (PhiPi)) : 11/12/2008 1:01:58 AM GMT
  • HarleyHarley Posts: 997
    edited 2008-11-12 00:51
    Phil,

    Re: "...the perpetual joy of rediscovery!", how then does one even know it is a rediscovery? Recent 'turning 80' makes me wonder at times about such situations. "Seems like I've been here before" but can't recall when. yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Harley Shanko
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-12 01:00
    Harley said...
    ... how then does one even know it is a rediscovery?
    True enough. Noted and corrected!

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • mparkmpark Posts: 1,305
    edited 2008-11-13 01:01
    Where the heck *is* Hippy?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-11-13 11:19
    Haven't had a chance to read what you want properly yet but I think it is possible. Have a look at DOL (here http://forums.parallax.com/showthread.php?p=701497 for some ideas. I'll have a better look tomorrow and see if I can give you a couple of hints.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-11-13 12:29
    I want to mimic the C statement
    void (*f[noparse]/noparse)(void);

    However, if all PUB main are defined identical in terms of arguments and return type,
    for example PUB main(a1,a2): retval

    then also the following should be possible I assume
    long (*f[noparse]/noparse)(long,long);

    by using
    val := jb[noparse][[/noparse]index].main(arg1,arg2)


    regards peter
  • BradCBradC Posts: 2,601
    edited 2008-11-13 12:40
    If you define it the way mpark demonstrated above, then yes that is precisely the way to do it.

    Don't worry about return type, it's always a long whether or not a return type is defined, but the parameter counts must match or you'll tie the stack in a knot.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pull my finger!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-13 19:29
    Didn't he log on from Hawaii a little while ago? Perhaps he's still on vacation.
    mpark said...
    Where the heck *is* Hippy?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • bdickensbdickens Posts: 110
    edited 2008-11-14 15:56
    Interesting; I was planning on defining my servo objects as

    OBJ
    Servo[noparse][[/noparse]12]: "Generic Servo"

    CON
    Throttle 1
    Rudder 2

    Then setting the properties for each one as needed.

    Result := Servo[noparse][[/noparse]Throttle].SetDirection(counterclock)
    Result := Servo[noparse][[/noparse]Throttle].SetZero(0)
    Result := Servo[noparse][[/noparse]Rudder].SetDirection(clock)
    Result := Servo[noparse][[/noparse]Rudder].SetZero(128)


    I lifted this from the Blinker code in the education kit. But it allows me to encapsulate a lot of ugly calculations. So will this work conceptually ?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-11-14 21:16
    @bdickens, That will work fine. However, don't forget that the array index starts from 0 and not 1.

    @peter v. Like brad said the only thing that matters is that the method is in the same spot in the method table (first method in the file works well) and that it has the same number of arguments.

    As to who discovered it I can't remember but it came from someone wanting to initialise and start a whole heap of different objects with a loop. It's really just a side effect of spin not doing any bounds checking on the array. For interests sake a method call gets compiled to a spin byte code that has the index of the object in the method table and then the index of the method in the new table. The table is just a list of pointers to where the other objects are. The only slight complication is that all the pointers are offsets and not the actual location (which is actually very helpful for some things). There is a page on the propeller wiki that hippy wrote if you want to know more about it.
  • hippyhippy Posts: 1,981
    edited 2008-11-15 19:51
    mpark said...
    Where the heck *is* Hippy?
    Still here - Yoo Hoo !!!
    Paul Baker (Parallax) said...
    Didn't he log on from Hawaii a little while ago? Perhaps he's still on vacation.
    Hawaii ? I wish ! If I ever get that close to America you can guarantee I'll drop in to say hello to the Parallax staff - keep a cup of rosey lee ready just in case smile.gif

    Started a new job; in at the deep end so have been quite pre-occupied in that respect and battling some consequences of the Great Global Economic Downturn (TM).

    When everything settles down I hope to be back and contributing more and getting my half-finished projects completed. Just been a bit hectic ( and will be for a time ) but I've not deserted, just been tied up elsewhere. I'll try to drop in more frequently.

    Isn't it always the way - (1) Real life gets in the way of fun, (2) You can have money but not the time or time but no money (3) When you have no money the exchange rate is in your favour, when you get some money ... smile.gif

    Post Edited (hippy) : 11/15/2008 8:05:49 PM GMT
Sign In or Register to comment.