Commit Graph

64 Commits

Author SHA1 Message Date
Lars Knoll af22149dd8 Avoid side effects when en exception has been thrown.
We don't want to check for exceptions after every single
line on our runtime methods. A better way to handle this
is to add the check in all methods that have direct side
effects (as e.g. writing to a property of the JS stack).

We also need to return whereever we throw an exception.
To simplify the code, ExecutionContext::throwXxx methods now
return a ReturnedValue (always undefined) for convenience.

Change-Id: Ide6c804f819c731a3f14c6c43121d08029c9fb90
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-29 10:38:59 +01:00
Lars Knoll e0284ab41f Properly propagate parse errors
Replace all try/catch statements used when parsing
with checks for engine->hasException.

Change-Id: I4493cb600d5a3eb095c2003bb88bd031403e47c9
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-29 10:38:55 +01:00
Lars Knoll 34bf0139c7 Rework IR code generation for try/catch/finally
Simplify the generated code. Add a special block to catch
exceptions thrown inside a catch() statement.

store the exception on the stack when entering finally and
rethrow it at the end. This ensure correct behavior for
break/continue/return statements inside finally.

Don't check for exceptions after calling push_catch_scope
and pop_scope in the JIT'ed code. This can lead to infinite
loops when throwing inside an exception handler.

Change-Id: I67e9325794e2fd25b0773b21e02fbaadb43faab0

Change-Id: Ic1ea9c0c43eec1d49177dc1ab4552a1da04e96fe
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-29 10:38:48 +01:00
Lars Knoll 5229a8b259 Rework exception handling
Start the work to remove c++ exceptions from our JS
exception handling. Rather rely on engine->hasException.
Check the flag after we return from any runtime call in the
JIT.

Implement new try/catch handling code in qv4codegen and
for the JIT that doesn't rely on exceptions. As an added
bonus, we can remove the Try statement in the IR.

Change-Id: Ic95addd6ae03371c43c47e04cac26afdce23a061
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-29 10:38:45 +01:00
Lars Knoll 9fd4591a61 Remove internal method from public API
QQmlError is public API and shouldn't expose an internal
method.

Change-Id: I7caf06af9340fefec5c96103395fe74acbf19497
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-19 14:59:11 +02:00
Simon Hausmann 12c3579136 Speed up exception propagation
Avoid catch (...) with re-throw as it turns that this is very slow because it
throws a new exception and the unwinder starts from scratch. Instead use stack
allocated objects and cleaning destructors to restore state before continuing
with the propagation of exceptions.

Change-Id: I6d95026bcd60b58cb6258a9dae28623a46739532
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-10-16 16:03:57 +02:00
Simon Hausmann c1c526aafb Speed up stack trace generation for the JIT
It turns out that in QML it is not unusual that during early binding
evaluations due to the undefined order, the evaluation tries to look up
properties in objects that aren't initialized yet and thus exceptions are
thrown. Eeach thrown exception saves a stack trace, which is expensive to
generate when using the JIT, as it does full stack unwinding.

This patch implements a more light-weight approach by storing the instruction
pointer in the context before leaving JIT generated code.

Change-Id: I95e1cfd01179247dfc2c1df949828f474a23161b
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-10-16 16:03:54 +02:00
Lars Knoll b538231cb2 Fix GC issues with usage of raw RegExp pointers
Properly protect them through Scoped values.

Change-Id: I5a0a1d5580d55ecff493419baa8959751a65f1d3
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-15 20:57:37 +02:00
Lars Knoll e964fc3426 Fix the remaining objects against self destruction
This makes pretty much all test cases pass with exact
garbage collection.

Change-Id: Ia874e3c17c3984afb7cfe370f9bd3ad8fe46699a
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-11 16:01:24 +02:00
Simon Hausmann 7d9780b630 Cleanup exception handling
The code in the Exception class operates entirely on the engine's data,
so move it into ExecutionEngine instead. This eliminates the need for
a QV4::Exception class and catches and old code that tries to still do
catch (Exception &) instead of catch (...)

Change-Id: Ie608bec6af652038aca6c9423c225a4d7eb13b39
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-10-03 09:24:41 +02:00
Simon Hausmann c860d1399b Change exception handling API
This patch changes the exception handling API in the engine slightly, encapsulating
any use of direct throw statements and catch blocks with concrete types. In the future
we need to be able to change the way these are implemented, in order to ensure that
the correct stack unwinding code is triggered for throw and re-throw.

This patch separates the C++ exception object thrown from the V4 exception
(that includes value, throwing context pointer) and stores the latter inside
the engine.

In order for that to compile, ExecutionEngine::StackTrace and StackFrame had to
move into the QV4 namespace directly.

In addition the syntax for catching exceptions changes from

try {
    ...
} catch (QV4::Exception &ex) {
    ex.accept(context);
    QV4::ScopedValue exceptionValue(scope, ex.value());
}

to

try {
    ...
} catch (...) {
    QV4::ScopedValue exception(scope, context->catchException());
}

Context::catchException() checks if there's a "current" exception in the engine,
and if not assumes that we caught an unrelated exception and consequently re-throws.

