views:

2621

answers:

8

I'm building a .NET 3.5 application and have the need to evaluate JS code on the server - basically a user provided rule set that can work within a browser or on the server. Managed JS is not an option, because the JS code would be provided at runtime. Aptana's Jaxer is also not an option. So I was looking into using a build of the V8 engine within my app.

I built the source successfully into a DLL, but that DLL is not not a managed library and is not COM either. V8 is just plain C++.

Any ideas as to how to interop with this type of DLL in C#? Also, I'm open to other suggestions for SpiderMonkey or another JS engine.

Thanks in advance.

UPDATE:

I was able to use Ryan's solution. I just updated the references to the build for the latest from trunk. It worked great. Thanks Ryan.

+15  A: 

I realize that this may not be an exact answer to your question, but I figured I would put my 2 cents worth in as I doubt to many people have tried this.

I got it to work by created a managed wrapper using mixed mode C++. There are other ways to do it, but I was going to attempt to make a full wrapper that could be used from any .Net language.

Getting the lib to compile in such a way that it could be included in a mixed mode project was a little bit of a challenge. I had to modify the runtime library (in the SConstruct file) used to /MD and /MDd so that it would be compatible with the /clr switch.

So far I have only simple scripts running as I have not implemented callbacks, custom methods, objects and such.

Here is a quick sample of what the usage looks like for one of my test apps:

V8DotNet.Shell shell = new V8DotNet.Shell();

shell.ExecuteScript(@"print('V8 version is: ' + version());");

It runs more complicated scripts like a base64 encoder fine as well. But for now I can only add custom items from the c++ side.

I am willing to provide more information + code if anyone is interested as I may not ever pick this project back up. But, I'm afraid it way to much code to go into a post here so we would have to find some other medium like google code or codePlex.

Edit:


OK, I've uploaded the code. I do have to put a disclaimer on this: The project is very early and I am an amateur at C++ at best so don't get your hopes up to much. Also, this project was created/done just after chrome was released so the version of v8 included may be old.

That said, here it is: http://ryanscook.com/Files/V8-DotNet.zip (21.5 MB)

In the package you'll find the following items of interest:

V8Net-Library\V8.Net\V8.Net.sln - This is the solution that has the managed C++ wrapper proj and a C# console app for testing.

Dependencies\V8 - This is my V8 code that I used to build the V8 lib.

Hope it helps!

Ryan Cook
"I had to modify the runtime library (in the SConstruct file) used to /MD and /MDd so that it would be compatible with the /clr switch." - not a very hard challenge! :)
leppie
Well finding our what had to be done was. The typing is always the easy part. I guess my lack of experience with C++ added to the frustration a bit.
Ryan Cook
By all means, do post it!
Jason Bunting
So now that you have it running, can you interop with it at all? (eg: have the javascript code manipulate an external .NET object somehow?)
Orion Edwards
Orion: Not yet. I only put a couple hours into the wrapper as a test and never went back to complete it.
Ryan Cook
Thanks for the code - you are very generous!!
NathanD
Ryan: See my update above. Your code works fine with the build from the latest from trunk. Thanks.
NathanD
A: 

From what I hear compiling it with IJW (Managed C++) should just work - but I may be really wrong, I have never touch MC++.

Jonathan C Dickinson
A: 

Ryan: Thanks for the help. I'd like to do a build with the /clr switch - like what you did. I have no experience whatsoever with Scons. Mind letting me know in more detail the changes you made to SConstruct file. Particularly, where you specify the /clr switch. Also, It looks like for a shared lib, /MD is already used.

Thanks in advance for your help.

NathanD
To be honest I can't remember the exact details of my v8 build. The /clr is in the wrapper library. Basically I had to create a slightly modified v8.lib so I could link it to my mixed mode dll. I will try to find time today to post the projects somewhere for you to download.
Ryan Cook
A: 

Microsoft are building a real javascript - not "JScript" - runtime (along with IronPython, IronRuby, and VB10) on the CLR using the DLR, but I can't find any downloads or content for it. Perhaps this will arrive with C# 4?

Orion Edwards
Yeah, I had heard about the JS DLR implementation a while back, so I first looked into that. But, unfortunately MSFT doesn't seem to have released anything for JS yet, just IronPython. Bummer.
NathanD
Managed JavaScript on the DLR is dead. See this answer: http://code.google.com/apis/v8/design.html
James Hugard
I don't understand how the V8 design docs have anything to do with Javascript on the DLR? The point of javascript on the DLR wouldn't be just for pure speed (although that'd be a nice bonus), but to interop cleanly with the existing .NET ecosystem, which V8 doesn't do
Orion Edwards
A: 

Ryan, Your code was great but I need a way to add a FunctionTemplate that will call a C# function. On you demo you created the print function but it was declared as a native C++ function.

Thanks in advance for your help

A: 

Hey Ryan, is there anyway you can repost your project? It doesn't seem to be available any longer. Thanks.

Dan
I just checked and it seems to be up.
Ryan Cook
+4  A: 

Check out v8sharp. It supports executing JS inside of a .NET application. It also allows you to register .NET types with the v8 engine so that your JS code can interact with your .NET code. I am in the process of adding support for hooking function/delegate support.

Shaun
@Shaun -- this is great! thanks! can we have more examples please?
shawndumas
v8sharp is greate,I have used in my project.
guaike
+7  A: 

You can try Javascript .NET:

http://javascriptdotnet.codeplex.com/

It lets you create a V8 context from .NET and register CLI objects in it so you can manipulate them and call members from the Javascript code. It compiles the Javascript at runtime.

Check it out.

Michel Boissé