1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using Microsoft.JScript;
|
20
|
|
using System;
|
21
|
|
using System.Collections;
|
22
|
|
using System.Reflection;
|
23
|
|
using System.CodeDom.Compiler;
|
24
|
|
using Seasar.Framework.Exceptions;
|
25
|
|
|
26
|
|
namespace Seasar.Framework.Util
|
27
|
|
{
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
public sealed class JScriptUtil
|
32
|
|
{
|
33
|
|
private static CodeDomProvider provider_ = new JScriptCodeProvider();
|
34
|
|
private static Type evaluateType_;
|
35
|
|
|
36
|
|
private const string EVAL_SOURCE = @"
|
37
|
|
package Seasar.Framework.Util.JScript
|
38
|
|
{
|
39
|
|
class Evaluator
|
40
|
|
{
|
41
|
|
public static function Eval(expr : String,unsafe : boolean,
|
42
|
|
self : Object,out : Object,err : Object, container : Object) : Object
|
43
|
|
{
|
44
|
|
if(unsafe)
|
45
|
|
{
|
46
|
|
return eval(expr,'unsafe');
|
47
|
|
}
|
48
|
|
else
|
49
|
|
{
|
50
|
|
return eval(expr);
|
51
|
|
}
|
52
|
|
}
|
53
|
|
}
|
54
|
|
}";
|
55
|
|
|
56
|
0
|
private JScriptUtil()
|
57
|
|
{
|
58
|
|
}
|
59
|
|
|
60
|
1
|
static JScriptUtil()
|
61
|
|
{
|
62
|
1
|
CompilerParameters parameters = new CompilerParameters();
|
63
|
1
|
parameters.GenerateInMemory = true;
|
64
|
1
|
CompilerResults results = provider_.CompileAssemblyFromSource(parameters, EVAL_SOURCE);
|
65
|
1
|
Assembly assembly = results.CompiledAssembly;
|
66
|
1
|
evaluateType_ = assembly.GetType("Seasar.Framework.Util.JScript.Evaluator");
|
67
|
|
}
|
68
|
|
|
69
|
7
|
public static object Evaluate(string exp,Hashtable ctx, object root)
|
70
|
|
{
|
71
|
7
|
try
|
72
|
|
{
|
73
|
7
|
return evaluateType_.InvokeMember("Eval",BindingFlags.InvokeMethod,
|
74
|
|
null,null,new object[] {exp,true,ctx["self"],ctx["out"],ctx["err"],root});
|
75
|
|
}
|
76
|
|
catch(Exception ex)
|
77
|
|
{
|
78
|
0
|
throw new JScriptEvaluateRuntimeException(exp,ex);
|
79
|
|
}
|
80
|
|
}
|
81
|
|
|
82
|
305
|
public static object Evaluate(string exp, object root)
|
83
|
|
{
|
84
|
305
|
try
|
85
|
|
{
|
86
|
305
|
return evaluateType_.InvokeMember("Eval",BindingFlags.InvokeMethod,
|
87
|
|
null,null,new object[] {exp,true,null,null,null,root});
|
88
|
|
}
|
89
|
|
catch(Exception ex)
|
90
|
|
{
|
91
|
2
|
throw new JScriptEvaluateRuntimeException(exp,ex);
|
92
|
|
}
|
93
|
|
}
|
94
|
|
}
|
95
|
|
}
|
96
|
|
|