Alex Rivera | Logout

How can I detect which javascript engine (v8 or JSC) is used at runtime in Android?

Asked 2011-07-20T20:57:53.040
13

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.

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?

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


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

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.

Edit
Report

2 Answers

6

Although the answer above points out the best methods, I thought I'd point out another way of doing this from inside a native library.

void *dlWebCoreHandle = dlopen("libwebcore.so", RTLD_NOW);
void *v8GetVersion = dlsym(dlWebCoreHandle, "_ZN2v82V810GetVersionEv");
if (v8GetVersion == NULL) {
    /* does not appear to be V8 */
} ... etc.

The exported symbols are mangled, unfortunately, so there is no 100% guarantee that the compiler used by your firmware manufacturer mangled the symbol in the same way (use nm --defined-only libwebcore.so -g on a library with symbols). One would expose this function via the JNI and check from inside Java code.

The libwebcore.so library also lists V8_Fatal as one of the symbols, which is not susceptible to mangling.

JSC will have some other exported symbols that you can check for from inside a native library. If both are non-existent you can fallback to other methods.

answered 2012-08-06T11:46:11.767
2

var s = '';
for (x in {
    3: 3,
    1: 1
  }) {
  s += x
}
if (s === '31') {
  alert('JSC');
} else {
  alert('V8');
}

By the way this will categorize Firefox as 'JSC' and newer IE versions will look like V8.

My blog post has more V8 sniffing tricks: http://erikcorry.blogspot.dk/2012/12/which-version-of-v8-do-i-have.html

answered 2012-12-07T09:36:22.637

Your Answer