partiallyUnwind() is also gone and replaced with rethrowException(), in order to
encapsulate the re-throw.

Lastly, in the future nesting try/catch blocks isn't going to be possible due to
limitations in the common C++ ABI with regards to foreign exceptions.

Change-Id: Ic81c75b057a2147e3176d8e0b4d326c14278b47d
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-10-02 16:07:33 +02:00
Lars Knoll 1e454c8aa6 Remove more uses of Value
Change-Id: I889e760f75b485a28e1f2a2c26b2337ae9bfafac
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-02 16:07:33 +02:00
Lars Knoll 430dfd326c Fix ObjectIterator API to be GC safe
Change-Id: I3a9c48d53d8dbadcb9b32c00fcef1f89447c4b8c
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-10-02 16:07:33 +02:00
Lars Knoll aacebc74b2 Remove more occurrences of QV4::Value
Change-Id: I66c370680d7e6bee2e73a7a940aa96ab4009ec57
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-30 08:05:38 +02:00
Lars Knoll 62d1b5a08a Fix API for Object::define*Property
use ValueRef instead of const Value &.

Change-Id: I3fd0ca829870db27f036825d713c53dc0600be07
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:58 +02:00
Lars Knoll 112531bc23 Less QV4::Value usage
Fix some usages in qv4engine, and fix return types
in methods in qqmlxmlhttprequest.

Change-Id: I3d6225ca01bf7ea77fcc424914c8392bb6c3a454
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:54 +02:00
Lars Knoll 18d4794e3f Fix Value usage in ErrorObjects
Change-Id: Iaa14ad5a8d3f085843e49195f8f4bb7bb020b9b6
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:43 +02:00
Lars Knoll 150731fc68 Remove more direct QV4::Value usage
Remove Value::fromString(String *), and make
Encode safe against encoding raw Managed * pointers.

Change-Id: Ibca4668e1cbeaf85c78169d14386281659d33ef6
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:39 +02:00
Lars Knoll cf2a253f2f Move Value::fromBool, ... to a new Primitive class
This will simplify finding the remaining direct usages of
QV4::Value that need fixing.

Change-Id: I223099727436d5748027c84c53d9dfc4028e38ed
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:24 +02:00
Lars Knoll 7872b38006 Use SafeValue in more places
Change-Id: Ic15c1419c74f22bd7639ce8746ff11b15240b718
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:19 +02:00
Lars Knoll 0f2cf9074d Fix CallContext to not hold arguments on the C stack anymore
Change-Id: I35f46cce4f243d4b8b2bac9244f8fc26836f413b
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-28 13:33:08 +02:00
Lars Knoll 12fd2ccd3f Fix Persistent/WeakValue API
Don't use unprotected Values in the API anymore.

Change-Id: I8851628227fca374de24701bc8ee0908b5ae3923
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-26 09:05:16 +02:00
Lars Knoll 47bf40dd49 Prevent objects from being collected while in their constructor
While objects are being constructed, we don't have a reference to them
on the JS stack yet. So the constructor needs to protect itself against
being collected by putting the this object onto the JS stack.

Added an environment switch MM_EXACT_GC to test exact garbage
collection.

Change-Id: Ie37665a954de800359c272ffbebbe1488e7a8ace
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 78b49cf836 Convert the last methods in qv4object_p.h
Change-Id: I4fda83a0832760c277e629d4e658da718c0bf92b
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 700ba1bcb3 Use a StringRef for Managed::get()
also store "toString" and "valueOf" as identifiers
in the engine and fix two places where we compared
strings the wrong way.

Change-Id: I70612221e72d43ed0e3c496e4209681bf254cded
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 3c325823a7 Cleanup Object::define*Property API
Change-Id: I99125908a9bc1d41a2642c409af9704def7a0832
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 8b3623ee7b Start using StringRef for parameter passing
Change-Id: If2c41daeda2862cd1162c5da8163a9d62fe4111d
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 055f71f87d Introduce a Referenced<T> class to pass Objects into methods
Added some convenience typedefs (StringRef, ObjectRef, ReturnedString,
ScopedString, ...)
Used StringRef in newBuiltinFunction() for testing.
Cleaned up the duplicated code for thrower functions.

Change-Id: I7b7676690cbe70d9eabb0a5afd0d922f0be3aefd
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll 2d781c4ca4 Cleanup ExecutionEngine::newBuiltinFunction() usages
And change the return type to be GC safe

Change-Id: I6d7513962370fea4072a3d8c6b2c6f2d1705992e
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Lars Knoll e441692b0b Further work towards an exact GC
Add some more convenience in the helper classes
in qscopedvalue_p.h
Make accesses to CallData safer, and change
ExecutionEngine::newObject() to return a safe
pointer.

Change-Id: I980909754ce9681cf6faa1355bab3a1e5d6dd186
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-22 01:06:20 +02:00
Simon Hausmann 57f7545f67 Fix destruction of QV4::CompiledData::CompilationUnit objects
Now that we store them per QML file, they live in the type cache and
can outlive the engine. Therefore the engine needs to free to unlink
any remaining units upon destruction. This needs to be done after the
"death" of the memory manager, which is likely to sweep away any
function objects that also hold a reference to the compilation units.

