I've got a class called EcmaEval which allows users of my application execute arbitary javascript. The implementation of the class is at the end of this question. I allow users to access an "environment" object which provides methods and properties which are useful for them to script with.
The problem is that I need to expose a C# dynamic object to JScript, but it doesn't work. Has anyone done this before - should it work?
So, if I've got a plain old object with a string property called name it works:
test test = new test();
test.Name = "Daniel Bryars";
EcmaEval ecmaEval = new EcmaEval(new List<String>
{
Assembly.GetExecutingAssembly().Location
}, test);
String ecma = "environment.Name";
String result = ecmaEval.Eval<String>(ecma);
Assert.AreEqual("Daniel Bryars", result);
BUT if I pass my EcmaEval object a dynamic object then it doesn't (the property Name is null):
dynamic expandoObject = new ExpandoObject();
expandoObject.Name = "Daniel Bryars";
EcmaEval ecmaEval = new EcmaEval(new List<String>
{
Assembly.GetExecutingAssembly().Location
}, expandoObject);
String ecma = "environment.Name";
String result = ecmaEval.Eval<String>(ecma);
Assert.AreEqual("Daniel Bryars", result);
(result is null.)
Here's the implementation of EcmaEval. There's another class involved called JSObjectToDotNetConversion which coerces JSObjects to C# Objects (it uses reflection to new up a C# object and set the fields and/or properties) but the implementation of that class isn't relevent.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.JScript;
namespace Aeriandi.ApplicationBlocks.BusinessBaseObjects.Ecma
{
/// <summary>
/// Exposes the JScrip eval function as a .net method.
/// This uses the "safe" JScript.Eval so no disk, or network access is allowed.
/// </summary>
public class EcmaEval
{
private readonly object _evaluator;
private readonly Type _evaluatorType;
private readonly Object _environment;
public EcmaEval() : this (new List<string>(), null )
{
}
public EcmaEval(List<String> referencedAssemblies, Object environment)
{
if (null == referencedAssemblies)
{
throw new ArgumentNullException("referencedAssemblies", "The argument referencedAssemblies must not be null");
}
_environment = environment;
JScriptCodeProvider compiler = new JScriptCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
foreach (String referencedAssembly in referencedAssemblies)
{
parameters.ReferencedAssemblies.Add(referencedAssembly);
}
string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String, environment : Object)
{
return eval(expr);
}
}
}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
public Object Eval(Type returnType, String ecmaScript)
{
ecmaScript = WrapInBrackets(ecmaScript);
Object result = _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { ecmaScript, _environment }
);
return JSObjectToDotNetConversion.Coerce(returnType, result);
}
public T Eval<T>(String ecmaScript)
{
return (T) Eval(typeof (T), ecmaScript);
}
private static String WrapInBrackets(String ecmaScript)
{
//You can't start a block of js with a { because it's ambiguous (according to the spec)
//so we wrap everything in brackets.
return String.Format("({0})", ecmaScript);
}
}
}