2

I would like to allow scripts running in Jint to access a pre-existing API I have set up as a namespace. By that I mean I have a single namespace that contains the API, including other namespaces. I do not want to allow the scripts access to the rest of the code - including the .Net framework.

I have already posted this on the Jint forum here: http://jint.codeplex.com/discussions/310772 However, no disrespect to them but the forum does not appear to be very active and I would like to be able to answer this ASAP so I am posting here as well.

A while ago, ThomasMaierhofer achieved something similar to this that I could probably modify to work this out here: http://jint.codeplex.com/discussions/211291

To my inexperienced brain this seems like a really neat way of exposing an API to the engine, but I have never seen it done like this before.

So my questions are: Would this work? And if so, why has it not been done like this before? And is there any way I could achieve this without modifying the Jint source so I can easily update the Jint .dll as new versions become available?

EDIT: The current API I have consists of multiple classes each with multiple functions. I can expose specific instances of these classes absolutely fine using SetParameter. Jint also has an AllowClr property that allows the script to access the CLR by fully qualifying namespaces. This is the example code they give demonstrating what happens if you set this to false. Source: http://jint.codeplex.com/wikipage?title=Using%20.NET%20classes%20from%20scripts

string stringBuilder = @"
    var sb = new System.Text.StringBuilder();
    return sb.ToString();
    ";
var engine = new JintEngine();
engine.AllowClr = false;
engine.Run(stringBuilder); // throws a SecurityException

I would like to allow this, but only for a specific namespace, not everything else. I hope that makes the question clearer.

Thanks for your help,

Sam.

P.S. I am working in VB.Net but answers involving C# are fine.

SeriousSamP
  • 436
  • 3
  • 18

1 Answers1

2

Jint has basically two way to achieve what you want: SetParameter, in wich you can pass an object instance that will be callable from the interpreter by accessing it with the name you decided, and SetFunction(), to let the engine call directly a function in the hosting code. You can work with reflection if you want to expose programmatically a lot of objects by some convention, and I think this would fulfill you requirements without modifyng the jint code.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115