![]() |
|
||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
MIVA® COMPILER: Dynamic Function CallsMiva parser built-in the Virtual MachineOne of the most often posted problems with the compiler was a simple replacement of a very efficient dynamic function call, that in interpreted Miva Script could be done in the following way: <MvEVAL EXPR="{&[g.function];()}">
Up to date, the only proposed replacements were using MvIFs (or possibly MvDOs). I have now tested a more efficient and very simple way for doing the same in compiled Miva programs without the necessity to build long MvIF blocks. The method allows also interpreting Miva expressions in real-time (in compiled environment of course). The Miva Virtual Machine contains an interpreter that is capable of executing functions and expressions (not the tags) in real-time. Currently it may be used only through a tricky code, but I wonder why there is not a simple 'execute()' function that would allow doing the same directly and would allow reading the result simpler!? Any reason for hiding this feature from us, Miva Corp.? I see now that it is possible without any problems, the code is in the Miva Virtual Machine, and it works just fine, so why not offering it officially? Here is the code - amazingly simple: <MvFILTER NAME="test" FILTER_TYPE="variable" FILTER="g.function">
The 'test' database has to be open, of course, and it should be empty for the best performance. Better yet is having a single record in it - the NOT test.d.EOF may be then used to test the return result of the function or expression (only true / false). You can assign code to the g.function dynamically in the run-time and you may use functions with and without arguments. The same code may be used for executing/interpreting Miva expressions (including multiple function calls) without recompiling the program: <MvFILTER NAME="test" FILTER_TYPE="variable" FILTER="g.expression">
The possibilities of using it are almost unlimited. It can be dangerous similarly as the macro, of course too, so be careful when using it!!! Please note that if a function is not defined (at user functions) or used (at built-in functions) in the compiled file, you would get a run-time error. So for example you get a run-time error if you try to assign 'glosub(...)' to the filter in run-time and it is nowhere used in the script. Once it is used (coded and compiled), it has also an entry in the function table of the compiled code and can be called in run-time without problems. Test ScriptHere is a sample test script, asigning the function name or the expression dynamically from a user input in a HTML form:
Compile it, put a test.dbf with one record in the data dir, run it, and type typeMeIn(0) and typeMeIn(1) into the form. It should return the argument. Try different Miva expressions and built-in functions, too. Note that although the filter executes almost any expression, only true / false result is returned through the test.d.EOF. For extracting the result value, please see below. Return values“I read the online article and I am uncomfortable about the use of the global variable to gain access to the function return but I have an alternative that should work. Passing values by reference.”
Yes, of course, a reference can be used too, but it makes no big difference if used directly - it requires designing all function in this way and does not allow pure built-in functions and Miva expressions. Additionally and unfortunately references do no seem to work yet smoothly in the v0.9 compiler. Better is writing a small assign function. Again you can use a global variable or a reference for passing the result. The proposed reference is here indeed probably better suited. The following example with the reference does not yet work compiled, although uncompiled there is no problem:
You would then call the functions or expressions in the MvFilter in this way: assign(variablename,function(arguments))or assign(variablename,expression)
For example: assign(g.ver,'Version ' $ gettoken(s.mivaversion,'.',1) $ '.xx')
Enjoy! Some Useful LinksMiva Script Reference ManualMiva Merchant User List Archive |
||||||||||
|
Miva and some other terms used on this page are registerd trademarks of the Miva Corporation |