Shop OBEX P1 Docs P2 Docs Learn Events
I need function pointers!!! — Parallax Forums

I need function pointers!!!

KyeKye Posts: 2,200
edited 2011-01-27 17:43 in Propeller 1
SPIN, you're letting me down. I like need some function pointers for indirecting RTC access for my file system driver.

I look forward to C on the Prop 2 - Or atleast some type of C -> SPIN bytecode translator.

Comments

  • wjsteelewjsteele Posts: 697
    edited 2011-01-27 13:50
    I thought someone hacked them into some sample code a while back... perhaps it was the Beau himself?

    Bill
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-27 14:07
    Kye,

    I posted a MethodPointer object to the OBEX earlier this month. It is located at http://obex.parallax.com/objects/697/ .

    It was discussed in the thread at http://forums.parallax.com/showthread.php?128397-Callbacks-and-Method-Pointers-in-Spin&highlight=pointer . The technique I use doesn't modify the method tables or the Spn bytecodes (except for a small amount done at initialization). It just requires a 4-word buffer for each method pointer that is used.

    Dave
  • jazzedjazzed Posts: 11,803
    edited 2011-01-27 17:34
    Just use the object array unbounded method.
    obj
      ' normal code
      serial           : "FullDuplexSerial"
      ' insert other normal object here
    
      ' unbound method code is last
      objarray[1] : "method0" ' contains PUB method to call per array index
      opt1method  : "method1" ' contains PUB method to call per array index
      opt2method  : "method2" ' contains PUB method to call per array index
      optNmethod  : "methodN" ' contains PUB method to call per array index
      ' no more objects
    

    Call method by object index. This has risks of course like if Parallax updates the compiler to do object array bounds checks - one would hope that our list of desirable enhancements might get addressed first. YMMV.
  • RossHRossH Posts: 5,519
    edited 2011-01-27 17:43
    jazzed wrote: »
    Just use the object array unbounded method.
    obj
      ' normal code
      serial           : "FullDuplexSerial"
      ' insert other normal object here
    
      ' unbound method code is last
      objarray[1] : "method0" ' contains PUB method to call per array index
      opt1method  : "method1" ' contains PUB method to call per array index
      opt2method  : "method2" ' contains PUB method to call per array index
      optNmethod  : "methodN" ' contains PUB method to call per array index
      ' no more objects
    

    Call method by object index. This has risks of course like if Parallax updates the compiler to do object array bounds checks - one would hope that our list of desirable enhancements might get addressed first. YMMV.

    Very cute! I had forgotten all about object arrays - thank goodness for languages that do not do any bounds checking!

    Ross.
Sign In or Register to comment.