diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-05 08:37:34 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-05 08:37:34 +0200 |
| commit | 52c74a1c33ce12b93ec5830f5a6348d9578c7c6e (patch) | |
| tree | e763fa21a5bc057b36a42af8caa9766bb1cfd8b3 /web/adapter.cpp | |
| parent | 57f194ef57e393d84e2d3c73115ec528e02ea3c9 (diff) | |
| download | nissy-core-52c74a1c33ce12b93ec5830f5a6348d9578c7c6e.tar.gz nissy-core-52c74a1c33ce12b93ec5830f5a6348d9578c7c6e.zip | |
Simplify callback business (web version)
Diffstat (limited to '')
| -rw-r--r-- | web/adapter.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/web/adapter.cpp b/web/adapter.cpp index 0022b0d..3df3a54 100644 --- a/web/adapter.cpp +++ b/web/adapter.cpp | |||
| @@ -1,15 +1,10 @@ | |||
| 1 | extern "C" { | ||
| 2 | extern int addCallbackFunction(/* args intentionally unspecified */); | ||
| 3 | extern void callFunction(int, const char *); | ||
| 4 | extern int callFunctionInt(int); | ||
| 5 | } | ||
| 6 | |||
| 7 | #include "../cpp/nissy.h" | 1 | #include "../cpp/nissy.h" |
| 8 | #include "storage.h" | 2 | #include "storage.h" |
| 9 | #include "logging.h" | 3 | #include "logging.h" |
| 10 | 4 | ||
| 11 | #include <emscripten.h> | 5 | #include <emscripten.h> |
| 12 | #include <emscripten/bind.h> | 6 | #include <emscripten/bind.h> |
| 7 | #include <functional> | ||
| 13 | #include <map> | 8 | #include <map> |
| 14 | #include <set> | 9 | #include <set> |
| 15 | #include <string> | 10 | #include <string> |
| @@ -17,6 +12,8 @@ extern "C" { | |||
| 17 | 12 | ||
| 18 | EM_ASYNC_JS(void, fake_async, (), {}); | 13 | EM_ASYNC_JS(void, fake_async, (), {}); |
| 19 | 14 | ||
| 15 | std::map<std::string, nissy::solver> loaded_solvers; | ||
| 16 | |||
| 20 | const std::set<std::string> available_solvers | 17 | const std::set<std::string> available_solvers |
| 21 | { | 18 | { |
| 22 | "h48h0k4", | 19 | "h48h0k4", |
| @@ -27,8 +24,6 @@ const std::set<std::string> available_solvers | |||
| 27 | "h48h5k2", | 24 | "h48h5k2", |
| 28 | }; | 25 | }; |
| 29 | 26 | ||
| 30 | std::map<std::string, nissy::solver> loaded_solvers; | ||
| 31 | |||
| 32 | bool is_solver_available(const std::string& name) | 27 | bool is_solver_available(const std::string& name) |
| 33 | { | 28 | { |
| 34 | return available_solvers.contains(name); | 29 | return available_solvers.contains(name); |
| @@ -153,16 +148,20 @@ bool init_solver_generate(const std::string& name) | |||
| 153 | 148 | ||
| 154 | int poll_status(void *arg) | 149 | int poll_status(void *arg) |
| 155 | { | 150 | { |
| 156 | if (arg == NULL || *(int *)arg == -1) | 151 | if (arg == nullptr) |
| 157 | return nissy::status::RUN.value; | 152 | return nissy::status::RUN.value; |
| 158 | 153 | ||
| 159 | return callFunctionInt(*(int *)arg); | 154 | std::function<int(void)> poll((int (*)(void))arg); |
| 155 | return poll(); | ||
| 160 | } | 156 | } |
| 161 | 157 | ||
| 158 | // The parameter js_poll_status is of type int here, but actually it is a | ||
| 159 | // pointer to a JS function. The type will have to be changed to a 64-bit | ||
| 160 | // integer when we move to WASM64. | ||
| 162 | nissy::solver::solve_result solve(std::string name, | 161 | nissy::solver::solve_result solve(std::string name, |
| 163 | nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, | 162 | nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, |
| 164 | unsigned maxmoves, unsigned maxsols, unsigned optimal, unsigned threads, | 163 | unsigned maxmoves, unsigned maxsols, unsigned optimal, unsigned threads, |
| 165 | int poll_status_id) | 164 | int js_poll_status) |
| 166 | { | 165 | { |
| 167 | // Here we use a dirty trick to make this function always return the | 166 | // Here we use a dirty trick to make this function always return the |
| 168 | // same kind of JavaScript object. If we did not do this, the returned | 167 | // same kind of JavaScript object. If we did not do this, the returned |
| @@ -178,8 +177,11 @@ nissy::solver::solve_result solve(std::string name, | |||
| 178 | {.err = nissy::error::INVALID_SOLVER}; | 177 | {.err = nissy::error::INVALID_SOLVER}; |
| 179 | } | 178 | } |
| 180 | 179 | ||
| 180 | // TODO: when running multiple solvers at the same time, we could use | ||
| 181 | // poll_status_id as intended (i.e. an id of some sort) | ||
| 181 | return loaded_solvers.at(name).solve(cube, nissflag, minmoves, | 182 | return loaded_solvers.at(name).solve(cube, nissflag, minmoves, |
| 182 | maxmoves, maxsols, optimal, threads, poll_status, &poll_status_id); | 183 | maxmoves, maxsols, optimal, threads, |
| 184 | poll_status, &js_poll_status); | ||
| 183 | } | 185 | } |
| 184 | 186 | ||
| 185 | EMSCRIPTEN_BINDINGS(Nissy) | 187 | EMSCRIPTEN_BINDINGS(Nissy) |
| @@ -244,5 +246,4 @@ EMSCRIPTEN_BINDINGS(Nissy) | |||
| 244 | emscripten::return_value_policy::take_ownership()); | 246 | emscripten::return_value_policy::take_ownership()); |
| 245 | emscripten::function("setLogger", &set_logger, | 247 | emscripten::function("setLogger", &set_logger, |
| 246 | emscripten::allow_raw_pointers()); | 248 | emscripten::allow_raw_pointers()); |
| 247 | emscripten::function("addCallbackFunction", &addCallbackFunction); | ||
| 248 | } | 249 | } |
