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

Invalid Rust closure invocation from JavaScript: Improve error message #3401

Open
ActuallyHappening opened this issue Apr 24, 2023 · 0 comments
Labels

Comments

@ActuallyHappening
Copy link

Describe the Bug

Pass a Rust closure into the JavaScript world that takes a String (haven't tested &str) and invoke the Rust closure with a JavaScript object (e.g. {}).
You will get a very annoying error that doesn't describe what you did at all: [Error] RuntimeError: Unreachable code should not be executed (evaluating 'realloc(ptr, len, len = offset + arg.length * 3)')
This is not a bug, however the error message can be improved

Steps to Reproduce

From Rust side, put a closure into the 'global' or window scope:

#[wasm_bindgen(inline_js="export func register(f) {window._func=f}")]
extern "C" {
	fn register(f: &Closure<dyn FnMut(String)>);
}

Then register it:

register(&Closure::new(move |_| {
    //info!("RS: Closure called");
}));

From the JS side, load a script into the HTML and execute it, calling window._func with a JS Object (not the expected String):

//console.log("JS: Calling _func");
window._func({});

Expected Behavior

An error should occur, since the Rust closure was invoked with the wrong arguments.
I would like a clear and concise error message of what wasm-bindgen expected to happen compared to what did happen.
Maybe an error message like Did you call a Rust closure with invalid arguments? instead of RuntimeError: Unreachable code should not be executed (evaluating 'realloc(ptr, len, len = offset + arg.length * 3)'

Actual Behaviour

This error message was logged using the console_error_panic_hook crate running on Safari (using leptos to insert JS script, and calling from a JS firebase callback fn), although my error seems generic (but I haven't tested it on other platforms).

[Error] panicked at 'assertion failed: old_size > 0', /Users/____/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-0.2.84/src/lib.rs:1582:13

Stack:

@http://localhost:3000/pkg/leptos_start.js:319:22
wasm-stub@[wasm code]
<?>.wasm-function[console_error_panic_hook::Error::new::h6003d8e0d95f40a6]@[wasm code]
<?>.wasm-function[console_error_panic_hook::hook_impl::hed4b238597fbcbc2]@[wasm code]
<?>.wasm-function[console_error_panic_hook::hook::h234aaf9173a64a91]@[wasm code]
<?>.wasm-function[core::ops::function::Fn::call::hdd0a65b357887f78]@[wasm code]
<?>.wasm-function[std::panicking::rust_panic_with_hook::h053fef1466f0a872]@[wasm code]
<?>.wasm-function[std::panicking::begin_panic_handler::{{closure}}::hc1e7cbf0244868f5]@[wasm code]
<?>.wasm-function[std::sys_common::backtrace::__rust_end_short_backtrace::h5e48744231590951]@[wasm code]
<?>.wasm-function[rust_begin_unwind]@[wasm code]
<?>.wasm-function[core::panicking::panic_fmt::h527018da77170278]@[wasm code]
<?>.wasm-function[core::panicking::panic::h0baae8c244a97243]@[wasm code]
<?>.wasm-function[__wbindgen_realloc]@[wasm code]
wasm-stub@[wasm code]
1225@[native code]
passStringToWasm0@http://localhost:3000/pkg/leptos_start.js:128:34
__wbg_adapter_38@http://localhost:3000/pkg/leptos_start.js:229:31
real@http://localhost:3000/pkg/leptos_start.js:214:33
emit_event@http://localhost:3000/bundle.js:15238:16
@http://localhost:3000/bundle.js:15291:23
@http://localhost:3000/bundle.js:15267:17
onValue@http://localhost:3000/bundle.js:14503:39
exceptionGuard@http://localhost:3000/bundle.js:3824:15
eventListRaise@http://localhost:3000/bundle.js:13622:31
eventQueueRaiseQueuedEventsMatchingPredicate@http://localhost:3000/bundle.js:13597:35
eventQueueRaiseEventsForChangedPath@http://localhost:3000/bundle.js:13586:53
repoOnDataUpdate@http://localhost:3000/bundle.js:13804:44
@http://localhost:3000/bundle.js:13703:33
onDataPush_@http://localhost:3000/bundle.js:6756:35
onDataMessage_@http://localhost:3000/bundle.js:6750:33
onDataMessage_@[native code]
onDataMessage_@http://localhost:3000/bundle.js:5596:28
onPrimaryMessageReceived_@http://localhost:3000/bundle.js:5590:36
@http://localhost:3000/bundle.js:5492:55
appendFrame_@http://localhost:3000/bundle.js:5109:31
handleIncomingFrame@http://localhost:3000/bundle.js:5157:38
@http://localhost:3000/bundle.js:5056:41


	(anonymous function) (leptos_start.js:316)
	wasm-stub
	<?>.wasm-function[console_error_panic_hook::error::h729d7e127797de6e]
	<?>.wasm-function[console_error_panic_hook::hook_impl::hed4b238597fbcbc2]
	<?>.wasm-function[console_error_panic_hook::hook::h234aaf9173a64a91]
	<?>.wasm-function[core::ops::function::Fn::call::hdd0a65b357887f78]
	<?>.wasm-function[std::panicking::rust_panic_with_hook::h053fef1466f0a872]
	<?>.wasm-function[std::panicking::begin_panic_handler::{{closure}}::hc1e7cbf0244868f5]
	<?>.wasm-function[std::sys_common::backtrace::__rust_end_short_backtrace::h5e48744231590951]
	<?>.wasm-function[rust_begin_unwind]
	<?>.wasm-function[core::panicking::panic_fmt::h527018da77170278]
	<?>.wasm-function[core::panicking::panic::h0baae8c244a97243]
	<?>.wasm-function[__wbindgen_realloc]
	wasm-stub
	1225
	passStringToWasm0 (leptos_start.js:128)
	__wbg_adapter_38 (leptos_start.js:229)
	real (leptos_start.js:214)
	emit_event (bundle.js:15238)
	(anonymous function) (bundle.js:15291)
	(anonymous function) (bundle.js:15267)
	onValue (bundle.js:14503)
	exceptionGuard (bundle.js:3824)
	eventListRaise (bundle.js:13622)
	eventQueueRaiseQueuedEventsMatchingPredicate (bundle.js:13597)
	eventQueueRaiseEventsForChangedPath (bundle.js:13586)
	repoOnDataUpdate (bundle.js:13804)
	(anonymous function) (bundle.js:13703)
	onDataPush_ (bundle.js:6756)
	onDataMessage_ (bundle.js:6750)
	onDataMessage_
	onDataMessage_ (bundle.js:5596)
	onPrimaryMessageReceived_ (bundle.js:5590)
	(anonymous function) (bundle.js:5492)
	appendFrame_ (bundle.js:5109)
	handleIncomingFrame (bundle.js:5157)
	(anonymous function) (bundle.js:5056)

This error also happened (full stack omitted):

[Error] RuntimeError: Unreachable code should not be executed (evaluating 'realloc(ptr, len, len = offset + arg.length * 3)')

	(anonymous function) (bundle.js:3835)

Additional Context

Using Safari, happy to add any more details upon request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant