Skip to content

Problem compiling script #1002

@cpyrgas

Description

@cpyrgas

https://www.xsharp.eu/forum/private-product/2958-scripting-script-runasync-not-working-anymore

In the following code (and attached project), comping the small script seems to succeed, but when running it, nothing is printed on screen, so it looks like it does not get executed, or the compile code is empty(?). By removing the 2nd assignment line in the script (x := 456), then it all works as expected, printing 999,123,123.

USING LanguageService.CodeAnalysis.Scripting
USING LanguageService.CodeAnalysis.XSharp.Scripting 

FUNCTION Start( ) AS VOID
	VAR sb := System.Text.StringBuilder{} 
	sb:AppendLine("System.Console.WriteLine( 999 )")
	sb:AppendLine("local x := 123 AS OBJECT")                        
	sb:AppendLine("System.Console.WriteLine( x )")
	sb:AppendLine("x := 456") // removing this, the code works as expected, printing 123 twice
	sb:AppendLine("System.Console.WriteLine( x )")
	VAR script := GetCompiledScript(sb:ToString())
	VAR ct := System.Threading.CancellationToken{}
	VAR t := script:RunAsync(NULL, ct)
	t:Wait()
RETURN  

FUNCTION GetCompiledScript(source AS STRING) AS Script
    LOCAL scoptions AS ScriptOptions    
    scoptions := ScriptOptions.Default
    VAR allReferences := System.AppDomain.CurrentDomain:GetAssemblies()
    VAR references := System.Collections.Generic.List<System.Reflection.Assembly>{}
    FOREACH VAR ass IN allReferences
        IF String.IsNullOrEmpty(ass:Location) == FALSE
            IF ass:IsDynamic == FALSE
                references:Add(ass)
            ENDIF
        ENDIF
    NEXT
    scoptions := scoptions:WithReferences(references)
	LOCAL _script := XSharpScript.Create(source, scoptions, NULL, NULL) AS Script
    VAR ct := System.Threading.CancellationToken{}
	VAR errors := _script:Compile(ct)                                      
	? "Erors:", errors:Length
	?
RETURN _script

ScriptTest.zip

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions