aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/sqlite3-api-cleanup.js
blob: d989faccaf2b6a593b48d440c1601ed0e6807850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
  2022-07-22

  The author disclaims copyright to this source code.  In place of a
  legal notice, here is a blessing:

  *   May you do good and not evil.
  *   May you find forgiveness for yourself and forgive others.
  *   May you share freely, never taking more than you give.

  ***********************************************************************

  This file is the tail end of the sqlite3-api.js constellation,
  intended to be appended after all other sqlite3-api-*.js files so
  that it can finalize any setup and clean up any global symbols
  temporarily used for setting up the API's various subsystems.
*/
'use strict';
if('undefined' !== typeof Module){ // presumably an Emscripten build
  /**
     Replace sqlite3ApiBootstrap() with a variant which plugs in the
     Emscripten-based config for all config options which the client
     does not provide.
  */
  const SAB = self.sqlite3ApiBootstrap;
  self.sqlite3ApiBootstrap = function(apiConfig){
    apiConfig = apiConfig || {};
    const configDefaults = {
      Module: Module /* ==> Emscripten-style Module object. Currently
                        needs to be exposed here for test code. NOT part
                        of the public API. */,
      exports: Module['asm'],
      memory: Module.wasmMemory /* gets set if built with -sIMPORT_MEMORY */
    };
    const config = {};
    Object.keys(configDefaults).forEach(function(k){
      config[k] = Object.getOwnPropertyDescriptor(apiConfig, k)
        ? apiConfig[k] : configDefaults[k];
    });
    // Copy over any properties apiConfig defines but configDefaults does not...
    Object.keys(apiConfig).forEach(function(k){
      if(!Object.getOwnPropertyDescriptor(config, k)){
        config[k] = apiConfig[k];
      }
    });
    return SAB(config);
  };

  /**
     For current (2022-08-22) purposes, automatically call
     sqlite3ApiBootstrap().  That decision will be revisited at some
     point, as we really want client code to be able to call this to
     configure certain parts. If the global sqliteApiConfig property
     is available, it is assumed to be a config object for
     sqlite3ApiBootstrap().
  */
  //console.warn("self.sqlite3ApiConfig = ",self.sqlite3ApiConfig);
  const sqlite3 = self.sqlite3ApiBootstrap(self.sqlite3ApiConfig || Object.create(null));
  delete self.sqlite3ApiBootstrap;

  if(self.location && +self.location.port > 1024){
    console.warn("Installing sqlite3 bits as global S for dev-testing purposes.");
    self.S = sqlite3;
  }

  /* Clean up temporary references to our APIs... */
  delete sqlite3.capi.util /* arguable, but these are (currently) internal-use APIs */;
  //console.warn("Module.sqlite3 =",Module.sqlite3);
  Module.sqlite3 = sqlite3 /* Currently needed by test code and sqlite3-worker1.js */;
}