Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

javascript - Referencing Google's V8 engine from a .NET app

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 too 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-LibraryV8.NetV8.Net.sln - This is the solution that has the managed C++ wrapper proj and a C# console app for testing.

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

Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...