c#4中的DLR和Javascript解释? [英] DLR and Javascript interpretation in c# 4?

查看:187
本文介绍了c#4中的DLR和Javascript解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我写了一个简单的代码,使用 c# Jint

  var script = @
function show()
{
return parseInt('123asd'); // in js it's 123
};
return show();;

var result = new JintEngine()。Run(script);
Console.WriteLine(result);

parseInt('123asd')是: 123



但我得到的结果是:






  • 也许我看不到整幅画面,但是如果世界另一边的程序员给我发送他的脚本文件,我(和他)期望结果是一致的!


  • 如果我错了,在什么情况下我会使用.Net上运行其他代码? (我将不得不



解决方案

这似乎是Jint的 partseInt 函数的当前行为。



相关代码:

  //最常见的情况
double result;
if(double.TryParse(number,NumberStyles.Any,CultureInfo.InvariantCulture,out result)){
// parseInt(12.42)== 42
return NumberClass.New(sign * Math .Floor(结果));
}
else {
return this [NaN];
}

.NET的 double.TryParse 不会将'123asd'解析为 123 ,但返回无法解析整个字符串。 p>

您应该在Jint中填写错误报告。



E:这是标准的问题。您无法在所有实施中获得100%兼容的标准。总会有差异。只需看看IE与Chrome vs Firefox vs Opera。所有这些都使用不同的JavaScript引擎,所以你可以期待差异。如果你真的想要交叉兼容,你应该按照标准执行,把所有的非标准行为视为实现的错误。如果您尝试解决这个错误或者报告错误,并且希望能够被修复,这取决于你。


I want to execute a javascript code from c# using DLR.

So I wrote a simple code using c# and Jint :

  var script = @"
                               function show( )
                               {
                                      return  parseInt('123asd'); //in js it's 123
                               };
                               return show();";

                var result = new JintEngine().Run(script);
                Console.WriteLine(result);

parseInt('123asd') in javascript is : 123

But the result I get is :

  • Maybe I don't see the whole picture, but if a programmer on the other side of the world sends me his script file, I (and him) expect the result to be consistent!

  • If I was mistaken, in what scenario would I use running other code on .Net? (I will have to be very very suspicious for every line of code...)

解决方案

This seems to be current behavior of Jint's partseInt function.

Related code:

// most common case
double result;
if(double.TryParse(number,NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
    // parseInt(12.42) == 42
    return NumberClass.New(sign * Math.Floor(result));
}
else {
    return this["NaN"];
}

.NET's double.TryParse won't parse '123asd' as 123, but returns that it cannot parse the whole string.

You should fill in bug report in Jint.

E: This is problem with "standards". You can never get standards 100% compatible across all implementations. There will always be differences. Just look at IE vs Chrome vs Firefox vs Opera. All of those use different javascript engines, so you can expect differences. If you really want to be cross-compatible, you should implement according to standard and treat all non-standard behavior as bug in implementation. It is up to you if you try to work around this bug or report it and expect it to be fixed.

这篇关于c#4中的DLR和Javascript解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