0

I'm trying to execute JavaScript in a C# program, and I'm trying to determine:

Would it be better to create a state machine for JavaScript, or should it be compiled into MSIL before executing it?

I'm really looking for techniques rather than solutions. Some attempt to compile the JavaScript, there by treating it as a programming language, others use a state machine and treat it as it was meant to be : a scripting language.

Any documentation on the methods used for executing is welcome also.

Cheeso
  • 180,104
  • 92
  • 446
  • 681
  • See existing discussion: http://stackoverflow.com/questions/2137320/javascript-engines-advantages – Diodeus - James MacFarlane Aug 19 '11 at 18:52
  • @Diodeus, thanks; i didn't know what to search for. –  Aug 19 '11 at 18:56
  • what do you mean by portraying "create a state machine" as an alternative to "compile Javascript"? When you say "create a state machine" do you mean to imply "create an interpreter"? If so, there's no advantage to you building your own js interpreter or execution engine. There are good resources that already exist - choose and use one of them. – Cheeso Aug 19 '11 at 18:58
  • @Chesso, the other resources either can't cope with bad JavaScript or are incompatible with my solution. Also there are so many that evaluating all of them is getting me nowhere :( –  Aug 19 '11 at 19:05
  • a simple `try...catch` can easily deal with bad Javascript. If you are having trouble evaluating them, maybe you should sharpen the description of your requirements, so that people may be able to offer more pointed recommendations. – Cheeso Aug 19 '11 at 19:12

2 Answers2

0

The easiest way to run JS in a C# program is to use one of the existing .Net JS runtimes: IronJS, Jurassic, and Jint. If you're interested in adding another .Net-based interpreter to the mix, examining their code is where I'd start; If you're embedding JS in your program then using a preexisting one is probably the way to go.

Dan Davies Brackett
  • 9,163
  • 2
  • 29
  • 50
  • I can't look at any open source GNU code, since the solution is closed source. –  Aug 22 '11 at 19:29
0

You haven't explained your scenario - how much javascript, how often does it get executed, how often does it change, how closely integrated it needs to be with the C# logic, and so on.

If it rarely changes, then it may be smart to compile it into MSIL via one of the alternatives. If it changes "constantly" then it might make sense to stand up a JS compiler and send it JS as necessary. It's something like a REPL for Javascript, that you'd use programmatically. Node.js offers a server that can do REPL operations; it could listen on a port, you send it JS to evaluate, and it sends back the result. There's a free w3sockets component that could be used from cscript.exe to do something similar: for example you could build a "host" Javascript shell that listens on a socket, executes the js it receives, then sends back the result.

If it needs to be closely integrated with the C# code, then you'll have to be smarter about this. One way to do it might be to host the JS in a Windows Script Component and have the C# code invoke the JS logic via COM. You can also do the converse - have the JS host invoke the C# layer via COM to gather input and deliver results.


EDIT
A better way is to use the IActiveScript stuff - this is the official way Microsoft makes it possible to host scripting within an application. There's a winforms example published here.

OR better, see parse and execute JS by C#

Using that code I put together a demonstration of how to parse and invoke Javascript from a .NET app, using IActiveScript.

enter image description here

You can download a VS2010 project for this app here.

Community
  • 1
  • 1
Cheeso
  • 180,104
  • 92
  • 446
  • 681
  • The JavaScript is written by the end user, and can be anything. I've seen the javascript compiler's that are out there. basically if there is an error in the javascript the entire script will fail to compile. –  Aug 22 '11 at 19:32