sqlite3-api-cleanup.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. 2022-07-22
  3. The author disclaims copyright to this source code. In place of a
  4. legal notice, here is a blessing:
  5. * May you do good and not evil.
  6. * May you find forgiveness for yourself and forgive others.
  7. * May you share freely, never taking more than you give.
  8. ***********************************************************************
  9. This file is the tail end of the sqlite3-api.js constellation,
  10. intended to be appended after all other sqlite3-api-*.js files so
  11. that it can finalize any setup and clean up any global symbols
  12. temporarily used for setting up the API's various subsystems.
  13. In Emscripten builds it's run in the context of a Module.postRun
  14. handler.
  15. */
  16. 'use strict';
  17. if('undefined' !== typeof Module){ // presumably an Emscripten build
  18. /**
  19. Install a suitable default configuration for sqlite3ApiBootstrap().
  20. */
  21. const SABC = Object.assign(
  22. Object.create(null), {
  23. exports: ('undefined'===typeof wasmExports)
  24. ? Module['asm']/* emscripten <=3.1.43 */
  25. : wasmExports /* emscripten >=3.1.44 */,
  26. memory: Module.wasmMemory /* gets set if built with -sIMPORTED_MEMORY */
  27. },
  28. globalThis.sqlite3ApiConfig || {}
  29. );
  30. /**
  31. For current (2022-08-22) purposes, automatically call
  32. sqlite3ApiBootstrap(). That decision will be revisited at some
  33. point, as we really want client code to be able to call this to
  34. configure certain parts. Clients may modify
  35. globalThis.sqlite3ApiBootstrap.defaultConfig to tweak the default
  36. configuration used by a no-args call to sqlite3ApiBootstrap(),
  37. but must have first loaded their WASM module in order to be
  38. able to provide the necessary configuration state.
  39. */
  40. //console.warn("globalThis.sqlite3ApiConfig = ",globalThis.sqlite3ApiConfig);
  41. globalThis.sqlite3ApiConfig = SABC;
  42. let sqlite3;
  43. try{
  44. sqlite3 = globalThis.sqlite3ApiBootstrap();
  45. }catch(e){
  46. console.error("sqlite3ApiBootstrap() error:",e);
  47. throw e;
  48. }finally{
  49. delete globalThis.sqlite3ApiBootstrap;
  50. delete globalThis.sqlite3ApiConfig;
  51. }
  52. Module.sqlite3 = sqlite3 /* Needed for customized sqlite3InitModule() to be able to
  53. pass the sqlite3 object off to the client. */;
  54. }else{
  55. console.warn("This is not running in an Emscripten module context, so",
  56. "globalThis.sqlite3ApiBootstrap() is _not_ being called due to lack",
  57. "of config info for the WASM environment.",
  58. "It must be called manually.");
  59. }