NodeRT
Nadav Bar
Microsoft R&D
Who am I?
Nadav Bar, Microsoft R&D
github: https://github.com/nadavbar
blog: www.nadavos.com
email: nadavbar@gmail.com
Node & Microsoft
-
Node is fully compatible with windows (as of July 2011)
-
Microsoft actively contributes to node.js source code
-
Azure websites have full support for node.js
Background - WinRT APIs
- Windows 8 introduced a new set of APIs in order to replace the old Win32 APIs – WinRT (Windows Runtime)
- Better architecture: object oriented, async. Very in line with node.js methodologies.
- Exposes OS & device capabilities: sensors, location, media, etc..
- Cross device: same code can target PC, Tablet, Phone and different CPU architectures.
- Same set of APIs is available across C++, C#, JavaScript.
Background - WinRT APIs
WinRT in node.js
Great! So can we use it node.js?
Nope, JavaScript bindings only work for Windows store applications!
But…we can write an addon in c++ and expose it to node
Native node addon
Text
=
Winrt in Node.js – 1st try
Suppose we want to access the accelerometer readings:
Winrt in Node.js – 1st try
Ok, cool – but what about the light sensor?
Winrt in Node.js – 1st try
- Well, there are a bunch of other sensors…and APIs..
- Things started to get pretty repetitive
- Class names and types will be different, but the same flow is used every time:
- Convert input parameters from JavaScript to c++
- Execute WinRT method
- Convert the result from c++ to JavaScript
There must be a better way!
Winrt in Node.js – 2nd try
- WinRT comes with a set of metadata files, which describe the API.
- We can use c# to load the metadata files using reflection and iterate over the API programmatically
- c#?! But we want JavaScript, and we are already using c++…
Winrt in Node.js – 2nd try
- WinRT comes with a set of metadata files, which describe the API.
- We can use c# load the metadata files using reflection and iterate over the API programmatically
- c#?! But we want JavaScript, and we are already using c++…
NodeRT - Demo #1
NodeRT – WinRT module generator
- Create one-to-one mappings between the WinRT classes and JavaScript objects
- Use a web templating engine – but render c++ code instead of HTML
- We use RAZOR, the open source templating engine behind ASP.NET
- We can use c# constructs inside the templates, easily access the metadata
- The output is a c++ source file that compiles into a node native addon.
NodeRT - Template Example
NodeRT – WinRT module generator
- Generated modules expose the same API as the WinRT ones
- Support classes, methods, properties, events
- Each namespace compiles into a separate module
- Reflection is only done during code generation
- Actual calls into WinRT are lightweight and made on-demand
WinRT APIs + NodeRT
Streams Support
- WinRT has its own set of streams
- We wanted to provide node.js style “stream programming” for WinRT streams
- nodert-streams module, available on npm
Native node addon
Demo #2
Thank you!
Questions?
Node.js everywhere
By Nadav Bar
Node.js everywhere
- 2,066