Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Accessing globals/defines from MCompile 01 Dec 2022 12:21 #24601

  • fxm
  • fxm's Avatar
  • Topic Author


  • Posts: 50
  • Hi,

    Is there a way to compile a codeblock from a string at runtime that can access globals and defines?

    I've tried MCompile and creating an instance of XSharp.Runtime.MacroCompiler but can't seem to get it to work.

    Thanks,
    Fergus
    GLOBAL cGlobal := "Global" AS STRING
    DEFINE cDefine := "Define" AS STRING
    
    FUNCTION Start() AS VOID STRICT
    
    LOCAL compileTimeCodeblock AS CODEBLOCK
    LOCAL cbStr  AS STRING
    LOCAL runTimeCodeblock AS CODEBLOCK
    
    compileTimeCodeblock := {|| cGlobal } 
    Console.WriteLine(Eval(compileTimeCodeblock)) //Global
    
    cbStr := "{|| GetGlobal() }"
    runTimeCodeblock := MCompile(cbStr)
    Console.WriteLine(Eval(runTimeCodeblock)) //Global
    
    cbStr := "{|| cGlobal }" 
    runTimeCodeblock := MCompile(cbStr)  
    Console.WriteLine(Eval(runTimeCodeblock)) //Error - Variable does not exist
    
    Console.ReadKey()
    
    FUNCTION GetGlobal() AS STRING
    RETURN cGlobal
    

    Please Log in or Create an account to join the conversation.

    Accessing globals/defines from MCompile 01 Dec 2022 12:53 #24602

    • robert
    • robert's Avatar


  • Posts: 3286
  • Fergus,

    At this moment globals are not supported.
    We found some problems when we did this because there could be conflicts between field names in index expressions and global names.
    For example in a customer application there was a global "Test" and also a string field "Test" in a DBF.
    The DBF had an index expression "UPPER(TEST)" and the macro compiler was picking up the Test global (which was not a string in this case).
    This resulted in runtime errors that were very difficult to diagnose.

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Accessing globals/defines from MCompile 01 Dec 2022 15:11 #24604

    • fxm
    • fxm's Avatar
    • Topic Author


  • Posts: 50
  • Ok, thanks for the explanation Robert!

    Please Log in or Create an account to join the conversation.

    Accessing globals/defines from MCompile 08 Dec 2022 09:44 #24697

    • robert
    • robert's Avatar


  • Posts: 3286
  • Fergus,
    I think I may have found a way to support this.
    I will include that in the next build.
    The way it works is this:
    - The macrocompiler itself does not include support for Globals, to make sure that a global cannot interfere with field names during macro compilation
    - instead it sees the name and adds a call to __VarGet(), a runtime function that is used to find the value of either a field or a (private or public) memvar.
    - I have changed __VarGet() to check for a field first and when that does not exist then check for a global in the app
    - if the global does not exist then the original behavior is executed and the runtime checks for a memory variable

    This seems to work as intended.

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Accessing globals/defines from MCompile 10 Dec 2022 08:49 #24718

    • fxm
    • fxm's Avatar
    • Topic Author


  • Posts: 50
  • Fastastic, thanks Robert.

    I look forward to trying it whenever it's ready.

    Fergus

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1