Skip to content

☕ Bridge your C# soul to the power of Javascript (V8 Javascript Engine / NodeJs)

License

Notifications You must be signed in to change notification settings

Oceanswave/Espresso-1

 
 

Repository files navigation

Espresso / Espresso-VE / Espresso-ND


Latest V8 Engine : from NodeJs v11.12.0

(for NodeJS 10.15.3, visit => https://github.com/prepare/Espresso/tree/v_10_15_3)


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

11

12

13


macOS, x64

26

27


Linux, Ubuntu 16, x64

20

21

23


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.

Examples

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

esprnd0 esprnd


Espresso-ND with Http2

NodeJs 9 has built-in http2 server.

so, test it :)

node_9_3

pic 1: http2 protocol example, on node v9.3.0


node_9_3_1

pic2: (1) console screen, (2) http2 server says 'Hello World' to Firefox


see how to build it at prepare#30


License:

MIT, 2013, Federico Di Gregorio fog@initd.org, https://github.com/fogzot/vroomjs

MIT, 2015-2019, WinterDev

About

☕ Bridge your C# soul to the power of Javascript (V8 Javascript Engine / NodeJs)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 74.8%
  • C# 12.8%
  • JavaScript 12.4%