emscripten-core / emscripten-core/emscripten
SDL_RWseek does not work for files in emscripten
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
SDL_RWseek does not work for files in emscripten. Here is some sample code to demonstrate it.
Please include the following in your bug report:
**Version of emscripten/emsdk:**
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 71513a71cdf380efd6a44be6939e2cb979a62407)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin
**Full link command and output with `-v` appended:**
```
emcc -v main.cpp -o main.html -s USE_SDL=2 --preload-file nob.txt && emrun main.html
```
```
"/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin/clang" --version
"/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -I/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/SDL2 --sysroot=/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot -Xclang -iwithsysroot/include/compat -v main.cpp -c -o /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/emscripten_temp_6wxi0cpu/main_0.o
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 71513a71cdf380efd6a44be6939e2cb979a62407)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin
(in-process)
"/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin/clang-17" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -target-linker-version 857.1 -v -fcoverage-compilation-dir=/Users/bilalshafi/Desktop/emcc_error -resource-dir /usr/local/Cellar/emscripten/3.1.43/libexec/llvm/lib/clang/17 -D EMSCRIPTEN -I /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/SDL2 -isysroot /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot -internal-isystem /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/3.1.43/libexec/llvm/lib/clang/17/include -internal-isystem /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/wasm32-emscripten -internal-isystem /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include -fdeprecated-macro -fdebug-compilation-dir=/Users/bilalshafi/Desktop/emcc_error -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/emscripten_temp_6wxi0cpu/main_0.o -x c++ main.cpp
clang -cc1 version 17.0.0 based upon LLVM 17.0.0git default target x86_64-apple-darwin22.1.0
ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/SDL2
/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/compat
/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/c++/v1
/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/lib/clang/17/include
/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include
End of search list.
"/usr/local/Cellar/emscripten/3.1.43/libexec/tools/file_packager" main.data --from-emcc --preload nob.txt
"/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin/wasm-ld" -o main.wasm /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/emscripten_temp_6wxi0cpu/main_0.o -L/usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/lib/wasm32-emscripten /usr/local/Cellar/emscripten/3.1.43/libexec/cache/sysroot/lib/wasm32-emscripten/libSDL2.a -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/tmp0wx3q22plibemscripten_js_symbols.so --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export=__wasm_call_ctors --export-table -z stack-size=65536 --initial-memory=16777216 --no-entry --max-memory=16777216 --stack-first
"/usr/local/Cellar/emscripten/3.1.43/libexec/binaryen/bin/wasm-emscripten-finalize" --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers main.wasm -o main.wasm --detect-features
"/usr/local/opt/node/bin/node" /usr/local/Cellar/emscripten/3.1.43/libexec/src/compiler.js /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/tmpspk4h72p.json
"/usr/local/Cellar/emscripten/3.1.43/libexec/llvm/bin/llvm-objcopy" main.wasm main.wasm --remove-section=.debug* --remove-section=producers
"/usr/local/opt/node/bin/node" /usr/local/Cellar/emscripten/3.1.43/libexec/tools/preprocessor.js /var/folders/q3/8rybzktn6wdftp6fym7kdls00000gn/T/emscripten_temp_6wxi0cpu/settings.js shell.html
/usr/local/Cellar/emscripten/3.1.43/libexec/emrun.py:19: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
import cgi
Now listening at http://0.0.0.0:6931/
```
Sample Code. The sample code reads some data, seeks back, and then reads the same data again. It should print out two 'Hello's but it doesn't. I have checked. Using g++ it does produce two 'Hello's.
nob.txt
```
Hello
```
main.cpp
```
#include
#ifdef __EMSCRIPTEN__
#include
#endif
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_RWops *rw = SDL_RWFromFile("nob.txt", "rb");
char first_word[5] = {};
auto first_bytes_read = SDL_RWread(rw, first_word, 1, 5);
auto seek_error = SDL_RWseek(rw, -sizeof(first_word), RW_SEEK_CUR);
if (seek_error == -1) {
printf("Seek not supported\n");
}
char second_word[5] = {};
auto second_bytes_read = SDL_RWread(rw, second_word, 1, 5);
// Ends up reading 0 bytes
printf("%.*s:%zu\n", (int)first_bytes_read, first_word, first_bytes_read);
printf("%.*s:%zu\n", (int)second_bytes_read, second_word, second_bytes_read);
SDL_Quit();
}
```
Output
```
Hello:5
:0
```
It should have two hellos. If seeking is not supported by emscripten, SDL_RWseek should return -1, however I checked and it doesn't.
Contributor guide
Assessment
This issue has not been assessed yet.