Javascript Interpreter for C# (no dependencies)
#scripting #script-engine
|
11 年之前 | |
---|---|---|
.nuget | 11 年之前 | |
Jint | 11 年之前 | |
Jint.Benchmark | 11 年之前 | |
Jint.Repl | 11 年之前 | |
Jint.Tests | 11 年之前 | |
Jint.Tests.CommonScripts | 11 年之前 | |
Jint.Tests.Ecma | 11 年之前 | |
Jint.Tests.Scaffolding | 12 年之前 | |
.gitignore | 12 年之前 | |
CREDITS.txt | 11 年之前 | |
Jint.sln | 11 年之前 | |
Jint.sln.DotSettings | 11 年之前 | |
LICENSE.txt | 11 年之前 | |
README.md | 11 年之前 |
Jint is a Javascript interpreter for .NET. Jint doesn't compile Javascript to .NET bytecode and in this sense might be best suited for projects requiring to run relatively small scripts faster, or which need to run on different platforms. It's available on nuget at https://www.nuget.org/packages/Jint
This example defines a new value named log
pointing to Console.WriteLine
, then executes
a script calling log('Hello World!')
.
var engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine))
;
engine.Execute(@"
function hello() {
log("Hello World");
};
hello();
");
Here, the variable x
is set to 3
and x * x
is executed in JavaScript. The result is returned to .NET directly, in this case as a double
value 9
.
var square = new Engine()
.SetValue("x", 3) // define a new variable
.Execute("x * x") // execute a statement
.GetCompletionValue() // get the latest statement completion value
.ToObject() // converts the value to .NET
;
You can also directly pass POCOs or anonymous objects and use them from JavaScript. In this example for instance a new Person
instance is manipulated from JavaScript.
var p = new Person {
Name = "Mickey Mouse"
};
var engine = new Engine()
.SetValue("p", p)
.Execute("p.Name === 'Mickey Mouse')
;
If you need to pass a JavaScript callback to the CLR, then it will be converted to a Delegate
. You can also use it as a parameter of a CLR method from JavaScript.
var function = new Engine()
.Execute("function add(a, b) { return this + a + b; }");
.GetGlobalValue("add")
.ToObject() as Delegate;
var result = function.DynamicInvoke(
new JsValue("foo") /* thisArg */,
new JsValue[] { 1, "bar" } /* arguments */,
); // "foo1bar"
You can allow an engine to access any .NET class by configuring the engine instance like this:
var engine = new Engine(cfg => cfg.AllowClr())
Then you have access to the System
namespace as a global value. Here is how it's used in the context on the command line utility:
jint> var file = new System.IO.File('log.txt');
jint> file.WriteLine('Hello World !');
jint> file.Dispose();
And even create shortcuts to commong .NET methods
jint> var log = System.Console.WriteLine;
jint> log('Hello World !');
=> "Hello World !"
Loading custom assemblies dynamically can be done by using standard reflection System.Reflection.Assembly.Load
. You will also need
to assign local namespace the same way System
does it for you, by using importNamespace
:
var Foo = importNamespace('Foo');
var bar = new Foo.Bar();
log(bar.ToString());
IDictionary<string, object>
and dynamic)