6

Long time reader, very first question. fsi.exe is a .NET executable and therefore contains its own assembly complete with all the yummy methods and whatnot fsi uses to execute F# scripts.

Looking at the assembly in .NET Reflector (pick your class, but Shell is the best example) reveals a bunch of garbage* names that look like decorated C++ functions (say, from Dependency Walker). Incidentally and slightly beside the point, F# assemblies compile in much the same way, with lots of garbage* names, which leads me to think fsi.exe was written in F#, perhaps as a proof-of-usability?

Anyway, here's my question: has anyone delved into fsi.exe and figured out how to embed it into a .NET application? Because I'd like to use F# as a scripting language, but programs compile to (surprise) programs, and scripts must be executed by fsi.exe, which is unacceptable in my domain (I need a persistent VM). I don't expect a how-to guide on using fsi.exe, but I'm curious to know if anyone has played with it and, if so, what have you discovered about how it works?

Thanks for your time.

* Garbage at a casual glance. Obviously they are formatted this particular way for a particular reason that is beneath the hood.

Phil H
  • 19,928
  • 7
  • 68
  • 105
Tom
  • 121
  • 8

2 Answers2

7

As far as I know, fsi.exe doesn't expose any API that you could use to embed it in your application (e.g. to implement scripting). I think there are essentially two options:

  • If you want to use existing fsi.exe directly, the only way is to start it using .NET Process class and communicate with it somehow. This should be doable - you can send commands to the process using standard input. To read output back, you can use standard output, but this gives you only limited information. It probably would be possible to use .NET Remoting to communicate between fsi.exe and your application (e.g. your objects loaded in script context in FSI would send information to your application via Remoting).

  • Another alternative is to use the open-source release of F# and modify fsi.exe to expose all information you need as an API. This requires more effort in order to understand how fsi.exe works, but it should be doable. You probably don't need that many additions - the state maintained by FSI contains things like global variables.

Regarding the license - you probably cannot use fsi.exe distributed with Visual Studio. I'm not sure about fsi.exe that comes with the CTP release. Compiling it from source (available here) will be definitely fine (because it has open-source release).

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Thanks for the input, Tomas. After looking at the F# power pack, I indeed see that it was written in F#, which is delightfully ironic. I'm in the process of poring over the source. This is probably too advanced for me to adapt, but it's interesting nonetheless. I'd love to rate you up for the comprehensive response, but I don't have the rep for it. – Tom Jan 14 '11 at 20:14
  • Is it possible to somehow embed it in another AppDomain? That way you won't have the overhead of another .net Process and maybe easier to manage architecturally. – ashley aberneithy Jul 25 '11 at 12:47
  • @ashley - Unfortunately no, at the moment it has to be started as a separate process. – Tomas Petricek Jul 26 '11 at 18:34
  • Thanks Tomas, do you know if there are plans to make this change? That'll save me making a change to the FSI source and recompiling – ashley aberneithy Aug 03 '11 at 10:04
  • @ashley - It may change in the future. The www.tryfsharp.org web site uses something like that (embedded in Silverlight), so some work on this has been done. But I don't know when (and if) is this going to be released. – Tomas Petricek Aug 03 '11 at 12:49
  • 1
    Any update on the status of an eval in F#? Any new possibilities other than running in a separate Process? – Doug Blank Feb 25 '12 at 04:19
1

There's some working code here:

http://www.codeproject.com/Articles/241577/Embedded-scripting-using-Fsharp

Chui Tey
  • 5,436
  • 2
  • 35
  • 44