Added try/catch in JSPortal for installing/executing JS functions

Docs
This commit is contained in:
Ralph Wessel
2024-08-19 22:35:30 +01:00
parent c454191ab5
commit 6c6230ba32
3 changed files with 33 additions and 24 deletions
@@ -19,8 +19,7 @@ namespace {
bridge: The parent bridge object (provides access to bridge methods)
--------------------------------------------------------------------*/
GetAccounts::GetAccounts() : JSBridgeMethod{"GetAccounts", [&]() { return getAccounts(); }} {
} //GetAccounts::GetAccounts
GetAccounts::GetAccounts() : JSBridgeMethod{"GetAccounts", [&]() { return getAccounts(); }} {}
/*--------------------------------------------------------------------
@@ -21,17 +21,15 @@ namespace connector::interfac::browser::bridge {
@param bridge The parent bridge object (provides access to bridge methods)
*/
GetAccounts();
/*!
Cppy constructor
@param bridge The parent bridge object (provides access to bridge methods)
*/
GetAccounts(const GetAccounts& source) = default;
/*!
Object cloning
@return A clone of this object
*/
GetAccounts* clonePtr() const override { return new GetAccounts{*this}; }
// MARK: - Functions (const)
/*!
Get an argument instance for the function (used to deserialise/unpack incoming arguments)
@return An argument instance
+29 -17
View File
@@ -68,11 +68,14 @@ namespace speckle::interfac::browser {
template<typename FunctionBinding>
bool JSPortal<FunctionBinding>::execute(const speckle::utility::String& code) const {
#ifdef ARCHICAD
auto engine = getJSEngine();
return engine ? engine->ExecuteJS(code) : false;
#else
return false; //Implement as required
try {
auto engine = getJSEngine();
return engine ? engine->ExecuteJS(code) : false;
} catch(...) {
///TODO: Need to discuss the best course of action to notify of a failure
}
#endif
return false;
} //JSPortal<FunctionBinding>::execute
@@ -86,19 +89,28 @@ namespace speckle::interfac::browser {
template<typename FunctionBinding>
bool JSPortal<FunctionBinding>::install(std::shared_ptr<JSObject<FunctionBinding>> object) {
#ifdef ARCHICAD
auto engine = getJSEngine();
if (!engine)
return false;
JS::Object* acObject = new JS::Object(object->getName());
for (auto& function : *object) {
acObject->AddItem(new JS::Function(function->getName(), [&] (GS::Ref<JS::Base> args) {
return function->execute(args);
}));
}
if (engine->RegisterAsynchJSObject(acObject)) {
base::push_back(object);
object->setPortal(*this);
return true;
try {
auto engine = getJSEngine();
if (!engine)
return false;
JS::Object* acObject = new JS::Object(object->getName());
for (auto& function : *object) {
acObject->AddItem(new JS::Function(function->getName(), [&] (GS::Ref<JS::Base> args) {
try {
return function->execute(args);
} catch(...) {
///TODO: Need to discuss the best course of action to notify of a failure
}
return GS::Ref<JS::Base>{};
}));
}
if (engine->RegisterAsynchJSObject(acObject)) {
base::push_back(object);
object->setPortal(*this);
return true;
}
} catch(...) {
///TODO: Need to discuss the best course of action to notify of a failure
}
#endif
return false;