2
0

Javascript Interpreter for C# (no dependencies)
#scripting #script-engine

Sebastien Ros 83a32d6bb7 #95: Fixing interop calls returning incompatible types 11 жил өмнө
.nuget 9d9f18d68f Including xUnit runners package 11 жил өмнө
Jint 83a32d6bb7 #95: Fixing interop calls returning incompatible types 11 жил өмнө
Jint.Benchmark faed32717c Improving parser performance 11 жил өмнө
Jint.Repl dae18eb8a3 Bumping version number 11 жил өмнө
Jint.Tests 83a32d6bb7 #95: Fixing interop calls returning incompatible types 11 жил өмнө
Jint.Tests.CommonScripts ee506b1f3d Updating sunspider scripts location 11 жил өмнө
Jint.Tests.Ecma 136bcbdcf5 Merge pull request #65 from honestegg/fix-date-functions 11 жил өмнө
Jint.Tests.Scaffolding b6d60818da Adding Ecma 262 tests 12 жил өмнө
.gitignore 8c986316c5 Ignoring VS14 ide files 11 жил өмнө
CREDITS.txt 5f713ad215 Using FastDtoa to serialize numbers 11 жил өмнө
Jint.sln faed32717c Improving parser performance 11 жил өмнө
Jint.sln.DotSettings 478662033e Refactoring Date to reflect specifications 11 жил өмнө
LICENSE.txt f53137698a Adding license information 11 жил өмнө
README.md 1cbcf7ac07 Update README.md 11 жил өмнө

README.md

Build status

Jint

Jint is a Javascript interpreter for .NET which provides full ECMA 5.1 compliance and can run on any .NET plaftform. Because it doesn' generate any .NET bytecode nor use the DLR it runs relatively small scripts faster. It's available as a PCL on Nuget at https://www.nuget.org/packages/Jint.

Features

Examples

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')
        ;

You can invoke JavaScript function reference

    var add = new Engine()
        .Execute("function add(a, b) { return a + b; }");
        .GetValue("add")
        ;

    add.Invoke(1, 2); // -> 3

or directly by name

    var engine = new Engine()
        .Execute("function add(a, b) { return a + b; }")
        ;

    engine.Invoke("add", 1, 2); // -> 3

Accessing .NET assemblies and classes

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 !"

When allowing the CLR, you can optionally pass custom assemblies to load types from.

    var engine = new Engine(cfg => cfg
        .AllowClr(typeof(Bar).Assembly)
    );

and then to assign local namespaces the same way System does it for you, use importNamespace

    jint> var Foo = importNamespace('Foo');
    jint> var bar = new Foo.Bar();
    jint> log(bar.ToString());

Generic types are also supported. Here is how to declare, instantiate and use a List<string>:

    jint> var ListOfString = System.Collections.Generic.List(System.String);
    jint> var list = new ListOfString();
    jint> list.Add('foo');
    jint> list.Add(1); // automatically converted to String
    jint> list.Count; // 2

Implemented features:

  • ECMAScript 5.1 test suite (http://test262.ecmascript.org/)
  • Manipulate CLR objects from JavaScript, including:
    • Single values
    • Objects
    • Properties
    • Methods
    • Delegates
    • Anonymous objects
  • Convert JavaScript values to CLR objects
    • Primitive values
    • Object -> expando objects (IDictionary<string, object> and dynamic)
    • Array -> object[]
    • Date -> DateTime
    • number -> double
    • string -> string
    • boolean -> bool
    • Regex -> RegExp
    • Function -> Delegate

Continuous Integration kindly provided by