Giter Site home page Giter Site logo

node-sharp's Introduction

Loading .Net libraries in Node Js

This is a native nodejs extension which allows us to load dot-net assemblies, create objects and call methods on them.

Based upon

saary's node.net and v8sharp

The interface

Instantiate the module, load a dll and create a class. Currently it searches the active directory of the node process for assemblies.

var sharp = require('./Sharp').Sharp();
var stub = sharp.load('StubClass.dll');
var oc = stub.new('StubClass.Output');

Then the class exposes all methods for synchronous and asynchronous callbacks.

var list = oc.call('ReturnList');
// list = [17, 6, 81]  // some random set of integers
oc.async('ReturnObject', function(err, data){ 
    console.dir(data); 
});

There is also a .methods() call to return a list of callable method names.

You can call constructors and access static methods now too.

var constructed = stub.new('StubClass.Constructor', 1, "string");
var st = stub.static('StubClass.Static');
st.methods(); // list all static methods
var val = st.call('GetString');

What is it for?

Some of the linked resources from node.net, and research on the subject will tell you that C++/CLI has a performance hit in node over native javascript. This add-on uses System.Reflection for most operations, and additional marshaling into managed types which further add to the cost. So in short - your production system probably shouldn't rely on this path. It is intended for prototyping, when you already have classes to do what you want in C#. And the same advice as native modules apply - if your calls are long and async, then the costs of transforming the data are a smaller fraction.

Things that work

  • Listing dlls in the current directory
  • Loading managed assemblies (doesn't have to be .dll, loading Sharp.node has all the fun generated internals)
  • Call zero argument constructors
  • Calling methods where types can be converted

Speaking of - type conversion (mostly inherited from v8sharp)

  • string
  • int
  • date -> datetime
  • arrays -> typed arrays and lists
  • object -> dictionary of string and objects, and objects with public properties.
  • returned objects are returned

What's next?

From the previous list, there are many things which could be added to make the process a little more seamless.

  • access to fields
  • overloaded methods (honestly, this is hard when you're not a compiler)
  • also playing with node-gyp and npm to get the build install to be more streamlined.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.