Shop OBEX P1 Docs P2 Docs Learn Events
Callback hack — Parallax Forums

Callback hack

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2006-06-26 22:32 in Propeller 1
Attached is an object I wrote called callback. It's a hack that allows methods in other objects to call routines in the caller's object. Where would you use something like this? One example would be in a formatting routine that formats character-based output (e.g. dec and hex). By using a callback, the formatter doesn't have to call the output object's output routine — or even know that the output object exists. It just sends the data, one character at a time, to the caller's callback routine, which then directs the output to the proper object. It's like an indirect call, where a pointer to the output subroutine is sent to the formatter. In this way, the output object needs to be declared only in the calling routine and nowhere else.

Because Spin doesn't allow expressions like @proc, where "proc" is the name of some procedure, another way had to be found to identify the callback routine. In Spin's object code, each procedure has an index number, beginning at one for the first public procedure, and counting up by one through the public procedures in order, then through the private procedures. (At least I think that's right. Chip?) Each object that uses a callback will have to include a procedure subroutine like the one shown in the demo program. This creates a token consisting of the object's base address and the desired procedure's index number.

Because callback calls are not resolved at compile time, there's no way to check the argument count, so it's up to the programmer to make sure it's correct. I've provided four routines for zero, one, two, and three arguments.

This code is very experimental and relies completely on guesswork and inference. Use at your own risk!

-Phil

Comments

  • RinksCustomsRinksCustoms Posts: 531
    edited 2006-06-26 20:10
    I'm not that advanced , but the description either sounds like this will either be very efficient or very catastrophic coding. I vote for the better!!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-06-26 22:32
    'Turns out it's incomplete and doesn't work in all cases. 'More later...

    -Phil
Sign In or Register to comment.