Remove custom gpp_rethrow wrapper
Upstream Emscripten now natively propagates exception messages to JS.
This commit is contained in:
228
src/api.cpp
228
src/api.cpp
@@ -50,24 +50,6 @@ void gpp_log_error(GPContext *context, const char *text, void *data) {
|
|||||||
OUT; \
|
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 Uint8Array = val::global("Uint8Array");
|
||||||
const thread_local val Blob = val::global("Blob");
|
const thread_local val Blob = val::global("Blob");
|
||||||
const thread_local val File = val::global("File");
|
const thread_local val File = val::global("File");
|
||||||
@@ -75,150 +57,134 @@ const thread_local val arrayOf = val::global("Array")["of"];
|
|||||||
|
|
||||||
class Context {
|
class Context {
|
||||||
public:
|
public:
|
||||||
Context() : camera(nullptr), context(nullptr) {
|
Context()
|
||||||
gpp_rethrow([=]() {
|
: camera(GPP_CALL(Camera *, gp_camera_new(_))),
|
||||||
camera.reset(GPP_CALL(Camera *, gp_camera_new(_)));
|
context(gp_context_new()) {
|
||||||
context.reset(gp_context_new());
|
gp_context_set_error_func(context.get(), gpp_log_error, nullptr);
|
||||||
gp_context_set_error_func(context.get(), gpp_log_error, nullptr);
|
gpp_try(gp_camera_init(camera.get(), context.get()));
|
||||||
gpp_try(gp_camera_init(camera.get(), context.get()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val supportedOps() {
|
val supportedOps() {
|
||||||
return gpp_rethrow([=]() {
|
auto ops =
|
||||||
auto ops =
|
GPP_CALL(CameraAbilities, gp_camera_get_abilities(camera.get(), _))
|
||||||
GPP_CALL(CameraAbilities, gp_camera_get_abilities(camera.get(), _))
|
.operations;
|
||||||
.operations;
|
|
||||||
|
|
||||||
val result = val::object();
|
val result = val::object();
|
||||||
result.set("captureImage", (ops & GP_OPERATION_CAPTURE_IMAGE) != 0);
|
result.set("captureImage", (ops & GP_OPERATION_CAPTURE_IMAGE) != 0);
|
||||||
result.set("captureVideo", (ops & GP_OPERATION_CAPTURE_VIDEO) != 0);
|
result.set("captureVideo", (ops & GP_OPERATION_CAPTURE_VIDEO) != 0);
|
||||||
result.set("captureAudio", (ops & GP_OPERATION_CAPTURE_AUDIO) != 0);
|
result.set("captureAudio", (ops & GP_OPERATION_CAPTURE_AUDIO) != 0);
|
||||||
result.set("capturePreview", (ops & GP_OPERATION_CAPTURE_PREVIEW) != 0);
|
result.set("capturePreview", (ops & GP_OPERATION_CAPTURE_PREVIEW) != 0);
|
||||||
result.set("config", (ops & GP_OPERATION_CONFIG) != 0);
|
result.set("config", (ops & GP_OPERATION_CONFIG) != 0);
|
||||||
result.set("triggerCapture", (ops & GP_OPERATION_TRIGGER_CAPTURE) != 0);
|
result.set("triggerCapture", (ops & GP_OPERATION_TRIGGER_CAPTURE) != 0);
|
||||||
return result;
|
return result;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool consumeEvents() {
|
bool consumeEvents() {
|
||||||
return gpp_rethrow([=]() {
|
bool had_events = false;
|
||||||
bool had_events = false;
|
for (;;) {
|
||||||
for (;;) {
|
CameraEventType event_type = GP_EVENT_UNKNOWN;
|
||||||
CameraEventType event_type = GP_EVENT_UNKNOWN;
|
gpp_unique_ptr<void, free> event_data(GPP_CALL(
|
||||||
gpp_unique_ptr<void, free> event_data(GPP_CALL(
|
void *, gp_camera_wait_for_event(camera.get(), 0, &event_type, _,
|
||||||
void *, gp_camera_wait_for_event(camera.get(), 0, &event_type, _,
|
context.get())));
|
||||||
context.get())));
|
if (event_type == GP_EVENT_TIMEOUT) {
|
||||||
if (event_type == GP_EVENT_TIMEOUT) {
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (event_type == GP_EVENT_UNKNOWN) {
|
|
||||||
// EM_ASM({ console.log(UTF8ToString($0)); }, event_data.get());
|
|
||||||
}
|
|
||||||
had_events = true;
|
|
||||||
}
|
}
|
||||||
return had_events;
|
if (event_type == GP_EVENT_UNKNOWN) {
|
||||||
});
|
// EM_ASM({ console.log(UTF8ToString($0)); }, event_data.get());
|
||||||
|
}
|
||||||
|
had_events = true;
|
||||||
|
}
|
||||||
|
return had_events;
|
||||||
}
|
}
|
||||||
|
|
||||||
val configToJS() {
|
val configToJS() {
|
||||||
return gpp_rethrow([=]() {
|
GPPWidget config(GPP_CALL(
|
||||||
GPPWidget config(
|
CameraWidget *, gp_camera_get_config(camera.get(), _, context.get())));
|
||||||
GPP_CALL(CameraWidget *,
|
return walk_config(config.get()).second;
|
||||||
gp_camera_get_config(camera.get(), _, context.get())));
|
|
||||||
return walk_config(config.get()).second;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setConfigValue(std::string name, val value) {
|
void setConfigValue(std::string name, val value) {
|
||||||
gpp_rethrow([=]() {
|
GPPWidget widget(GPP_CALL(
|
||||||
GPPWidget widget(GPP_CALL(
|
CameraWidget *, gp_camera_get_single_config(camera.get(), name.c_str(),
|
||||||
CameraWidget *, gp_camera_get_single_config(
|
_, context.get())));
|
||||||
camera.get(), name.c_str(), _, context.get())));
|
auto type = GPP_CALL(CameraWidgetType, gp_widget_get_type(widget.get(), _));
|
||||||
auto type =
|
switch (type) {
|
||||||
GPP_CALL(CameraWidgetType, gp_widget_get_type(widget.get(), _));
|
case GP_WIDGET_RANGE: {
|
||||||
switch (type) {
|
float number = value.as<float>();
|
||||||
case GP_WIDGET_RANGE: {
|
gpp_try(gp_widget_set_value(widget.get(), &number));
|
||||||
float number = value.as<float>();
|
break;
|
||||||
gpp_try(gp_widget_set_value(widget.get(), &number));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case GP_WIDGET_MENU:
|
|
||||||
case GP_WIDGET_RADIO:
|
|
||||||
case GP_WIDGET_TEXT: {
|
|
||||||
auto str = value.as<std::string>();
|
|
||||||
gpp_try(gp_widget_set_value(widget.get(), str.c_str()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case GP_WIDGET_TOGGLE: {
|
|
||||||
int on = value.as<bool>() ? 1 : 0;
|
|
||||||
gpp_try(gp_widget_set_value(widget.get(), &on));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case GP_WIDGET_DATE: {
|
|
||||||
auto seconds = static_cast<int>(value.as<double>() / 1000);
|
|
||||||
gpp_try(gp_widget_set_value(widget.get(), &seconds));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
throw std::logic_error("unimplemented");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
gpp_try(gp_camera_set_single_config(camera.get(), name.c_str(),
|
case GP_WIDGET_MENU:
|
||||||
widget.get(), context.get()));
|
case GP_WIDGET_RADIO:
|
||||||
});
|
case GP_WIDGET_TEXT: {
|
||||||
|
auto str = value.as<std::string>();
|
||||||
|
gpp_try(gp_widget_set_value(widget.get(), str.c_str()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GP_WIDGET_TOGGLE: {
|
||||||
|
int on = value.as<bool>() ? 1 : 0;
|
||||||
|
gpp_try(gp_widget_set_value(widget.get(), &on));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GP_WIDGET_DATE: {
|
||||||
|
auto seconds = static_cast<int>(value.as<double>() / 1000);
|
||||||
|
gpp_try(gp_widget_set_value(widget.get(), &seconds));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
throw std::logic_error("unimplemented");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gpp_try(gp_camera_set_single_config(camera.get(), name.c_str(),
|
||||||
|
widget.get(), context.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
val capturePreviewAsBlob() {
|
val capturePreviewAsBlob() {
|
||||||
return gpp_rethrow([=]() {
|
auto &file = get_file();
|
||||||
auto &file = get_file();
|
|
||||||
|
|
||||||
gpp_try(gp_camera_capture_preview(camera.get(), &file, context.get()));
|
gpp_try(gp_camera_capture_preview(camera.get(), &file, context.get()));
|
||||||
|
|
||||||
auto params = blob_chunks_and_opts(file);
|
auto params = blob_chunks_and_opts(file);
|
||||||
return Blob.new_(std::move(params.first), std::move(params.second));
|
return Blob.new_(std::move(params.first), std::move(params.second));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val captureImageAsFile() {
|
val captureImageAsFile() {
|
||||||
return gpp_rethrow([=]() {
|
auto &file = get_file();
|
||||||
auto &file = get_file();
|
|
||||||
|
|
||||||
{
|
{
|
||||||
struct TempCameraFile {
|
struct TempCameraFile {
|
||||||
Camera &camera;
|
Camera &camera;
|
||||||
GPContext &context;
|
GPContext &context;
|
||||||
CameraFilePath path;
|
CameraFilePath path;
|
||||||
|
|
||||||
~TempCameraFile() {
|
~TempCameraFile() {
|
||||||
gp_camera_file_delete(&camera, path.folder, path.name, &context);
|
gp_camera_file_delete(&camera, path.folder, path.name, &context);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TempCameraFile camera_file{
|
TempCameraFile camera_file{
|
||||||
.camera = *camera,
|
.camera = *camera,
|
||||||
.context = *context,
|
.context = *context,
|
||||||
};
|
};
|
||||||
|
|
||||||
strcpy(camera_file.path.folder, "/");
|
strcpy(camera_file.path.folder, "/");
|
||||||
strcpy(camera_file.path.name, "web-gphoto2");
|
strcpy(camera_file.path.name, "web-gphoto2");
|
||||||
|
|
||||||
gpp_try(gp_camera_capture(camera.get(), GP_CAPTURE_IMAGE,
|
gpp_try(gp_camera_capture(camera.get(), GP_CAPTURE_IMAGE,
|
||||||
&camera_file.path, context.get()));
|
&camera_file.path, context.get()));
|
||||||
|
|
||||||
gpp_try(gp_camera_file_get(camera.get(), camera_file.path.folder,
|
gpp_try(gp_camera_file_get(camera.get(), camera_file.path.folder,
|
||||||
camera_file.path.name, GP_FILE_TYPE_NORMAL,
|
camera_file.path.name, GP_FILE_TYPE_NORMAL,
|
||||||
&file, context.get()));
|
&file, context.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
gpp_try(gp_file_set_name(&file, "image."));
|
gpp_try(gp_file_set_name(&file, "image."));
|
||||||
gpp_try(gp_file_adjust_name_for_mime_type(&file));
|
gpp_try(gp_file_adjust_name_for_mime_type(&file));
|
||||||
auto name = val(GPP_CALL(const char *, gp_file_get_name(&file, _)));
|
auto name = val(GPP_CALL(const char *, gp_file_get_name(&file, _)));
|
||||||
|
|
||||||
auto params = blob_chunks_and_opts(file);
|
auto params = blob_chunks_and_opts(file);
|
||||||
return File.new_(std::move(params.first), std::move(name),
|
return File.new_(std::move(params.first), std::move(name),
|
||||||
std::move(params.second));
|
std::move(params.second));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user