* @brief Compiles an output-widget transmit() in the live helper environment without applying. */
| 6240 | * @brief Compiles an output-widget transmit() in the live helper environment without applying. |
| 6241 | */ |
| 6242 | API::CommandResponse API::Handlers::ProjectHandler::outputWidgetDryRun(const QString& id, |
| 6243 | const QJsonObject& params) |
| 6244 | { |
| 6245 | if (!params.contains(QStringLiteral("code"))) |
| 6246 | return CommandResponse::makeError( |
| 6247 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: code")); |
| 6248 | |
| 6249 | const auto code = params.value(QStringLiteral("code")).toString(); |
| 6250 | DataModel::FrameBuilder::instance().refreshTableStoreFromProjectModel(); |
| 6251 | |
| 6252 | QJSEngine engine; |
| 6253 | engine.installExtensions(QJSEngine::ConsoleExtension | QJSEngine::GarbageCollectionExtension); |
| 6254 | #ifdef BUILD_COMMERCIAL |
| 6255 | Widgets::Output::Base::installProtocolHelpers(engine); |
| 6256 | #endif |
| 6257 | DataModel::FrameBuilder::instance().injectTableApiJS(&engine); |
| 6258 | |
| 6259 | const auto wrapped = |
| 6260 | QStringLiteral("(function() { %1\n" |
| 6261 | "return typeof transmit === 'function' ? transmit : undefined; })()") |
| 6262 | .arg(code); |
| 6263 | auto transmitFn = engine.evaluate(wrapped, QStringLiteral("output_widget_dryrun.js")); |
| 6264 | if (transmitFn.isError()) { |
| 6265 | QJsonObject result; |
| 6266 | result[QStringLiteral("ok")] = false; |
| 6267 | result[QStringLiteral("compileError")] = |
| 6268 | transmitFn.property(QStringLiteral("message")).toString(); |
| 6269 | result[QStringLiteral("line")] = transmitFn.property(QStringLiteral("lineNumber")).toInt(); |
| 6270 | return CommandResponse::makeSuccess(id, result); |
| 6271 | } |
| 6272 | |
| 6273 | if (!transmitFn.isCallable()) { |
| 6274 | QJsonObject result; |
| 6275 | result[QStringLiteral("ok")] = false; |
| 6276 | result[QStringLiteral("compileError")] = |
| 6277 | QStringLiteral("Script compiled but did not define transmit(value). Output-widget " |
| 6278 | "transmit scripts MUST define `function transmit(value)` returning the " |
| 6279 | "bytes to send (a Uint8Array, a byte array, or a string). The function " |
| 6280 | "is named `transmit`, not `output` or `send`."); |
| 6281 | return CommandResponse::makeSuccess(id, result); |
| 6282 | } |
| 6283 | |
| 6284 | QJsonObject result; |
| 6285 | result[QStringLiteral("ok")] = true; |
| 6286 | result[QStringLiteral("hasTransmit")] = true; |
| 6287 | if (params.contains(QStringLiteral("inputValue"))) |
| 6288 | result[QStringLiteral("sampleRun")] = |
| 6289 | runOutputWidgetSample(engine, |
| 6290 | transmitFn, |
| 6291 | params.value(QStringLiteral("inputValue")), |
| 6292 | params.value(QStringLiteral("hex")).toBool()); |
| 6293 | else |
| 6294 | result[QStringLiteral("hint")] = |
| 6295 | QStringLiteral("Compiled and transmit(value) is defined. Pass inputValue (and hex:true " |
| 6296 | "for hex byte input) to also execute it and see the produced bytes."); |
| 6297 | |
| 6298 | return CommandResponse::makeSuccess(id, result); |
| 6299 | } |
nothing calls this directly
no test coverage detected