Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qjsengine.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// Qt-Security score:significant
4
5#include "qjsengine.h"
6
7#include <private/qjsengine_p.h>
8#include <private/qjsvalue_p.h>
9#include <private/qqmlbuiltinfunctions_p.h>
10#include <private/qqmldebugconnector_p.h>
11#include <private/qqmlglobal_p.h>
12#include <private/qqmlscriptblob_p.h>
13#include <private/qqmltypeloader_p.h>
14#include <private/qv4dateobject_p.h>
15#include <private/qv4engine_p.h>
16#include <private/qv4errorobject_p.h>
17#include <private/qv4globalobject_p.h>
18#include <private/qv4mm_p.h>
19#include <private/qv4module_p.h>
20#include <private/qv4qmetaobjectwrapper_p.h>
21#include <private/qv4qobjectwrapper_p.h>
22#include <private/qv4runtime_p.h>
23#include <private/qv4script_p.h>
24#include <private/qv4stackframe_p.h>
25#include <private/qv4symbol_p.h>
26
27#include <QtQml/qjsvalue.h>
28#include <QtQml/qqmlengine.h>
29
30#include <QtCore/qcoreapplication.h>
31#include <QtCore/qdatetime.h>
32#include <QtCore/qdir.h>
33#include <QtCore/qfile.h>
34#include <QtCore/qfileinfo.h>
35#include <QtCore/qmetaobject.h>
36#include <QtCore/qmutex.h>
37#include <QtCore/qpluginloader.h>
38#include <QtCore/qstringlist.h>
39#include <QtCore/qthread.h>
40#include <QtCore/qvariant.h>
41#include <QtCore/qwaitcondition.h>
42
43Q_DECLARE_METATYPE(QList<int>)
44
45/*!
46 \since 5.0
47 \class QJSEngine
48 \reentrant
49
50 \brief The QJSEngine class provides an environment for evaluating JavaScript code.
51
52 \ingroup qtjavascript
53 \inmodule QtQml
54
55 \section1 Evaluating Scripts
56
57 Use evaluate() to evaluate script code.
58
59 \snippet code/src_script_qjsengine.cpp 0
60
61 evaluate() returns a QJSValue that holds the result of the
62 evaluation. The QJSValue class provides functions for converting
63 the result to various C++ types (e.g. QJSValue::toString()
64 and QJSValue::toNumber()).
65
66 The following code snippet shows how a script function can be
67 defined and then invoked from C++ using QJSValue::call():
68
69 \snippet code/src_script_qjsengine.cpp 1
70
71 As can be seen from the above snippets, a script is provided to the
72 engine in the form of a string. One common way of loading scripts is
73 by reading the contents of a file and passing it to evaluate():
74
75 \snippet code/src_script_qjsengine.cpp 2
76
77 Here we pass the name of the file as the second argument to
78 evaluate(). This does not affect evaluation in any way; the second
79 argument is a general-purpose string that is stored in the \c Error
80 object for debugging purposes.
81
82 For larger pieces of functionality, you may want to encapsulate
83 your code and data into modules. A module is a file that contains
84 script code, variables, etc., and uses export statements to describe
85 its interface towards the rest of the application. With the help of
86 import statements, a module can refer to functionality from other modules.
87 This allows building a scripted application from smaller connected building blocks
88 in a safe way. In contrast, the approach of using evaluate() carries the risk
89 that internal variables or functions from one evaluate() call accidentally pollute the
90 global object and affect subsequent evaluations.
91
92 The following example provides a module that can add numbers:
93
94 \code
95 export function sum(left, right)
96 {
97 return left + right
98 }
99 \endcode
100
101 This module can be loaded with QJSEngine::import() if it is saved under
102 the name \c{math.mjs}:
103
104 \code
105 QJSvalue module = myEngine.importModule("./math.mjs");
106 QJSValue sumFunction = module.property("sum");
107 QJSValue result = sumFunction.call(args);
108 \endcode
109
110 Modules can also use functionality from other modules using import
111 statements:
112
113 \code
114 import { sum } from "./math.mjs";
115 export function addTwice(left, right)
116 {
117 return sum(left, right) * 2;
118 }
119 \endcode
120
121 Modules don't have to be files. They can be values registered with
122 QJSEngine::registerModule():
123
124 \code
125 import version from "version";
126
127 export function getVersion()
128 {
129 return version;
130 }
131 \endcode
132
133 \code
134 QJSValue version(610);
135 myEngine.registerModule("version", version);
136 QJSValue module = myEngine.importModule("./myprint.mjs");
137 QJSValue getVersion = module.property("getVersion");
138 QJSValue result = getVersion.call();
139 \endcode
140
141 Named exports are supported, but because they are treated as members of an
142 object, the default export must be an ECMAScript object. Most of the newXYZ
143 functions in QJSValue will return an object.
144
145 \code
146 QJSValue name("Qt6");
147 QJSValue obj = myEngine.newObject();
148 obj.setProperty("name", name);
149 myEngine.registerModule("info", obj);
150 \endcode
151
152 \code
153 import { name } from "info";
154
155 export function getName()
156 {
157 return name;
158 }
159 \endcode
160
161 \section1 Engine Configuration
162
163 The globalObject() function returns the \b {Global Object}
164 associated with the script engine. Properties of the Global Object
165 are accessible from any script code (i.e. they are global
166 variables). Typically, before evaluating "user" scripts, you will
167 want to configure a script engine by adding one or more properties
168 to the Global Object:
169
170 \snippet code/src_script_qjsengine.cpp 3
171
172 Adding custom properties to the scripting environment is one of the
173 standard means of providing a scripting API that is specific to your
174 application. Usually these custom properties are objects created by
175 the newQObject() or newObject() functions.
176
177 \section1 Script Exceptions
178
179 evaluate() can throw a script exception (e.g. due to a syntax
180 error). If it does, then evaluate() returns the value that was thrown
181 (typically an \c{Error} object). Use \l QJSValue::isError() to check
182 for exceptions.
183
184 For detailed information about the error, use \l QJSValue::toString() to
185 obtain an error message, and use \l QJSValue::property() to query the
186 properties of the \c Error object. The following properties are available:
187
188 \list
189 \li \c name
190 \li \c message
191 \li \c fileName
192 \li \c lineNumber
193 \li \c stack
194 \endlist
195
196 \snippet code/src_script_qjsengine.cpp 4
197
198 \section1 Script Object Creation
199
200 Use newObject() to create a JavaScript object; this is the
201 C++ equivalent of the script statement \c{new Object()}. You can use
202 the object-specific functionality in QJSValue to manipulate the
203 script object (e.g. QJSValue::setProperty()). Similarly, use
204 newArray() to create a JavaScript array object.
205
206 \section1 QObject Integration
207
208 Use newQObject() to wrap a QObject (or subclass)
209 pointer. newQObject() returns a proxy script object; properties,
210 children, and signals and slots of the QObject are available as
211 properties of the proxy object. No binding code is needed because it
212 is done dynamically using the Qt meta object system.
213
214 \snippet code/src_script_qjsengine.cpp 5
215
216 Use newQMetaObject() to wrap a QMetaObject; this gives you a
217 "script representation" of a QObject-based class. newQMetaObject()
218 returns a proxy script object; enum values of the class are available
219 as properties of the proxy object.
220
221 Constructors exposed to the meta-object system (using Q_INVOKABLE) can be
222 called from the script to create a new QObject instance with
223 JavaScriptOwnership. For example, given the following class definition:
224
225 \snippet code/src_script_qjsengine.cpp 7
226
227 The \c staticMetaObject for the class can be exposed to JavaScript like so:
228
229 \snippet code/src_script_qjsengine.cpp 8
230
231 Instances of the class can then be created in JavaScript:
232
233 \snippet code/src_script_qjsengine.cpp 9
234
235 \note Currently only classes using the Q_OBJECT macro are supported; it is
236 not possible to expose the \c staticMetaObject of a Q_GADGET class to
237 JavaScript.
238
239 \section2 Dynamic QObject Properties
240
241 Dynamic QObject properties are not supported. For example, the following code
242 will not work:
243
244 \snippet code/src_script_qjsengine.cpp 6
245
246 \section1 Extensions
247
248 QJSEngine provides a compliant ECMAScript implementation. By default,
249 familiar utilities like logging are not available, but they can be
250 installed via the \l installExtensions() function.
251
252 \sa QJSValue, {Making Applications Scriptable},
253 {List of JavaScript Objects and Functions}
254
255*/
256
257/*!
258 \enum QJSEngine::Extension
259
260 This enum is used to specify extensions to be installed via
261 \l installExtensions().
262
263 \value TranslationExtension Indicates that translation functions (\c qsTr(),
264 for example) should be installed. This also installs the Qt.uiLanguage property.
265
266 \value ConsoleExtension Indicates that console functions (\c console.log(),
267 for example) should be installed.
268
269 \value GarbageCollectionExtension Indicates that garbage collection
270 functions (\c gc(), for example) should be installed.
271
272 \value AllExtensions Indicates that all extension should be installed.
273
274 \b TranslationExtension
275
276 The relation between script translation functions and C++ translation
277 functions is described in the following table:
278
279 \table
280 \header \li Script Function \li Corresponding C++ Function
281 \row \li qsTr() \li QObject::tr()
282 \row \li QT_TR_NOOP() \li QT_TR_NOOP()
283 \row \li qsTranslate() \li QCoreApplication::translate()
284 \row \li QT_TRANSLATE_NOOP() \li QT_TRANSLATE_NOOP()
285 \row \li qsTrId() \li qtTrId()
286 \row \li QT_TRID_NOOP() \li QT_TRID_NOOP()
287 \endtable
288
289 This flag also adds an \c arg() function to the string prototype.
290
291 For more information, see the \l {Internationalization with Qt}
292 documentation.
293
294 \b ConsoleExtension
295
296 The \l {Console API}{console} object implements a subset of the
297 \l {https://developer.mozilla.org/en-US/docs/Web/API/Console}{Console API},
298 which provides familiar logging functions, such as \c console.log().
299
300 The list of functions added is as follows:
301
302 \list
303 \li \c console.assert()
304 \li \c console.debug()
305 \li \c console.exception()
306 \li \c console.info()
307 \li \c console.log() (equivalent to \c console.debug())
308 \li \c console.error()
309 \li \c console.time()
310 \li \c console.timeEnd()
311 \li \c console.trace()
312 \li \c console.count()
313 \li \c console.warn()
314 \li \c {print()} (equivalent to \c console.debug())
315 \endlist
316
317 For more information, see the \l {Console API} documentation.
318
319 \b GarbageCollectionExtension
320
321 The \c gc() function is equivalent to calling \l collectGarbage().
322*/
323
324QT_BEGIN_NAMESPACE
325
326static void checkForApplicationInstance()
327{
328 if (!QCoreApplication::instance())
329 qFatal("QJSEngine: Must construct a QCoreApplication before a QJSEngine");
330}
331
332/*!
333 Constructs a QJSEngine object.
334
335 The globalObject() is initialized to have properties as described in
336 \l{ECMA-262}, Section 15.1.
337*/
338QJSEngine::QJSEngine()
339 : QJSEngine(nullptr)
340{
341}
342
343/*!
344 Constructs a QJSEngine object with the given \a parent.
345
346 The globalObject() is initialized to have properties as described in
347 \l{ECMA-262}, Section 15.1.
348*/
349
350QJSEngine::QJSEngine(QObject *parent)
351 : QJSEngine(*new QJSEnginePrivate, parent)
352{
353 QJSEnginePrivate::addToDebugServer(this);
354}
355
356/*!
357 \internal
358*/
359QJSEngine::QJSEngine(QJSEnginePrivate &dd, QObject *parent)
360 : QObject(dd, parent)
361{
362 dd.v4Engine = std::make_unique<QV4::ExecutionEngine>(this);
363 m_v4Engine = dd.v4Engine.get();
364 checkForApplicationInstance();
365}
366
367/*!
368 Destroys this QJSEngine.
369
370 Garbage is not collected from the persistent JS heap during QJSEngine
371 destruction. If you need all memory freed, call collectGarbage() manually
372 right before destroying the QJSEngine.
373*/
374QJSEngine::~QJSEngine()
375{
376 m_v4Engine->inShutdown = true;
377 QJSEnginePrivate::removeFromDebugServer(this);
378 m_v4Engine->publicEngine = nullptr;
379}
380
381/*!
382 \fn QV4::ExecutionEngine *QJSEngine::handle() const
383 \internal
384*/
385
386/*!
387 Runs the garbage collector.
388
389 The garbage collector will attempt to reclaim memory by locating and disposing of objects that are
390 no longer reachable in the script environment.
391
392 Normally you don't need to call this function; the garbage collector will automatically be invoked
393 when the QJSEngine decides that it's wise to do so (i.e. when a certain number of new objects
394 have been created). However, you can call this function to explicitly request that garbage
395 collection should be performed as soon as possible.
396
397
398 \sa {Garbage Collection}
399 \sa {Qt::}{gc()}
400 */
401void QJSEngine::collectGarbage()
402{
403 m_v4Engine->memoryManager->runFullGC();
404}
405
406/*!
407 \since 5.6
408
409 Installs JavaScript \a extensions to add functionality that is not
410 available in a standard ECMAScript implementation.
411
412 The extensions are installed on the given \a object, or on the
413 \l {globalObject()}{Global Object} if no object is specified.
414
415 Several extensions can be installed at once by \c {OR}-ing the enum values:
416
417 \code
418 installExtensions(QJSEngine::TranslationExtension | QJSEngine::ConsoleExtension);
419 \endcode
420
421 \sa Extension
422*/
423void QJSEngine::installExtensions(QJSEngine::Extensions extensions, const QJSValue &object)
424{
425 QV4::ExecutionEngine *otherEngine = QJSValuePrivate::engine(&object);
426 if (otherEngine && otherEngine != m_v4Engine) {
427 qWarning("QJSEngine: Trying to install extensions from a different engine");
428 return;
429 }
430
431 QV4::Scope scope(m_v4Engine);
432 QV4::ScopedObject obj(scope, QJSValuePrivate::asReturnedValue(&object));
433 if (!obj)
434 obj = scope.engine->globalObject;
435
436 QV4::GlobalExtensions::init(obj, extensions);
437}
438
439/*!
440 \since 5.14
441 Interrupts or re-enables JavaScript execution.
442
443 If \a interrupted is \c true, any JavaScript executed by this engine
444 immediately aborts and returns an error object until this function is
445 called again with a value of \c false for \a interrupted.
446
447 This function is thread safe. You may call it from a different thread
448 in order to interrupt, for example, an infinite loop in JavaScript.
449*/
450void QJSEngine::setInterrupted(bool interrupted)
451{
452 m_v4Engine->isInterrupted.storeRelaxed(interrupted);
453}
454
455/*!
456 \since 5.14
457 Returns whether JavaScript execution is currently interrupted.
458
459 \sa setInterrupted()
460*/
461bool QJSEngine::isInterrupted() const
462{
463 return m_v4Engine->isInterrupted.loadRelaxed();
464}
465
466static QUrl urlForFileName(const QString &fileName)
467{
468 if (!fileName.startsWith(QLatin1Char(':')))
469 return QUrl::fromLocalFile(fileName);
470
471 QUrl url;
472 url.setPath(fileName.mid(1));
473 url.setScheme(QLatin1String("qrc"));
474 return url;
475}
476
477/*!
478 Evaluates \a program, using \a lineNumber as the base line number,
479 and returns the result of the evaluation.
480
481 The script code will be evaluated in the context of the global object.
482
483 \note If you need to evaluate inside a QML context, use \l QQmlExpression
484 instead.
485
486 The evaluation of \a program can cause an \l{Script Exceptions}{exception} in the
487 engine; in this case the return value will be the exception
488 that was thrown (typically an \c{Error} object; see
489 QJSValue::isError()).
490
491 \a lineNumber is used to specify a starting line number for \a
492 program; line number information reported by the engine that pertains
493 to this evaluation will be based on this argument. For example, if
494 \a program consists of two lines of code, and the statement on the
495 second line causes a script exception, the exception line number
496 would be \a lineNumber plus one. When no starting line number is
497 specified, line numbers will be 1-based.
498
499 \a fileName is used for error reporting. For example, in error objects
500 the file name is accessible through the "fileName" property if it is
501 provided with this function.
502
503 \a exceptionStackTrace is used to report whether an uncaught exception was
504 thrown. If you pass a non-null pointer to a QStringList to it, it will set
505 it to list of "stackframe messages" if the script threw an unhandled
506 exception, or an empty list otherwise. A stackframe message has the format
507 function name:line number:column:file name
508 \note In some cases, e.g. for native functions, function name and file name
509 can be empty and line number and column can be -1.
510
511 \note If an exception was thrown and the exception value is not an
512 Error instance (i.e., QJSValue::isError() returns \c false), the
513 exception value will still be returned. Use \c exceptionStackTrace->isEmpty()
514 to distinguish whether the value was a normal or an exceptional return
515 value.
516
517 \sa QQmlExpression::evaluate
518*/
519QJSValue QJSEngine::evaluate(const QString& program, const QString& fileName, int lineNumber, QStringList *exceptionStackTrace)
520{
521 QV4::ExecutionEngine *v4 = m_v4Engine;
522 QV4::Scope scope(v4);
523 QV4::ScopedValue result(scope);
524
525 QV4::Script script(v4->rootContext(), QV4::Compiler::ContextType::Global, program, urlForFileName(fileName).toString(), lineNumber);
526 script.setStrictMode(false);
527 if (v4->currentStackFrame)
528 script.setStrictMode(v4->currentStackFrame->v4Function->isStrict());
529 else if (v4->globalCode)
530 script.setStrictMode(v4->globalCode->isStrict());
531 script.setInheritContext();
532 script.parse();
533 if (!scope.hasException())
534 result = script.run();
535 if (exceptionStackTrace)
536 exceptionStackTrace->clear();
537 if (scope.hasException()) {
538 QV4::StackTrace trace;
539 result = v4->catchException(&trace);
540 if (exceptionStackTrace) {
541 for (auto &&frame: std::as_const(trace)) {
542 exceptionStackTrace->push_back(QLatin1StringView("%1:%2:%3:%4").arg(
543 frame.function,
544 QString::number(qAbs(frame.line)),
545 QString::number(frame.column),
546 frame.source)
547 );
548 }
549 }
550 }
551 if (v4->isInterrupted.loadRelaxed())
552 result = v4->newErrorObject(QStringLiteral("Interrupted"));
553
554 if (const auto cu = script.compilationUnit())
555 v4->trimCompilationUnitsForUrl(cu->finalUrl());
556 return QJSValuePrivate::fromReturnedValue(result->asReturnedValue());
557}
558
559/*!
560 Imports the module located at \a fileName and returns a module namespace object that
561 contains all exported variables, constants and functions as properties.
562
563 If this is the first time the module is imported in the engine, the file is loaded
564 from the specified location in either the local file system or the Qt resource system
565 and evaluated as an ECMAScript module. The file is expected to be encoded in UTF-8 text.
566
567 Subsequent imports of the same module will return the previously imported instance. Modules
568 are singletons and remain around until the engine is destroyed.
569
570 The specified \a fileName will internally be normalized using \l QFileInfo::canonicalFilePath().
571 That means that multiple imports of the same file on disk using different relative paths will
572 load the file only once.
573
574 \note If an exception is thrown during the loading of the module, the return value
575 will be the exception (typically an \c{Error} object; see QJSValue::isError()).
576
577 \sa registerModule()
578
579 \since 5.12
580 */
581QJSValue QJSEngine::importModule(const QString &fileName)
582{
583 QUrl url = urlForFileName(QFileInfo(fileName).canonicalFilePath());
584 if (!fileName.endsWith(QLatin1String(".mjs")))
585 url.setFragment(QLatin1String("module"));
586
587 QQmlRefPointer<QQmlScriptBlob> scriptBlob = m_v4Engine->typeLoader()->getScript(url);
588
589 if (scriptBlob->isError()) {
590 const QList<QQmlError> errors = scriptBlob->errors();
591 switch (errors.length()) {
592 case 0:
593 Q_UNREACHABLE_RETURN(QJSValue());
594 case 1: {
595 const QQmlError &error = errors[0];
596 m_v4Engine->throwSyntaxError(
597 error.description(), error.url().toString(), error.line(), error.column());
598 return QJSValuePrivate::fromReturnedValue(m_v4Engine->catchException());
599 }
600 default: {
601 QString errorString = QStringLiteral("Importing module failed:");
602 for (const QQmlError &error : errors) {
603 errorString += QLatin1String("\n ");
604 errorString += error.toString();
605 }
606 m_v4Engine->throwSyntaxError(errorString);
607 return QJSValuePrivate::fromReturnedValue(m_v4Engine->catchException());
608 }
609 }
610 }
611
612 // We've just created the URL from a local file. So it has to be synchronous.
613 Q_ASSERT(scriptBlob->isComplete());
614
615 const auto module
616 = m_v4Engine->executableCompilationUnit(scriptBlob->scriptData()->compilationUnit());
617
618 // If there is neither a native nor a compiled module, we should have seen an exception
619 Q_ASSERT(module);
620
621 QV4::Scope scope(m_v4Engine);
622 QV4::ScopedValue value(scope, module->value());
623 if (!value->isEmpty())
624 return QJSValuePrivate::fromReturnedValue(value->asReturnedValue());
625
626 QV4::Scoped<QV4::Module> moduleNamespace(scope, module->instantiate());
627 if (m_v4Engine->hasException)
628 return QJSValuePrivate::fromReturnedValue(m_v4Engine->catchException());
629
630 module->evaluate();
631 if (m_v4Engine->isInterrupted.loadRelaxed()) {
632 return QJSValuePrivate::fromReturnedValue(
633 m_v4Engine->newErrorObject(QStringLiteral("Interrupted"))->asReturnedValue());
634 }
635
636 return QJSValuePrivate::fromReturnedValue(moduleNamespace->asReturnedValue());
637}
638
639/*!
640 Registers a QJSValue to serve as a module. After this function is called,
641 all modules that import \a moduleName will import the value of \a value
642 instead of loading \a moduleName from the filesystem.
643
644 Any valid QJSValue can be registered, but named exports (i.e.
645 \c {import { name } from "info"} are treated as members of an object, so
646 the default export must be created with one of the newXYZ methods of
647 QJSEngine.
648
649 Because this allows modules that do not exist on the filesystem to be imported,
650 scripting applications can use this to provide built-in modules, similar to
651 Node.js.
652
653 Returns \c true on success, \c false otherwise.
654
655 \note The QJSValue \a value is not called or read until it is used by another module.
656 This means that there is no code to evaluate, so no errors will be seen until
657 another module throws an exception while trying to load this module.
658
659 \warning Attempting to access a named export from a QJSValue that is not an
660 object will trigger a \l{Script Exceptions}{exception}.
661
662 \sa importModule()
663 */
664bool QJSEngine::registerModule(const QString &moduleName, const QJSValue &value)
665{
666 QV4::Scope scope(m_v4Engine);
667 QV4::ScopedValue v4Value(scope, QJSValuePrivate::asReturnedValue(&value));
668 m_v4Engine->registerNativeModule(QUrl(moduleName), v4Value);
669 if (m_v4Engine->hasException)
670 return false;
671 return true;
672}
673
674/*!
675 Creates a JavaScript object of class Object.
676
677 The prototype of the created object will be the Object
678 prototype object.
679
680 \sa newArray(), QJSValue::setProperty()
681*/
682QJSValue QJSEngine::newObject()
683{
684 QV4::Scope scope(m_v4Engine);
685 QV4::ScopedValue v(scope, m_v4Engine->newObject());
686 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
687}
688
689/*!
690 \since 6.2
691
692 Creates a JavaScript object of class Symbol, with value \a name.
693
694 The prototype of the created object will be the Symbol prototype object.
695
696 \sa newObject()
697*/
698QJSValue QJSEngine::newSymbol(const QString &name)
699{
700 QV4::Scope scope(m_v4Engine);
701 QV4::ScopedValue v(scope, QV4::Symbol::create(m_v4Engine, u'@' + name));
702 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
703}
704
705/*!
706 \since 5.12
707
708 Creates a JavaScript object of class Error, with \a message as the error
709 message.
710
711 The prototype of the created object will be \a errorType.
712
713 \sa newObject(), throwError(), QJSValue::isError()
714*/
715QJSValue QJSEngine::newErrorObject(QJSValue::ErrorType errorType, const QString &message)
716{
717 QV4::Scope scope(m_v4Engine);
718 QV4::ScopedObject error(scope);
719 switch (errorType) {
720 case QJSValue::RangeError:
721 error = m_v4Engine->newRangeErrorObject(message);
722 break;
723 case QJSValue::SyntaxError:
724 error = m_v4Engine->newSyntaxErrorObject(message);
725 break;
726 case QJSValue::TypeError:
727 error = m_v4Engine->newTypeErrorObject(message);
728 break;
729 case QJSValue::URIError:
730 error = m_v4Engine->newURIErrorObject(message);
731 break;
732 case QJSValue::ReferenceError:
733 error = m_v4Engine->newReferenceErrorObject(message);
734 break;
735 case QJSValue::EvalError:
736 error = m_v4Engine->newEvalErrorObject(message);
737 break;
738 case QJSValue::GenericError:
739 error = m_v4Engine->newErrorObject(message);
740 break;
741 case QJSValue::NoError:
742 return QJSValue::UndefinedValue;
743 }
744 return QJSValuePrivate::fromReturnedValue(error->asReturnedValue());
745}
746
747/*!
748 Creates a JavaScript object of class Array with the given \a length.
749
750 \sa newObject()
751*/
752QJSValue QJSEngine::newArray(uint length)
753{
754 QV4::Scope scope(m_v4Engine);
755 QV4::ScopedArrayObject array(scope, m_v4Engine->newArrayObject());
756 if (length < 0x1000)
757 array->arrayReserve(length);
758 array->setArrayLengthUnchecked(length);
759 return QJSValuePrivate::fromReturnedValue(array.asReturnedValue());
760}
761
762/*!
763 Creates a JavaScript object that wraps the given QObject \a
764 object, using JavaScriptOwnership.
765
766 Signals and slots, properties and children of \a object are
767 available as properties of the created QJSValue.
768
769 If \a object is a null pointer, this function returns a null value.
770
771 If a default prototype has been registered for the \a object's class
772 (or its superclass, recursively), the prototype of the new script
773 object will be set to be that default prototype.
774
775 If the given \a object is deleted outside of the engine's control, any
776 attempt to access the deleted QObject's members through the JavaScript
777 wrapper object (either by script code or C++) will result in a
778 \l{Script Exceptions}{script exception}.
779
780 \sa QJSValue::toQObject()
781*/
782QJSValue QJSEngine::newQObject(QObject *object)
783{
784 QV4::ExecutionEngine *v4 = m_v4Engine;
785 QV4::Scope scope(v4);
786 if (object) {
787 QQmlData *ddata = QQmlData::get(object, true);
788 if (!ddata || !ddata->explicitIndestructibleSet)
789 QQmlEngine::setObjectOwnership(object, QQmlEngine::JavaScriptOwnership);
790 }
791 QV4::ScopedValue v(scope, QV4::QObjectWrapper::wrap(v4, object));
792 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
793}
794
795/*!
796 \since 5.8
797
798 Creates a JavaScript object that wraps the given QMetaObject
799 The \a metaObject must outlive the script engine. It is recommended to only
800 use this method with static metaobjects.
801
802
803 When called as a constructor, a new instance of the class will be created.
804 Only constructors exposed by Q_INVOKABLE will be visible from the script engine.
805
806 \sa newQObject(), {QObject Integration}
807*/
808
809QJSValue QJSEngine::newQMetaObject(const QMetaObject* metaObject) {
810 QV4::ExecutionEngine *v4 = m_v4Engine;
811 QV4::Scope scope(v4);
812 QV4::ScopedValue v(scope, QV4::QMetaObjectWrapper::create(v4, metaObject));
813 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
814}
815
816/*! \fn template <typename T> QJSValue QJSEngine::newQMetaObject()
817
818 \since 5.8
819 Creates a JavaScript object that wraps the static QMetaObject associated
820 with class \c{T}.
821
822 \sa newQObject(), {QObject Integration}
823*/
824
825
826/*!
827 Returns this engine's Global Object.
828
829 By default, the Global Object contains the built-in objects that are
830 part of \l{ECMA-262}, such as Math, Date and String. Additionally,
831 you can set properties of the Global Object to make your own
832 extensions available to all script code. Non-local variables in
833 script code will be created as properties of the Global Object, as
834 well as local variables in global code.
835*/
836QJSValue QJSEngine::globalObject() const
837{
838 QV4::Scope scope(m_v4Engine);
839 QV4::ScopedValue v(scope, m_v4Engine->globalObject);
840 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
841}
842
843QJSPrimitiveValue QJSEngine::createPrimitive(QMetaType type, const void *ptr)
844{
845 QV4::Scope scope(m_v4Engine);
846 QV4::ScopedValue v(scope, m_v4Engine->metaTypeToJS(type, ptr));
847 return QV4::ExecutionEngine::createPrimitive(v);
848}
849
850QJSManagedValue QJSEngine::createManaged(QMetaType type, const void *ptr)
851{
852 QJSManagedValue result(m_v4Engine);
853 *result.d = m_v4Engine->metaTypeToJS(type, ptr);
854 return result;
855}
856
857/*!
858 * \internal
859 * used by QJSEngine::toScriptValue
860 */
861QJSValue QJSEngine::create(QMetaType type, const void *ptr)
862{
863 QV4::Scope scope(m_v4Engine);
864 QV4::ScopedValue v(scope, scope.engine->metaTypeToJS(type, ptr));
865 return QJSValuePrivate::fromReturnedValue(v->asReturnedValue());
866}
867
868bool QJSEngine::convertPrimitive(const QJSPrimitiveValue &value, QMetaType type, void *ptr)
869{
870 switch (value.type()) {
871 case QJSPrimitiveValue::Undefined:
872 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::undefinedValue(), type, ptr);
873 case QJSPrimitiveValue::Null:
874 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::nullValue(), type, ptr);
875 case QJSPrimitiveValue::Boolean:
876 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::fromBoolean(value.toBoolean()), type, ptr);
877 case QJSPrimitiveValue::Integer:
878 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::fromInt32(value.toInteger()), type, ptr);
879 case QJSPrimitiveValue::Double:
880 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::fromDouble(value.toDouble()), type, ptr);
881 case QJSPrimitiveValue::String:
882 return convertString(value.toString(), type, ptr);
883 }
884
885 Q_UNREACHABLE_RETURN(false);
886}
887
888bool QJSEngine::convertManaged(const QJSManagedValue &value, int type, void *ptr)
889{
890 return convertManaged(value, QMetaType(type), ptr);
891}
892
893bool QJSEngine::convertManaged(const QJSManagedValue &value, QMetaType type, void *ptr)
894{
895 return QV4::ExecutionEngine::metaTypeFromJS(*value.d, type, ptr);
896}
897
898bool QJSEngine::convertString(const QString &string, QMetaType metaType, void *ptr)
899{
900 // have a string based value without engine. Do conversion manually
901 if (metaType == QMetaType::fromType<bool>()) {
902 *reinterpret_cast<bool*>(ptr) = string.size() != 0;
903 return true;
904 }
905 if (metaType == QMetaType::fromType<QString>()) {
906 *reinterpret_cast<QString*>(ptr) = string;
907 return true;
908 }
909 if (metaType == QMetaType::fromType<QUrl>()) {
910 *reinterpret_cast<QUrl *>(ptr) = QUrl(string);
911 return true;
912 }
913
914 double d = QV4::RuntimeHelpers::stringToNumber(string);
915 switch (metaType.id()) {
916 case QMetaType::Int:
917 *reinterpret_cast<int*>(ptr) = QV4::Value::toInt32(d);
918 return true;
919 case QMetaType::UInt:
920 *reinterpret_cast<uint*>(ptr) = QV4::Value::toUInt32(d);
921 return true;
922 case QMetaType::Long:
923 *reinterpret_cast<long*>(ptr) = QV4::Value::toInteger(d);
924 return true;
925 case QMetaType::ULong:
926 *reinterpret_cast<ulong*>(ptr) = QV4::Value::toInteger(d);
927 return true;
928 case QMetaType::LongLong:
929 *reinterpret_cast<qlonglong*>(ptr) = QV4::Value::toInteger(d);
930 return true;
931 case QMetaType::ULongLong:
932 *reinterpret_cast<qulonglong*>(ptr) = QV4::Value::toInteger(d);
933 return true;
934 case QMetaType::Double:
935 *reinterpret_cast<double*>(ptr) = d;
936 return true;
937 case QMetaType::Float:
938 *reinterpret_cast<float*>(ptr) = d;
939 return true;
940 case QMetaType::Short:
941 *reinterpret_cast<short*>(ptr) = QV4::Value::toInt32(d);
942 return true;
943 case QMetaType::UShort:
944 *reinterpret_cast<unsigned short*>(ptr) = QV4::Value::toUInt32(d);
945 return true;
946 case QMetaType::Char:
947 *reinterpret_cast<char*>(ptr) = QV4::Value::toInt32(d);
948 return true;
949 case QMetaType::UChar:
950 *reinterpret_cast<unsigned char*>(ptr) = QV4::Value::toUInt32(d);
951 return true;
952 case QMetaType::QChar:
953 *reinterpret_cast<QChar*>(ptr) = QChar(QV4::Value::toUInt32(d));
954 return true;
955 case QMetaType::Char16:
956 *reinterpret_cast<char16_t *>(ptr) = QV4::Value::toUInt32(d);
957 return true;
958 default:
959 return false;
960 }
961}
962
963/*!
964 \internal
965 convert \a value to \a type, store the result in \a ptr
966*/
967bool QJSEngine::convertV2(const QJSValue &value, QMetaType metaType, void *ptr)
968{
969 if (const QString *string = QJSValuePrivate::asQString(&value))
970 return convertString(*string, metaType, ptr);
971
972 // Does not need scoping since QJSValue still holds on to the value.
973 return QV4::ExecutionEngine::metaTypeFromJS(QV4::Value::fromReturnedValue(QJSValuePrivate::asReturnedValue(&value)),
974 metaType, ptr);
975}
976
977bool QJSEngine::convertVariant(const QVariant &value, QMetaType metaType, void *ptr)
978{
979 // TODO: We could probably avoid creating a QV4::Value in many cases, but we'd have to
980 // duplicate much of metaTypeFromJS and some methods of QV4::Value itself here.
981 QV4::Scope scope(handle());
982 QV4::ScopedValue scoped(scope, scope.engine->fromVariant(value));
983 return QV4::ExecutionEngine::metaTypeFromJS(scoped, metaType, ptr);
984}
985
986bool QJSEngine::convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to)
987{
988 // TODO: We could probably avoid creating a QV4::Value in many cases, but we'd have to
989 // duplicate much of metaTypeFromJS and some methods of QV4::Value itself here.
990 QV4::Scope scope(handle());
991 QV4::ScopedValue scoped(scope, scope.engine->fromData(fromType, from));
992 return QV4::ExecutionEngine::metaTypeFromJS(scoped, toType, to);
993}
994
995QString QJSEngine::convertQObjectToString(QObject *object)
996{
997 return QV4::QObjectWrapper::objectToString(
998 handle(), object ? object->metaObject() : nullptr, object);
999}
1000
1001QString QJSEngine::convertDateTimeToString(const QDateTime &dateTime)
1002{
1003 return QV4::DateObject::dateTimeToString(dateTime, handle());
1004}
1005
1006double QJSEngine::convertDateTimeToNumber(const QDateTime &dateTime)
1007{
1008 return QV4::DateObject::dateTimeToNumber(dateTime);
1009}
1010
1011QDate QJSEngine::convertDateTimeToDate(const QDateTime &dateTime)
1012{
1013 return QV4::DateObject::dateTimeToDate(dateTime);
1014}
1015
1016/*! \fn template <typename T> QJSValue QJSEngine::toScriptValue(const T &value)
1017
1018 Creates a QJSValue with the given \a value.
1019
1020 \sa fromScriptValue(), coerceValue()
1021*/
1022
1023/*! \fn template <typename T> QJSManagedValue QJSEngine::toManagedValue(const T &value)
1024
1025 Creates a QJSManagedValue with the given \a value.
1026
1027 \sa fromManagedValue(), coerceValue()
1028*/
1029
1030/*! \fn template <typename T> QJSPrimitiveValue QJSEngine::toPrimitiveValue(const T &value)
1031
1032 Creates a QJSPrimitiveValue with the given \a value.
1033
1034 Since QJSPrimitiveValue can only hold int, bool, double, QString, and the
1035 equivalents of JavaScript \c null and \c undefined, the value will be
1036 coerced aggressively if you pass any other type.
1037
1038 \sa fromPrimitiveValue(), coerceValue()
1039*/
1040
1041/*! \fn template <typename T> T QJSEngine::fromScriptValue(const QJSValue &value)
1042
1043 Returns the given \a value converted to the template type \c{T}.
1044
1045 \sa toScriptValue(), coerceValue()
1046*/
1047
1048/*! \fn template <typename T> T QJSEngine::fromManagedValue(const QJSManagedValue &value)
1049
1050 Returns the given \a value converted to the template type \c{T}.
1051
1052 \sa toManagedValue(), coerceValue()
1053*/
1054
1055/*! \fn template <typename T> T QJSEngine::fromPrimitiveValue(const QJSPrimitiveValue &value)
1056
1057 Returns the given \a value converted to the template type \c{T}.
1058
1059 Since QJSPrimitiveValue can only hold int, bool, double, QString, and the
1060 equivalents of JavaScript \c null and \c undefined, the value will be
1061 coerced aggressively if you request any other type.
1062
1063 \sa toPrimitiveValue(), coerceValue()
1064*/
1065
1066/*! \fn template <typename T> T QJSEngine::fromVariant(const QVariant &value)
1067
1068 Returns the given \a value converted to the template type \c{T}.
1069 The conversion is done in JavaScript semantics. Those differ from
1070 qvariant_cast's semantics. There are a number of implicit
1071 conversions between JavaScript-equivalent types that are not
1072 performed by qvariant_cast by default.
1073
1074 \sa coerceValue(), fromScriptValue(), {QVariant::}{qvariant_cast()}
1075*/
1076
1077/*! \fn template <typename From, typename To> T QJSEngine::coerceValue(const From &from)
1078
1079 Returns the given \a from converted to the template type \c{To}.
1080 The conversion is done in JavaScript semantics. Those differ from
1081 qvariant_cast's semantics. There are a number of implicit
1082 conversions between JavaScript-equivalent types that are not
1083 performed by qvariant_cast by default. This method is a generalization of
1084 all the other conversion methods in this class.
1085
1086 \sa fromVariant(), {QVariant::}{qvariant_cast()}, fromScriptValue(), toScriptValue()
1087*/
1088
1089/*!
1090 Throws a run-time error (exception) with the given \a message.
1091
1092 This method is the C++ counterpart of a \c throw() expression in
1093 JavaScript. It enables C++ code to report run-time errors to QJSEngine.
1094 Therefore it should only be called from C++ code that was invoked by a
1095 JavaScript function through QJSEngine.
1096
1097 When returning from C++, the engine will interrupt the normal flow of
1098 execution and call the next pre-registered exception handler with
1099 an error object that contains the given \a message. The error object
1100 will point to the location of the top-most context on the JavaScript
1101 caller stack; specifically, it will have properties \c lineNumber,
1102 \c fileName and \c stack. These properties are described in
1103 \l{Script Exceptions}.
1104
1105 In the following example a C++ method in \e FileAccess.cpp throws an error
1106 in \e qmlFile.qml at the position where \c readFileAsText() is called:
1107
1108 \code
1109 // qmlFile.qml
1110 function someFunction() {
1111 ...
1112 var text = FileAccess.readFileAsText("/path/to/file.txt");
1113 }
1114 \endcode
1115
1116 \code
1117 // FileAccess.cpp
1118 // Assuming that FileAccess is a QObject-derived class that has been
1119 // registered as a singleton type and provides an invokable method
1120 // readFileAsText()
1121
1122 QJSValue FileAccess::readFileAsText(const QString & filePath) {
1123 QFile file(filePath);
1124
1125 if (!file.open(QIODevice::ReadOnly)) {
1126 jsEngine->throwError(file.errorString());
1127 return QString();
1128 }
1129
1130 ...
1131 return content;
1132 }
1133 \endcode
1134
1135 It is also possible to catch the thrown error in JavaScript:
1136 \code
1137 // qmlFile.qml
1138 function someFunction() {
1139 ...
1140 var text;
1141 try {
1142 text = FileAccess.readFileAsText("/path/to/file.txt");
1143 } catch (error) {
1144 console.warn("In " + error.fileName + ":" + "error.lineNumber" +
1145 ": " + error.message);
1146 }
1147 }
1148 \endcode
1149
1150 If you need a more specific run-time error to describe an exception, you can use the
1151 \l {QJSEngine::}{throwError(QJSValue::ErrorType errorType, const QString &message)}
1152 overload.
1153
1154 \since Qt 5.12
1155 \sa {Script Exceptions}
1156*/
1157void QJSEngine::throwError(const QString &message)
1158{
1159 m_v4Engine->throwError(message);
1160}
1161
1162/*!
1163 \overload throwError()
1164
1165 Throws a run-time error (exception) with the given \a errorType and
1166 \a message.
1167
1168 \code
1169 // Assuming that DataEntry is a QObject-derived class that has been
1170 // registered as a singleton type and provides an invokable method
1171 // setAge().
1172
1173 void DataEntry::setAge(int age) {
1174 if (age < 0 || age > 200) {
1175 jsEngine->throwError(QJSValue::RangeError,
1176 "Age must be between 0 and 200");
1177 }
1178 ...
1179 }
1180 \endcode
1181
1182 \since Qt 5.12
1183 \sa {Script Exceptions}, newErrorObject()
1184*/
1185void QJSEngine::throwError(QJSValue::ErrorType errorType, const QString &message)
1186{
1187 QV4::Scope scope(m_v4Engine);
1188 QJSValue error = newErrorObject(errorType, message);
1189 QV4::ScopedObject e(scope, QJSValuePrivate::asReturnedValue(&error));
1190 if (!e)
1191 return;
1192 m_v4Engine->throwError(e);
1193}
1194
1195/*!
1196 \overload throwError()
1197
1198 Throws a pre-constructed run-time \a error (exception). This way you can
1199 use \l newErrorObject() to create the error and customize it as necessary.
1200
1201 \since 6.1
1202 \sa {Script Exceptions}, newErrorObject()
1203*/
1204void QJSEngine::throwError(const QJSValue &error)
1205{
1206 // safe, QJSValue holds a persistent reference
1207 m_v4Engine->throwError(
1208 QV4::Value::fromReturnedValue(
1209 QJSValuePrivate::convertToReturnedValue(m_v4Engine, error)));
1210}
1211
1212/*!
1213 * Returns \c true if the last JavaScript execution resulted in an exception or
1214 * if throwError() was called. Otherwise returns \c false. Mind that evaluate()
1215 * catches any exceptions thrown in the evaluated code.
1216 *
1217 * \since Qt 6.1
1218 */
1219bool QJSEngine::hasError() const
1220{
1221 return m_v4Engine->hasException;
1222}
1223
1224/*!
1225 * If an exception is currently pending, catches it and returns it as a
1226 * QJSValue. Otherwise returns undefined as QJSValue. After calling this method
1227 * hasError() returns \c false.
1228 *
1229 * \since Qt 6.1
1230 */
1231QJSValue QJSEngine::catchError()
1232{
1233 if (m_v4Engine->hasException)
1234 return QJSValuePrivate::fromReturnedValue(m_v4Engine->catchException());
1235 else
1236 return QJSValue();
1237}
1238
1239/*!
1240 \property QJSEngine::uiLanguage
1241 \brief the language to be used for translating user interface strings
1242 \since 5.15
1243
1244 This property holds the name of the language to be used for user interface
1245 string translations. It is exposed for reading and writing as \c{Qt.uiLanguage} when
1246 the QJSEngine::TranslationExtension is installed on the engine. It is always exposed
1247 in instances of QQmlEngine.
1248
1249 You can set the value freely and use it in bindings. It is recommended to set it
1250 after installing translators in your application. By convention, an empty string
1251 means no translation from the language used in the source code is intended to occur.
1252*/
1253void QJSEngine::setUiLanguage(const QString &language) {
1254 Q_D(QJSEngine);
1255 d->uiLanguage = language; // property takes care of signal emission if necessary
1256}
1257
1258QString QJSEngine::uiLanguage() const
1259{
1260 Q_D(const QJSEngine);
1261 return d->uiLanguage;
1262}
1263
1264QJSEnginePrivate *QJSEnginePrivate::get(QV4::ExecutionEngine *e)
1265{
1266 return e->jsEngine()->d_func();
1267}
1268
1269QJSEnginePrivate::~QJSEnginePrivate() = default;
1270
1271void QJSEnginePrivate::addToDebugServer(QJSEngine *q)
1272{
1273 if (QCoreApplication::instance()->thread() != q->thread())
1274 return;
1275
1276 QQmlDebugConnector *server = QQmlDebugConnector::instance();
1277 if (!server || server->hasEngine(q))
1278 return;
1279
1280 // Initialize the type loader before attaching debug services. We'll need it anyway and
1281 // we don't want the preview service to block on resolving some paths from QLibraryInfo.
1282 QQmlTypeLoader::get(q);
1283
1284 server->open();
1285 server->addEngine(q);
1286}
1287
1288void QJSEnginePrivate::removeFromDebugServer(QJSEngine *q)
1289{
1290 QQmlDebugConnector *server = QQmlDebugConnector::instance();
1291 if (server && server->hasEngine(q))
1292 server->removeEngine(q);
1293}
1294
1295/*!
1296 \since 5.5
1297 \relates QJSEngine
1298
1299 Returns the QJSEngine associated with \a object, if any.
1300
1301 This function is useful if you have exposed a QObject to the JavaScript environment
1302 and later in your program would like to regain access. It does not require you to
1303 keep the wrapper around that was returned from QJSEngine::newQObject().
1304 */
1305QJSEngine *qjsEngine(const QObject *object)
1306{
1307 QQmlData *data = QQmlData::get(object);
1308 if (!data || data->jsWrapper.isNullOrUndefined())
1309 return nullptr;
1310 return data->jsWrapper.engine()->jsEngine();
1311}
1312
1313
1314/*!
1315 \enum QJSEngine::ObjectOwnership
1316
1317 ObjectOwnership controls whether or not the JavaScript memory manager automatically destroys the
1318 QObject when the corresponding JavaScript object is garbage collected by the
1319 engine. The two ownership options are:
1320
1321 \value CppOwnership The object is owned by C++ code and the JavaScript memory manager will never
1322 delete it. The JavaScript destroy() method cannot be used on these objects. This
1323 option is similar to QScriptEngine::QtOwnership.
1324
1325 \value JavaScriptOwnership The object is owned by JavaScript. When the object
1326 is returned to the JavaScript memory manager as the return value of a method call, the JavaScript
1327 memory manager will track it and delete it if there are no remaining JavaScript references to it
1328 and it has no QObject::parent(). An object tracked by one QJSEngine will be deleted during that
1329 QJSEngine's destructor. Thus, JavaScript references between objects with JavaScriptOwnership from
1330 two different engines will not be valid if one of these engines is deleted. This option is similar
1331 to QScriptEngine::ScriptOwnership.
1332
1333 Generally an application doesn't need to set an object's ownership explicitly. The JavaScript
1334 memory manager uses a heuristic to set the default ownership. By default, an object that is
1335 created by the JavaScript memory manager has JavaScriptOwnership. The exception to this are the
1336 root objects created by calling QQmlComponent::create() or QQmlComponent::beginCreate(), which
1337 have CppOwnership by default. The ownership of these root-level objects is considered to have been
1338 transferred to the C++ caller.
1339
1340 Objects not-created by the JavaScript memory manager have CppOwnership by default. The exception
1341 to this are objects returned from C++ method calls; their ownership will be set to
1342 JavaScriptOwnership. This applies only to explicit invocations of Q_INVOKABLE methods or slots,
1343 but not to property getter invocations.
1344
1345 Calling setObjectOwnership() overrides the default ownership.
1346
1347 \sa {Data Ownership}
1348*/
1349
1350/*!
1351 Sets the \a ownership of \a object.
1352
1353 An object with \c JavaScriptOwnership is not garbage collected as long
1354 as it still has a parent, even if there are no references to it.
1355
1356 \sa QJSEngine::ObjectOwnership
1357*/
1358void QJSEngine::setObjectOwnership(QObject *object, ObjectOwnership ownership)
1359{
1360 if (!object)
1361 return;
1362
1363 QQmlData *ddata = QQmlData::get(object, true);
1364 if (!ddata)
1365 return;
1366
1367 ddata->indestructible = (ownership == CppOwnership)?true:false;
1368 ddata->explicitIndestructibleSet = true;
1369}
1370
1371/*!
1372 Returns the ownership of \a object.
1373
1374 \sa QJSEngine::ObjectOwnership
1375*/
1376QJSEngine::ObjectOwnership QJSEngine::objectOwnership(QObject *object)
1377{
1378 if (!object)
1379 return CppOwnership;
1380
1381 QQmlData *ddata = QQmlData::get(object, false);
1382 if (!ddata)
1383 return CppOwnership;
1384 else
1385 return ddata->indestructible?CppOwnership:JavaScriptOwnership;
1386}
1387
1388QT_END_NAMESPACE
1389
1390#include "moc_qjsengine.cpp"
QJSEngine * qjsEngine(const QObject *object)