Integrate a custom preprocessor to the JS build process to facilitate creation of both vanilla JS and ES6 Module builds from the same source files. There is still some build-level reworking pending to make an ESM build a first-class deliverable.

FossilOrigin-Name: 10c723d96d61d2e552ec1102563d58f1eb11bc3d30e03606fd8e0279c5a9043a
This commit is contained in:
stephan
2022-11-19 02:58:03 +00:00
8 changed files with 1623 additions and 21 deletions
+53 -5
View File
@@ -225,17 +225,18 @@ sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js
SOAP.js := $(dir.api)/sqlite3-opfs-async-proxy.js
sqlite3-worker1.js := $(dir.api)/sqlite3-worker1.js
sqlite3-worker1-promiser.js := $(dir.api)/sqlite3-worker1-promiser.js
define CP_XAPI
define COPY_XAPI
sqlite3-api.ext.jses += $$(dir.dout)/$$(notdir $(1))
$$(dir.dout)/$$(notdir $(1)): $(1) $$(MAKEFILE)
cp $$< $$@
endef
$(foreach X,$(SOAP.js) $(sqlite3-worker1.js) $(sqlite3-worker1-promiser.js),\
$(eval $(call CP_XAPI,$(X))))
$(eval $(call COPY_XAPI,$(X))))
all: $(sqlite3-api.ext.jses)
sqlite3-api.js := $(dir.tmp)/sqlite3-api.js
$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE)
sqlite3-api.c-pp.js := $(dir.tmp)/sqlite3-api.c-pp.js
$(sqlite3-api.c-pp.js): $(sqlite3-api.jses) $(MAKEFILE)
@echo "Making $@..."
@for i in $(sqlite3-api.jses); do \
echo "/* BEGIN FILE: $$i */"; \
@@ -256,7 +257,7 @@ $(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE)
########################################################################
# --post-js and --pre-js are emcc flags we use to append/prepend JS to
# the generated emscripten module file.
pre-js.js := $(dir.api)/pre-js.js
pre-js.js := $(dir.tmp)/pre-js.js
post-js.js := $(dir.tmp)/post-js.js
post-jses := \
$(dir.api)/post-js-header.js \
@@ -269,7 +270,8 @@ $(post-js.js): $(post-jses) $(MAKEFILE)
cat $$i; \
echo "/* END FILE: $$i */"; \
done > $@
extern-post-js.js := $(dir.api)/extern-post-js.js
extern-post-js.js.in := $(dir.api)/extern-post-js.js
extern-post-js.js := $(dir.tmp)/extern-post-js.js
extern-pre-js.js := $(dir.api)/extern-pre-js.js
pre-post-common.flags := \
--post-js=$(post-js.js) \
@@ -288,6 +290,52 @@ $(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js)
echo '*/'; \
} > $@
########################################################################
# Transform $(1) to $(2) via ./c-pp -f $(1) ...
#
# Historical notes:
#
# - We first attempted to use gcc and/or clang to preprocess JS files
# in the same way we would normally do C files, but C-specific quirks
# of each makes that untennable.
#
# - We implemented c-pp.c (the C-Minus Pre-processor) as a custom
# generic/file-format-agnostic preprocessor to enable us to pack
# code for different target builds into the same JS files. Most
# notably, some ES6 module (a.k.a. ESM) features cannot legally be
# referenced at all in non-ESM code, e.g. the "import" and "export"
# keywords. This preprocessing step permits us to swap out sections
# of code where necessary for ESM and non-ESM (a.k.a. vanilla JS)
# require different implementations. The alternative to such
# preprocessing, would be to have separate source files for ES6
# builds, which would have a higher maintenance burden than c-pp.c
# seems likely to.
#
# c-pp.c was written specifically for the sqlite project's JavaScript
# builds but is maintained as a standalone project:
# https://fossil.wanderinghorse.net/r/c-pp
bin.c-pp := ./c-pp
$(bin.c-pp): c-pp.c $(sqlite3.c) $(MAKEFILE)
$(CC) -O0 -o $@ c-pp.c $(sqlite3.c) '-DCMPP_DEFAULT_DELIM="//#"' -I$(dir.top)
ifneq (,$(filter esm,$(MAKECMDGOALS)))
js.cpp.defines ?= -DSQLITE_JS_ESM
esm: $(filter-out esm,$(MAKECMDGOALS))
else
js.cpp.defines ?=
endif
define C-PP.JS
# $1 = X.js. $2 = output file to generate by filtering $(1) through
# $(bin.cpp) -E -CC.
$(2): $(1) $$(MAKEFILE) $$(bin.c-pp)
$$(bin.c-pp) $(js.cpp.defines) -f $(1) -o $$@
CLEAN_FILES += $(2)
endef
$(eval $(call C-PP.JS,$(dir.tmp)/sqlite3-api.c-pp.js,$(sqlite3-api.js)))
$(eval $(call C-PP.JS,$(dir.api)/pre-js.js,$(dir.tmp)/pre-js.js))
$(eval $(call C-PP.JS,$(extern-post-js.js.in),$(extern-post-js.js)))
# /end CPP-of-JS bits
########################################################################
########################################################################
# call-make-pre-js creates rules for pre-js-$(1).js. $1 = the base
# name of the JS file on whose behalf this pre-js is for.
+8
View File
@@ -131,3 +131,11 @@ into the build-generated `sqlite3.js` along with `sqlite3-api.js`.
`sqlite3InitModule()` function with one which, after the module is
loaded, also initializes the asynchronous parts of the sqlite3
module. For example, the OPFS VFS support.
## Preprocessing of Source Files
Certain files in the build require preprocessing to filter in/out
parts which differ between vanilla JS builds and ES6 Module
(a.k.a. esm) builds. The preprocessor itself is in
[](/file/ext/wasm/c-pp.c) and the associates flags and rules are in
[](/file/ext/wasm/GNUmakefile).
+9
View File
@@ -4,6 +4,9 @@
most of the associated JS code, runs outside of the
Emscripten-generated module init scope, in the current
global scope. */
//#if SQLITE_JS_ESM
const toexport =
//#endif
(function(){
/**
In order to hide the sqlite3InitModule()'s resulting Emscripten
@@ -103,4 +106,10 @@
exports["sqlite3InitModule"] = sqlite3InitModule;
/* AMD modules get injected in a way we cannot override,
so we can't handle those here. */
//#if SQLITE_JS_ESM
return self.sqlite3InitModule;
//#endif
})();
//#if SQLITE_JS_ESM
export default toexport;
//#endif
+6 -1
View File
@@ -29,6 +29,10 @@ sqlite3InitModuleState.debugModule('self.location =',self.location);
4) If none of the above apply, (prefix+path) is returned.
*/
Module['locateFile'] = function(path, prefix) {
//#if SQLITE_JS_ESM
return new URL(path, import.meta.url).href;
//#else
'use strict';
let theFile;
const up = this.urlParams;
if(up.has(path)){
@@ -47,6 +51,7 @@ Module['locateFile'] = function(path, prefix) {
"result =", theFile
);
return theFile;
//#endif /* SQLITE_JS_EMS */
}.bind(sqlite3InitModuleState);
/**
@@ -65,7 +70,7 @@ Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){
const uri = Module.locateFile(
callee.uri, (
('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/)
? '' : scriptDirectory)
? "" : scriptDirectory)
);
sqlite3InitModuleState.debugModule(
"instantiateWasm() uri =", uri
+8 -3
View File
@@ -166,7 +166,12 @@ const installOpfsVfs = function callee(options){
opfsVfs.dispose();
return promiseReject_(err);
};
const W = new Worker(options.proxyUri);
const W =
//#if SQLITE_JS_ESM
new Worker(new URL(options.proxyUri, import.meta.url));
//#else
new Worker(options.proxyUri);
//#endif
W._originalOnError = W.onerror /* will be restored later */;
W.onerror = function(err){
// The error object doesn't contain any useful info when the
@@ -569,7 +574,7 @@ const installOpfsVfs = function callee(options){
const ndx = Math.random() * (f._n * 64) % f._n | 0;
a[i] = f._chars[ndx];
}
return a.join('');
return a.join("");
};
/**
@@ -1158,7 +1163,7 @@ const installOpfsVfs = function callee(options){
be set here.
*/
//"pragma cache_size=-8388608;"
].join('')
].join("")
);
}
+1525
View File
File diff suppressed because it is too large Load Diff
+13 -11
View File
@@ -1,5 +1,5 @@
C Small\sperformance\soptimization\sin\sbtree.c.
D 2022-11-19T00:22:12.991
C Integrate\sa\scustom\spreprocessor\sto\sthe\sJS\sbuild\sprocess\sto\sfacilitate\screation\sof\sboth\svanilla\sJS\sand\sES6\sModule\sbuilds\sfrom\sthe\ssame\ssource\sfiles.\sThere\sis\sstill\ssome\sbuild-level\sreworking\spending\sto\smake\san\sESM\sbuild\sa\sfirst-class\sdeliverable.
D 2022-11-19T02:58:03.867
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -488,21 +488,21 @@ F ext/userauth/sqlite3userauth.h 7f3ea8c4686db8e40b0a0e7a8e0b00fac13aa7a3
F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04
F ext/userauth/userauth.c 7f00cded7dcaa5d47f54539b290a43d2e59f4b1eb5f447545fa865f002fc80cb
F ext/wasm/EXPORTED_FUNCTIONS.fiddle.in 27450c8b8c70875a260aca55435ec927068b34cef801a96205adb81bdcefc65c
F ext/wasm/GNUmakefile dd6352a52e2742b78ea472acc41c6eea52165e25756eba4f1fea3f5dad1632b7
F ext/wasm/GNUmakefile 6a0d03e8d0b52b6851a364c1faaa0df8a07be1e8eb8aa9f87432aad74005a04e
F ext/wasm/README-dist.txt 2d670b426fc7c613b90a7d2f2b05b433088fe65181abead970980f0a4a75ea20
F ext/wasm/README.md ef39861aa21632fdbca0bdd469f78f0096f6449a720f3f39642594af503030e9
F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api 9120c2f8f51fa85f46dcf4dcb6b12f4a807d428f6089b99cdb08d8ddfcfd88b2
F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287
F ext/wasm/api/README.md 1350088aee90e959ad9a94fab1bb6bcb5e99d4d27f976db389050f54f2640c78
F ext/wasm/api/extern-post-js.js c197b7567496fc27766842f8c4f4963054bf8c667926ab3df4c91aa548939ce6
F ext/wasm/api/README.md 29276a845e57004e82efba61fa5866fd05f9137380a1dc26dc4c6d65264cd81c
F ext/wasm/api/extern-post-js.js 824f37f1957e15b150bb36e98621b3bf91b55f6af7055cedc831331129b4883d
F ext/wasm/api/extern-pre-js.js cc61c09c7a24a07dbecb4c352453c3985170cec12b4e7e7e7a4d11d43c5c8f41
F ext/wasm/api/post-js-footer.js cd0a8ec768501d9bd45d325ab0442037fb0e33d1f3b4f08902f15c34720ee4a1
F ext/wasm/api/post-js-header.js d6ab3dfef4a06960d28a7eaa338d4e2a1a5981e9b38718168bbde8fdb2a439b8
F ext/wasm/api/pre-js.js 287e462f969342b032c03900e668099fa1471d852df7a472de5bc349161d9c04
F ext/wasm/api/pre-js.js 749bbbac2f1a2192eaba80cf5e00a3219da78b3c0a84e3019b5eef30e0bc9b88
F ext/wasm/api/sqlite3-api-cleanup.js ecdc69dbfccfe26146f04799fcfd4a6f5790d46e7e3b9b6e9b0491f92ed8ae34
F ext/wasm/api/sqlite3-api-glue.js 056f44b82c126358a0175e08a892d56fadfce177b0d7a0012502a6acf67ea6d5
F ext/wasm/api/sqlite3-api-oo1.js e9a83489bbb4838ce0aee46eaaa9350e0e25a5b926b565e4f5ae8e840e4fbaed
F ext/wasm/api/sqlite3-api-opfs.js 5c6d0903e100b2918920d8c596ab037cd9a67df19e388ebde4fe085218780af0
F ext/wasm/api/sqlite3-api-opfs.js 4368a30586df3e11339a72082c77bdef670d619c6185c2dd1ebf5208c3ab0a5c
F ext/wasm/api/sqlite3-api-prologue.js fd526fa017fa2578673ca18158354515c719e719a5d93f2f6d0e43f39170430e
F ext/wasm/api/sqlite3-api-worker1.js e94ba98e44afccfa482874cd9acb325883ade50ed1f9f9526beb9de1711f182f
F ext/wasm/api/sqlite3-license-version-header.js a661182fc93fc2cf212dfd0b987f8e138a3ac98f850b1112e29b5fbdaecc87c3
@@ -513,6 +513,7 @@ F ext/wasm/api/sqlite3-worker1-promiser.js 0c7a9826dbf82a5ed4e4f7bf7816e825a52af
F ext/wasm/api/sqlite3-worker1.js 1e54ea3d540161bcfb2100368a2fc0cad871a207b8336afee1c445715851ec54
F ext/wasm/batch-runner.html 4deeed44fe41496dc6898d9fb17938ea3291f40f4bfb977e29d0cef96fbbe4c8
F ext/wasm/batch-runner.js 49609e89aaac9989d6c1ad3fae268e4878e1ad7bc5fd3e5c2f44959660780b2e
F ext/wasm/c-pp.c 92285f7bce67ed7b7020b40fde8ed0982c442b63dc33df9dfd4b658d4a6c0779
F ext/wasm/common/SqliteTestUtil.js d8bf97ecb0705a2299765c8fc9e11b1a5ac7f10988bbf375a6558b7ca287067b
F ext/wasm/common/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/common/testing.css 35889709547d89a6109ff83b25c11bbc91d8dd43aab8722e428655ca98880a06
@@ -2055,8 +2056,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P ff494449efd475878c549728cc22ee9b12d13674068781747fc042a0c1bd09c8
R 8e4332decceeea7e1973b8e5ac88e8a4
U drh
Z 80da1bc9f69008afc02dd88003bee918
P f710cce13577788cf3b95ed7089c3af2854271ff53f0a0b7b0619f315e331eff 6b826e700f6849eebfbba38e5948b96be245994e3e03ea30743114d3f5689c42
R a5784f7e95f9f4ca471c3d20c7b7d6ea
T +closed 6b826e700f6849eebfbba38e5948b96be245994e3e03ea30743114d3f5689c42 Closed\sby\sintegrate-merge.
U stephan
Z 22024ccae1db68511ad1978bbc10208a
# Remove this line to create a well-formed Fossil manifest.
+1 -1
View File
@@ -1 +1 @@
f710cce13577788cf3b95ed7089c3af2854271ff53f0a0b7b0619f315e331eff
10c723d96d61d2e552ec1102563d58f1eb11bc3d30e03606fd8e0279c5a9043a