V8Engine.Execute returns 'undefined',而 Microsoft.JScript 的 eval returns 一个值

V8Engine.Execute returns 'undefined', whereas Microsoft.JScript's eval returns a value

我希望用 V8.Net 引擎替换 C#.NET 4.0 程序中的 Microsoft.JScript 引擎,以便能够执行传入的 javascript 函数。该程序运行良好并正确执行了一些脚本,直到它命中以下内容;

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 命令 returns undefined,而我们的 Microsoft.JScript 实现 returns 9.091119999999998 我也不确定是否正确. 我觉得这与每个引擎在执行脚本结束时如何处理被认为是 'output' 的内容有关,但我不确定。

Lookup.IndexesMsgSize 是我们在 C#.NET 应用程序中定义的函数,并且在两个版本中都是一致的,我相信在这个实例中是 returns 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定义的class,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;
}

我希望有人能阐明为什么会出现上述结果,

提前致谢!

JS 引擎在您将脚本传递给它时使用 eval 函数。反过来, undefined 是脚本执行的正确结果,因为 eval() returns the value of the last expression evaluated MDN eval docs

在您的代码中:

var t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;

Variable Statement 是最后执行的表达式。它总是 returns undefined.

A variable statement declares variables that are created as defined in 10.5. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

在你的例子中 t_fn + (n_byte * t_byte) + t_sch_ohd; 是 AssignmentExpression 初始化器。 VariableStatement -> AssignmentExpression;

因此,您的代码如下:

  1. Perform AssignmentExpression
  2. Perform VariableStatement

有几种方法可以解决您的问题:

  • 隐式定义变量:
t_total = t_fn + (n_byte * t_byte) + t_sch_ohd;
  • Return 结果显式为:
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/