1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
Actionaz
Copyright (C) 2008-2014 Jonathan Mercier-Ganady
Actionaz is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Actionaz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact : jmgr@jmgr.info
*/
#include "codeclass.h"
#include <QScriptValue>
#include <QScriptEngine>
#include <QStringList>
#include <QScriptValueIterator>
namespace Code
{
void CodeClass::throwError(QScriptContext *context, QScriptEngine *engine, const QString &errorType, const QString &message, const QString &parent)
{
QScriptValue errorTypeValue = engine->globalObject().property(errorType);
if(!errorTypeValue.isValid())
{
errorTypeValue = engine->newFunction(emptyFunction);
engine->globalObject().setProperty(errorType, errorTypeValue);
errorTypeValue.setProperty("prototype", engine->globalObject().property(parent).construct());
}
QScriptValue thrownError = errorTypeValue.construct();
thrownError.setProperty("message", message);
thrownError.setProperty("name", errorType);
context->throwValue(thrownError);
}
CodeClass::CodeClass()
: QObject(),
QScriptable()
{
}
void CodeClass::throwError(const QString &errorType, const QString &message, const QString &parent) const
{
throwError(context(), engine(), errorType, message, parent);
}
QScriptValue CodeClass::constructor(CodeClass *object, QScriptContext *context, QScriptEngine *engine)
{
if(context->isCalledAsConstructor())
{
engine->reportAdditionalMemoryCost(object->additionalMemoryCost());
return engine->newQObject(context->thisObject(), object, QScriptEngine::ScriptOwnership);
}
else
return constructor(object, engine);
}
QScriptValue CodeClass::constructor(CodeClass *object, QScriptEngine *engine)
{
engine->reportAdditionalMemoryCost(object->additionalMemoryCost());
return engine->newQObject(object, QScriptEngine::ScriptOwnership);
}
QByteArray CodeClass::toEncoding(const QString &string, Encoding encoding)
{
switch(encoding)
{
case Native:
return string.toLocal8Bit();
case Ascii:
return string.toAscii();
case Latin1:
return string.toLatin1();
case UTF8:
return string.toUtf8();
default:
return QByteArray();
}
}
QString CodeClass::fromEncoding(const QByteArray &byteArray, Encoding encoding)
{
switch(encoding)
{
case Native:
return QString::fromLocal8Bit(byteArray.data(), byteArray.size());
case Ascii:
return QString::fromAscii(byteArray.data(), byteArray.size());
case Latin1:
return QString::fromLatin1(byteArray.data(), byteArray.size());
case UTF8:
return QString::fromUtf8(byteArray.data(), byteArray.size());
default:
return QString();
}
}
QStringList CodeClass::arrayParameterToStringList(const QScriptValue &scriptValue)
{
QStringList back;
QScriptValueIterator it(scriptValue);
while(it.hasNext())
{
it.next();
back << it.value().toString();
}
return back;
}
QScriptValue CodeClass::stringListToArrayParameter(QScriptEngine *engine, const QStringList &stringList)
{
if(stringList.count() == 0)
return engine->undefinedValue();
QScriptValue back = engine->newArray(stringList.count());
for(int index = 0; index < stringList.count(); ++index)
back.setProperty(index, stringList.at(index));
return back;
}
QScriptValue CodeClass::emptyFunction(QScriptContext *context, QScriptEngine *engine)
{
if(context->isCalledAsConstructor())
return context->thisObject();
else
return engine->newObject();
}
}
|