aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2018-06-22 13:04:29 +0200
committerLars Knoll <lars.knoll@qt.io>2018-07-02 19:29:21 +0000
commitd21ef386ece52490d428daacb8f7f4658e9f78e5 (patch)
tree49ca7e73d391a430cd797ca988304e3e338a7457 /src
parent22ef109ccdc64e0f9afb82b1b7b3d4c39e31a5ab (diff)
Add basic support for subclassing
Set the prototype chain up correctly when subclassing. Many things still won't work, as we do not yet support the super keyword at all. Change-Id: Id5dbe3de9ffad98d67b7e6f6958b2cd727733786 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qml/jsruntime/qv4runtime.cpp22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp
index 5f8b7d31ca..7f2738321c 100644
--- a/src/qml/jsruntime/qv4runtime.cpp
+++ b/src/qml/jsruntime/qv4runtime.cpp
@@ -1485,16 +1485,11 @@ ReturnedValue Runtime::method_objectLiteral(ExecutionEngine *engine, int classId
return o.asReturnedValue();
}
-ReturnedValue Runtime::method_createClass(ExecutionEngine *engine, int classIndex, const Value &heritage, const Value *computedNames)
+ReturnedValue Runtime::method_createClass(ExecutionEngine *engine, int classIndex, const Value &superClass, const Value *computedNames)
{
const CompiledData::CompilationUnit *unit = engine->currentStackFrame->v4Function->compilationUnit;
const QV4::CompiledData::Class *cls = unit->data->classAt(classIndex);
- if (!heritage.isEmpty()) {
- // ####
- return engine->throwTypeError(QStringLiteral("classes with heritage not yet supported."));
- }
-
Scope scope(engine);
ScopedString name(scope);
if (cls->nameIndex != UINT_MAX)
@@ -1502,6 +1497,21 @@ ReturnedValue Runtime::method_createClass(ExecutionEngine *engine, int classInde
// ### fix heritage
ScopedObject protoParent(scope, engine->objectPrototype());
ScopedObject constructorParent(scope, engine->functionPrototype());
+ if (!superClass.isEmpty()) {
+ if (superClass.isNull()) {
+ protoParent = Encode::null();
+ } else {
+ // ### check that the heritage object is a constructor
+ if (!superClass.isFunctionObject())
+ return engine->throwTypeError(QStringLiteral("The superclass is not a function object."));
+ const FunctionObject *s = static_cast<const FunctionObject *>(&superClass);
+ ScopedValue result(scope, s->get(scope.engine->id_prototype()));
+ if (!result->isObject() && !result->isNull())
+ return engine->throwTypeError(QStringLiteral("The value of the superclass's prototype property is not an object."));
+ protoParent = *result;
+ constructorParent = superClass;
+ }
+ }
ScopedObject proto(scope, engine->newObject());
proto->setPrototypeUnchecked(protoParent);