Using third party js libraries with Jint

Uchitha

I'm working on a feature where user defined, anonymous, javascript functions retrieved from a database need to be executed server side in the context of ASP.Net application.

I'm evaluating Jint for this purpose (Latest version from NuGet). I have been able to run functions that do basic operations and return values without an issue as below.

    public void Do()
    {
        var jint = new Engine();
        var add = jint.Execute(@"var f = " + GetJsFunction()).GetValue("f");
        var value = add.Invoke(5, 4);
        Console.Write("Result: " + value);
    }

    private string GetJsFunction()
    {
        return "function (x,y) {" +
               "    return x+y;" +
               "}";
    }

My question is whether Jint facilitates the execution of javascript functions which uses third party libraries like lodash? If so, how would I go about making the Jint engine aware of it (i.e third party library)?

An example would be the execution of following function.

  private string GetFunction()
    {
        return "function (valueJson) { " +
               "   var value = JSON.parse(valueJson);" +
               "   var poi = _.find(value,{'Name' : 'Mike'});" +
               "   return poi; " +
               "}";

    }

Thanks a lot in advance.

Uchitha

I think I have figured this out. It's no different to executing a custom function. You just read the third party library from a file (project resource) and invoke execute on Jint engine. See below;

 private void ImportLibrary(Engine jint, string file)
    {
        const string prefix = "JintApp.Lib."; //Project location where libraries like lodash are located

        var assembly = Assembly.GetExecutingAssembly();
        var scriptPath = prefix + file; //file is the name of the library file
        using (var stream = assembly.GetManifestResourceStream(scriptPath))
        {
            if (stream != null)
            {
                using (var sr = new StreamReader(stream))
                {
                    var source = sr.ReadToEnd();
                    jint.Execute(source);
                }
            }
        }

    }

We can call this function for all the third party libraries that needs to be added.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using third party js libraries with Jint

From Dev

Using third party libraries with netbeans

From Java

How to download a file with Node.js (without using third-party libraries)?

From Dev

Using third-party libraries in Eclipse RCP Tycho app

From Dev

AngularJS: Using third party libraries without exposing a global variable

From Dev

Reverse a known algorithm in Python using no third party libraries

From Dev

Using Maven's shade plugin for third party libraries?

From Dev

How to install third party libraries

From Dev

how to #include third party libraries

From Dev

Listing third party libraries in project

From Dev

how to #include third party libraries

From Dev

How to install third party libraries

From Dev

Using third party controls

From Dev

Allow VS throw third party libraries exceptions

From Dev

Odoo: how to add third party libraries

From Dev

Different data type lengths in third party libraries

From Dev

Singleton vs. third-party libraries

From Java

How to use third party libraries with Scala REPL?

From Dev

Best way to extend third party libraries in AngularJs

From Dev

Compiling and linking third party libraries in VS 2015

From Dev

Mapping value objects from third party libraries

From Dev

What about new versions of third party libraries?

From Dev

How to deal with warnings/hints in third party libraries?

From Dev

Kivy launcher and dependencies/third party libraries

From Dev

Third Party Libraries in EJB-JAR

From Dev

UML - Representing third party libraries in class diagram

From Dev

How to deal with warnings/hints in third party libraries?

From Dev

openshift symfony install third party libraries

From Dev

Best way to extend third party libraries in AngularJs

Related Related

HotTag

Archive