Skip to content

JohnMasen/Espresso

 
 

Repository files navigation

Espresso / Espresso-VE / Espresso-ND

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.

Current V8 Engine => We use V8 from node v7.10.0

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 (var js = new JsEngine()) {
		// Create a global variable on the JS side.
		js.Execute("var x = {'answer':42, 'tellme':function (x) { return x+' '+this.answer; }}");
		// Get it and use "dynamic" to tell the compiler to use runtime binding.
		dynamic x = js.GetVariable("x");
		// Call the method and print the result. This will print:
		// "What is the answer to ...? 42"
		Console.WriteLine(x.tellme("What is the answer to ...?"));
	}

Access properties and call methods on CLR objects from Javascript:

	class Test
	{
		public int Value { get; set; }
		public void PrintValue(string msg)
		{
			Console.WriteLine(msg+" "+Value);
		}
	}
	
	using (var js = new JsEngine()) {
		js.SetVariable("m", new Test());
		// Sets the property from Javascript.
		js.Execute("m.Value = 42");
		// Call a method on the CLR object from Javascript. This prints:
		// "And the answer is (again!): 42"
		js.Execute("m.PrintValue('And the answer is (again!):')");
	}

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 slide18 slide19

see how to build it at prepare/Espresso#30

About

☕ V8 to .Net Bridge, Bridge your C# soul to the power of V8 Javascript engine (V8 / NodeJs 8.1.0)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 65.3%
  • C# 24.4%
  • JavaScript 10.3%