emscripten-core / emscripten-core/emscripten

Passing an exception into ccall as string sends empty string

Open
#17,766 5 comments 0 reactions 0 assignees View on GitHub
good first bug
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Version of emscripten/emsdk: 3.1.20-git (from homebrew)

This is user-error, but feels like it should have been caught higher up (maybe in the string/stack alloc?), and should be a simple UX fix

I passed an exception into ccall;
```
const ReturnType = null;
const ArgTypes = ['number','string'];
const ArgValues = [Handle,Error];
try
{
Module.ccall('OnStreamingDownloadFinished',ReturnType,ArgTypes,ArgValues);
}
catch(e)
{...}
```
Didn't twig it wasn't a string for a while as chrome's debugger shows it as a string, the type convertor doesn't error, (and debugger even makes it look like a string), and it manages to allocate & copy a string (of length 1) because
`var len = (str.length << 2) + 1; -->`
`var len = (undefined << 2) + 1; == 1`

Feels to me like either
a) convertor should throw if object has no length property (or just isn't a string!) thereby forcing users to pass strings if they said they would
b) force the input value to be a string `str = `${str}` (so type takes precedent)

![Screenshot 2022-08-31 at 15 26 14](https://user-images.githubusercontent.com/2184197/187703204-ac0ae324-8f33-4054-8d19-3c173ed8ddcb.png)
(Screenshot to highlight UX)

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.