Qt Script V8 Port: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (Remove category (There are no other pages in that category))
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Developing_Qt::Qt_Script]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
{{Outdated | reason=This project is [https://bugreports.qt.io/browse/QTBUG-12503 abandoned]}}
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


= QtScript V8 Port =
[http://code.google.com/p/v8/ V8] is Google's open source JavaScript engine, used in the Google Chrome browser. The QtScript V8 port is an effort to implement the existing [http://doc.qt.nokia.com/latest/qtscript.html QtScript API] on top of V8.


&quot;THE PROJECT WAS ABANDONED&amp;quot;:https://bugreports.qt.io/browse/QTBUG-12503
The master task for researching whether Qt (including QtWebKit) can be "switched over" to using V8 is [http://bugreports.qt.nokia.com/browse/QTBUG-12502 QTBUG-12502].
 
&quot;V8&amp;quot;:http://code.google.com/p/v8/ is Google's open source JavaScript engine, used in the Google Chrome browser. The QtScript V8 port is an effort to implement the existing &quot;QtScript API&amp;quot;:http://doc.qt.nokia.com/latest/qtscript.html on top of V8.
 
The master task for researching whether Qt (including QtWebKit) can be &quot;switched over&amp;quot; to using V8 is &quot;QTBUG-12502&amp;quot;:http://bugreports.qt.nokia.com/browse/QTBUG-12502.


== Goal ==
== Goal ==
Line 17: Line 14:
== Getting the Code ==
== Getting the Code ==


&quot;http://qt.gitorious.org/+qt-developers/qt/qt-script-ng&amp;quot;:http://qt.gitorious.org/+qt-developers/qt/qt-script-ng
[http://qt.gitorious.org/+qt-developers/qt/qt-script-ng http://qt.gitorious.org/+qt-developers/qt/qt-script-ng]


The repository is is a full clone of Qt that can be configured and built like usual.<br />It includes a copy of V8-Isolates in src/3rdparty/v8, which is automatically built as part of building QtScript.<br />'''master-v8''' is the currently active branch.
The repository is is a full clone of Qt that can be configured and built like usual.
It includes a copy of V8-Isolates in src/3rdparty/v8, which is automatically built as part of building QtScript.
'''master-v8''' is the currently active branch.


== Workflow ==
== Workflow ==


The port was started by removing the old QtScript implementation completely, and creating &quot;stubs&amp;quot; (empty implementations) for all functions in the QtScript API. Thus, the starting point was a QtScript library that's fully source and binary compatible with previous QtScript releases. When an unimplemented API function is invoked, it will issue a &quot;Not implemented&amp;quot; warning, and otherwise do nothing. This enables us to incrementally implement the API, while all the time being able to test the implementation against existing autotest suites and applications (including QML).
The port was started by removing the old QtScript implementation completely, and creating "stubs" (empty implementations) for all functions in the QtScript API. Thus, the starting point was a QtScript library that's fully source and binary compatible with previous QtScript releases. When an unimplemented API function is invoked, it will issue a "Not implemented" warning, and otherwise do nothing. This enables us to incrementally implement the API, while all the time being able to test the implementation against existing autotest suites and applications (including QML).


Our workflow consists of running an application/autotest and fixing failures, one at a time. Eventually, there will be no more failures, and no show-stopping performance regressions against previous QtScript releases, at which time the project can be considered &quot;Done&amp;quot;.
Our workflow consists of running an application/autotest and fixing failures, one at a time. Eventually, there will be no more failures, and no show-stopping performance regressions against previous QtScript releases, at which time the project can be considered "Done".


Test failures are tracked at &quot;QTBUG-17640&amp;quot;:http://bugreports.qt.nokia.com/browse/QTBUG-17640.
Test failures are tracked at [http://bugreports.qt.nokia.com/browse/QTBUG-17640 QTBUG-17640].


== About V8 ==
== About V8 ==
Line 33: Line 32:
Like QtScript, V8 provides a C++ API for embedding JavaScript into an application, and for integrating with native application code.
Like QtScript, V8 provides a C++ API for embedding JavaScript into an application, and for integrating with native application code.


The V8 &quot;Embedder's Guide&amp;quot;:http://code.google.com/intl/no/apis/v8/embed.html gives a good introduction to the V8 C++ API. If you plan to work on the QtScript port, it's a good idea to first familiarize yourself with the V8 design and concepts (handles, handle scopes, accessors, interceptors). The core API is all in one file in the V8 repository: &quot;include/v8.h&amp;quot;:http://code.google.com/p/v8/source/browse/branches/bleeding_edge/include/v8.h. There are additional includes for e.g. debugging and profiling APIs.
The V8 [http://code.google.com/intl/no/apis/v8/embed.html Embedder's Guide] gives a good introduction to the V8 C++ API. If you plan to work on the QtScript port, it's a good idea to first familiarize yourself with the V8 design and concepts (handles, handle scopes, accessors, interceptors). The core API is all in one file in the V8 repository: [http://code.google.com/p/v8/source/browse/branches/bleeding_edge/include/v8.h include/v8.h]. There are additional includes for e.g. debugging and profiling APIs.


The &quot;How to Download and Build V8&amp;quot;:http://code.google.com/intl/no/apis/v8/build.html page has instructions for getting the V8 source code and building it. However, for starters you shouldn't have to worry about that, since the QtScript V8 port includes its own copy of V8 that's built as part of building QtScript.
The [http://code.google.com/intl/no/apis/v8/build.html How to Download and Build V8] page has instructions for getting the V8 source code and building it. However, for starters you shouldn't have to worry about that, since the QtScript V8 port includes its own copy of V8 that's built as part of building QtScript.


If you want to follow V8 development closely, consider subscribing to the &quot;v8-dev list&amp;quot;:http://groups.google.com/group/v8-dev. This is a rather high-volume list, consisting mostly of automated mails from code reviews and commits. There's also a &quot;v8-users list&amp;quot;:http://groups.google.com/group/v8-users for discussing how to use V8.
If you want to follow V8 development closely, consider subscribing to the [http://groups.google.com/group/v8-dev v8-dev list]. This is a rather high-volume list, consisting mostly of automated mails from code reviews and commits. There's also a [http://groups.google.com/group/v8-users v8-users list] for discussing how to use V8.


== V8 Isolates ==
== V8 Isolates ==


''V8 Isolates'' is a research project by Google to support multiple V8 &quot;instances&amp;quot; per process. This feature will enable Chromium to support &quot;in-process workers&amp;quot;:http://code.google.com/p/v8/issues/detail?id=510. Coincidentally, this feature is a requirement for the QtScript V8 port, since the QtScript API is re-entrant.
''V8 Isolates'' is a research project by Google to support multiple V8 "instances" per process. This feature will enable Chromium to support [http://code.google.com/p/v8/issues/detail?id=510 in-process workers]. Coincidentally, this feature is a requirement for the QtScript V8 port, since the QtScript API is re-entrant.


V8 Isolates is developed in the <code>experimental/isolates<code> branch in the V8 repository. Essentially, this branch gathers all static variables in V8 into thread-local storage. Isolates is the branch we track and import into QtScript.
V8 Isolates is developed in the <code>experimental/isolates<code> branch in the V8 repository. Essentially, this branch gathers all static variables in V8 into thread-local storage. Isolates is the branch we track and import into QtScript.


Google aims to &quot;eventually land Isolates on the main development branch&amp;quot;:http://code.google.com/p/v8/issues/detail?id=510#c15.
Google aims to [http://code.google.com/p/v8/issues/detail?id=510#c15 eventually land Isolates on the main development branch].


== Relevant V8 Bugs ==
== Relevant V8 Bugs ==


We use the &quot;V8 issue tracker&amp;quot;:http://code.google.com/p/v8/issues/list to report and track bugs relevant to QtScript, such as ECMA compliance issues, build issues, V8 API behavior, and suggestions for additional API. This section lists those issues.
We use the [http://code.google.com/p/v8/issues/list V8 issue tracker] to report and track bugs relevant to QtScript, such as ECMA compliance issues, build issues, V8 API behavior, and suggestions for additional API. This section lists those issues.


* &quot;510&amp;quot;:http://code.google.com/p/v8/issues/detail?id=510: Several V8 instances in a process
* [http://code.google.com/p/v8/issues/detail?id=510 510]: Several V8 instances in a process
* &quot;1204&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1204: Limitation of setData method
* [http://code.google.com/p/v8/issues/detail?id=1204 1204]: Limitation of setData method
* &quot;1205&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1205: Isolate::setData
* [http://code.google.com/p/v8/issues/detail?id=1205 1205]: Isolate::setData
* &quot;1021&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1021: Prototype Setter Interceptor not working
* [http://code.google.com/p/v8/issues/detail?id=1021 1021]: Prototype Setter Interceptor not working
* &quot;1022&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1022: Interceptor &quot;blocking&amp;quot; setter in prototype
* [http://code.google.com/p/v8/issues/detail?id=1022 1022]: Interceptor "blocking" setter in prototype
* &quot;1028&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1028: RangeError in Array ctor (ECMA compliance)
* [http://code.google.com/p/v8/issues/detail?id=1028 1028]: RangeError in Array ctor (ECMA compliance)
* &quot;<s>1035</s>&quot;:http://code.google.com/p/v8/issues/detail?id=1035: Memory leak in Isolate class
* [http://code.google.com/p/v8/issues/detail?id=1035 -1035-]: Memory leak in Isolate class
* &quot;<s>1036</s>&quot;:http://code.google.com/p/v8/issues/detail?id=1036: Memory leak in Logger class
* [http://code.google.com/p/v8/issues/detail?id=1036 -1036-]: Memory leak in Logger class
* &quot;884&amp;quot;:http://code.google.com/p/v8/issues/detail?id=884: shell sample Infinite loop in JSObject::LookupCallbackSetterInPrototypes (''Possibly a gcc bug; watch out for it in release builds'')
* [http://code.google.com/p/v8/issues/detail?id=884 884]: shell sample Infinite loop in JSObject::LookupCallbackSetterInPrototypes (''Possibly a gcc bug; watch out for it in release builds'')
* &quot;435&amp;quot;:http://code.google.com/p/v8/issues/detail?id=435: The .length property of a function always returns zero (''actually, of native functions. Having this fixed would also provide one clean way to make the length parameter of QScriptEngine::newFunction() work'')
* [http://code.google.com/p/v8/issues/detail?id=435 435]: The .length property of a function always returns zero (''actually, of native functions. Having this fixed would also provide one clean way to make the length parameter of QScriptEngine::newFunction() work'')
* &quot;1061&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1061: Some function not working in Isolate branch (work arounded in our patched V8)
* [http://code.google.com/p/v8/issues/detail?id=1061 1061]: Some function not working in Isolate branch (work arounded in our patched V8)
* &quot;1072&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1072: TryCatch doesn't catch exceptions thrown from an accessor.
* [http://code.google.com/p/v8/issues/detail?id=1072 1072]: TryCatch doesn't catch exceptions thrown from an accessor.
* &quot;1078&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1078: Assert when changing the prototype of the global object. (some test change the protoype of the global object)
* [http://code.google.com/p/v8/issues/detail?id=1078 1078]: Assert when changing the prototype of the global object. (some test change the protoype of the global object)
* &quot;862&amp;quot;:http://code.google.com/p/v8/issues/detail?id=862: GetOwnProperty() should return enumerable properties for String chars (''ECMA compliance item that we check in our autotests'')
* [http://code.google.com/p/v8/issues/detail?id=862 862]: GetOwnProperty() should return enumerable properties for String chars (''ECMA compliance item that we check in our autotests'')
* &quot;<s>1256</s>&quot;:http://code.google.com/p/v8/issues/detail?id=1256: v8::Array::New() ignores length argument (causes QScriptEngine::newArray(123) to be slow, since we have to set the length explicitly)
* [http://code.google.com/p/v8/issues/detail?id=1256 -1256-]: v8::Array::New() ignores length argument (causes QScriptEngine::newArray(123) to be slow, since we have to set the length explicitly)
* &quot;831&amp;quot;:http://code.google.com/p/v8/issues/detail?id=831: regexp performance seems to degrade exponentially with length of failed (&quot;1259&amp;quot;:http://code.google.com/p/v8/issues/detail?id=1259 has been merged into this issue)
* [http://code.google.com/p/v8/issues/detail?id=831 831]: regexp performance seems to degrade exponentially with length of failed ([http://code.google.com/p/v8/issues/detail?id=1259 1259] has been merged into this issue)


== Reporting V8 Bugs ==
== Reporting V8 Bugs ==


If you find a bug in V8 that affects QtScript, you should create a an upstream report in the &quot;V8 issue tracker&amp;quot;:http://code.google.com/p/v8/issues/list. It's important that the report has a minimal testcase, and in particular that it doesn't depend on QtScript; i.e., the testcase should use the V8 API directly.
If you find a bug in V8 that affects QtScript, you should create a an upstream report in the [http://code.google.com/p/v8/issues/list V8 issue tracker]. It's important that the report has a minimal testcase, and in particular that it doesn't depend on QtScript; i.e., the testcase should use the V8 API directly.


Since we're working with a (not-necessarily-up-to-date) copy of V8 Isolates, it's possible that the bug isn't present in upstream V8 (because it's been fixed already). You should first try to reproduce the bug with V8 trunk or bleeding_edge, and report it straight away if it's present there. If you can't reproduce the bug in trunk or bleeding_edge, but can reproduce it in the Isolates branch, it's either because the fix hasn't been merged into Isolates yet, or there's an Isolates-specific bug. Depending on the nature of the bug, you'll have to try to decide which it is.
Since we're working with a (not-necessarily-up-to-date) copy of V8 Isolates, it's possible that the bug isn't present in upstream V8 (because it's been fixed already). You should first try to reproduce the bug with V8 trunk or bleeding_edge, and report it straight away if it's present there. If you can't reproduce the bug in trunk or bleeding_edge, but can reproduce it in the Isolates branch, it's either because the fix hasn't been merged into Isolates yet, or there's an Isolates-specific bug. Depending on the nature of the bug, you'll have to try to decide which it is.
Line 76: Line 75:
The purpose of the testcase is to make it easy for the V8 developers to reproduce the bug. Here's a template main.cpp:
The purpose of the testcase is to make it easy for the V8 developers to reproduce the bug. Here's a template main.cpp:


</code><br />#include &lt;v8.h&amp;gt;<br />#include &lt;assert.h&amp;gt;<br />#include &lt;stdio.h&amp;gt;
</code>
#include <v8.h>
#include <assert.h>
#include <stdio.h>


int main(int argc, char **argv)<br />{<br /> v8::V8::SetFlagsFromCommandLine(&amp;argc, argv, true);
int main(int argc, char **argv)
{
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);


v8::HandleScope handle_scope;<br /> v8::Handle&amp;lt;v8::ObjectTemplate&amp;gt; global = v8::ObjectTemplate::New();<br /> // Put your global object properties here…<br /> // global-&gt;Set(v8::String::New(&quot;foo&amp;quot;), v8::Integer::New(123));
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Put your global object properties here…
// global->Set(v8::String::New("foo"), v8::Integer::New(123));


v8::Persistent&amp;lt;v8::Context&amp;gt; context = v8::Context::New(NULL, global);<br /> assert(!context.IsEmpty());<br /> v8::Context::Scope context_scope(context);
v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
assert(!context.IsEmpty());
v8::Context::Scope context_scope(context);


v8::TryCatch try_catch;
v8::TryCatch try_catch;


// Put your script here…<br /> v8::Handle&amp;lt;v8::String&amp;gt; source = v8::String::New(&quot;'hello'&quot;);<br /> assert(!source.IsEmpty());
// Put your script here…
v8::Handle<v8::String> source = v8::String::New("'hello'");
assert(!source.IsEmpty());


v8::Handle&amp;lt;v8::Script&amp;gt; script = v8::Script::Compile(source);<br /> assert(!script.IsEmpty());
v8::Handle<v8::Script> script = v8::Script::Compile(source);
assert(!script.IsEmpty());


v8::Handle&amp;lt;v8::Value&amp;gt; result = script-&gt;Run();<br /> assert(!result.IsEmpty());
v8::Handle<v8::Value> result = script->Run();
assert(!result.IsEmpty());


// Print the result…<br /> v8::String::Utf8Value str(result);<br /> printf(&quot;%s\n&amp;quot;, *str);
// Print the result…
v8::String::Utf8Value str(result);
printf("%s\n", *str);


context.Dispose();<br /> v8::V8::Dispose();<br /> return 0;<br />}<code>
context.Dispose();
v8::V8::Dispose();
return 0;
}<code>


Assuming you've built V8 in $V8DIR, here's how you can build it with GCC:
Assuming you've built V8 in $V8DIR, here's how you can build it with GCC:
Line 102: Line 120:
Once the testcase is working, you can strip away everything that's not needed (asserts and such) before submitting.
Once the testcase is working, you can strip away everything that's not needed (asserts and such) before submitting.


Finally, add a link to the newly created upstream bug in the previous section (&quot;Relevant V8 Bugs&amp;quot;), and preferably refer to the bug in our JIRA (&quot;This depends on the following upstream bug: …&quot;), and source code (e.g., update the QEXPECT_FAIL message of a test).
Finally, add a link to the newly created upstream bug in the previous section ("Relevant V8 Bugs"), and preferably refer to the bug in our JIRA ("This depends on the following upstream bug: …"), and source code (e.g., update the QEXPECT_FAIL message of a test).


== Hacking on the V8 Build ==
== Hacking on the V8 Build ==
Line 110: Line 128:
By default, QtScript uses a copy of V8 located in <code>src/3rdparty/v8<code>
By default, QtScript uses a copy of V8 located in <code>src/3rdparty/v8<code>


V8 is built as a separate (static) library that QtScript links against. The default (upstream) build system for V8 is &quot;SCons&amp;quot;:http://www.scons.org/. However, to avoid a dependency on SCons (which isn't available on all Qt platform — Symbian in particular), we currently maintain a QMake project file for building V8: </code>src/script/v8.pro</code> This file is a (manual) port of (a subset of) the SConstruct and src/SConscript files in the V8 repository.
V8 is built as a separate (static) library that QtScript links against. The default (upstream) build system for V8 is [http://www.scons.org/ SCons]. However, to avoid a dependency on SCons (which isn't available on all Qt platform — Symbian in particular), we currently maintain a QMake project file for building V8: </code>src/script/v8.pro</code> This file is a (manual) port of (a subset of) the SConstruct and src/SConscript files in the V8 repository.


Currently, Linux and Mac OS are the only regularly tested platforms.
Currently, Linux and Mac OS are the only regularly tested platforms.
Line 116: Line 134:
=== generated/libraries.cpp ===
=== generated/libraries.cpp ===


V8 implements large parts of its functionality (such as the core ECMA objects) in JavaScript itself; see the .js files in the V8 src/ directory. V8 evaluates these files as part of &quot;bootstrapping&amp;quot; the V8 environment. In order to avoid an external dependency on the .js files, their contents are embedded in the V8 library (analogous to how Qt resource (.qrc) files are used by Qt). A Python script in the V8 directory (tools/js2c.py) generates a C++ file which is literally a char array of the .js contents, plus functions for retrieving script contents by index/name.
V8 implements large parts of its functionality (such as the core ECMA objects) in JavaScript itself; see the .js files in the V8 src/ directory. V8 evaluates these files as part of "bootstrapping" the V8 environment. In order to avoid an external dependency on the .js files, their contents are embedded in the V8 library (analogous to how Qt resource (.qrc) files are used by Qt). A Python script in the V8 directory (tools/js2c.py) generates a C++ file which is literally a char array of the .js contents, plus functions for retrieving script contents by index/name.


The js2c.py script is automatically invoked when building V8, and will regenerate libraries.cpp if any of the V8 .js files are modified.
The js2c.py script is automatically invoked when building V8, and will regenerate libraries.cpp if any of the V8 .js files are modified.
Line 126: Line 144:
==== mksnapshot ====
==== mksnapshot ====


The snapshot feature is aided by a tool, mksnapshot, which creates a V8 environment the &quot;normal&amp;quot; way (by evaluating .js files et al), and generates a C++ file &amp;#40;generated/snapshot.cpp&amp;amp;#41; containing the serialized V8 state. This file is then linked into QtScript to provide V8 with the snapshot.
The snapshot feature is aided by a tool, mksnapshot, which creates a V8 environment the "normal" way (by evaluating .js files et al), and generates a C++ file (generated/snapshot.cpp) containing the serialized V8 state. This file is then linked into QtScript to provide V8 with the snapshot.


mksnapshot is automatically built and run when building QtScript.
mksnapshot is automatically built and run when building QtScript.
Line 138: Line 156:
When cross-compiling, snapshots are disabled, since the mksnapshot tool would have to be run on the target system in order to generate the snapshot. (You could try generating it in an emulator, but the emulator would have to be perfectly compatible with the target — note that V8 detects certain CPU features at run time, and that the snapshot can contain native code that relies on certain features (instructions) being supported. If they aren't, undefined behavior (typically, a crash) will ensue.)
When cross-compiling, snapshots are disabled, since the mksnapshot tool would have to be run on the target system in order to generate the snapshot. (You could try generating it in an emulator, but the emulator would have to be perfectly compatible with the target — note that V8 detects certain CPU features at run time, and that the snapshot can contain native code that relies on certain features (instructions) being supported. If they aren't, undefined behavior (typically, a crash) will ensue.)


When deploying QtScript to devices, the snapshot would have to be generated in such a way that it's compatible with any supported device/configuration (the &quot;lowest common denominator&amp;quot; — e.g. ARMv5, VFPv2, …).
When deploying QtScript to devices, the snapshot would have to be generated in such a way that it's compatible with any supported device/configuration (the "lowest common denominator" — e.g. ARMv5, VFPv2, …).


==== Manually Disabling Snapshots ====
==== Manually Disabling Snapshots ====
Line 144: Line 162:
The snapshot feature is not enabled by default upstream, so it's perhaps not as well-tested as non-snapshot builds. If you encounter strange crashes in V8 code (typically at construction time), you can disable snapshots to quickly check whether that's the issue.
The snapshot feature is not enabled by default upstream, so it's perhaps not as well-tested as non-snapshot builds. If you encounter strange crashes in V8 code (typically at construction time), you can disable snapshots to quickly check whether that's the issue.


To disable snapshots, set the QMake variable V8SNAPSHOT to &quot;no&amp;quot;. E.g. <code>qmake -r &quot;V8SNAPSHOT=no&amp;quot;<code>
To disable snapshots, set the QMake variable V8SNAPSHOT to "no". E.g. <code>qmake -r "V8SNAPSHOT=no"<code>


and rebuild QtScript.
and rebuild QtScript.
Line 166: Line 184:
Lastly, in the Qt source tree, run
Lastly, in the Qt source tree, run


</code>util/scripts/mkdist-v8 &lt;tag&amp;gt;</code>
</code>util/scripts/mkdist-v8 <tag></code>


Everything will be staged, and you can run
Everything will be staged, and you can run
Line 178: Line 196:
If .cc files have been added, you must manually add them to src/script/v8/v8.pro.
If .cc files have been added, you must manually add them to src/script/v8/v8.pro.


You should try the build in both debug and release mode. If you get errors, try compiling V8 the normal way outside of Qt. Pay attention to the defines that are passed to the compiler; if there's a define missing for the QtScript/V8 build, add it to src/script/v8/v8.pri. (Note that the upstream V8 might not have been tested with(out) the defines we use; make sure that it's not a bug in V8, e.g. that the upstream V8 builds without ENABLE_LOGGING_AND_PROFILING defined (pass &quot;profilingsupport=off&amp;quot; to SCons).)
You should try the build in both debug and release mode. If you get errors, try compiling V8 the normal way outside of Qt. Pay attention to the defines that are passed to the compiler; if there's a define missing for the QtScript/V8 build, add it to src/script/v8/v8.pri. (Note that the upstream V8 might not have been tested with(out) the defines we use; make sure that it's not a bug in V8, e.g. that the upstream V8 builds without ENABLE_LOGGING_AND_PROFILING defined (pass "profilingsupport=off" to SCons).)


==== Excluding files ====
==== Excluding files ====

Latest revision as of 03:27, 20 April 2016

IMPORTANT: The content of this page is outdated. Reason: This project is abandoned
If you have checked or updated this page and found the content to be suitable, please remove this notice.
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

V8 is Google's open source JavaScript engine, used in the Google Chrome browser. The QtScript V8 port is an effort to implement the existing QtScript API on top of V8.

The master task for researching whether Qt (including QtWebKit) can be "switched over" to using V8 is QTBUG-12502.

Goal

The ideal outcome of this work is a new version of the QtScript library that QML, QtWebKit and Qt applications can readily benefit from (e.g., through improved script performance due to the V8 compiler technology).

We strive to preserve source, binary and behavioral with existing versions of QtScript, so that projects that already use QtScript can move to the V8-based version without much impact on their existing code.

Getting the Code

http://qt.gitorious.org/+qt-developers/qt/qt-script-ng

The repository is is a full clone of Qt that can be configured and built like usual. It includes a copy of V8-Isolates in src/3rdparty/v8, which is automatically built as part of building QtScript. master-v8 is the currently active branch.

Workflow

The port was started by removing the old QtScript implementation completely, and creating "stubs" (empty implementations) for all functions in the QtScript API. Thus, the starting point was a QtScript library that's fully source and binary compatible with previous QtScript releases. When an unimplemented API function is invoked, it will issue a "Not implemented" warning, and otherwise do nothing. This enables us to incrementally implement the API, while all the time being able to test the implementation against existing autotest suites and applications (including QML).

Our workflow consists of running an application/autotest and fixing failures, one at a time. Eventually, there will be no more failures, and no show-stopping performance regressions against previous QtScript releases, at which time the project can be considered "Done".

Test failures are tracked at QTBUG-17640.

About V8

Like QtScript, V8 provides a C++ API for embedding JavaScript into an application, and for integrating with native application code.

The V8 Embedder's Guide gives a good introduction to the V8 C++ API. If you plan to work on the QtScript port, it's a good idea to first familiarize yourself with the V8 design and concepts (handles, handle scopes, accessors, interceptors). The core API is all in one file in the V8 repository: include/v8.h. There are additional includes for e.g. debugging and profiling APIs.

The How to Download and Build V8 page has instructions for getting the V8 source code and building it. However, for starters you shouldn't have to worry about that, since the QtScript V8 port includes its own copy of V8 that's built as part of building QtScript.

If you want to follow V8 development closely, consider subscribing to the v8-dev list. This is a rather high-volume list, consisting mostly of automated mails from code reviews and commits. There's also a v8-users list for discussing how to use V8.

V8 Isolates

V8 Isolates is a research project by Google to support multiple V8 "instances" per process. This feature will enable Chromium to support in-process workers. Coincidentally, this feature is a requirement for the QtScript V8 port, since the QtScript API is re-entrant.

V8 Isolates is developed in the

experimental/isolates<code> branch in the V8 repository. Essentially, this branch gathers all static variables in V8 into thread-local storage. Isolates is the branch we track and import into QtScript.

Google aims to [http://code.google.com/p/v8/issues/detail?id=510#c15 eventually land Isolates on the main development branch].

== Relevant V8 Bugs ==

We use the [http://code.google.com/p/v8/issues/list V8 issue tracker] to report and track bugs relevant to QtScript, such as ECMA compliance issues, build issues, V8 API behavior, and suggestions for additional API. This section lists those issues.

* [http://code.google.com/p/v8/issues/detail?id=510 510]: Several V8 instances in a process
* [http://code.google.com/p/v8/issues/detail?id=1204 1204]: Limitation of setData method
* [http://code.google.com/p/v8/issues/detail?id=1205 1205]: Isolate::setData
* [http://code.google.com/p/v8/issues/detail?id=1021 1021]: Prototype Setter Interceptor not working
* [http://code.google.com/p/v8/issues/detail?id=1022 1022]: Interceptor "blocking" setter in prototype
* [http://code.google.com/p/v8/issues/detail?id=1028 1028]: RangeError in Array ctor (ECMA compliance)
* [http://code.google.com/p/v8/issues/detail?id=1035 -1035-]: Memory leak in Isolate class
* [http://code.google.com/p/v8/issues/detail?id=1036 -1036-]: Memory leak in Logger class
* [http://code.google.com/p/v8/issues/detail?id=884 884]: shell sample Infinite loop in JSObject::LookupCallbackSetterInPrototypes (''Possibly a gcc bug; watch out for it in release builds'')
* [http://code.google.com/p/v8/issues/detail?id=435 435]: The .length property of a function always returns zero (''actually, of native functions. Having this fixed would also provide one clean way to make the length parameter of QScriptEngine::newFunction() work'')
* [http://code.google.com/p/v8/issues/detail?id=1061 1061]: Some function not working in Isolate branch (work arounded in our patched V8)
* [http://code.google.com/p/v8/issues/detail?id=1072 1072]: TryCatch doesn't catch exceptions thrown from an accessor.
* [http://code.google.com/p/v8/issues/detail?id=1078 1078]: Assert when changing the prototype of the global object. (some test change the protoype of the global object)
* [http://code.google.com/p/v8/issues/detail?id=862 862]: GetOwnProperty() should return enumerable properties for String chars (''ECMA compliance item that we check in our autotests'')
* [http://code.google.com/p/v8/issues/detail?id=1256 -1256-]: v8::Array::New() ignores length argument (causes QScriptEngine::newArray(123) to be slow, since we have to set the length explicitly)
* [http://code.google.com/p/v8/issues/detail?id=831 831]: regexp performance seems to degrade exponentially with length of failed ([http://code.google.com/p/v8/issues/detail?id=1259 1259] has been merged into this issue)

== Reporting V8 Bugs ==

If you find a bug in V8 that affects QtScript, you should create a an upstream report in the [http://code.google.com/p/v8/issues/list V8 issue tracker]. It's important that the report has a minimal testcase, and in particular that it doesn't depend on QtScript; i.e., the testcase should use the V8 API directly.

Since we're working with a (not-necessarily-up-to-date) copy of V8 Isolates, it's possible that the bug isn't present in upstream V8 (because it's been fixed already). You should first try to reproduce the bug with V8 trunk or bleeding_edge, and report it straight away if it's present there. If you can't reproduce the bug in trunk or bleeding_edge, but can reproduce it in the Isolates branch, it's either because the fix hasn't been merged into Isolates yet, or there's an Isolates-specific bug. Depending on the nature of the bug, you'll have to try to decide which it is.

The purpose of the testcase is to make it easy for the V8 developers to reproduce the bug. Here's a template main.cpp:
  1. include <v8.h>
  2. include <assert.h>
  3. include <stdio.h>

int main(int argc, char **argv) {

v8::V8::SetFlagsFromCommandLine(&argc, argv, true);

v8::HandleScope handle_scope;

v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Put your global object properties here…
// global->Set(v8::String::New("foo"), v8::Integer::New(123));

v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);

assert(!context.IsEmpty());
v8::Context::Scope context_scope(context);

v8::TryCatch try_catch;

// Put your script here…

v8::Handle<v8::String> source = v8::String::New("'hello'");
assert(!source.IsEmpty());

v8::Handle<v8::Script> script = v8::Script::Compile(source);

assert(!script.IsEmpty());

v8::Handle<v8::Value> result = script->Run();

assert(!result.IsEmpty());

// Print the result…

v8::String::Utf8Value str(result);
printf("%s\n", *str);

context.Dispose();

v8::V8::Dispose();
return 0;

}

Assuming you've built V8 in $V8DIR, here's how you can build it with GCC:

g++ -I$V8DIR/include -L$V8DIR -lv8 template.cpp -o mybug

Once the testcase is working, you can strip away everything that's not needed (asserts and such) before submitting.

Finally, add a link to the newly created upstream bug in the previous section ("Relevant V8 Bugs"), and preferably refer to the bug in our JIRA ("This depends on the following upstream bug: …"), and source code (e.g., update the QEXPECT_FAIL message of a test).

Hacking on the V8 Build

This section provides information about how we build V8; this is useful to know if you encounter V8-related compile-time or runtime issues, for example.

By default, QtScript uses a copy of V8 located in

src/3rdparty/v8<code>

V8 is built as a separate (static) library that QtScript links against. The default (upstream) build system for V8 is [http://www.scons.org/ SCons]. However, to avoid a dependency on SCons (which isn't available on all Qt platform — Symbian in particular), we currently maintain a QMake project file for building V8:

src/script/v8.pro This file is a (manual) port of (a subset of) the SConstruct and src/SConscript files in the V8 repository.

Currently, Linux and Mac OS are the only regularly tested platforms.

generated/libraries.cpp

V8 implements large parts of its functionality (such as the core ECMA objects) in JavaScript itself; see the .js files in the V8 src/ directory. V8 evaluates these files as part of "bootstrapping" the V8 environment. In order to avoid an external dependency on the .js files, their contents are embedded in the V8 library (analogous to how Qt resource (.qrc) files are used by Qt). A Python script in the V8 directory (tools/js2c.py) generates a C++ file which is literally a char array of the .js contents, plus functions for retrieving script contents by index/name.

The js2c.py script is automatically invoked when building V8, and will regenerate libraries.cpp if any of the V8 .js files are modified.

Snapshots

By default, we build V8 with snapshot support. This is a feature of V8 that greatly improves creation time of the V8 environment. When using snapshots, instead of evaluating the thousands of lines of JavaScript contained in the built-in .js files (see previous section), V8 will initialize itself from a serialized version. This is possible since, for a given build of V8 on a given platform/CPU, the state of V8 after evaluating the built-in .js files will always be the same.

mksnapshot

The snapshot feature is aided by a tool, mksnapshot, which creates a V8 environment the "normal" way (by evaluating .js files et al), and generates a C++ file (generated/snapshot.cpp) containing the serialized V8 state. This file is then linked into QtScript to provide V8 with the snapshot.

mksnapshot is automatically built and run when building QtScript.

Versioning/Compatibility

The serialized V8 state is platform/CPU-specific, and isn't guaranteed to work across V8 versions (or the same V8 version compiled with different defines/compiler options). V8 has some code to check whether the snapshot data is actually useable; if it isn't, you'll hopefully get a failing assert during deserialization (rather than some obscure crash later).

Cross-compiling

When cross-compiling, snapshots are disabled, since the mksnapshot tool would have to be run on the target system in order to generate the snapshot. (You could try generating it in an emulator, but the emulator would have to be perfectly compatible with the target — note that V8 detects certain CPU features at run time, and that the snapshot can contain native code that relies on certain features (instructions) being supported. If they aren't, undefined behavior (typically, a crash) will ensue.)

When deploying QtScript to devices, the snapshot would have to be generated in such a way that it's compatible with any supported device/configuration (the "lowest common denominator" — e.g. ARMv5, VFPv2, …).

Manually Disabling Snapshots

The snapshot feature is not enabled by default upstream, so it's perhaps not as well-tested as non-snapshot builds. If you encounter strange crashes in V8 code (typically at construction time), you can disable snapshots to quickly check whether that's the issue.

To disable snapshots, set the QMake variable V8SNAPSHOT to "no". E.g.

qmake -r "V8SNAPSHOT=no"<code>

and rebuild QtScript.

==== Dynamic Snapshotting? ====

It would be awesome if, rather than being a compile-time step, V8 would create and use snapshots dynamically; e.g. the first time a V8 environment is created, a snapshot is automatically created, and subsequent V8 environments would be initialized from the snapshot.

=== Updating src/3rdparty/v8 ===

Updating Qt's copy of V8 is done using the util/scripts/mkdist-v8 script. It takes a git-svn checkout of V8 and copies a specified revision of it to src/3rdparty/v8.

Before running the script, you first need to set the git v8.url variable to point to your V8 checkout:

git config v8.url path/to/v8

Then, in the V8 repository, create a tag that references the SHA1 you want to import (e.g. HEAD):

git tag v8-snapshot-23032011<code>

Lastly, in the Qt source tree, run

util/scripts/mkdist-v8 <tag>

Everything will be staged, and you can run

git commit

to commit it all. But before you do that, you probably want to try to recompile QtScript.

Fixing the build

If .cc files have been added, you must manually add them to src/script/v8/v8.pro.

You should try the build in both debug and release mode. If you get errors, try compiling V8 the normal way outside of Qt. Pay attention to the defines that are passed to the compiler; if there's a define missing for the QtScript/V8 build, add it to src/script/v8/v8.pri. (Note that the upstream V8 might not have been tested with(out) the defines we use; make sure that it's not a bug in V8, e.g. that the upstream V8 builds without ENABLE_LOGGING_AND_PROFILING defined (pass "profilingsupport=off" to SCons).)

Excluding files