Remove custom gpp_rethrow wrapper

Upstream Emscripten now natively propagates exception messages to JS.
This commit is contained in:
Ingvar Stepanyan
2023-10-17 03:25:17 +01:00
parent 5652da70d0
commit d377c1426a

View File

@@ -50,24 +50,6 @@ void gpp_log_error(GPContext *context, const char *text, void *data) {
OUT; \
})
EM_JS([[noreturn]] void, throw_msg, (const char *msg),
{ throw new Error(UTF8ToString(msg)); });
// This is workaround for Emscripten throwing C++ exceptions as meaningless
// numbers (pointers) instead of actual messages.
// To rethrow actual message, we manually wrap all Embind exports into
// callbacks, those callbacks are passed to this function,
// it ensures that all destructors are executed correctly, catches C++ exception
// if any, extracts the message and rethrows it as a JS one. Easy-peasy...
template <typename Func>
auto gpp_rethrow(Func func) {
try {
return func();
} catch (std::exception &e) {
throw_msg(e.what());
}
}
const thread_local val Uint8Array = val::global("Uint8Array");
const thread_local val Blob = val::global("Blob");
const thread_local val File = val::global("File");
@@ -75,17 +57,14 @@ const thread_local val arrayOf = val::global("Array")["of"];
class Context {
public:
Context() : camera(nullptr), context(nullptr) {
gpp_rethrow([=]() {
camera.reset(GPP_CALL(Camera *, gp_camera_new(_)));
context.reset(gp_context_new());
Context()
: camera(GPP_CALL(Camera *, gp_camera_new(_))),
context(gp_context_new()) {
gp_context_set_error_func(context.get(), gpp_log_error, nullptr);
gpp_try(gp_camera_init(camera.get(), context.get()));
});
}
val supportedOps() {
return gpp_rethrow([=]() {
auto ops =
GPP_CALL(CameraAbilities, gp_camera_get_abilities(camera.get(), _))
.operations;
@@ -98,11 +77,9 @@ class Context {
result.set("config", (ops & GP_OPERATION_CONFIG) != 0);
result.set("triggerCapture", (ops & GP_OPERATION_TRIGGER_CAPTURE) != 0);
return result;
});
}
bool consumeEvents() {
return gpp_rethrow([=]() {
bool had_events = false;
for (;;) {
CameraEventType event_type = GP_EVENT_UNKNOWN;
@@ -118,25 +95,19 @@ class Context {
had_events = true;
}
return had_events;
});
}
val configToJS() {
return gpp_rethrow([=]() {
GPPWidget config(
GPP_CALL(CameraWidget *,
gp_camera_get_config(camera.get(), _, context.get())));
GPPWidget config(GPP_CALL(
CameraWidget *, gp_camera_get_config(camera.get(), _, context.get())));
return walk_config(config.get()).second;
});
}
void setConfigValue(std::string name, val value) {
gpp_rethrow([=]() {
GPPWidget widget(GPP_CALL(
CameraWidget *, gp_camera_get_single_config(
camera.get(), name.c_str(), _, context.get())));
auto type =
GPP_CALL(CameraWidgetType, gp_widget_get_type(widget.get(), _));
CameraWidget *, gp_camera_get_single_config(camera.get(), name.c_str(),
_, context.get())));
auto type = GPP_CALL(CameraWidgetType, gp_widget_get_type(widget.get(), _));
switch (type) {
case GP_WIDGET_RANGE: {
float number = value.as<float>();
@@ -166,22 +137,18 @@ class Context {
}
gpp_try(gp_camera_set_single_config(camera.get(), name.c_str(),
widget.get(), context.get()));
});
}
val capturePreviewAsBlob() {
return gpp_rethrow([=]() {
auto &file = get_file();
gpp_try(gp_camera_capture_preview(camera.get(), &file, context.get()));
auto params = blob_chunks_and_opts(file);
return Blob.new_(std::move(params.first), std::move(params.second));
});
}
val captureImageAsFile() {
return gpp_rethrow([=]() {
auto &file = get_file();
{
@@ -218,7 +185,6 @@ class Context {
auto params = blob_chunks_and_opts(file);
return File.new_(std::move(params.first), std::move(name),
std::move(params.second));
});
}
private: