我希望用Microsoft.JScript引擎替换C#.NET 4.0程序中的V8.Net引擎,以便能够执行C#.NET函数中传递的内容。该程序运行良好,并正确地执行若干脚本,直到它达到以下目标;
const t_sch_ohd = 0.1;
const t_fn = 8.302;
const t_byte = 0.04307;
var n_byte = Lookup.IndexedMsgSize (1);
var t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;V8Engine.Execute命令返回undefined,而我们的Microsoft.JScript实现返回9.091119999999998,我也不确定它是否正确。我觉得这与每个引擎如何处理在执行脚本结束时被认为是“输出”有关,但我不确定。
Lookup.IndexesMsgSize是我们在C#.NET应用程序中定义的函数,在这两个版本中都是一致的,我相信它在这个实例中返回16;
public uint IndexedMsgSize(uint mIndex)
{
// only intended for MsgProcess_Fn
if (m_message == null)
{
if (m_function == null)
{
throw new RaceException("Function reference not specified for Lookup.IndexedMsgSize");
}
uint count = (uint)m_function.RelatedMessagesByUDC[((int)mIndex) - 1].MessageLength;
return count;
}
else
{
throw new RaceException("message reference not allowed for Lookup.IndexedMsgSize");
}
}Microsoft.JScript评估的实现如下;
public string Execute(string script)
{
string result = "";
try
{
// EVALUATE THE SCRIPT
result = Evaluator.Eval(script);
//using (var engine = new ScriptEngine("jscript"))
//{
// ParsedScript parsed =
// engine.Parse("function MyFunc(x){return eval(x,'unsafe')}");
// engine.SetNamedItem("Lookup", m_lookup);
// result = parsed.CallMethod("MyFunc", script).ToString();
//}
}
catch (Exception ex)
{
// JScript error?
if (ex.Source.CompareTo("Microsoft.JScript") == 0)
{
// Yes, turn it into a Race JScript Exception to avoid a stack trace dump.
throw new RaceScriptException(ex as JScriptException);
}
// Not a JScript error, so just re-throw exception.
throw ex;
}
return result;
}Evaluator是通过.dll定义的类,Eval函数在Javascript中如下所示;
static function Eval(expression : String) : String {
return eval(expression,'unsafe');
}在我们的V8.Net实现中,我们只使用内置的V8Engine.Execute命令;
public string Execute(string script)
{
string result = "";
try
{
// EVALUATE THE SCRIPT
//Create the V8.Net Engine
V8Engine e = new V8Engine();
//Set the Lookup instance as a global object so that the JS code in the V8.Net wrapper can access it
e.GlobalObject.SetProperty("Lookup", m_lookup, null, true, ScriptMemberSecurity.Permanent);
//Execute the script
result = e.Execute(script);
//Deprecated code:
//result = Evaluator.Eval(script);
//using (var engine = new ScriptEngine("jscript"))
//{
// ParsedScript parsed =
// engine.Parse("function MyFunc(x){return eval(x,'unsafe')}");
// engine.SetNamedItem("Lookup", m_lookup);
// result = parsed.CallMethod("MyFunc", script).ToString();
//}
}
catch (Exception ex)
{
// V8.NET error?
if (ex.Source.CompareTo("V8.Net") == 0)
{
// Yes, turn it into a Race V8 Exception to avoid a stack trace dump.
throw (V8Exception)ex;
}
// Not a V8 error, so just re-throw exception.
throw ex;
}
return result;
}我希望有人能弄清楚为什么会发生这样的结果,
提前感谢!
发布于 2015-12-01 12:07:57
JS引擎在将脚本传递给它时使用eval函数。反过来,undefined是脚本执行的正确结果,因为eval() returns the value of the last expression evaluated MDN eval文档
在您的代码中:
var t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;可变语句是最后执行的表达式。它总是返回undefined。
变量语句声明按10.5中定义的变量。变量在创建时被初始化为未定义的变量。带有初始化项的变量在执行AssignmentExpression时(而不是在创建变量时)分配其VariableStatement的值。
在您的例子中,t_fn + (n_byte * t_byte) + t_sch_ohd;是AssignmentExpression初始化器。VariableStatement -> AssignmentExpression;
所以,您的代码如下:
有几种方法可以解决你的问题:
t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;var t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;
t_total;var t_total;
t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;Microsoft.JScript的工作方式与您预期的一样,仅仅是因为行为不当。
要深入了解JS的本质,请浏览http://es5.github.io/
https://stackoverflow.com/questions/33995597
复制相似问题