aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4jscall_p.h
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-03-12 15:46:29 +0100
committerUlf Hermann <ulf.hermann@qt.io>2021-03-17 13:10:22 +0100
commit9caea013ceb221b5617c4940e7bb9ee9fecdd631 (patch)
treef4480e860a965f6515e516475baeced69b5d91fd /src/qml/jsruntime/qv4jscall_p.h
parentb0e9c83f99aa7090c82b7c55894f5e264b74608a (diff)
Clean up JSCallData setup
We either have pre-populated arguments and thisObject, then we can just use them and keep them const. Or, we want to allocate and populate the arguments and the thisObject. Then, do allocate them in a separate object, and transform that into JSCallData afterwards if necessary. Furthermore, avoid alloc(0) as that just returns the current stack top. Writing to it will clobber other data. Rather, just use nullptr and crash if it's written to. Also, remove the useless operator-> from JSCallData. That one just confuses the reader. Change-Id: I8310911fcfe005b05a07b78fcb3791d991a0c2ce Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4jscall_p.h')
-rw-r--r--src/qml/jsruntime/qv4jscall_p.h72
1 files changed, 47 insertions, 25 deletions
diff --git a/src/qml/jsruntime/qv4jscall_p.h b/src/qml/jsruntime/qv4jscall_p.h
index 43dc0de727..15ede20be3 100644
--- a/src/qml/jsruntime/qv4jscall_p.h
+++ b/src/qml/jsruntime/qv4jscall_p.h
@@ -61,39 +61,61 @@ QT_BEGIN_NAMESPACE
namespace QV4 {
-struct JSCallData {
- JSCallData(const Value *thisObject, const Value *argv, int argc)
- : argc(argc), args(const_cast<Value *>(argv)), thisObject(const_cast<Value *>(thisObject))
+template<typename Args>
+CallData *callDatafromJS(const Scope &scope, const Args *args, const FunctionObject *f = nullptr)
+{
+ int size = int(offsetof(QV4::CallData, args)/sizeof(QV4::Value)) + args->argc;
+ CallData *ptr = reinterpret_cast<CallData *>(scope.alloc<Scope::Uninitialized>(size));
+ ptr->function = Encode::undefined();
+ ptr->context = Encode::undefined();
+ ptr->accumulator = Encode::undefined();
+ ptr->thisObject = args->thisObject ? args->thisObject->asReturnedValue() : Encode::undefined();
+ ptr->newTarget = Encode::undefined();
+ ptr->setArgc(args->argc);
+ if (args->argc)
+ memcpy(ptr->args, args->args, args->argc*sizeof(Value));
+ if (f)
+ ptr->function = f->asReturnedValue();
+ return ptr;
+}
+
+struct JSCallArguments
+{
+ JSCallArguments(const Scope &scope, int argc = 0)
+ : thisObject(scope.alloc()), args(scope.alloc(argc)), argc(argc)
{
}
- JSCallData(const Scope &scope, int argc = 0)
- : argc(argc), args(scope.alloc(argc)), thisObject(scope.alloc())
+ CallData *callData(const Scope &scope, const FunctionObject *f = nullptr) const
{
+ return callDatafromJS(scope, this, f);
}
- JSCallData *operator->() {
- return this;
+ Value *thisObject;
+ Value *args;
+ const int argc;
+};
+
+struct JSCallData
+{
+ JSCallData(const Value *thisObject, const Value *argv, int argc)
+ : thisObject(thisObject), args(argv), argc(argc)
+ {
}
- CallData *callData(const Scope &scope, const FunctionObject *f = nullptr) const {
- int size = int(offsetof(QV4::CallData, args)/sizeof(QV4::Value)) + argc;
- CallData *ptr = reinterpret_cast<CallData *>(scope.alloc<Scope::Uninitialized>(size));
- ptr->function = Encode::undefined();
- ptr->context = Encode::undefined();
- ptr->accumulator = Encode::undefined();
- ptr->thisObject = thisObject ? thisObject->asReturnedValue() : Encode::undefined();
- ptr->newTarget = Encode::undefined();
- ptr->setArgc(argc);
- if (argc)
- memcpy(ptr->args, args, argc*sizeof(Value));
- if (f)
- ptr->function = f->asReturnedValue();
- return ptr;
+ Q_IMPLICIT JSCallData(const JSCallArguments &args)
+ : thisObject(args.thisObject), args(args.args), argc(args.argc)
+ {
}
- int argc;
- Value *args;
- Value *thisObject;
+
+ CallData *callData(const Scope &scope, const FunctionObject *f = nullptr) const
+ {
+ return callDatafromJS(scope, this, f);
+ }
+
+ const Value *thisObject;
+ const Value *args;
+ const int argc;
};
inline
@@ -108,7 +130,7 @@ ReturnedValue FunctionObject::call(const JSCallData &data) const
return call(data.thisObject, data.args, data.argc);
}
-void populateJSCallArguments(ExecutionEngine *v4, JSCallData &jsCall, int argc,
+void populateJSCallArguments(ExecutionEngine *v4, JSCallArguments &jsCall, int argc,
void **args, const QMetaType *types);
struct ScopedStackFrame {