Change-Id: I3968d5995289e8d2bc1e3abbb1f8be88a0ab4e03
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-09-20 14:27:39 +02:00
Lars Knoll f79df5da07 Convert more methods to return a Returned<>
Change-Id: If294c9c4f574824c308b63a11da1337226180105
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:14:13 +02:00
Lars Knoll e4e90923c9 Convert some methods to use Returned<>
Change-Id: I631606cb5ab3b35b72104e70092a5200dd235fbc
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:14:10 +02:00
Lars Knoll 9dcc12d523 Convert a few more methods in ExecutionEngine to use protected pointers
Change-Id: I2e6b1b9dff52bbd16f35a00152c9ac557f8346eb
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:14:08 +02:00
Lars Knoll 49aead7f23 Extend the ReturnedValue mechanism to pointers to Managed objects
Add a Returned<T> that we can return instead of raw pointers
to Managed objects.

Start using the Returned<T> for a few methods.

Also clean up all our classes to use the Q_MANAGED macro instead
of manually defining their vtable.

Change-Id: I0a2962e47f3de955cd2cd8474f8f3fcc9e36d084
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:14:03 +02:00
Lars Knoll 16f92ad85c Convert builtin methods to return a ReturnedValue
Change-Id: I6b75adbf53a5be0deab023d2eed98ce2a7915551
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:13:47 +02:00
Lars Knoll 7d4e61dd82 Rename BuiltinFunctionOld to BuiltinFunction
This is the correct abstraction for a builtin method.
Time to get rid of the 'old' in the name.

Change-Id: Ia386d2cc8ef0e6c269ab24da6fc8ebfac52d7f69
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-18 13:13:44 +02:00
Lars Knoll e6180ecdc2 Properly unwind the js stack for generated code
Surround all calls into generated code with a
try {} catch {} statement that resets the jstack
to the correct position.

Like this we properly unwind the js stack in all cases, and
can also use stricter assertions in our ScopedCallData, etc.
classes to check that the stack is healthy.

Change-Id: I7ca03e06ea55007be683305d9c2a6898cf5fc689
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-11 13:02:00 +02:00
Lars Knoll 1986cf68d0 Create a stack for JS values and use it in the interpreter
First step towards being able to do an exact GC.

Create a stack for JS Values that is separate from the C++
stack.

Use the stack for generated methods (masm and moth).

Change-Id: I80ac0e5b5d86439dda5e9ea2b21fa0c57d8aef22
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-11 13:01:44 +02:00
Erik Verbruggen ce715a34b2 V4: add environment variable QV4_FORCE_INTERPRETER
By setting this variable, the interpreter will always be used, even when
the JIT is available.

Change-Id: I66a439b649fd22e0d10be4f07f1e7be392b012f5
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-11 09:42:12 +02:00
Lars Knoll ea0ea907ed Optimize String.replace and RegExp.exec
This speeds up the v8 regexp benchmark by a factor 2.5 :)

Change-Id: Ibd6b18ee28181aa712429cbec4598984e0c69820
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 6359ab63cd Optimize ScriptFunction::construct() and creation of FunctionObjects
Change-Id: I7df04171a26cbe659e85f14878cc4e51030e8a5a
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 4dbb2ab600 Remove more usages of Object::setPrototype()
Change-Id: I33383baf14e764ce79252a100a6d769bde75331a
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 3bf081203e Remove more usages of Object::setPrototype()
Change-Id: I8c49f61bc85e3d98dea90bf05db1a3f22d08f7b1
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll d8e31c098d Cleanup calls to Object::setPrototype()
Rather use the correct internalClass directly when constructing
the objects.

Change-Id: I8e916f1ce8f83d291c08ca6332fe85b1f57b90b5
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll edee5c3dc0 Move prototype pointer into QV4::InternalClass
The prototype is actually the same for most objects. By
moving it into the internal class, we can save 8 bytes
per object, as well as allowing for some future
optimizations.

Also fix a bug in the implementation of the Error
prototype objects.

Change-Id: I4d4b641055f644a9b088f27be34bfdb0446279b7
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 3ad8b0f0e8 Add the object's prototype to the InternalClass structure
Change-Id: Ifa97d3354a7a7afadf70f9ba540716bd5b1eef44
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 6c69cdb1af Remove unused arvument from Object constructor
Change-Id: I2c26bd8af9b352baed9b84a6b6401633ded4da85
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll da2f24d8e5 move methods to create a new context into the ExecutionContext class
This avoids one indirection when calling the methods and cleans up
the engine a bit.

Change-Id: I426f41e23f6a7262af95b9807b00920530fef642
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00
Lars Knoll 29f946cdad Add a SimpleScriptFunction class
Choose whether we use a stack based context for a function, when
the actual closure is generated, not at call time. This speeds up
function calling for leaf functions.

Change-Id: Ibcbf3acb5610a7f59b6474e982122df03c1c5298
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
2013-09-02 17:27:36 +02:00