|
|
|
|
|
- Using the dynamic procedure
- Dynamic code
- Parameters of a query
- Fields in HFSQL functions
- Deploying of an application that uses Compile
- Limitations
Compile (Function) In french: Compile Dynamically compiles a procedure whose source code is supplied. The created and compiled procedure is a procedure global to the project.
sRésultat is string
sRésultatCompile is string
sRésultatCompile = Compile("Proc_dynamique", MonCodeSource)
SWITCH sRésultatCompile
CASE "": sRésultat = ExecuteProcess("Proc_dynamique", trtProcedure)
CASE "ERR": sRésultat = "Compilation impossible." + CR + ErrorInfo()
OTHER CASE: sRésultat = sRésultatCompile
END
RETURN sRésultat
Formule is procedure = Compile("Proc_dynamique", MonCodeSource)
IF ErrorOccurred = False THEN
ExecuteProcess("Proc_Dynamique", trtProcedure)
ELSE
Info(ErrorInfo())
END
Formule is procedure = Compile(MonCodeSource)
IF ErrorOccurred = False THEN
Formule()
ELSE
Info(ErrorInfo())
END
Syntax
Dynamic compilation of a global procedure Hide the details
<Result> = Compile(<Procedure name> , <Source code> [, <Parameters>])
<Result>: Character string Compilation result:- Empty string ("") if the compilation was successful. The procedure can be run using ExecuteProcess and Execute with the trtProcedure constant.
- ERR if a serious error has occurred: file wdxxxcpl.dll not found, <Code source> incorrect, no project in progress, global procedure already created, etc.. ErrorInfo returns the details of the error.
- An error message if a compilation error was detected. This message corresponds to the caption of the error.
<Procedure name>: Character string Name of dynamic global procedure to create.To create a local procedure, all you have to do is specify the element name. For example: "WindowName.ProcedureName".. Warning: If the function Compile is called several times with the same procedure name, the last procedure created is automatically overwritten. <Source code>: Character string Source code in WLanguage of the procedure to compile dynamically. - If this code contains quotes, they must be doubled (see the example).
- This code may contain the call and declaration code of an internal procedure.
<Parameters>: Optional variable of type CodeWLangageCompilation
Remarks Using the dynamic procedure - If you are using the first syntax, to use your dynamic procedures, all you have to do is run the procedure via ExecuteProcess or Execute.
Another (inadvisable) solution: before executing the function Compile, declare the procedure name to the WLanguage compiler using the EXTERN keyword and use the procedure directly. - If you are using the second syntax, to use your dynamic procedures, all you have to do is start the procedure:
- If you are using the third syntax, to use your dynamic procedures, all you have to do is run the procedure via a direct call to the Procedure variable. A procedure that is dynamically compiled can take parameters. Example:
MonCodeSource is string = [
PROCÉDURE AdditionneXetY(aa, bb)
RENVOYER aa + bb
]
MaProc is procédure = Compile(MonCodeSource)
Trace(MaProc(2, 3))
Dynamic code Structures can be used in dynamic code. Constants cannot be used in dynamic code (defined with the CONSTANT keyword). When using constants in a code, all the occurrences of the constants are replaced with their value during the compilation in the editor but the correspondence between the name of constants and their value is not "embedded" in the application. Therefore, the dynamic compilation cannot use the constants. Let's see two alternatives: - Solution 1: Use variables instead of constants.
The code:
becomes for example
- 2nd solution: In the string containing the code to be dynamically compiled, simply replace the name of the constant with its value:
sCode is string
sCode = [
Info(CST_Nom)
]
sCode = Replace(sCode, "CST_Nom", CST_Nom, WholeWord + IgnoreCase)
IF Compile("ProcDyn", sCode) <> "" THEN
Error("Erreur de compilation de la procédure dynamique: ", ...
ErrorInfo())
ELSE
WHEN EXCEPTION IN
ExecuteProcess("ProcDyn", trtProcedure)
DO
Error("Erreur d'exécution de la procédure dynamique: ", ...
ExceptionInfo())
END
END
Parameters of a query If the source code used for the dynamic compilation runs a query with parameters ( HExecuteQuery), the parameters expected by the query must necessarily be specified in HExecuteQuery. For example, you must use:
HExecuteQuery("MaRequête", hQueryDefault, "Dupont")
instead of:
MaRequête.NomClient = "Dupont"
HExecuteQuery("MaRequête", hQueryDefault)
Fields in HFSQL functions If the source code used for dynamic compilation executes an HFSQL function requiring the name of a field as a parameter, this name must be given in the form of a string enclosed in quotation marks. For example, the code to use for the HReadSeekFirst function is as follows:
HReadSeekFirst(Article, "CodeArticle", "ValeurRecherchée")
instead of:
HReadSeekFirst(Article, CodeArticle, "ValeurRecherchée")
Deploying of an application that uses Compile When deploying an application that usesCompile (when creating the executable or deploying the site), you must specify all the libraries of the WINDEV/WEBDEV/WINDEV Mobile framework used by the code that is dynamically compiled. Indeed, this code being compiled dynamically, WINDEV, WEBDEV and WINDEV Mobile do not detect the framework libraries used.
Related Examples:
|
Unit examples (WINDEV): Dynamic compilation
[ + ] Dynamic compilation of WLanguage code (stored in string format), execution of the dynamically generated procedure and process of possible runtime errors. The dynamic compilation is very useful when mathematical calculations are proposed to the end user for example.
|
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|