0.0.1-fg • Published 8 years ago

mono.csharp v0.0.1-fg

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

CSharpScriptEngine 0.0.1

JOI Javascript Oriented Interoperability.

This module requires .NET 4.0 or higher. It works by starting my C# Evaluator based on Mono.CSharp from the Mono Runtime. Node will start a child REPL process. You send C# code by writing to the child's stdin and subscribe to its stdout.

Minimal

Minimal use. Define a string in C# environment.

const CSharpScriptEngine = require('mono.csharp')

let CSE = new CSharpScriptEngine()
CSE.on('data', data => {
	console.log(data)
})
CSE.write('string MyName = "Jochem Stoel";')

Less Minimal

Load assemblies programatically and inhabit their namespace.

/* IPC using CSE */
let CSE = new CSharpScriptEngine()

/* handle however you feel */
function handle(data) {
	console.log(data)
}

/* sends "using $namespace" to process stdin */
CSE.using('System')
CSE.using('System.IO')
CSE.loadAssembly('MyFile.dll')
CSE.using('MyNamespace')

CSE.write('int x = 4;')

/* data is a console line */
CSE.on('data', data => {
	handle(data)
})

/* initiate dialog with user/client */
CSE.REPL()

Methods

Using(namespace)

using will simply prepend "using " before line and append ";" after line before sending to stdin. Basically CSE.using('System.IO') is the same as CSE.write('using System.IO;')

/* can be written as chain */
CSE.using('System').using('System.IO').using('System.Reflection') ...

/* suggested way */
CSE.using('System.IO')
	.using('System.Windows.Forms')
	.loadAssembly('MyLibrary.dll')
	.using('MyNameSpace')
	.using('MyNameSpace.SubSpace')
	.write('var MyObj = new MyObject();')
.done()

on(what, that)

wrap child_process.stdout.on to on()

write(line)

write line to CSE process stdin

REPL(void)

initiate REPL dialog with user/client

done nothing more to do

kill the child

Random technical details that could be useful

When CSE.exe evaluates an expression, it will stdout print "true" if it parsed correctly or "false" when you forget a semi colon or whatever. You can use this in your on.data event handler to determine what is happening.

If you want, you could prototype a method "getVar" that sends Console.WriteLine(varName.toString()) to the child process and then catch it with your handler function.

This package/module is experimental in general implementation as well as for myself. I strongly advise not to implement this in real world applications. However, if you do find a useful purpose for it or want to contribute, don't be shy and email me jochemstoel(@)gmail.com. That is after all why I publish these things in the first place.

Jochem Stoel

Involuntary public figure.

0.0.1-fg

8 years ago

0.0.1-e

8 years ago

0.0.1-d

8 years ago

0.0.1-c

8 years ago

0.0.1-b

8 years ago

0.0.1-a

8 years ago

0.0.1

8 years ago