emscripten-core / emscripten-core/emscripten

Is it possible to pass a C++ lambda as argument to a js function ?

Open
#22,448 1 comment 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

Is the code below or equivalent possible ?
What is the best way to achieve this functionality ?

```cpp
class_>("_callback")
.constructor<>()
.function("opcall", &std::function::operator())
;

emscripten::val window = emscripten::val::global("window");

std::function wrapped = []() {
std::cout << "Hello " << "\n";
};

emscripten::val intervalId = window.call("setInterval", wrapped, 1000);
```

The code below does not work on the js side. It is called periodically but produce an error on each call

```
Uncaught SyntaxError: missing ] after element list14 [lython.js line 3599 > Function:6:9](http://192.168.2.20:8000/build/Release/bin/lython.js%20line%203599%20%3E%20Function)note: [ opened at line 6, column 1
```

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.