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

后端 未结 6 2035
孤城傲影
孤城傲影 2020-12-23 22:26

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 Goo

6条回答
  •  时光取名叫无心
    2020-12-23 23:23

    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

提交回复
热议问题