gilzoide / unity-sqlite-net

SQLite-net for Unity, supports Windows, Linux, macOS, iOS, tvOS, visionOS, Android and WebGL
MIT License
51 stars 5 forks source link

Cannot find SQLite native functions on WebGL (Unity 2020.3.42f1) #25

Open jet-zhao opened 1 week ago

jet-zhao commented 1 week ago

I can make a WebGL build successfully. But when it start running, it reports the error showed below:

missing function: sqlite3_libversion_number Invoking error handler due to abort(-1) at Error at jsStackTrace (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:16334) at stackTrace (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:16505) at abort (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:748) at _sqlite3_libversion_number (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:334033) at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[166756]:0x2a6c09f at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[166831]:0x2a70a6e at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[133323]:0x23a450a at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[177613]:0x2e07391 at Object.dynCall_viii (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:661037) at invoke_viii (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:442690) at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[132231]:0x237f32f at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[35531]:0x1040440 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[100167]:0x1e63645 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[130342]:0x2319f99 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[177574]:0x2e06e3b at Object.dynCall_iiiii (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:633509) at invoke_iiiii (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:390921) at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[121914]:0x2238bf5 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[121153]:0x2216440 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[7245]:0x3f4784 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[7243]:0x3f4454 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[11432]:0x5675a3 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[11430]:0x567296 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[11434]:0x5677c6 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[11665]:0x580e94 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[14189]:0x6736b6 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[13862]:0x64e2a9 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[13862]:0x64e2be at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[13857]:0x64de43 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[13850]:0x64c067 at http://localhost:64382/Build/Build_WebGL.wasm.gz:wasm-function[177596]:0x2e071ec at Object.dynCall_v (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:648224) at browserIterationFunc (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:194663) at Object.runIter (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:197724) at Browser_mainLoop_runner (http://localhost:64382/Build/Build_WebGL.framework.js.gz:2:196186) missing function: sqlite3_open_v2 missing function: sqlite3_prepare_v2 missing function: sqlite3_step missing function: sqlite3_errmsg16 missing function: sqlite3_finalize SQLiteException: Exception of type 'SQLite.SQLiteException' was thrown. at SQLite.SQLiteCommand.Select (System.Boolean[] isStr) [0x00000] in <00000000000000000000000000000000>:0

The "libgilzoide-sqlite-net.bc" file under Plugins/lib/webgl/ has been checked default for WebGL platform. I don't know how to fix this issue. Could you help? Thanks a lot.

gilzoide commented 5 days ago

Hey @jet-zhao, thanks for the report! I haven't seen this error while testing, weird stuff.

When I run nm Plugins/lib/webgl/libgilzoide-sqlite-net.bc | grep sqlite3_, I can find all symbols that your error message say are missing, like "sqlite3_libversion_number", "sqlite3_open_v2", "sqlite3_prepare_v2" and so forth.

The "libgilzoide-sqlite-net.bc" file under Plugins/lib/webgl/ has been checked default for WebGL platform

Yes, that's the native plugin for the WebGL platform. I've tested web builds here on Unity 2022.3 and have no problems. Which version of Unity are you using? Although I don't think that the version of Unity is the real issue here... But maybe it is? I know that since 2021.2, static libraries (.a files) are recommended over LLVM bitcode (.bc files).

jet-zhao commented 4 days ago

Hey @jet-zhao, thanks for the report! I haven't seen this error while testing, weird stuff.

When I run nm Plugins/lib/webgl/libgilzoide-sqlite-net.bc | grep sqlite3_, I can find all symbols that your error message say are missing, like "sqlite3_libversion_number", "sqlite3_open_v2", "sqlite3_prepare_v2" and so forth.

The "libgilzoide-sqlite-net.bc" file under Plugins/lib/webgl/ has been checked default for WebGL platform

Yes, that's the native plugin for the WebGL platform. I've tested web builds here on Unity 2022.3 and have no problems. Which version of Unity are you using? Although I don't think that the version of Unity is the real issue here... But maybe it is? I know that since 2021.2, static libraries (.a files) are recommended over LLVM bitcode (.bc files).

Hi, thanks for your reply. I found the reason. By default, the three files lied in Plugins/sqlite-amalgamation/ are not selected for any platforms. Check the right platform and the issue is fixed.

gilzoide commented 4 days ago

the three files lied in Plugins/sqlite-amalgamation/ are not selected for any platforms

Actually, they are selected but only for the Apple platforms iOS/tvOS/visionOS. It works if you mark them for WebGL, but one cannot specify compile flags on Unity directly for them in the import settings, like it is possible on Apple platforms. That's why I prebuilt it in a .bc file instead, to be able to use a custom build of SQLite with extensions such as FTS5 and RTree enabled.

What Unity should be doing is linking your build to the libgilzoide-sqlite-net.bc file, which contains all native code for SQLite.

Say, what version of Unity are you using?

tealcwu commented 2 days ago

Hi gilzoide,

When I build and run WebGL build, it reports disk IO exception as below: "SQLiteException: disk I/O error at SQLite.SQLite3.Prepare2 (System.IntPtr db, System.String query) [0x00000] in <00000000000000000000000000000000>:0 "

I'm not sure if it's the similar issue with jet reported. I just testing WebGL with the demo in the readme to show player info in texts. But it failes now.

The Unity version I'm using is 2022.1.20f1. And I have selected the 3 files in Plugins/sqlite-amalgamation for WebGL platform.

Would you kind to give me some tips on this problem? Thanks!

tealcwu commented 2 days ago

image

jet-zhao commented 2 days ago

the three files lied in Plugins/sqlite-amalgamation/ are not selected for any platforms

Actually, they are selected but only for the Apple platforms iOS/tvOS/visionOS. It works if you mark them for WebGL, but one cannot specify compile flags on Unity directly for them in the import settings, like it is possible on Apple platforms. That's why I prebuilt it in a .bc file instead, to be able to use a custom build of SQLite with extensions such as FTS5 and RTree enabled.

What Unity should be doing is linking your build to the libgilzoide-sqlite-net.bc file, which contains all native code for SQLite.

Say, what version of Unity are you using?

Hey, it's 2020.3.42f1

gilzoide commented 15 hours ago

Hey @tealcwu. Would you mind creating a new issue for your problem? Even though they are both "runtime issues", they are not the same problem. I'll rename this issue to make it more specific.

tealcwu commented 15 hours ago

@gilzoide hi gil, just created issue#26.

gilzoide commented 15 hours ago

Hey, it's 2020.3.42f1

Ok, thanks for the info. I didn't test Unity 2020, only 2021.3+

I had some problems regarding emscripten version when trying to prebuild native plugins, it's quite likely that the problem is emscripten version incompatibility: I built this one with emscripten 2 provided by Unity 2021 and it cannot link in Unity 2020, which uses emscripten 1.38 (link from Unity docs). Now, when you mark sqlite3.c for building in Unity 2020, it works because emscripten can link it properly with your code. If this is the case, I'd expect some error log to occur during your build 🤔 Or maybe it uses different symbol naming, idk.

I'll try building with emscripten 1.X and will be back.

gilzoide commented 15 hours ago

Hey @jet-zhao , I was able to compile libgilzoide-sqlite-net.bc using Emscripten 1.40.1, it will likely link correctly in Unity 2020.3 now. Could you please test the version in branch support-emscripten-1 and see if it works for you?