Yaaf.FSharp.Scripting


Working with IFsiSessions

Creating sessions

ScriptHost.CreateNew() and ScriptHost.Create() provide several optional parameters to configure the scripting environment:

  • fsiObj: obj: The optional "fsi" object to use (defaults to using the one defined in FSharp.Compiler.Service.dll) You should have the same members as https://github.com/Microsoft/visualfsharp/blob/fsharp4/src/fsharp/fsiaux.fsi#L19 . All your members are called via reflection.
  • reportGlobal: bool: When true all error messages and outputs contain the complete output (instead of only the output of the last command). This only affects error messages. Defaults to false
  • outWriter: TextWriter: A custom TextWriter object where we additionally write the stdout of the executing code (for long running scripts).
  • fsiOutWriter: TextWriter: A custom TextWriter object where we additionally write the fsi stdout (for long running scripts).
  • errWriter: TextWriter: A custom TextWriter object where we additionally write the stderr of the executing code (for long running scripts).
  • fsiErrWriter: TextWriter: A custom TextWriter object where we additionally write the fsi stderr (for long running scripts).
  • preventStdOut: bool: A value indicating whether we should completly block the executing script from writing to the current stdout and stderr. Defaults to false.

You should use preventStdOut = true if you want to control the script output yourself. You can work with either the live output or with the return values. Return values will be constant no matter the configuration options!

Example usage ("Live Output"):

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open Yaaf.FSharp.Scripting

// Setup for all future interactions
use liveSession =
    try ScriptHost.CreateNew
          (preventStdOut = true,
           outWriter = ScriptHost.CreateForwardWriter (printfn "Script stdout: %s"),
           errWriter = ScriptHost.CreateForwardWriter (printfn "Script stderr: %s"))
    with :? FsiEvaluationException as e ->
        printf "FsiEvaluationSession could not be created."
        printf "%s" e.Result.Error.Merged
        reraise ()
liveSession.EvalInteraction """printf "Some test" """

The standard output is:

Script stdout: Some test

Example usage ("Return values"):

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
// Use the "WithOutput" members and work with the return type
use directSession =
    try ScriptHost.CreateNew(preventStdOut = true)
    with :? FsiEvaluationException as e ->
        printf "FsiEvaluationSession could not be created."
        printf "%s" e.Result.Error.Merged
        reraise ()
let v = directSession.EvalInteractionWithOutput """printfn "direct result" """
printfn "And We have: %s" v.Output.ScriptOutput

The standard output is:

And We have: direct result

The value v is:

{Output = {FsiOutput = "val it : unit = ()
";
           ScriptOutput = "direct result
";
           Merged = "direct result
val it : unit = ()
";};
 Error = {FsiOutput = "";
          ScriptOutput = "";
          Merged = "";};}

Example usage ("Use both"):

Note that you can use both systems at the same time as well (the return values are always available).

1: 
2: 
let b = liveSession.EvalInteractionWithOutput """printfn "both is possible" """
printfn "The returned result: %s" b.Output.ScriptOutput

The standard output is:

Script stdout: both is possible
Script stdout: 

The returned result: both is possible

The value b is:

{Output = {FsiOutput = "val it : unit = ()
";
           ScriptOutput = "both is possible
";
           Merged = "both is possible
val it : unit = ()
";};
 Error = {FsiOutput = "";
          ScriptOutput = "";
          Merged = "";};}
namespace Yaaf
namespace Yaaf.FSharp
namespace Yaaf.FSharp.Scripting
val liveSession : IFsiSession

Full name: IntroExamples.liveSession
type ScriptHost =
  private new : unit -> ScriptHost
  static member Create : opts:FsiOptions * ?fsiObj:obj * ?reportGlobal:bool * ?outWriter:TextWriter * ?fsiOutWriter:TextWriter * ?errWriter:TextWriter * ?fsiErrWriter:TextWriter * ?preventStdOut:bool -> IFsiSession
  static member CreateForwardWriter : f:(string -> unit) * ?revertRedirect:bool * ?removeNewLines:bool -> TextWriter
  static member CreateNew : ?defines:string list * ?fsiObj:obj * ?reportGlobal:bool * ?outWriter:TextWriter * ?fsiOutWriter:TextWriter * ?errWriter:TextWriter * ?fsiErrWriter:TextWriter * ?preventStdOut:bool -> IFsiSession

Full name: Yaaf.FSharp.Scripting.ScriptHost
static member ScriptHost.CreateNew : ?defines:string list * ?fsiObj:obj * ?reportGlobal:bool * ?outWriter:System.IO.TextWriter * ?fsiOutWriter:System.IO.TextWriter * ?errWriter:System.IO.TextWriter * ?fsiErrWriter:System.IO.TextWriter * ?preventStdOut:bool -> IFsiSession
static member ScriptHost.CreateForwardWriter : f:(string -> unit) * ?revertRedirect:bool * ?removeNewLines:bool -> System.IO.TextWriter
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
type FsiEvaluationException =
  inherit Exception
  new : info:SerializationInfo * context:StreamingContext -> FsiEvaluationException
  new : msg:string * input:string * args:string list option * result:InteractionResult * inner:Exception -> FsiEvaluationException
  val private result: InteractionResult
  val private input: string
  val private arguments: string list option
  override GetObjectData : info:SerializationInfo * StreamingContext -> unit
  override ToString : unit -> string
  member Input : string
  member Result : InteractionResult

Full name: Yaaf.FSharp.Scripting.FsiEvaluationException

--------------------
new : info:System.Runtime.Serialization.SerializationInfo * context:System.Runtime.Serialization.StreamingContext -> FsiEvaluationException
new : msg:string * input:string * args:string list option * result:InteractionResult * inner:System.Exception -> FsiEvaluationException
val e : FsiEvaluationException
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
property FsiEvaluationException.Result: InteractionResult
InteractionResult.Error: OutputData
OutputData.Merged: string
val reraise : unit -> 'T

Full name: Microsoft.FSharp.Core.Operators.reraise
member IFsiSession.EvalInteraction : s:string -> unit
val directSession : IFsiSession

Full name: IntroExamples.directSession
val v : InteractionResult

Full name: IntroExamples.v
abstract member IFsiSession.EvalInteractionWithOutput : string -> InteractionResult
InteractionResult.Output: OutputData
OutputData.ScriptOutput: string
val b : InteractionResult

Full name: IntroExamples.b
Fork me on GitHub