Skip to content

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

Open
@Delaunay

Description

@Delaunay

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

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

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

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

    emscripten::val intervalId = window.call<emscripten::val>("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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions