Convert builtin methods from v8 to v4

Change-Id: I37006c60f0de131eefa53d8ee81844c2fb47f8b9
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
This commit is contained in:
Lars Knoll 2013-05-25 15:38:27 +02:00 committed by Simon Hausmann
parent b5d861510e
commit 68827e982f
5 changed files with 1037 additions and 931 deletions

View File

@ -80,6 +80,7 @@ struct Lookup;
struct ExecutionEngine;
struct VariantObject;
struct QObjectWrapper;
struct QtObject;
struct ManagedVTable
{
@ -180,7 +181,10 @@ public:
Type_QmlUrlList,
// Wrapped QVariant
Type_QVariant
Type_QVariant,
// Qt Object
Type_QtObject
};
ExecutionEngine *engine() const;
@ -213,6 +217,8 @@ public:
QQmlSequence<QStringList, Type_QmlQStringList> *asQmlQStringList() { return type == Type_QmlQStringList ? reinterpret_cast<QQmlSequence<QStringList, Type_QmlQStringList> *>(this): 0; }
QQmlSequence<QList<QUrl>, Type_QmlUrlList> *asQmlUrlList() { return type == Type_QmlUrlList ? reinterpret_cast<QQmlSequence<QList<QUrl>, Type_QmlUrlList> *>(this): 0; }
QtObject *asQtObject() {return type == Type_QtObject ? reinterpret_cast<QtObject *>(this) : 0; }
bool isListType() const { return type >= Type_QmlIntList && type <= Type_QmlUrlList; }
bool isArrayObject() const { return type == Type_ArrayObject; }

File diff suppressed because it is too large Load Diff

View File

@ -54,63 +54,97 @@
//
#include <QtCore/qglobal.h>
#include <private/qv8_p.h>
#include <private/qv4object_p.h>
#include <qv4v8_p.h>
class QQmlEngine;
QT_BEGIN_NAMESPACE
namespace QQmlBuiltinFunctions
namespace QV4 {
struct QtObject : Object
{
QV4::Value gc(const v8::Arguments &args);
QV4::Value consoleError(const v8::Arguments &args);
QV4::Value consoleLog(const v8::Arguments &args);
QV4::Value consoleProfile(const v8::Arguments &args);
QV4::Value consoleProfileEnd(const v8::Arguments &args);
QV4::Value consoleTime(const v8::Arguments &args);
QV4::Value consoleTimeEnd(const v8::Arguments &args);
QV4::Value consoleCount(const v8::Arguments &args);
QV4::Value consoleTrace(const v8::Arguments &args);
QV4::Value consoleWarn(const v8::Arguments &args);
QV4::Value consoleAssert(const v8::Arguments &args);
QV4::Value consoleException(const v8::Arguments &args);
QV4::Value isQtObject(const v8::Arguments &args);
QV4::Value rgba(const v8::Arguments &args);
QV4::Value hsla(const v8::Arguments &args);
QV4::Value colorEqual(const v8::Arguments &args);
QV4::Value font(const v8::Arguments &args);
QV4::Value rect(const v8::Arguments &args);
QV4::Value point(const v8::Arguments &args);
QV4::Value size(const v8::Arguments &args);
QV4::Value vector2d(const v8::Arguments &args);
QV4::Value vector3d(const v8::Arguments &args);
QV4::Value vector4d(const v8::Arguments &args);
QV4::Value quaternion(const v8::Arguments &args);
QV4::Value matrix4x4(const v8::Arguments &args);
QV4::Value lighter(const v8::Arguments &args);
QV4::Value darker(const v8::Arguments &args);
QV4::Value tint(const v8::Arguments &args);
QV4::Value formatDate(const v8::Arguments &args);
QV4::Value formatTime(const v8::Arguments &args);
QV4::Value formatDateTime(const v8::Arguments &args);
QV4::Value openUrlExternally(const v8::Arguments &args);
QV4::Value fontFamilies(const v8::Arguments &args);
QV4::Value md5(const v8::Arguments &args);
QV4::Value btoa(const v8::Arguments &args);
QV4::Value atob(const v8::Arguments &args);
QV4::Value quit(const v8::Arguments &args);
QV4::Value resolvedUrl(const v8::Arguments &args);
QV4::Value createQmlObject(const v8::Arguments &args);
QV4::Value createComponent(const v8::Arguments &args);
#ifndef QT_NO_TRANSLATION
QV4::Value qsTranslate(const v8::Arguments &args);
QV4::Value qsTranslateNoOp(const v8::Arguments &args);
QV4::Value qsTr(const v8::Arguments &args);
QV4::Value qsTrNoOp(const v8::Arguments &args);
QV4::Value qsTrId(const v8::Arguments &args);
QV4::Value qsTrIdNoOp(const v8::Arguments &args);
QtObject(ExecutionEngine *v4, QQmlEngine *qmlEngine);
static Value method_isQtObject(SimpleCallContext *ctx);
static Value method_rgba(SimpleCallContext *ctx);
static Value method_hsla(SimpleCallContext *ctx);
static Value method_colorEqual(SimpleCallContext *ctx);
static Value method_font(SimpleCallContext *ctx);
static Value method_rect(SimpleCallContext *ctx);
static Value method_point(SimpleCallContext *ctx);
static Value method_size(SimpleCallContext *ctx);
static Value method_vector2d(SimpleCallContext *ctx);
static Value method_vector3d(SimpleCallContext *ctx);
static Value method_vector4d(SimpleCallContext *ctx);
static Value method_quaternion(SimpleCallContext *ctx);
static Value method_matrix4x4(SimpleCallContext *ctx);
static Value method_lighter(SimpleCallContext *ctx);
static Value method_darker(SimpleCallContext *ctx);
static Value method_tint(SimpleCallContext *ctx);
static Value method_formatDate(SimpleCallContext *ctx);
static Value method_formatTime(SimpleCallContext *ctx);
static Value method_formatDateTime(SimpleCallContext *ctx);
static Value method_openUrlExternally(SimpleCallContext *ctx);
static Value method_fontFamilies(SimpleCallContext *ctx);
static Value method_md5(SimpleCallContext *ctx);
static Value method_btoa(SimpleCallContext *ctx);
static Value method_atob(SimpleCallContext *ctx);
static Value method_quit(SimpleCallContext *ctx);
static Value method_resolvedUrl(SimpleCallContext *ctx);
static Value method_createQmlObject(SimpleCallContext *ctx);
static Value method_createComponent(SimpleCallContext *ctx);
static Value method_locale(SimpleCallContext *ctx);
static Value method_binding(SimpleCallContext *ctx);
static Value method_get_platform(SimpleCallContext *ctx);
static Value method_get_application(SimpleCallContext *ctx);
#ifndef QT_NO_IM
static Value method_get_inputMethod(SimpleCallContext *ctx);
#endif
QV4::Value stringArg(const v8::Arguments &args);
QV4::Value locale(const v8::Arguments &args);
QV4::Value binding(const v8::Arguments &args);
QObject *m_platform;
QObject *m_application;
};
struct ConsoleObject : Object
{
ConsoleObject(ExecutionEngine *v4);
static Value method_error(SimpleCallContext *ctx);
static Value method_log(SimpleCallContext *ctx);
static Value method_profile(SimpleCallContext *ctx);
static Value method_profileEnd(SimpleCallContext *ctx);
static Value method_time(SimpleCallContext *ctx);
static Value method_timeEnd(SimpleCallContext *ctx);
static Value method_count(SimpleCallContext *ctx);
static Value method_trace(SimpleCallContext *ctx);
static Value method_warn(SimpleCallContext *ctx);
static Value method_assert(SimpleCallContext *ctx);
static Value method_exception(SimpleCallContext *ctx);
};
struct GlobalExtensions {
static void init(Object *globalObject);
#ifndef QT_NO_TRANSLATION
static Value method_qsTranslate(SimpleCallContext *ctx);
static Value method_qsTranslateNoOp(SimpleCallContext *ctx);
static Value method_qsTr(SimpleCallContext *ctx);
static Value method_qsTrNoOp(SimpleCallContext *ctx);
static Value method_qsTrId(SimpleCallContext *ctx);
static Value method_qsTrIdNoOp(SimpleCallContext *ctx);
#endif
static Value method_gc(SimpleCallContext *ctx);
// on String:prototype
static Value string_arg(SimpleCallContext *ctx);
};
}
QT_END_NAMESPACE

View File

@ -134,8 +134,6 @@ QV8Engine::QV8Engine(QJSEngine* qq)
, m_engine(0)
, m_xmlHttpRequestData(0)
, m_listModelData(0)
, m_platform(0)
, m_application(0)
{
QML_MEMORY_SCOPE_STRING("QV8Engine::QV8Engine");
qMetaTypeId<QJSValue>();
@ -490,118 +488,9 @@ QVariant QV8Engine::toBasicVariant(const QV4::Value &value)
struct StaticQtMetaObject : public QObject
{
static const QMetaObject *get()
{ return &staticQtMetaObject; }
};
void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global)
{
using namespace QQmlBuiltinFunctions;
v8::Handle<v8::Object> console = v8::Object::New();
v8::Handle<v8::Function> consoleLogFn = V8FUNCTION(consoleLog, this);
console->Set(v8::String::New("debug"), consoleLogFn);
console->Set(v8::String::New("log"), consoleLogFn);
console->Set(v8::String::New("info"), consoleLogFn);
console->Set(v8::String::New("warn"), V8FUNCTION(consoleWarn, this));
console->Set(v8::String::New("error"), V8FUNCTION(consoleError, this));
console->Set(v8::String::New("assert"), V8FUNCTION(consoleAssert, this));
console->Set(v8::String::New("count"), V8FUNCTION(consoleCount, this));
console->Set(v8::String::New("profile"), V8FUNCTION(consoleProfile, this));
console->Set(v8::String::New("profileEnd"), V8FUNCTION(consoleProfileEnd, this));
console->Set(v8::String::New("time"), V8FUNCTION(consoleTime, this));
console->Set(v8::String::New("timeEnd"), V8FUNCTION(consoleTimeEnd, this));
console->Set(v8::String::New("trace"), V8FUNCTION(consoleTrace, this));
console->Set(v8::String::New("exception"), V8FUNCTION(consoleException, this));
v8::Handle<v8::Object> qt = v8::Object::New();
// Set all the enums from the "Qt" namespace
const QMetaObject *qtMetaObject = StaticQtMetaObject::get();
for (int ii = 0; ii < qtMetaObject->enumeratorCount(); ++ii) {
QMetaEnum enumerator = qtMetaObject->enumerator(ii);
for (int jj = 0; jj < enumerator.keyCount(); ++jj) {
qt->Set(v8::String::New(enumerator.key(jj)), QV4::Value::fromInt32(enumerator.value(jj)));
}
}
qt->Set(v8::String::New("Asynchronous"), QV4::Value::fromInt32(0));
qt->Set(v8::String::New("Synchronous"), QV4::Value::fromInt32(1));
qt->v4Value().asObject()->defineDefaultProperty(m_v4Engine, QStringLiteral("include"), QV4Include::include);
qt->Set(v8::String::New("isQtObject"), V8FUNCTION(isQtObject, this));
qt->Set(v8::String::New("rgba"), V8FUNCTION(rgba, this));
qt->Set(v8::String::New("hsla"), V8FUNCTION(hsla, this));
qt->Set(v8::String::New("colorEqual"), V8FUNCTION(colorEqual, this));
qt->Set(v8::String::New("font"), V8FUNCTION(font, this));
qt->Set(v8::String::New("rect"), V8FUNCTION(rect, this));
qt->Set(v8::String::New("point"), V8FUNCTION(point, this));
qt->Set(v8::String::New("size"), V8FUNCTION(size, this));
qt->Set(v8::String::New("vector2d"), V8FUNCTION(vector2d, this));
qt->Set(v8::String::New("vector3d"), V8FUNCTION(vector3d, this));
qt->Set(v8::String::New("vector4d"), V8FUNCTION(vector4d, this));
qt->Set(v8::String::New("quaternion"), V8FUNCTION(quaternion, this));
qt->Set(v8::String::New("matrix4x4"), V8FUNCTION(matrix4x4, this));
qt->Set(v8::String::New("formatDate"), V8FUNCTION(formatDate, this));
qt->Set(v8::String::New("formatTime"), V8FUNCTION(formatTime, this));
qt->Set(v8::String::New("formatDateTime"), V8FUNCTION(formatDateTime, this));
qt->Set(v8::String::New("openUrlExternally"), V8FUNCTION(openUrlExternally, this));
qt->Set(v8::String::New("fontFamilies"), V8FUNCTION(fontFamilies, this));
qt->Set(v8::String::New("md5"), V8FUNCTION(md5, this));
qt->Set(v8::String::New("btoa"), V8FUNCTION(btoa, this));
qt->Set(v8::String::New("atob"), V8FUNCTION(atob, this));
qt->Set(v8::String::New("resolvedUrl"), V8FUNCTION(resolvedUrl, this));
qt->Set(v8::String::New("locale"), V8FUNCTION(locale, this));
qt->Set(v8::String::New("binding"), V8FUNCTION(binding, this));
if (m_engine) {
qt->SetAccessor(v8::String::New("platform"), getPlatform, 0, v8::External::New(this));
qt->SetAccessor(v8::String::New("application"), getApplication, 0, v8::External::New(this));
#ifndef QT_NO_IM
qt->SetAccessor(v8::String::New("inputMethod"), getInputMethod, 0, v8::External::New(this));
#endif
qt->Set(v8::String::New("lighter"), V8FUNCTION(lighter, this));
qt->Set(v8::String::New("darker"), V8FUNCTION(darker, this));
qt->Set(v8::String::New("tint"), V8FUNCTION(tint, this));
qt->Set(v8::String::New("quit"), V8FUNCTION(quit, this));
qt->Set(v8::String::New("createQmlObject"), V8FUNCTION(createQmlObject, this));
qt->Set(v8::String::New("createComponent"), V8FUNCTION(createComponent, this));
}
#ifndef QT_NO_TRANSLATION
global->Set(v8::String::New("qsTranslate"), V8FUNCTION(qsTranslate, this));
global->Set(v8::String::New("QT_TRANSLATE_NOOP"), V8FUNCTION(qsTranslateNoOp, this));
global->Set(v8::String::New("qsTr"), V8FUNCTION(qsTr, this));
global->Set(v8::String::New("QT_TR_NOOP"), V8FUNCTION(qsTrNoOp, this));
global->Set(v8::String::New("qsTrId"), V8FUNCTION(qsTrId, this));
global->Set(v8::String::New("QT_TRID_NOOP"), V8FUNCTION(qsTrIdNoOp, this));
#endif
global->Set(v8::String::New("print"), consoleLogFn);
global->Set(v8::String::New("console"), console);
global->Set(v8::String::New("Qt"), qt);
global->Set(v8::String::New("gc"), V8FUNCTION(QQmlBuiltinFunctions::gc, this));
{
#define STRING_ARG "(function(stringArg) { "\
" String.prototype.arg = (function() {"\
" return stringArg.apply(this, arguments);"\
" })"\
"})"
QV4::Script registerArg(m_v4Engine->rootContext, STRING_ARG);
QV4::FunctionObject *registerArgFunc = registerArg.run().asFunctionObject();
Q_ASSERT(registerArgFunc);
QV4::Value args = V8FUNCTION(stringArg, this)->v4Value();
registerArgFunc->call(QV4::Value::fromObject(registerArgFunc), &args, 1);
#undef STRING_ARG
}
QV4::GlobalExtensions::init(m_v4Engine->globalObject);
QQmlLocale::registerStringLocaleCompare(m_v4Engine);
QQmlDateExtension::registerExtension(m_v4Engine);
@ -1330,34 +1219,6 @@ int QV8Engine::consoleCountHelper(const QString &file, quint16 line, quint16 col
return number;
}
v8::Handle<v8::Value> QV8Engine::getPlatform(v8::Handle<v8::String>, const v8::AccessorInfo &info)
{
QV8Engine *engine = reinterpret_cast<QV8Engine*>(v8::External::Cast(info.Data().get())->Value());
if (!engine->m_platform) {
// Only allocate a platform object once
engine->m_platform = new QQmlPlatform(engine->m_engine);
}
return engine->newQObject(engine->m_platform);
}
v8::Handle<v8::Value> QV8Engine::getApplication(v8::Handle<v8::String>, const v8::AccessorInfo &info)
{
QV8Engine *engine = reinterpret_cast<QV8Engine*>(v8::External::Cast(info.Data().get())->Value());
if (!engine->m_application) {
// Only allocate an application object once
engine->m_application = QQml_guiProvider()->application(engine->m_engine);
}
return engine->newQObject(engine->m_application);
}
#ifndef QT_NO_IM
v8::Handle<v8::Value> QV8Engine::getInputMethod(v8::Handle<v8::String>, const v8::AccessorInfo &info)
{
QV8Engine *engine = reinterpret_cast<QV8Engine*>(v8::External::Cast(info.Data().get())->Value());
return engine->newQObject(QQml_guiProvider()->inputMethod(), CppOwnership);
}
#endif
void QV8GCCallback::registerGcPrologueCallback()
{
QV8Engine::ThreadData *td = QV8Engine::threadData();

View File

@ -375,12 +375,6 @@ public:
void addRelationshipForGC(QObject *object, const QV4::PersistentValue &handle);
void addRelationshipForGC(QObject *object, QObject *other);
static v8::Handle<v8::Value> getPlatform(v8::Handle<v8::String> property, const v8::AccessorInfo &info);
static v8::Handle<v8::Value> getApplication(v8::Handle<v8::String> property, const v8::AccessorInfo &info);
#ifndef QT_NO_IM
static v8::Handle<v8::Value> getInputMethod(v8::Handle<v8::String> property, const v8::AccessorInfo &info);
#endif
struct ThreadData {
ThreadData();
~ThreadData();
@ -423,9 +417,6 @@ protected:
QHash<QString, quint32> m_consoleCount;
QObject *m_platform;
QObject *m_application;
QVariant toBasicVariant(const QV4::Value &);
void initializeGlobal(v8::Handle<v8::Object>);