emscripten-core / emscripten-core/emscripten

Use functor as callback

Open
#18,600 0 comments 0 reactions 0 assignees View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Please include the following in your bug report:

**Version of emscripten/emsdk:**

```
emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.30 (cfe2bdfe2692457cb5f5770672f6e5ccb3ffc2f2)
clang version 16.0.0 (https://github.com/llvm/llvm-project 800f0f1546b2352ba42a4777149afb13cb874fcd)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/paulo/Developer/workspaces/cpp/emsdk/upstream/bin
```

**Problem:**

Im trying use the functor as callback seeing the functor example, but don't know how.

**Bind Code:**

```
// BIND: void functor string

EMSCRIPTEN_BINDINGS(xplpc_void_functor_string)
{
em::class_>("XVoidFunctorString")
.constructor<>()
.function("opcall", &std::function::operator());
}
```

**Javascript Code:**

```
// js lambda
const f = (response: string) => {
XCallbackList.shared().execute(key, response);
};

// functor type
const FunctorType = XPLPC.shared().module.XVoidFunctorString;

// functor instance
const callback = new FunctorType();

// how i connect "functor" and "f" to use on function below as callback?
XPLPC.shared().module.ProxyClient.call(request.data(), callback);
```

**Real Binded C++ Function That Receive Callback/Functor:**

```
ProxyClient
{

static void call(const std::string &data, std::function callback)
{
callback("test");
}

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.