Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusting the function passed to func_new/func_wrap to be universal reference so that we can perfectly forward it on. #62

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

Daniel9z
Copy link
Contributor

@Daniel9z Daniel9z commented Dec 12, 2024

Adjusting the function passed to func_new/func_wrap to be universal reference so that we can perfectly forward it on.

The issue, as shown by the test cases, is that previously the function was always copied when passed to func_new/wrap. This works fine for pointers, but when using a callable (like a lambda) it's unnecessarily copied. Furthermore, for callables which aren't allowed to be copied compilation actually fails.

The fix is to move the callable when the callable is passed in as an xvalue.

Tests failing before these changes: https://github.com/bytecodealliance/wasmtime-cpp/actions/runs/12304195157?pr=62
Error:

/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unique_ptr.h:1065:34: error: call to deleted constructor of 'CallableFunc'
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                                 ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/wasmtime-cpp/wasmtime-cpp/include/wasmtime.hh:3094:51: note: in instantiation of function template specialization 'std::make_unique<CallableFunc, CallableFunc &>' requested here
        ty.ptr.get(), Func::raw_callback<F>, std::make_unique<F>(f).release(),

@Daniel9z Daniel9z force-pushed the main branch 9 times, most recently from 589ff04 to 4b3e424 Compare December 12, 2024 20:35
…eference so that we can perfectly forward it on.

The issue, as shown by the test cases, is that previously the function was always copied when passed to func_new/wrap. This works fine for pointers, but when using a callable it's unnecessarily copied. Furthermore, for callables which aren't allowed to be copied (like lambda's with refernces, compilation actually fails.

The fix is to move the callable when the callable is passed in as an xvalue.
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't pretend to really know what's going on here but thanks for the PR and thanks for the test! That's sufficient for me :)

@alexcrichton alexcrichton merged commit 780eeba into bytecodealliance:main Dec 12, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants