Page 1 of 1

Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 3:36 pm
by Mistrel
Currently we have CallFunctionFast() for calling a pointer to a function. But is this really necessary? I think it would be great if we could just shorthand call any variable defined as a pointer:

As it is currently:

Code: Select all

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a=@PrintTest()

CallFunctionFast(*a)
How it could be:

Code: Select all

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a=@PrintTest()

*a()

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 3:50 pm
by NicTheQuick
You can use Prototypes.

Code: Select all

Prototype.i testProc()

Procedure PrintTest()
  Debug "Test"
EndProcedure

*a.testProc =@PrintTest()

*a()

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 6:28 pm
by mk-soft
:wink:

Code: Select all

-TOP

Procedure fc0()
  Debug "Func 0"
EndProcedure

Procedure fc1()
  Debug "Func 1"
EndProcedure

Procedure fc2()
  Debug "Func 2"
EndProcedure

Procedure fc3()
  Debug "Func 3"
EndProcedure

Prototype protoInvoke()

Structure udtInvoke
  Invoke.protoInvoke
EndStructure

Dim fc.udtInvoke(3)
fc(0)\Invoke = @fc0()
fc(1)\Invoke = @fc1()
fc(2)\Invoke = @fc2()
fc(3)\Invoke = @fc3()

For n = 1 To 4
  For i = 0 To 3
    fc(i)\Invoke()
  Next
  Delay(500)
Next

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 6:34 pm
by Mistrel
I know how to use Prototypes. The entire premise of this request is to provide a shorthand for CallFunctionFast() where I don't have to declare ahead of time what arguments are to be passed.

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 7:32 pm
by nco2k
i have a better idea, lets remove that outdated CallFunction(Fast) garbage. 8)

c ya,
nco2k

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 7:50 pm
by kenmo
Mistrel wrote:a shorthand for CallFunctionFast() where I don't have to declare ahead of time what arguments are to be passed.
I understand your request.

The problem I see is:
CallFunctionFast() and Prototypes tell the compiler how many parameters to pass to the function.
But *a is just an integer that points to a memory location.
Calling *a(...), the compiler doesn't know how many parameters or their types.
If you call *a(5000), does it pass 5000 as a Word, Integer, Quad?
Sounds like a source of stack corruption problems.


Unless you are saying, only allow this shorthand for calls with 0 parameters?

Re: Allow pointer-defined variables to be callable

Posted: Mon Jun 11, 2018 8:59 pm
by NicTheQuick
@kenmo
Correct. The Compiler doesn't know about the parameters. So he can not compile the correct code for a function call to a function pointer which value will be set at runtime.