fsi.exe Assembly: Anyone know how to embed it?
Asked Answered
C

2

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.

Cheekpiece answered 9/1, 2011 at 1:26 Comment(5)
So basically, you want eval?Baucom
I'm sort of wondering, does F#'s license allow embedding fsi in the first place?Rossini
@Juliet: Why would it be a licensing issue?Sciuroid
Not anymore, it is open source no with an Apache license. Which is the solution, you can use the source to embed it.Macaroon
@Martinho: No, I want a scripting engine.Cheekpiece
S
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).

Superordinate answered 9/1, 2011 at 9:21 Comment(6)
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.Cheekpiece
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.Rosabella
@ashley - Unfortunately no, at the moment it has to be started as a separate process.Superordinate
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 recompilingRosabella
@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.Superordinate
Any update on the status of an eval in F#? Any new possibilities other than running in a separate Process?Martens
B
1

There's some working code here:

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

Blennioid answered 20/9, 2012 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.