Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4context.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QString>
5#include <qv4context_p.h>
6#include <qv4object_p.h>
7#include <qv4objectproto_p.h>
8#include <private/qv4mm_p.h>
10#include "qv4function_p.h"
11#include "qv4stackframe_p.h"
12#include "qv4symbol_p.h"
13
14using namespace QV4;
15
18
19Heap::CallContext *ExecutionContext::newBlockContext(CppStackFrame *frame, int blockIndex)
20{
21 Function *function = frame->v4Function;
22
23 Heap::InternalClass *ic = function->executableCompilationUnit()->runtimeBlocks.at(blockIndex);
24 uint nLocals = ic->size;
25 size_t requiredMemory = sizeof(CallContext::Data) - sizeof(Value) + sizeof(Value) * nLocals;
26
27 ExecutionEngine *v4 = function->internalClass->engine;
28 Heap::CallContext *c = v4->memoryManager->allocManaged<CallContext>(requiredMemory, ic);
29 c->init();
30 c->type = Heap::ExecutionContext::Type_BlockContext;
31
32 Heap::ExecutionContext *outer = static_cast<Heap::ExecutionContext *>(frame->context()->m());
33 c->outer.set(v4, outer);
34 if (frame->isJSTypesFrame()) {
35 c->function.set(v4, static_cast<Heap::JavaScriptFunctionObject *>(
37 static_cast<JSTypesStackFrame *>(frame)->jsFrame->function).m()));
38 } else {
39 c->function.set(v4, nullptr);
40 }
41
42 c->locals.size = nLocals;
43 c->locals.alloc = nLocals;
44
45 c->setupLocalTemporalDeadZone(function->executableCompilationUnit()->unitData()->blockAt(blockIndex));
46
47 return c;
48}
49
51 Heap::CallContext *callContext)
52{
53 uint nLocals = callContext->locals.alloc;
54 size_t requiredMemory = sizeof(CallContext::Data) - sizeof(Value) + sizeof(Value) * nLocals;
55
56 Heap::CallContext *c = engine->memoryManager->allocManaged<CallContext>(
57 requiredMemory, callContext->internalClass);
58 memcpy(c, callContext, requiredMemory);
59
60 return c;
61}
62
64{
65 Function *function = frame->v4Function;
66 Heap::ExecutionContext *outer = static_cast<Heap::ExecutionContext *>(frame->context()->m());
67
68 uint nFormals = qMax(static_cast<uint>(frame->argc()), function->nFormals);
69 uint localsAndFormals = function->compiledFunction->nLocals + nFormals;
70 size_t requiredMemory = sizeof(CallContext::Data) - sizeof(Value) + sizeof(Value) * (localsAndFormals);
71
72 ExecutionEngine *v4 = outer->internalClass->engine;
73 Heap::CallContext *c = v4->memoryManager->allocManaged<CallContext>(requiredMemory, function->internalClass);
74 c->init();
75
76 c->outer.set(v4, outer);
77 c->function.set(v4, static_cast<Heap::JavaScriptFunctionObject *>(
78 Value::fromStaticValue(frame->jsFrame->function).m()));
79
80 const CompiledData::Function *compiledFunction = function->compiledFunction;
81 uint nLocals = compiledFunction->nLocals;
82 c->locals.size = nLocals;
83 c->locals.alloc = localsAndFormals;
84 // memory allocated from the JS heap is 0 initialized, so check if empty is 0
86
87 c->setupLocalTemporalDeadZone(compiledFunction);
88
89 Value *args = c->locals.values + nLocals;
90 ::memcpy(args, frame->argv(), frame->argc() * sizeof(Value));
91 c->nArgs = frame->argc();
92 for (uint i = frame->argc(); i < function->nFormals; ++i)
94
95 return c;
96}
97
98Heap::ExecutionContext *ExecutionContext::newWithContext(Heap::Object *with) const
99{
100 Heap::ExecutionContext *c = engine()->memoryManager->alloc<ExecutionContext>(Heap::ExecutionContext::Type_WithContext);
101 c->outer.set(engine(), d());
102 c->activation.set(engine(), with);
103
104 return c;
105}
106
107Heap::ExecutionContext *ExecutionContext::newCatchContext(CppStackFrame *frame, int blockIndex, Heap::String *exceptionVarName)
108{
109 Scope scope(frame->context());
110 ScopedString name(scope, exceptionVarName);
111 ScopedValue val(scope, scope.engine->catchException(nullptr));
112 ScopedContext ctx(scope, newBlockContext(frame, blockIndex));
113 ctx->setProperty(name, val);
114 return ctx->d();
115}
116
118{
119 Scope scope(this);
120
121 // find the right context to create the binding on
122 ScopedObject activation(scope);
123 ScopedContext ctx(scope, this);
124 while (ctx) {
125 switch (ctx->d()->type) {
126 case Heap::ExecutionContext::Type_CallContext:
127 if (!activation) {
128 Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx->d());
129 if (!c->activation)
130 c->activation.set(scope.engine, scope.engine->newObject());
131 activation = c->activation;
132 }
133 break;
134 case Heap::ExecutionContext::Type_QmlContext: {
135 // this is ugly, as it overrides the inner callcontext, but has to stay as long
136 // as bindings still get their own callcontext
137 activation = ctx->d()->activation;
138 break;
139 }
140 case Heap::ExecutionContext::Type_GlobalContext: {
141 Q_ASSERT(scope.engine->globalObject->d() == ctx->d()->activation);
142 if (!activation)
143 activation = ctx->d()->activation;
144 break;
145 }
146 case Heap::ExecutionContext::Type_BlockContext:
147 // never create activation records on block contexts
148 default:
149 break;
150 }
151 ctx = ctx->d()->outer;
152 }
153
154 PropertyKey id = name->toPropertyKey();
155 if (activation->getOwnProperty(id) != Attr_Invalid)
156 return;
157 ScopedProperty desc(scope);
159 attrs.setConfigurable(deletable);
160 if (!activation->defineOwnProperty(id, desc, attrs))
161 scope.engine->throwTypeError();
162}
163
164static bool unscopable(ExecutionEngine *engine, Heap::Object *withObject, PropertyKey id)
165{
166 if (!withObject)
167 return false;
168 Scope scope(engine);
169 ScopedObject w(scope, withObject);
170 ScopedObject o(scope, w->get(scope.engine->symbol_unscopables()));
171 if (o) {
172 ScopedValue blocked(scope, o->get(id));
173 return blocked->toBoolean();
174 }
175 return false;
176}
177
179{
180 PropertyKey id = name->toPropertyKey();
181
182 Heap::ExecutionContext *ctx = d();
183 ExecutionEngine *engine = ctx->internalClass->engine;
184
185 for (; ctx; ctx = ctx->outer) {
186 switch (ctx->type) {
187 case Heap::ExecutionContext::Type_BlockContext:
188 case Heap::ExecutionContext::Type_CallContext: {
189 Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
190 uint index = c->internalClass->indexOfValueOrGetter(id);
191 if (index < UINT_MAX)
192 // ### throw in strict mode?
193 return false;
195 }
196 case Heap::ExecutionContext::Type_WithContext: {
197 if (ctx->activation) {
198 Scope scope(this);
199 ScopedObject object(scope, ctx->activation);
200 if (object && object->hasProperty(id)) {
201 bool u = ::unscopable(engine, ctx->activation, id);
202 if (engine->hasException)
203 return false;
204 if (u)
205 break;
206 return object->deleteProperty(id);
207 }
208 }
209 break;
210 }
211 case Heap::ExecutionContext::Type_GlobalContext: {
212 if (ctx->activation) {
213 Scope scope(this);
214 ScopedObject object(scope, ctx->activation);
215 if (object && object->hasProperty(id))
216 return object->deleteProperty(id);
217 }
218 break;
219 }
220 case Heap::ExecutionContext::Type_QmlContext:
221 // can't delete properties on qml objects
222 break;
223 }
224 }
225
227}
228
230{
231 PropertyKey id = name->toPropertyKey();
232
233 Heap::ExecutionContext *ctx = d();
234 QV4::ExecutionEngine *engine = ctx->internalClass->engine;
235
236 for (; ctx; ctx = ctx->outer) {
237 switch (ctx->type) {
238 case Heap::ExecutionContext::Type_WithContext: {
239 Scope scope(engine);
240 ScopedObject w(scope, ctx->activation);
241 if (w->hasProperty(id)) {
242 bool u = ::unscopable(engine, ctx->activation, id);
243 if (engine->hasException)
244 return TypeError;
245 if (u)
246 break;
247 if (!w->put(name, value))
248 return TypeError;
249 return NoError;
250 }
251 break;
252 }
253 case Heap::ExecutionContext::Type_BlockContext:
254 case Heap::ExecutionContext::Type_CallContext: {
255 Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
256 uint index = c->internalClass->indexOfValueOrGetter(id);
257 if (index < UINT_MAX) {
258 static_cast<Heap::CallContext *>(c)->locals.set(engine, index, value);
259 return NoError;
260 }
261 }
263 case Heap::ExecutionContext::Type_GlobalContext:
264 if (ctx->activation) {
265 auto member = ctx->activation->internalClass->findValueOrSetter(id);
266 if (member.index < UINT_MAX) {
267 Scope scope(engine);
268 ScopedObject a(scope, ctx->activation);
269 if (!a->putValue(member.index, member.attrs, value))
270 return TypeError;
271 return NoError;
272 }
273 }
274 break;
275 case Heap::ExecutionContext::Type_QmlContext: {
276 Scope scope(engine);
277 ScopedObject activation(scope, ctx->activation);
278 if (!activation->put(name, value))
279 return TypeError;
280 return NoError;
281 }
282 }
283
284 }
285
286 return RangeError;
287}
288
290{
291 PropertyKey id = name->toPropertyKey();
292
293 Heap::ExecutionContext *ctx = d();
294 QV4::ExecutionEngine *engine = ctx->internalClass->engine;
295
296 for (; ctx; ctx = ctx->outer) {
297 switch (ctx->type) {
298 case Heap::ExecutionContext::Type_BlockContext:
299 case Heap::ExecutionContext::Type_CallContext: {
300 Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
301
302 const uint index = c->internalClass->indexOfValueOrGetter(id);
303 if (index < c->locals.alloc)
304 return c->locals[index].asReturnedValue();
305
306 // TODO: We should look up the module imports here, but those are part of the CU:
307 // imports[index - c->locals.size];
308 // See QTBUG-118478
309
311 }
312 case Heap::ExecutionContext::Type_WithContext:
313 if (ctx->activation) {
314 Scope scope(this);
315 ScopedObject activation(scope, ctx->activation);
316 if (activation->hasProperty(id)) {
317 bool u = ::unscopable(engine, ctx->activation, id);
318 if (engine->hasException)
319 return false;
320 if (u)
321 break;
322 return activation->get(id);
323 }
324 }
325 break;
326 case Heap::ExecutionContext::Type_GlobalContext:
327 case Heap::ExecutionContext::Type_QmlContext: {
328 if (ctx->activation) {
329 Scope scope(this);
330 ScopedObject activation(scope, ctx->activation);
331 bool hasProperty = false;
332 ReturnedValue v = activation->get(id, nullptr, &hasProperty);
333 if (hasProperty)
334 return v;
335 }
336 break;
337 }
338 }
339 }
341}
342
344{
345 base->setM(nullptr);
346 PropertyKey id = name->toPropertyKey();
347
348 Heap::ExecutionContext *ctx = d();
349 QV4::ExecutionEngine *engine = ctx->internalClass->engine;
350
351 for (; ctx; ctx = ctx->outer) {
352 switch (ctx->type) {
353 case Heap::ExecutionContext::Type_BlockContext:
354 case Heap::ExecutionContext::Type_CallContext: {
355 Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
356
357 const uint index = c->internalClass->indexOfValueOrGetter(id);
358 if (index < c->locals.alloc)
359 return c->locals[index].asReturnedValue();
360
361 // TODO: We should look up the module imports here, but those are part of the CU:
362 // imports[index - c->locals.size];
363 // See QTBUG-118478
364
366 }
367 case Heap::ExecutionContext::Type_GlobalContext: {
368 if (ctx->activation) {
369 Scope scope(this);
370 ScopedObject activation(scope, ctx->activation);
371 bool hasProperty = false;
372 ReturnedValue v = activation->get(name, &hasProperty);
373 if (hasProperty)
374 return v;
375 }
376 break;
377 }
378 case Heap::ExecutionContext::Type_WithContext:
379 if (ctx->activation) {
380 Scope scope(this);
381 ScopedObject activation(scope, ctx->activation);
382 if (activation->hasProperty(id)) {
383 bool u = ::unscopable(engine, ctx->activation, id);
384 if (engine->hasException)
385 return false;
386 if (u)
387 break;
388 base->setM(activation->d());
389 return activation->get(id);
390 }
391 }
392 break;
393 case Heap::ExecutionContext::Type_QmlContext: {
394 Scope scope(this);
395 ScopedObject o(scope, ctx->activation);
396 bool hasProperty = false;
397 ReturnedValue v = o->get(id, nullptr, &hasProperty);
398 if (hasProperty) {
399 base->setM(o->d());
400 return v;
401 }
402 break;
403 }
404 }
405 }
407}
408
409void Heap::CallContext::setArg(uint index, Value v)
410{
411 locals.set(internalClass->engine, locals.size + index, v);
412}
ManagedType::Data * allocManaged(std::size_t size, Heap::InternalClass *ic)
Definition qv4mm_p.h:199
ManagedType::Data * alloc(Args &&... args)
Definition qv4mm_p.h:307
EGLContext ctx
quint64 ReturnedValue
Scoped< String > ScopedString
@ Attr_Invalid
@ Attr_Data
Scoped< ExecutionContext > ScopedContext
#define Q_FALLTHROUGH()
static struct AttrInfo attrs[]
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLuint object
[3]
GLuint name
const GLubyte * c
GLuint GLfloat * val
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int uint
Definition qtypes.h:34
static const uint base
Definition qurlidna.cpp:20
static bool unscopable(ExecutionEngine *engine, Heap::Object *withObject, PropertyKey id)
#define DEFINE_MANAGED_VTABLE(classname)
QFrame frame
[0]
QJSValueList args
QJSEngine engine
[0]
StaticValue function
static constexpr ReturnedValue undefined()
MemoryManager * memoryManager
CppStackFrame * currentStackFrame
static Heap::ExecutionContext * newCatchContext(CppStackFrame *frame, int blockIndex, Heap::String *exceptionVarName)
static Heap::CallContext * cloneBlockContext(ExecutionEngine *engine, Heap::CallContext *callContext)
bool deleteProperty(String *name)
ReturnedValue getProperty(String *name)
Heap::ExecutionContext * newWithContext(Heap::Object *with) const
static Heap::CallContext * newBlockContext(QV4::CppStackFrame *frame, int blockIndex)
Error setProperty(String *name, const Value &value)
void createMutableBinding(String *name, bool deletable)
ReturnedValue getPropertyAndBase(String *name, Value *base)
static Heap::CallContext * newCallContext(JSTypesStackFrame *frame)
ReturnedValue throwReferenceError(const Value &value)
Heap::Object * newObject()
ReturnedValue catchException(StackTrace *trace=nullptr)
Symbol * symbol_unscopables() const
ReturnedValue throwTypeError()
bool isStrict() const
ExecutionEngine * engine() const
bool defineOwnProperty(PropertyKey id, const Property *p, PropertyAttributes attrs)
bool hasProperty(PropertyKey id) const
PropertyAttributes getOwnProperty(PropertyKey id, Property *p=nullptr) const
bool put(StringOrSymbol *name, const Value &v, Value *receiver=nullptr)
ReturnedValue get(StringOrSymbol *name, bool *hasProperty=nullptr, const Value *receiver=nullptr) const
ExecutionEngine * engine
constexpr ReturnedValue asReturnedValue() const
static constexpr Value fromStaticValue(StaticValue staticValue)
Definition qv4value_p.h:44
Value()=default
bool toBoolean() const
Definition qv4value_p.h:97
static constexpr Value undefinedValue()
Definition qv4value_p.h:191