开发者

How can I use Google Chrome's V8 JavaScript interpreter from Delphi?

I'd like to embed the V8 JavaScript interpreter that ships with Google Chrome within my Delphi application. I'm aware of the chromium embedded open-source project from Google, bu开发者_如何转开发t I'd like to know if anyone was aware of any Pascal/Delphi wrappers?

There is an example project included with the zip file on the site I linked, which is written in C++. If nothing else, I'll slowly and painfully work to convert it.

UPDATE:

I just want to embed the V8 JavaScript interpreter, not the Chromium browser.


The most ideal solution would be to create a wrapper, preferably which consumes the original source unmodified, and compile that wrapper to an OBJ file (using C++) which then is linked into Delphi, where another "wrapper" exposes the engine via a more standard object pascal syntax. This approach would then allow for changes in the engine without the need for a complete conversion each time new functionality or additional performance is added. The only disadvantage of this approach is that there will be some performance lost while navigating the layers...but I would expect it to be minimal.


For the record: hgourvest has posted Delphi Chromium Embedded to Google Code.

Edit 2013-01-15:

Another project by the same author this time wrapping CEF3: DCEF3


If it is in fact Chrome as a browser you want to embed into your application, you should check out Google Chrome Frame, it exposes COM interfaces, primarily to integrate into Internet Explorer, but in theory we should be able to access them also.

(I'm not sure because I would like to have a go at this myself, but it's on a (long) list of really neat things to try when I get around to them.) Update: I've had a quick go at it, got a "No interface supported" error, and posted it here.


  • If you're using Prism, read this: http://blogs.remobjects.com/blogs/ck/2010/02/23/p1175. RemObjects made a free and open source scripting engine that supports ECMA Script (JavaScript) and PascalScript. It's all pascal code, but it depends on DotNet (RemObjects wrote it in Prism of course).

  • There's a V8 wrapper for Python. Might be interesting to see how they pulled it off.

  • There's a V8 Swig script here, but getting Swig to work with Delphi might be a challenge on its own: http://v8.googlecode.com/issues/attachment?aid=7578402775385555895&name=jav8.i

  • You can use the JavaScript engine from FireFox from within your Delphi application with Delphi / Kylix <-> SpiderMonkey Bridge


I've been using the SpiderMonkey bridge also, without any problems. Runs reasonably fast, without a huge footprint, and have had no Unicode issues yet!


Start from Jun 1, 2016, we have v8delphiwrapper, kudos to the developer @zolagiggszhou. And I'd like to show you some code example:

Run js code and return result as string:

Memo2.Text := FEngine.eval(Memo1.Text);

Accessing Delphi object from js:

1 - Assuming you have a Delphi class like this:

  TJsAccessableClass = class
  public
    function add(a,b: Double): Double;
    function httpEncode(const s: string): string;
  end;

2 - You register it with the v8 js engine:

  FObjectTemplate2 := FEngine.RegisterRttiClass(TJsAccessableClass);
  FJsAccessableObject := FObjectTemplate2.CreateInstance(TJsAccessableClass.Create);
  Fv8GlobalObject.SetObject('delphiObj', FJsAccessableObject);

3 - Now you can call your Delphi method from js:

var s = delphiObj.httpEncode('/~!f234');

Very cool! More example please check v8delphiwrapper sample project

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