A fork of node-clr with precompiled Electron native modules and duck typing.
To build native modules for a new Electron version:
build.cmd electron.version.here
# npm install electron-clr
# node
> require('electron-clr').init();
> System.Console.WriteLine('Hello, {0}!', 'world');
'Hello, world!'
> var now = new System.DateTime(2013, 7, 1);
> now.ToString();
'2013/07/01 0:00:00'
> require('clr').init({ assemblies: [ 'System.Windows.Forms' ] });
> with (System.Windows.Forms) {
> var f = new Form();
>
> var p = new FlowLayoutPanel();
> f.Controls.Add(p);
>
> var t = new TextBox();
> t.Text = 'world';
> p.Controls.Add(t);
>
> var b = new Button();
> b.Text = 'Greet';
> b.Click.add(function (thiz, ea) {
> console.log('clicked');
> MessageBox.Show('Hello, ' + t.Text + '!');
> });
> p.Controls.Add(b);
>
> Application.Run(f);
> }
(running WinForm application)
This library is built and tested on following environment:
- Node.js v0.12.1
- .NET Framework 4.5
- Visual Studio 2013
- Node.js native module build environment
Initialize CLR rutime with given options. Returns global namespace
.
options
{Object}assemblies
{Array} - An array of assembly name (partial name, full name or absolute path to .dll/.exe). Defaults to['mscorlib', 'System', 'System.Core']
.global
{boolean} - iftrue
, CLR global namespace objects are injected into javascript global object. Defaults totrue
.
- {Object}
CLR namespace objects contain nested namespaces or types.
- {Function}
CLR type functions work as constructor for corresponding CLR types. The constructor returns wrapped CLR object.
var now = new System.DateTime(2013, 7, 1);
The code above invokes CLR constructor DateTime (Int32, Int32, Int32)
and returns {Object} that wraps DateTime
instance.
CLR type also contains static members.
var now = System.DateTime.Now;
The code above invokes CLR static property getter DateTime.Now
.
Bind generic parameters to type and returns bound CLR type.
var list = new (System.Collections.Generic.List.of(System.Int32))();
- {Object}
Javascript object that wraps CLR instance, which contains instance members.
var now = System.DateTime.Now;
now.ToString();
> '2013/07/01 0:00:00'
- {Function}
CLR methods can be invoked as Javascript function. Arguments and return value are marshalled as conventions below.
System.Console.WriteLine('Hello, {0}!', 'world');
> 'Hello, world!'
- {Getter/Setter}
CLR properties/fields are exposed as object's getter or setter function.
var now = System.DateTime.Now;
Get object's default indexed property
var a =
- {Object}
CLR events can be hooked by add
and remove
function.
Add javascript event handler to specified event.
handler
{Function}
Remove javascript event handler from event.
** This isn't working right now **
handler
{Function}
V8 => CLR:
null
orundefined
=>null
boolean
=>System.Boolean
number
=> Any numeric type orSystem.Double
string
=>System.String
orSystem.Char
or EnumsFunction
=> Any delegate type orSystem.Func<System.Object[], System.Object>
Array
=> Any type of array orSystem.Object[]
Buffer
=>System.Byte[]
object
=>System.Dynamic.ExpandoObject
CLR => V8:
null
=>null
System.Boolean
=>boolean
- Any numberic type excepts System.Decimal =>
number
System.String
orSystem.Char
=>string
- Any other types => CLR wrapped object
const implementedInterface = new IMyInterface({
MyFunc: () => 10,
set_Prop: value => {},
get_Prop: () => 12
});
You can use .NET threads. All Javascript callback functions are invoked in main event loop.
var t = new System.Threading.Thread(function () {
console.log('Hello, world!');
});
t.Start();
> 'Hello, world!' // will be invoked asynchronously, but in main thread
- Unit test
- Better marshaling
Object
=> class withDataContractAttribute
- handle cyclic reference
- New Event api, resembles to EventEmitter
- Prototype chain which reflects CLR inheritance
- Generic method
- cast
- valueOf (explicit conversion from wrapped CLR object to javascript types)
- Async invocation
- Compiler API