V8 Bindings for Xamarin for Android
Though V8 is very heavy, you can try YantraJS
, very light weight JavaScript engine written in C#.
<PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />
Visual Studio > Tools > Android > Android Adb Command Prompt
adb forward tcp:9222 tcp:9222
If you want to change the default 9222 port, you can specify in the parameters.
using Xamarin.Android.V8;
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;
});
// JS : printf("a");
// script location is useful for debugging
context.Evaluate(scriptText, scriptLocation);
// Object.create() JavaScript Equivalent in c#
var obj = context["Object"].InvokeMethod("create");
As V8 Engine has its own representation of native types, you need to create them in order to pass them as parameters for JavaScript methods.
var jsString = context.CreateString("TEXT");
var jsNumber = context.CreateNumber(100);
context["console"].InvokeMethod("log", jsString);
context["console"].InvokeMethod("log", jsNumber);
// clr object
var s = "Akash";
context.EvaluateTemplate($"console.log({s})");
This method serializes every clr object input as IJSValue by doing Wrap
serialization. You can however serialize and then send object as parameter to templated string. It will store all object in global and evaluate JavaScript, you do not have to worry about the escape as each input is serialized and they are substituted as parameter to placeholders.
When you use method context.Convert
method to automatically create native JS values from native types, it will only wrap C# custom object, you cannot call any method or access property from JavaScript on wrapped object. This is done to improve performance. So when you pass C# objects in and out, engine will not create methods and properties on them.
In order to access methods and properties of C# object, you have to serialize them.
// you can access all properties, no methods
var jsDictObject = context.Marshal( customClrObject , SerializationMode.Copy);
// you can access all properties and invoke method as well
var jsClrObject = context.Marshal( customClrObject , SerializationMode.Reference);
This method will create a deep copy of CLR Object as dictionary which you can easily access inside JavaScript code. This method will fail if there are self referencing objects in the object graph. This limitation may be removed in future, but right now it will throw an exception.
This method is also very slow as deep copy operation will take more time.
Deserialization will also be slow as it will completely construct new object with all properties.
Keeps reference along with serialization, every property is serialized as getter/setter, upon deserialization, same object will be returned.
This method is useful for self referencing objects, but this may cause memory leak if you keep reference in JavaScript and JavaScript garbage collector fails to dispose object.
Deserialization is faster as it simply returns referenced object.
Same as Reference but it only keeps weak reference, you will get object disposed if you try to access object in JavaScript and it is disposed in CLR. CLR is very aggressive while disposing objects, so this may not work if you do not keep reference in CLR. This is also recommended method as it will avoid memory leaks.
This is default serialization method for any object. Object will simply be wrapped and no methods/properties are exposed.
- https://kerry.lothrop.de/c-libraries/
- https://devblogs.microsoft.com/cppblog/developing-xamarin-android-native-applications/
- https://devblogs.microsoft.com/wp-content/uploads/sites/9/2019/02/XamarinNativeExample.zip
- https://github.com/rjamesnw/v8dotnet/blob/master/Source/V8.NET-Proxy/ProxyTypes.h
- https://devblogs.microsoft.com/cppblog/android-and-ios-development-with-c-in-visual-studio/
- https://github.com/lothrop/XamarinNative
- https://github.com/xamcat/mobcat-samples/tree/master/cpp_with_xamarin
- https://stackoverflow.com/questions/31541451/create-shared-library-from-cpp-files-and-static-library-with-g
- https://hyperandroid.com/2020/02/12/compile-v8-arm-arm64-ia32/
- https://hyperandroid.com/2020/02/12/android-v8-embedding-guide/
- https://hyperandroid.com/2020/02/12/v8-inspector-from-an-embedder-standpoint/