我如何检测其JavaScript引擎(V8或JSC)在运行时使用的机器人?机器人、引擎、JavaScript、JSC

2023-09-12 09:19:00 作者:适可而止

较新的Andr​​oid版本(> 2.2),包括JavaScript引擎,而旧版本只有JSC的V8引擎。然而,根据http://blogs.nitobi.com/joe/2011/01/14/android-your-js-engine-is-not-always-v8/,其JavaScript引擎在运行时使用似乎依赖于环境变量present在编译时( JS_ENGINE ),以及该设备的硬件规格:

Newer versions of Android ( > 2.2) include the v8 javascript engine, while older versions only had JSC. However, according to http://blogs.nitobi.com/joe/2011/01/14/android-your-js-engine-is-not-always-v8/, which javascript engine is used at runtime seems to depend on an environment variable present at build-time (JS_ENGINE), as well as the hardware specs of the device:

# The default / alternative engine depends on the device class.
# On devices with a lot of memory (e.g. Passion/Sholes), the
# default is V8. On everything else, the only choice is JSC.

我的问题是:有没有什么办法,我可以决定哪些JavaScript引擎在使用从网页,内嵌的WebView,或应用程序中

My question is this: is there any way that I can determine which javascript engine is in use from within a webpage, an embedded WebView, or an application?

如果答案是否定的,没有任何人知道它的JS引擎所使用的Andr​​oid模拟器?

If the answer is no, does anybody know which JS engine is used by the Android emulator?

我问这个的原因是因为这个问题:http://$c$c.google.com/p/android/issues/detail?id=12987

The reason I'm asking this is because of this issue: http://code.google.com/p/android/issues/detail?id=12987

基本上,它可能是JavaScript的到Java的JSC桥断在Android 2.3.X,而这会影响我想要的应用程序写入。我看到一个段错误的地方深的JNI我的模拟器,而不是对物理设备的少数几个我测试过。我想,以确定这是否是一个模拟器,唯一一个JSC-唯一的事情,或完全不同了。

Basically, it may be that the javascript-to-java bridge in JSC is broken on Android 2.3.X, and this impacts the application I'm trying to write. I'm seeing a segfault from somewhere deep in the JNI on my emulator, but not on the handful of physical devices I've tested. I'm trying to determine if this is an emulator-only thing, a JSC-only thing, or something different altogether.

推荐答案

我认为更好的问题是:为什么你关心吗?你基本上落入了浏览器检测的陷阱,很多人陷入了90年代末/ 00年初的。自那时以来,虽然,我们已经了解到,它的特征检测的这是比较有用的方法,尤其是因为在给定的浏览器支持的功能是(主要)一个移动的目标。有code现在,在IE9以其显着,提高了DOM和JavaScript支持运行,这是不使用这些功能,因为它做的浏览器检测和对IE6的技术回落。

I think the better question is: Why do you care? You're basically falling into the "browser detection" trap that a lot of people fell into in the late 90's / early 00's. Since then, though, we've learned that it's feature detection that's the more useful approach, not least because the features supported in a given browser were (mostly) a moving target. There's code now, running on IE9 with its dramatically-improved DOM and JavaScript support, that's not using those features because it's doing browser detection and falling back on IE6 techniques.

所以,而不是担心V8与JSC,只是担心你想要的功能。我不知道JSC任何东西,但例如,让我们假设它不会对数组的的forEach 方法V8拥有(的ECMAScript的第5版标准的一部分)。而不是抛出一个大大的V8与JSC杆,你会怎么做:

So rather than worrying about V8 vs. JSC, just worry about the features you want. I don't know anything about JSC, but for instance let's assume it doesn't have the forEach method on arrays that V8 has (part of the ECMAScript 5th edition standard). Rather than throwing a big "V8 vs. JSC" lever, you'd do:

if (typeof Array.prototype.forEach === "function") {
    // Code that expects `forEach`
}
else {
    // Code that falls back
}

(你的code落在后面可能的添加的的forEach 原型,或​​者本次测试是在你自己的迭代器功能,你想知道是否要推迟到本机实现,或提供自己的。)

(Your "code that falls back" might add forEach to the prototype, or maybe this test is in your own iterator function and you want to know whether to defer to a native implementation or supply your own.)

和类似的其他特征要使用,可能会或可能不会present

And similarly for other features you want to use that may or may not be present.

但如果你真的需要检测V8与JSC(和您的评论看来你可能),的这个页面似乎证明这样做的一种手段,但它似乎非常脆弱。这是我略编辑的版本(并非最不重要的,以取代 window.devicePixelRatio 与测试 WebkitAppearance  —的前者给误报至少在其他一些浏览器[火狐,例如,它采用蛤蚧,不是WebKit的):

But if you really need to detect V8 vs. JSC (and from your comment it seems you may), this page seems to demonstrate a means of doing so, though it seems awfully fragile. Here's my slightly-edited version (not least to replace window.devicePixelRatio with the test for WebkitAppearance— the former gives false positives on at least some other browsers [Firefox, for instance, which uses Gecko, not WebKit]):

var v8string = 'function%20javaEnabled%28%29%20%7B%20%5Bnative%20code%5D%20%7D';

if ('WebkitAppearance' in document.documentElement.style)  //If (probably) WebKit browser
{
   if (escape(navigator.javaEnabled.toString()) === v8string)
   {
      display('V8 detected');
   }
   else
   {
      display('JSC detected');
   }
}
else {
  display("Not a WebKit browser");
}

活生生的例子

Works的箱检测浏览器(它也使用V 8)和Safari(它也使用JSC)之间的差。

Works for me detecting the difference between Chrome (which also uses V8) and Safari (which also uses JSC).