<< Click to Display Table of Contents >> Navigation: Using Delphi classes and functions > Magic functions |
Magic function is a special function used in following cases
•number of parameters can vary
•Parameter types are not known
•parameter cannot be passed in a standard way (for example - array)
•function needs Scripter instance to work.
•function has var parameters (for example Insert or Delete)
Magic function has following declaration:
function MagicFunction(const Sender: TScriptParser; var Params: TScriptStack;
StackTop, ParamCount: integer): Variant;
Sender is Scripter instance in which function is called.
Params is pointer to current runtime stack (array of TExVariable)
StackTop is index of top stack variable
ParamCount - number of parameters in call.
function MagicInc(const Sender: THtScriptParser; var Params: TScriptStack;
StackTop, ParamCount: integer): Variant;
begin
if ParamCount = 1 then
begin
if Params[StackTop].IsTemp then
Sender.Tokenizer.Error(sVarRequiredforInc);
Result := Params[StackTop].Value + 1;
Params[StackTop].Value := Result;
end else begin
if Params[StackTop - 1].IsTemp then
Sender.Tokenizer.Error(sVarRequiredforInc);
Result := Params[StackTop - 1].Value + Params[StackTop].Value;
Params[StackTop - 1].Value := Result;
end;
end;
HtScriptGlobal.RegisterMagicFunc('Inc', MagicInc);
When magic function is registered with different names, current (called) function name can be obtained via call stack:
Name := TScriptFunc(Sender.DebugStack[Sender.DebugStack.Count - 1]).Name