aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/jsruntime')
-rw-r--r--src/qml/jsruntime/qv4module.cpp77
-rw-r--r--src/qml/jsruntime/qv4module_p.h6
-rw-r--r--src/qml/jsruntime/qv4qmlcontext.cpp15
-rw-r--r--src/qml/jsruntime/qv4qmlcontext_p.h7
4 files changed, 86 insertions, 19 deletions
diff --git a/src/qml/jsruntime/qv4module.cpp b/src/qml/jsruntime/qv4module.cpp
index 19a036374f..237ada8321 100644
--- a/src/qml/jsruntime/qv4module.cpp
+++ b/src/qml/jsruntime/qv4module.cpp
@@ -46,6 +46,8 @@
#include <private/qv4symbol_p.h>
#include <private/qv4identifiertable_p.h>
+#include <QScopeGuard>
+
using namespace QV4;
DEFINE_OBJECT_VTABLE(Module);
@@ -98,20 +100,60 @@ void Heap::Module::init(ExecutionEngine *engine, CompiledData::CompilationUnit *
This->setPrototypeUnchecked(nullptr);
}
+void Module::evaluate()
+{
+ if (d()->evaluated)
+ return;
+ d()->evaluated = true;
+
+ CompiledData::CompilationUnit *unit = d()->unit;
+
+ unit->evaluateModuleRequests();
+
+ ExecutionEngine *v4 = engine();
+ Function *moduleFunction = unit->runtimeFunctions[unit->data->indexOfRootFunction];
+ CppStackFrame frame;
+ frame.init(v4, moduleFunction, nullptr, 0);
+ frame.setupJSFrame(v4->jsStackTop, Value::undefinedValue(), d()->scope,
+ Value::undefinedValue(), Value::undefinedValue());
+
+ frame.push();
+ v4->jsStackTop += frame.requiredJSStackFrameSize();
+ auto frameCleanup = qScopeGuard([&frame]() {
+ frame.pop();
+ });
+ Moth::VME::exec(&frame, v4);
+}
+
+const Value *Module::resolveExport(PropertyKey id) const
+{
+ if (d()->unit->isESModule()) {
+ if (!id.isString())
+ return nullptr;
+ Scope scope(engine());
+ ScopedString name(scope, id.asStringOrSymbol());
+ return d()->unit->resolveExport(name);
+ } else {
+ InternalClassEntry entry = d()->scope->internalClass->find(id);
+ if (entry.isValid())
+ return &d()->scope->locals[entry.index];
+ return nullptr;
+ }
+}
+
ReturnedValue Module::virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty)
{
if (id.isSymbol())
return Object::virtualGet(m, id, receiver, hasProperty);
const Module *module = static_cast<const Module *>(m);
- Scope scope(m->engine());
- ScopedString expectedName(scope, id.toStringOrSymbol(scope.engine));
- const Value *v = module->d()->unit->resolveExport(expectedName);
+ const Value *v = module->resolveExport(id);
if (hasProperty)
*hasProperty = v != nullptr;
if (!v)
return Encode::undefined();
if (v->isEmpty()) {
+ Scope scope(m->engine());
ScopedValue propName(scope, id.toStringOrSymbol(scope.engine));
return scope.engine->throwReferenceError(propName);
}
@@ -124,9 +166,7 @@ PropertyAttributes Module::virtualGetOwnProperty(const Managed *m, PropertyKey i
return Object::virtualGetOwnProperty(m, id, p);
const Module *module = static_cast<const Module *>(m);
- Scope scope(m->engine());
- ScopedString expectedName(scope, id.toStringOrSymbol(scope.engine));
- const Value *v = module->d()->unit->resolveExport(expectedName);
+ const Value *v = module->resolveExport(id);
if (!v) {
if (p)
p->value = Encode::undefined();
@@ -135,6 +175,7 @@ PropertyAttributes Module::virtualGetOwnProperty(const Managed *m, PropertyKey i
if (p)
p->value = v->isEmpty() ? Encode::undefined() : v->asReturnedValue();
if (v->isEmpty()) {
+ Scope scope(m->engine());
ScopedValue propName(scope, id.toStringOrSymbol(scope.engine));
scope.engine->throwReferenceError(propName);
}
@@ -147,9 +188,7 @@ bool Module::virtualHasProperty(const Managed *m, PropertyKey id)
return Object::virtualHasProperty(m, id);
const Module *module = static_cast<const Module *>(m);
- Scope scope(m->engine());
- ScopedString expectedName(scope, id.toStringOrSymbol(scope.engine));
- const Value *v = module->d()->unit->resolveExport(expectedName);
+ const Value *v = module->resolveExport(id);
return v != nullptr;
}
@@ -173,11 +212,7 @@ bool Module::virtualDeleteProperty(Managed *m, PropertyKey id)
if (id.isSymbol())
return Object::virtualDeleteProperty(m, id);
const Module *module = static_cast<const Module *>(m);
- Scope scope(m->engine());
- ScopedString expectedName(scope, id.toStringOrSymbol(scope.engine));
- if (!expectedName)
- return true;
- const Value *v = module->d()->unit->resolveExport(expectedName);
+ const Value *v = module->resolveExport(id);
if (v)
return false;
return true;
@@ -202,7 +237,7 @@ PropertyKey ModuleNamespaceIterator::next(const Object *o, Property *pd, Propert
Scope scope(module->engine());
ScopedString exportName(scope, scope.engine->newString(exportedNames.at(exportIndex)));
exportIndex++;
- const Value *v = module->d()->unit->resolveExport(exportName);
+ const Value *v = module->resolveExport(exportName->toPropertyKey());
if (pd) {
if (v->isEmpty())
scope.engine->throwReferenceError(exportName);
@@ -218,7 +253,17 @@ OwnPropertyKeyIterator *Module::virtualOwnPropertyKeys(const Object *o, Value *t
{
const Module *module = static_cast<const Module *>(o);
*target = *o;
- return new ModuleNamespaceIterator(module->d()->unit->exportedNames());
+
+ QStringList names;
+ if (module->d()->unit->isESModule()) {
+ names = module->d()->unit->exportedNames();
+ } else {
+ Heap::InternalClass *scopeClass = module->d()->scope->internalClass;
+ for (uint i = 0; i < scopeClass->size; ++i)
+ names << scopeClass->keyAt(i);
+ }
+
+ return new ModuleNamespaceIterator(names);
}
Heap::Object *Module::virtualGetPrototypeOf(const Managed *)
diff --git a/src/qml/jsruntime/qv4module_p.h b/src/qml/jsruntime/qv4module_p.h
index 0cab161b82..dca0678fe9 100644
--- a/src/qml/jsruntime/qv4module_p.h
+++ b/src/qml/jsruntime/qv4module_p.h
@@ -62,7 +62,8 @@ namespace Heap {
#define ModuleMembers(class, Member) \
Member(class, NoMark, CompiledData::CompilationUnit *, unit) \
Member(class, Pointer, CallContext *, scope) \
- Member(class, HeapValue, HeapValue, self)
+ Member(class, HeapValue, HeapValue, self) \
+ Member(class, NoMark, bool, evaluated)
DECLARE_EXPORTED_HEAP_OBJECT(Module, Object) {
DECLARE_MARKOBJECTS(Module)
@@ -75,6 +76,9 @@ DECLARE_EXPORTED_HEAP_OBJECT(Module, Object) {
struct Q_QML_EXPORT Module : public Object {
V4_OBJECT2(Module, Object)
+ void evaluate();
+ const Value *resolveExport(PropertyKey key) const;
+
static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty);
static PropertyAttributes virtualGetOwnProperty(const Managed *m, PropertyKey id, Property *p);
static bool virtualHasProperty(const Managed *m, PropertyKey id);
diff --git a/src/qml/jsruntime/qv4qmlcontext.cpp b/src/qml/jsruntime/qv4qmlcontext.cpp
index cc0b0feeee..88b0822f42 100644
--- a/src/qml/jsruntime/qv4qmlcontext.cpp
+++ b/src/qml/jsruntime/qv4qmlcontext.cpp
@@ -54,6 +54,7 @@
#include <private/qqmljavascriptexpression_p.h>
#include <private/qjsvalue_p.h>
#include <private/qv4qobjectwrapper_p.h>
+#include <private/qv4module_p.h>
QT_BEGIN_NAMESPACE
@@ -87,8 +88,20 @@ ReturnedValue QQmlContextWrapper::virtualGet(const Managed *m, PropertyKey id, c
QV4::ExecutionEngine *v4 = resource->engine();
QV4::Scope scope(v4);
- if (v4->callingQmlContext() != *resource->d()->context)
+ if (v4->callingQmlContext() != *resource->d()->context) {
+ if (resource->d()->module) {
+ Scoped<Module> module(scope, resource->d()->module);
+ bool hasProp = false;
+ ScopedValue value(scope, module->get(id, receiver, &hasProp));
+ if (hasProp) {
+ if (hasProperty)
+ *hasProperty = hasProp;
+ return value->asReturnedValue();
+ }
+ }
+
return Object::virtualGet(m, id, receiver, hasProperty);
+ }
bool hasProp = false;
ScopedValue result(scope, Object::virtualGet(m, id, receiver, &hasProp));
diff --git a/src/qml/jsruntime/qv4qmlcontext_p.h b/src/qml/jsruntime/qv4qmlcontext_p.h
index 4fe34a0a06..dd6de3323d 100644
--- a/src/qml/jsruntime/qv4qmlcontext_p.h
+++ b/src/qml/jsruntime/qv4qmlcontext_p.h
@@ -66,7 +66,12 @@ struct QQmlContextWrapper;
namespace Heap {
-struct QQmlContextWrapper : Object {
+#define QQmlContextWrapperMembers(class, Member) \
+ Member(class, Pointer, Module *, module)
+
+DECLARE_HEAP_OBJECT(QQmlContextWrapper, Object) {
+ DECLARE_MARKOBJECTS(QQmlContextWrapper);
+
void init(QQmlContextData *context, QObject *scopeObject);
void destroy();