Latest V8 Engine : from NodeJs v13.5.0
V8 js engine with C# (in-process), => Espresso-VE
NodeJS engine with C# (in-process), => Espresso-ND
Espresso (from vroomjs ) is a bridge between the .NET CLR (think C# or F#) and the V8 Javascript engine that uses P/Invoke and a thin C layer (libespr).
Now, Espresso can run on .net20+ and .netcore/.netstandard
so We can run the engine on Windows7+, macOS, and Linux(tested with Ubuntu 16)
Windows7
macOS, x64
Linux, Ubuntu 16, x64
With Espresso it is possible to execute arbitrary javascript code and get the
result as a managed primitive type (for integers, numbers, strings, dates and
arrays of primitive types) or as a JsObject
wrapper that allows to
dynamically access properties and call functions on Javascript objects.
Each JsEngine
is an isolated V8 context and all objects allocated on the
Javascript side are persistent over multiple calls. It is possible to set and
get global variables. Variable values can be primitive types, CLR objects or
JsObjects
wrapping Javascript objects. CLR instances are kept alive as long
as used in Javascript code (so it isn't required to track them in client code:
they won't be garbage collected as long as references on the V8 side) and it is
possible to access their properties and call methods from JS code.
Execute some Javascript:
using (var js = new JsEngine())
{
var x = (int)js.Execute("3.14159+2.71828");
Console.WriteLine(x); // prints 5.85987
}
Create and return a Javascript object, then call a method on it:
using (JsContext js = jsEngine.CreateContext())
{
var t = new TestClass();
js.SetVariableFromAny("o", t);
js.Execute("var x = { nested: o }; x.nested.Int32Property = 42");
var x = js.GetVariable("x") as JsObject;
Assert.That(x["nested"], Is.EqualTo(t));
Assert.That(t.Int32Property, Is.EqualTo(42));
}
Access properties and call methods on CLR objects from Javascript:
class TestMe1
{
public int B()
{
return 100;
}
public bool C()
{
return true;
}
}
using (JsEngine engine = new JsEngine())
using (JsContext ctx = engine.CreateContext())
{
GC.Collect();
System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
stwatch.Start();
TestMe1 t1 = new TestMe1();
for (int i = 2000; i >= 0; --i)
{
ctx.SetVariableFromAny("x", t1);
object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()");
}
stwatch.Stop();
Console.WriteLine("met2 managed reflection:" + stwatch.ElapsedMilliseconds.ToString());
//Assert.That(result, Is.EqualTo(100));
}
Espresso-ND
Espresso-ND is special edition of the Espresso, It is NodeJS in dll form + Espresso Bridge code,
so you can run NodeJS app in-process with .NET Code
see example, run nodejs http server
Espresso-ND with Http2
NodeJs 9 has built-in http2 server.
so, test it :)
pic 1: http2 protocol example, on node v9.3.0
pic2: (1) console screen, (2) http2 server says 'Hello World' to Firefox
see how to build it at #30
License:
MIT, 2013, Federico Di Gregorio [email protected], https://github.com/fogzot/vroomjs
MIT, 2015-2019, WinterDev