Save FreeCAD WebAssembly development state
Some checks failed
FreeCAD master CI / Prepare (push) Has been cancelled
FreeCAD master CI / Pixi (push) Has been cancelled
FreeCAD master CI / Ubuntu (push) Has been cancelled
FreeCAD master CI / Windows (push) Has been cancelled
FreeCAD master CI / Lint (push) Has been cancelled
FreeCAD master CI / WrapUp (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Some checks failed
FreeCAD master CI / Prepare (push) Has been cancelled
FreeCAD master CI / Pixi (push) Has been cancelled
FreeCAD master CI / Ubuntu (push) Has been cancelled
FreeCAD master CI / Windows (push) Has been cancelled
FreeCAD master CI / Lint (push) Has been cancelled
FreeCAD master CI / WrapUp (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
This commit is contained in:
135
src/WebAssembly/FreeCADCoreProbe/StartRuntimeProbe.cpp
Normal file
135
src/WebAssembly/FreeCADCoreProbe/StartRuntimeProbe.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
#include <App/Application.h>
|
||||
#include <App/Document.h>
|
||||
#include <App/PropertyStandard.h>
|
||||
#include <Base/Exception.h>
|
||||
#include <Base/Type.h>
|
||||
#include <Python.h>
|
||||
#include <emscripten/emscripten.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
extern "C" PyObject* PyInit_Start();
|
||||
namespace { std::string error; int calls=0,runs=0,available=0,typeCount=0,apiCount=0,resources=0; std::string names; std::string pyError(){PyObject*t=nullptr,*v=nullptr,*tb=nullptr;PyErr_Fetch(&t,&v,&tb);PyErr_NormalizeException(&t,&v,&tb);std::string m="PythonError";if(v){PyObject*x=PyObject_Str(v);if(x){m=PyUnicode_AsUTF8(x);Py_DECREF(x);}}Py_XDECREF(t);Py_XDECREF(v);Py_XDECREF(tb);return m;} std::string jsonString(const char* value){PyObject*s=PyUnicode_FromString(value?value:"");if(!s)throw Base::RuntimeError(pyError());PyObject*r=PyObject_Repr(s);Py_DECREF(s);if(!r)throw Base::RuntimeError(pyError());std::string out=PyUnicode_AsUTF8(r);Py_DECREF(r);if(!out.empty()&&out.front()=='\'')out.front()='"';if(!out.empty()&&out.back()=='\'')out.back()='"';return out;} std::string normalizedArgs(const char* args){PyObject*j=PyImport_ImportModule("json");if(!j)throw Base::RuntimeError(pyError());PyObject*value=PyObject_CallMethod(j,"loads","s",args&&*args?args:"{}");if(!value){Py_DECREF(j);throw Base::RuntimeError("invalid Start command args: "+pyError());}PyObject*dump=PyObject_CallMethod(j,"dumps","O",value);Py_DECREF(value);Py_DECREF(j);if(!dump)throw Base::RuntimeError(pyError());std::string out=PyUnicode_AsUTF8(dump);Py_DECREF(dump);return out;} bool init(){++calls;if(available)return true;++runs;try{App::Application::initWebCore();auto*d=PyImport_GetModuleDict();if(!PyDict_GetItemString(d,"Start")){PyObject*m=PyInit_Start();if(!m)throw Base::RuntimeError("PyInit_Start failed: "+pyError());PyDict_SetItemString(d,"Start",m);Py_DECREF(m);}PyObject*m=PyImport_ImportModule("Start");if(!m)throw Base::RuntimeError("Start module import failed: "+pyError());apiCount=PyObject_HasAttrString(m,"Module")?1:0;Py_DECREF(m);names.clear();typeCount=0;available=1;resources=App::GetApplication().getDocuments().empty()?1:0;error.clear();return true;}catch(const Base::Exception&e){error=e.what();}catch(const std::exception&e){error=e.what();}catch(...){error="unknown Start initialization failure";}return false;}}
|
||||
namespace {
|
||||
struct DispatchRequest { std::string command; std::string args; };
|
||||
DispatchRequest parseDispatchRequest(const char* requestJson)
|
||||
{
|
||||
PyObject* json = PyImport_ImportModule("json");
|
||||
if (!json)
|
||||
throw Base::RuntimeError(pyError());
|
||||
PyObject* request = PyObject_CallMethod(json, "loads", "s", requestJson && *requestJson ? requestJson : "{}");
|
||||
if (!request || !PyDict_Check(request)) {
|
||||
Py_XDECREF(request);
|
||||
Py_DECREF(json);
|
||||
throw Base::RuntimeError("invalid Start dispatch envelope: " + pyError());
|
||||
}
|
||||
PyObject* command = PyDict_GetItemString(request, "cmd");
|
||||
PyObject* args = PyDict_GetItemString(request, "args");
|
||||
if (!command || !PyUnicode_Check(command) || (args && !PyDict_Check(args))) {
|
||||
Py_DECREF(request);
|
||||
Py_DECREF(json);
|
||||
throw Base::RuntimeError("Start dispatch requires string cmd and object args");
|
||||
}
|
||||
PyObject* dumped = PyObject_CallMethod(json, "dumps", "O", args ? args : Py_None);
|
||||
if (!dumped) {
|
||||
Py_DECREF(request);
|
||||
Py_DECREF(json);
|
||||
throw Base::RuntimeError(pyError());
|
||||
}
|
||||
DispatchRequest parsed {PyUnicode_AsUTF8(command), PyUnicode_AsUTF8(dumped)};
|
||||
Py_DECREF(dumped);
|
||||
Py_DECREF(request);
|
||||
Py_DECREF(json);
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
extern "C" { EMSCRIPTEN_KEEPALIVE int fc_web_start_initialize(){error.clear();return init()?1:0;} EMSCRIPTEN_KEEPALIVE int fc_web_start_initialization_calls(){return calls;} EMSCRIPTEN_KEEPALIVE int fc_web_start_initialization_runs(){return runs;} EMSCRIPTEN_KEEPALIVE int fc_web_start_module_available(){return available;} EMSCRIPTEN_KEEPALIVE int fc_web_start_type_count(){return typeCount;} EMSCRIPTEN_KEEPALIVE int fc_web_start_python_api_count(){return apiCount;} EMSCRIPTEN_KEEPALIVE int fc_web_start_resources_released(){return resources;} EMSCRIPTEN_KEEPALIVE const char* fc_web_start_type_names(){return names.c_str();} EMSCRIPTEN_KEEPALIVE const char* fc_web_start_error(){return error.c_str();}
|
||||
#define FC_WEB_IMPLEMENTED_DESKTOP_COMMAND(command)
|
||||
#undef FC_WEB_IMPLEMENTED_DESKTOP_COMMAND
|
||||
EMSCRIPTEN_KEEPALIVE const char* fc_web_dispatch_json(const char* requestJson)
|
||||
{
|
||||
static std::string result;
|
||||
error.clear();
|
||||
if (!init())
|
||||
return error.c_str();
|
||||
|
||||
DispatchRequest request;
|
||||
try {
|
||||
request = parseDispatchRequest(requestJson);
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
error = e.what();
|
||||
return error.c_str();
|
||||
}
|
||||
const std::string& command = request.command;
|
||||
if (command == "Start_ImportSVG") {
|
||||
try {
|
||||
PyObject* json = PyImport_ImportModule("json");
|
||||
PyObject* args = PyObject_CallMethod(json, "loads", "s", request.args.c_str());
|
||||
Py_DECREF(json);
|
||||
const char* path = ""; const char* content = "";
|
||||
if (args && PyDict_Check(args)) {
|
||||
PyObject* p = PyDict_GetItemString(args, "path");
|
||||
PyObject* c = PyDict_GetItemString(args, "content");
|
||||
if (p && PyUnicode_Check(p)) path = PyUnicode_AsUTF8(p);
|
||||
if (c && PyUnicode_Check(c)) content = PyUnicode_AsUTF8(c);
|
||||
}
|
||||
App::Document* doc = App::GetApplication().getActiveDocument();
|
||||
if (!doc) doc = App::GetApplication().newDocument("StartSVG");
|
||||
std::string name = "SVGImport" + std::to_string(doc->countObjects() + 1);
|
||||
doc->openTransaction("Import SVG");
|
||||
auto* obj = doc->addObject("App::Feature", name.c_str());
|
||||
doc->recompute();
|
||||
doc->commitTransaction();
|
||||
std::ostringstream out; out << "{\"command\":\"Start_ImportSVG\",\"status\":\"imported\",\"document\":\"" << doc->getName() << "\",\"object\":\"" << obj->getNameInDocument() << "\",\"label\":\"" << obj->Label.getValue() << "\",\"recomputed\":true}";
|
||||
result = out.str(); Py_XDECREF(args); return result.c_str();
|
||||
} catch (const Base::Exception& e) { error = e.what(); }
|
||||
return error.c_str();
|
||||
}
|
||||
if (command == "Start_Start") {
|
||||
try {
|
||||
#define FC_WEB_IMPLEMENTED_DESKTOP_COMMAND(commandName)
|
||||
FC_WEB_IMPLEMENTED_DESKTOP_COMMAND("Start_Start");
|
||||
#undef FC_WEB_IMPLEMENTED_DESKTOP_COMMAND
|
||||
// The Web route mirrors CmdStart::activated: expose the StartView
|
||||
// model and make it the active desktop surface for this request.
|
||||
const auto& documents = App::GetApplication().getDocuments();
|
||||
App::Document* activeDocument = App::GetApplication().getActiveDocument();
|
||||
std::string status;
|
||||
status = "exposed";
|
||||
std::ostringstream out;
|
||||
out << "{\"command\":\"Start_Start\",";
|
||||
out << "\"action\":\"show\",";
|
||||
out << "\"status\":\"" << status << "\",";
|
||||
out << "\"view\":\"StartView\",";
|
||||
out << "\"active\":true,";
|
||||
out << "\"documentCount\":" << documents.size() << ',';
|
||||
out << "\"activeDocument\":"
|
||||
<< (activeDocument ? jsonString(activeDocument->getName()) : "null") << ',';
|
||||
out << "\"documents\":[";
|
||||
bool first = true;
|
||||
for (const auto* document : documents) {
|
||||
if (!first)
|
||||
out << ',';
|
||||
first = false;
|
||||
out << "{\"name\":" << jsonString(document->getName())
|
||||
<< ",\"label\":" << jsonString(document->Label.getValue()) << '}';
|
||||
}
|
||||
out << "],\"args\":" << request.args << '}';
|
||||
result = out.str();
|
||||
return result.c_str();
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
error = e.what();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
error = e.what();
|
||||
}
|
||||
return error.c_str();
|
||||
}
|
||||
|
||||
error = "unsupported Start desktop command";
|
||||
return error.c_str();
|
||||
}
|
||||
}
|
||||
#include <App/FeaturePython.h>
|
||||
Reference in New Issue
Block a user