Showing the changes to interface/src/scripting/OSCScriptingInterface.cpp (1 of 3 files). Show all
Unified · Split
| @@ -14,7 +14,9 @@ · 13 unmodified lines | ||
| 14 | 14 | #include <QtEndian> |
| 15 | 15 | #include <QNetworkDatagram> |
| 16 | 16 | |
| 17 | +#include "ScriptContext.h" | |
| 17 | 18 | #include "ScriptEngine.h" |
| 19 | +#include "ScriptValue.h" | |
| 18 | 20 | #include "OSCScriptingInterface.h" |
| 19 | 21 | |
| 20 | 22 | |
| @@ -179,26 +181,29 @@ void OSCScriptingInterface::readPacket() { · 158 unmodified lines | ||
| 179 | 181 | emit packetReceived(addr, args); |
| 180 | 182 | } |
| 181 | 183 | |
| 182 | −void OSCScriptingInterface::sendPacket(const QString& address, const QVariantList& arguments) { | |
| 183 | − Q_ASSERT(_socket); | |
| 184 | +ScriptValue OSCScriptingInterface::sendPacket(ScriptContext* context, ScriptEngine* engine) { | |
| 185 | + auto instance = DependencyManager::get<OSCScriptingInterface>(); | |
| 184 | 186 | |
| 185 | − // FIXME: how do you get the script engine??? | |
| 186 | − struct HackRemoveThis { | |
| 187 | − void raiseException(QString msg) { | |
| 188 | − qCCritical(osc_cat) << msg; | |
| 189 | − } | |
| 190 | − }; | |
| 187 | + if (context->argumentCount() == 0) { | |
| 188 | + engine->raiseException("Address is required"); | |
| 189 | + return engine->undefinedValue(); | |
| 190 | + } | |
| 191 | + | |
| 192 | + auto address = context->argument(0).toString(); | |
| 193 | + QVariantList arguments; | |
| 191 | 194 | |
| 192 | − auto scriptEngine = std::make_unique<HackRemoveThis>(); //_scriptManager->engine(); | |
| 195 | + for (int i = 1; i < context->argumentCount(); i++) { | |
| 196 | + arguments.append(context->argument(i).toVariant()); | |
| 197 | + } | |
| 193 | 198 | |
| 194 | 199 | if (address.length() < 2 || !address.startsWith('/') || address.endsWith('/')) { |
| 195 | − scriptEngine->raiseException("address must be at least two characters and start with '/'"); | |
| 196 | − return; | |
| 200 | + engine->raiseException("Address must be at least two characters and start with '/'"); | |
| 201 | + return engine->undefinedValue(); | |
| 197 | 202 | } |
| 198 | 203 | |
| 199 | 204 | if (auto match = invalidCharacters.match(address); match.hasMatch()) { |
| 200 | − scriptEngine->raiseException(QString("address contains invalid character '%1'").arg(match.captured())); | |
| 201 | − return; | |
| 205 | + engine->raiseException(QString("Address contains invalid character '%1'").arg(match.captured())); | |
| 206 | + return engine->undefinedValue(); | |
| 202 | 207 | } |
| 203 | 208 | |
| 204 | 209 | auto pad4 = [](QByteArray& array) { |
| @@ -226,8 +231,8 @@ void OSCScriptingInterface::sendPacket(const QString& address, const QVariantLis · 21 unmodified lines | ||
| 226 | 231 | auto map = arg.toMap(); |
| 227 | 232 | auto typeName = map.value("type").toString(); |
| 228 | 233 | if (!typeNameMap.contains(typeName[0])) { |
| 229 | − scriptEngine->raiseException(QString("Unknown type '%c' on argument %d").arg(typeName).arg(i)); | |
| 230 | − return; | |
| 234 | + engine->raiseException(QString("Unknown type '%c' on argument %d").arg(typeName).arg(i)); | |
| 235 | + return engine->undefinedValue(); | |
| 231 | 236 | } |
| 232 | 237 | expectedType = typeNameMap[typeName[0]]; |
| 233 | 238 | value = map.value("value"); |
| @@ -268,8 +273,8 @@ void OSCScriptingInterface::sendPacket(const QString& address, const QVariantLis · 34 unmodified lines | ||
| 268 | 273 | auto stringArg = value.toString(); |
| 269 | 274 | |
| 270 | 275 | if (stringArg.contains(QChar(0))) { |
| 271 | − scriptEngine->raiseException("String contains null (0x00) character"); | |
| 272 | − return; | |
| 276 | + engine->raiseException("String contains null (0x00) character"); | |
| 277 | + return engine->undefinedValue(); | |
| 273 | 278 | } |
| 274 | 279 | |
| 275 | 280 | bodyBytes.append(stringArg.toUtf8()); |
| @@ -301,8 +306,8 @@ void OSCScriptingInterface::sendPacket(const QString& address, const QVariantLis · 25 unmodified lines | ||
| 301 | 306 | break; |
| 302 | 307 | |
| 303 | 308 | default: |
| 304 | − scriptEngine->raiseException(QString("Unserializable type %1").arg(value.typeName())); | |
| 305 | − return; | |
| 309 | + engine->raiseException(QString("Unserializable type %1").arg(value.typeName())); | |
| 310 | + return engine->undefinedValue(); | |
| 306 | 311 | } |
| 307 | 312 | } |
| 308 | 313 | |
| @@ -313,7 +318,9 @@ void OSCScriptingInterface::sendPacket(const QString& address, const QVariantLis · 4 unmodified lines | ||
| 313 | 318 | // pack the header + body data together for the datagram |
| 314 | 319 | bytes.append(bodyBytes); |
| 315 | 320 | |
| 316 | − _socket->writeDatagram(bytes, QHostAddress(_sendHost.get()), _sendPort.get()); | |
| 321 | + instance->_socket->writeDatagram(bytes, QHostAddress(instance->_sendHost.get()), instance->_sendPort.get()); | |
| 322 | + | |
| 323 | + qCDebug(osc_cat) << QHostAddress(instance->_sendHost.get()) << instance->_sendPort.get() << ":" << bytes.toHex(' '); | |
| 317 | 324 | |
| 318 | − qCDebug(osc_cat) << QHostAddress(_sendHost.get()) << _sendPort.get() << ":" << bytes.toHex(' '); | |
| 325 | + return engine->undefinedValue(); | |
| 319 | 326 | } |