Skip to content

Commit

Permalink
Migrate to new JS function proxying API
Browse files Browse the repository at this point in the history
The previous one was marked as legacy, see commit:
emscripten-core/emscripten@f14f029
  • Loading branch information
kleisauke committed Jun 30, 2024
1 parent 31590ff commit 895d11c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
48 changes: 32 additions & 16 deletions src/bindings/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "connection.h"
#include "error.h"

#include <emscripten/threading.h>

namespace vips {

Source Source::new_from_file(const std::string &filename) {
Expand Down Expand Up @@ -35,10 +33,11 @@ int64_t SourceCustom::read_handler(VipsSourceCustom *source, void *buffer,
if (self->read_callback == nullptr)
return -1;

// Ensure that we call the JS function on the main thread, see:
// https://github.com/emscripten-core/emscripten/issues/11317
int64_t bytes_read = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_JPJ, self->read_callback, buffer, length);
int64_t bytes_read;
proxy_sync([&]() {
bytes_read = self->read_callback(buffer, length);
});

return bytes_read;
}

Expand All @@ -48,8 +47,11 @@ int64_t SourceCustom::seek_handler(VipsSourceCustom *source, int64_t offset,
if (self->seek_callback == nullptr)
return -1;

int64_t new_pos = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_JJI, self->seek_callback, offset, whence);
int64_t new_pos;
proxy_sync([&]() {
new_pos = self->seek_callback(offset, whence);
});

return new_pos;
}

Expand Down Expand Up @@ -91,8 +93,11 @@ int64_t TargetCustom::write_handler(VipsTargetCustom *target,
if (self->write_callback == nullptr)
return -1;

int64_t bytes_written = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_JPJ, self->write_callback, buffer, length);
int64_t bytes_written;
proxy_sync([&]() {
bytes_written = self->write_callback(buffer, length);
});

return bytes_written;
}

Expand All @@ -105,8 +110,11 @@ int64_t TargetCustom::read_handler(VipsTargetCustom *target, void *buffer,
if (self->read_callback == nullptr)
return -1;

int64_t bytes_read = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_JPJ, self->read_callback, buffer, length);
int64_t bytes_read;
proxy_sync([&]() {
bytes_read = self->read_callback(buffer, length);
});

return bytes_read;
}

Expand All @@ -116,8 +124,11 @@ int64_t TargetCustom::seek_handler(VipsTargetCustom *target, int64_t offset,
if (self->seek_callback == nullptr)
return -1;

int64_t new_pos = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_JJI, self->seek_callback, offset, whence);
int64_t new_pos;
proxy_sync([&]() {
new_pos = self->seek_callback(offset, whence);
});

return new_pos;
}

Expand All @@ -126,9 +137,14 @@ int TargetCustom::end_handler(VipsTargetCustom *target, void *user) {
if (self->end_callback == nullptr)
return 0;

return emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_I,
self->end_callback);
int result;
proxy_sync([&]() {
result = self->end_callback();
});

return result;
}

void TargetCustom::set_write_callback(emscripten::val js_func) {
emscripten::val ptr = emscripten::val::module_property("addFunction")(
js_func, emscripten::val("jpj"));
Expand Down
7 changes: 3 additions & 4 deletions src/bindings/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ void Image::eval_handler(VipsImage *image, VipsProgress *progress, void *user) {
if (self->progress_callback == nullptr)
return;

// Ensure that we call the JS function on the main thread, see:
// https://github.com/emscripten-core/emscripten/issues/11317
emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_VI, self->progress_callback, progress->percent);
proxy_sync([&]() {
self->progress_callback(progress->percent);
});
}

void Image::set_progress_callback(emscripten::val js_func) {
Expand Down
1 change: 0 additions & 1 deletion src/bindings/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <vector>

#include <emscripten/val.h>
#include <emscripten/threading.h>

namespace vips {

Expand Down
14 changes: 14 additions & 0 deletions src/bindings/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "option.h"

#include <emscripten/proxying.h>
#include <emscripten/threading.h>

namespace vips {

std::vector<int> blend_modes_to_int(emscripten::val v) {
Expand Down Expand Up @@ -39,4 +42,15 @@ std::vector<double> invert(const std::vector<double> &vector) {
return new_vector;
}

static void run(void *arg) {
std::function<void()> *f = reinterpret_cast<std::function<void()> *>(arg);
(*f)();
}

bool proxy_sync(const std::function<void()> &func) {
em_proxying_queue *q = emscripten_proxy_get_system_queue();
return emscripten_proxy_sync(q, emscripten_main_runtime_thread_id(), run,
(void *)&func);
}

} // namespace vips
7 changes: 7 additions & 0 deletions src/bindings/utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <functional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -71,4 +72,10 @@ std::vector<double> negate(const std::vector<double> &vector);

std::vector<double> invert(const std::vector<double> &vector);

/**
* Ensure that we call JS functions on the main runtime thread, see:
* https://github.com/emscripten-core/emscripten/issues/11317
*/
bool proxy_sync(const std::function<void()> &func);

} // namespace vips

0 comments on commit 895d11c

Please sign in to comment.