Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use V8 JavaScript engine in Xamarin Android

I have a Xamarin Forms application in which I have to call JavaScript and use its result. I've resolved it with JavaScriptCore on iOS and Chakra on Win10 (and I assume ChakraCore would work on Win8.1 as well), but I like to use the V8 JavaScript Engine on android. However I can't find how I can use it in Xamarin.

Is there even a way to use it?

like image 834
IThomas91 Avatar asked Jul 08 '16 08:07

IThomas91


People also ask

Can we use JavaScript in xamarin forms?

Summary. By integrating Jint, we can add JavaScript support into our Xamarin. Forms apps.

What is Chrome's V8 JavaScript engine?

Chrome V8 is a JavaScript engine, which means that it executes JavaScript code. Originally, JavaScript was written to be executed by web browsers. Chrome V8, or just V8, can execute JavaScript code either within or outside of a browser, which makes server-side scripting possible.

What version of JavaScript does V8 support?

Apps Script supports two JavaScript runtimes: the modern V8 runtime and an older one powered by Mozilla's Rhino JavaScript interpreter. The V8 runtime supports modern ECMAScript syntax and features. The V8 runtime documentation states: You can use modern ECMAScript syntax in scripts that are powered by the V8 runtime.


1 Answers

Disclaimer: I am the author.

https://github.com/web-atoms/xamarin-v8

Add NuGet Package

 <PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />

Code

  using(var context = new JSContext( /*Enable Debugging*/ true)) {

    // you can connect to dev tools by visiting url
     // devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9222/backend

  context["printf"] = context.CreateFunction(0, (c, a) => {
      // first parameter is context isself
      // second parameter is an array as IJSValue
        System.Diagnostics.Debug.WriteLine(a[0].ToString());
       return c.Undefined;
  });

   // script location is useful for debugging
   context.Evaluate(scriptText, scriptLocation);

}
like image 121
Akash Kava Avatar answered Oct 07 '22 21:10

Akash Kava