Use class member function for imp.wakeup

Is there a way to use a member function as a parameter to imp.wakeup ? I have a class that encapsulates a stepper motor. If I need to step the motor to a fixed position, I have a tracking class that will generate a pulse, and then sleep until the next pulse needs to be generated. The imp.sleep works ok if the sleep is very short, say 5ms, i.e. 5ms between pulses. If I try to slow the motor down using longer sleep time (say 15ms), then the imp goes offline and sometimes crashes at some point after the movement starts. There is a warning about long sleeps in the wiki.
What I would like to do is have an instance specific callback to pass to the imp.wakeup function because I have 2 motors. What I want to do is something like this

`
class TrackingMotor
{
stepsToGo = 0;

function GoToPosition( stepPosition )
{
stepsToGo = stepPosition;
stepToCompletion();
}
function stepToCompletion()
{
stepsToGo = stepsToGo - 1;
if( stepsToGo )
{
step();
imp.wakeup(0.025, this.stepToCompletion);
}
}
}

`

This doesn’t work. At runtime, there is an error that the index stepsToGo is not defined. I assume this is the first callback invocation. I have resorted to a workaround that uses non-member functions, but would like to know if there is a way to do what I am trying to do above.

Thanks in advance

This has to do with some of the weird ways Squirrel handles scope. I believe you need to need to bind the wakeup call to the scope of your class.

Try this:

imp.wakeup(0.025, this.stepToCompletion.bindenv(this));

That was the magic Works perfectly!
Thank you!