13961b263Sstephan/*
23961b263Sstephan  2022-07-22
33961b263Sstephan
43961b263Sstephan  The author disclaims copyright to this source code.  In place of a
53961b263Sstephan  legal notice, here is a blessing:
63961b263Sstephan
73961b263Sstephan  *   May you do good and not evil.
83961b263Sstephan  *   May you find forgiveness for yourself and forgive others.
93961b263Sstephan  *   May you share freely, never taking more than you give.
103961b263Sstephan
113961b263Sstephan  ***********************************************************************
123961b263Sstephan
133961b263Sstephan  This file is the tail end of the sqlite3-api.js constellation,
14ae708b2bSstephan  intended to be appended after all other sqlite3-api-*.js files so
15ae708b2bSstephan  that it can finalize any setup and clean up any global symbols
16ae708b2bSstephan  temporarily used for setting up the API's various subsystems.
173961b263Sstephan*/
183961b263Sstephan'use strict';
19ae708b2bSstephanif('undefined' !== typeof Module){ // presumably an Emscripten build
20e3cd6760Sstephan  /**
219a34509aSstephan     Install a suitable default configuration for sqlite3ApiBootstrap().
22e3cd6760Sstephan  */
2395639269Sstephan  const SABC = Object.assign(
2495639269Sstephan    Object.create(null), {
2595639269Sstephan      Module: Module /* ==> Currently needs to be exposed here for
2695639269Sstephan                        test code. NOT part of the public API. */,
2795639269Sstephan      exports: Module['asm'],
2895639269Sstephan      memory: Module.wasmMemory /* gets set if built with -sIMPORT_MEMORY */
2995639269Sstephan    },
3095639269Sstephan    self.sqlite3ApiConfig || Object.create(null)
3195639269Sstephan  );
32e3cd6760Sstephan
33e3cd6760Sstephan  /**
34ae708b2bSstephan     For current (2022-08-22) purposes, automatically call
35ae708b2bSstephan     sqlite3ApiBootstrap().  That decision will be revisited at some
36ae708b2bSstephan     point, as we really want client code to be able to call this to
379a34509aSstephan     configure certain parts. Clients may modify
389a34509aSstephan     self.sqlite3ApiBootstrap.defaultConfig to tweak the default
3995639269Sstephan     configuration used by a no-args call to sqlite3ApiBootstrap(),
4095639269Sstephan     but must have first loaded their WASM module in order to be
4195639269Sstephan     able to provide the necessary configuration state.
42e3cd6760Sstephan  */
43ae708b2bSstephan  //console.warn("self.sqlite3ApiConfig = ",self.sqlite3ApiConfig);
445b915007Sstephan  self.sqlite3ApiConfig = SABC;
455b915007Sstephan  let sqlite3;
465b915007Sstephan  try{
475b915007Sstephan    sqlite3 = self.sqlite3ApiBootstrap();
48e67a0f40Sstephan  }catch(e){
49e67a0f40Sstephan    console.error("sqlite3ApiBootstrap() error:",e);
50e67a0f40Sstephan    throw e;
515b915007Sstephan  }finally{
52ae708b2bSstephan    delete self.sqlite3ApiBootstrap;
5395639269Sstephan    delete self.sqlite3ApiConfig;
545b915007Sstephan  }
55e3cd6760Sstephan
563961b263Sstephan  if(self.location && +self.location.port > 1024){
575360f5fcSstephan    console.warn("Installing sqlite3 bits as global S for local dev/test purposes.");
58e3cd6760Sstephan    self.S = sqlite3;
593961b263Sstephan  }
60e3cd6760Sstephan
61e3cd6760Sstephan  /* Clean up temporary references to our APIs... */
62*8948fbeeSstephan  delete sqlite3.util /* arguable, but these are (currently) internal-use APIs */;
63eb97743cSstephan  Module.sqlite3 = sqlite3 /* Needed for customized sqlite3InitModule() to be able to
64eb97743cSstephan                              pass the sqlite3 object off to the client. */;
659a34509aSstephan}else{
669a34509aSstephan  console.warn("This is not running in an Emscripten module context, so",
679a34509aSstephan               "self.sqlite3ApiBootstrap() is _not_ being called due to lack",
689a34509aSstephan               "of config info for the WASM environment.",
699a34509aSstephan               "It must be called manually.");
70ae708b2bSstephan}
71