From b0ab21dee40144d8aec92df4a0c98eb069badeb3 Mon Sep 17 00:00:00 2001 From: stephan Date: Thu, 3 Nov 2022 22:14:47 +0000 Subject: [PATCH] Experimentally use clang's C preprocessor to filter #ifdef's out of the generated sqlite3-api.js, the goal being to see if we can feasibly use cpp to include ES6 module-specific code in the main code base and conditionally filter it out. FossilOrigin-Name: 718a6d371e61359d73c8f80afdb248e3d9b4d8df4c4e5c122ac884344e31035b --- ext/wasm/GNUmakefile | 53 ++++++++++++++++++++++++++++++-- ext/wasm/api/pre-js.js | 7 ++++- ext/wasm/api/sqlite3-api-opfs.js | 11 +++++-- manifest | 19 +++++++----- manifest.uuid | 2 +- 5 files changed, 76 insertions(+), 16 deletions(-) diff --git a/ext/wasm/GNUmakefile b/ext/wasm/GNUmakefile index 993c668697..069e2a1da9 100644 --- a/ext/wasm/GNUmakefile +++ b/ext/wasm/GNUmakefile @@ -96,7 +96,7 @@ dir.dout := $(dir.wasm)/jswasm # dir.tmp = output dir for intermediary build files, as opposed to # end-user deliverables. dir.tmp := $(dir.wasm)/bld -#CLEAN_FILES += $(wildcard $(dir.dout)/*) $(wildcard $(dir.tmp)/*) +CLEAN_FILES += $(dir.tmp)/* $(dir.dout)/* ifeq (,$(wildcard $(dir.dout))) dir._tmp := $(shell mkdir -p $(dir.dout)) endif @@ -235,7 +235,8 @@ $(foreach X,$(SOAP.js) $(sqlite3-worker1.js) $(sqlite3-worker1-promiser.js),\ all: $(sqlite3-api.ext.jses) sqlite3-api.js := $(dir.tmp)/sqlite3-api.js -$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE) +sqlite3-api.pre-cpp.js := $(dir.tmp)/sqlite3-api.pre-cpp.js +$(sqlite3-api.pre-cpp.js): $(sqlite3-api.jses) $(MAKEFILE) @echo "Making $@..." @for i in $(sqlite3-api.jses); do \ echo "/* BEGIN FILE: $$i */"; \ @@ -256,7 +257,8 @@ $(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.pre-cpp.js := $(dir.api)/pre-js.pre-cpp.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 \ @@ -288,6 +290,51 @@ $(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js) echo '*/'; \ } > $@ +######################################################################## +# Transform $(1) to $(2) via cpp -E -CC $(1) ... +# +# DO NOT use gcc as the preprocessor because it will emit its own +# license header to our output because that's a comment in its +# stdc-predef.h, which we cannot tell it to _not_ include. The only +# workaround to that is to allow gcc -E to strip all comments. The +# wasm build uses clang behind emcc, anyway, so we already have a +# clang dependency. However, the clang cpp refuses to read empty +# strings in the form '', so we have to be sure to use "" in JS code +# for those. +# +# It's tempting to build a custom mini-cpp-like binary for this +# purpose to avoid these dependencies and quirks. Maybe we could use +# lemon to do the heavy lifting for that, noting that we'd still need +# to tokenize it by hand (but only lines which start with "#" or +# backslash-continued lines, and we could treat all other lines as +# opaque content). +# +# In this build we may have #ifdef's (and the like) in arbitrary input +# JS files and we need to preprocess those before Emscripten gets +# ahold of them. We cannot simply preprocess the resulting +# Emscripten-generated sqlite3.js because (A) Emscripten may choke on +# C preprocessor directives in the input and (B) Emscripten's output +# may contain things which cpp cannot read (like single-quoted empty +# strings: ''). +bin.cpp ?= clang +ifneq (,$(filter ems,$(MAKECMDGOALS))) +js.cpp.defines ?= -DSQLITE_JS_ESM +ems: $(filter-out ems,$(MAKECMDGOALS)) +else +js.cpp.defines ?= +endif +define CPP_JS +# $1 = X.js. $2 = output file to generate by filtering $(1) through +# $(bin.cpp) -E -CC. +$(2): $(1) $$(MAKEFILE) + $$(bin.cpp) -E -CC -undef $(js.cpp.defines) -x c $(1) | sed -e '/^#/d' > $$@ +CLEAN_FILES += $(2) +endef +$(eval $(call CPP_JS,$(dir.tmp)/sqlite3-api.pre-cpp.js,$(dir.tmp)/sqlite3-api.js)) +$(eval $(call CPP_JS,$(dir.api)/pre-js.js,$(dir.tmp)/pre-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. diff --git a/ext/wasm/api/pre-js.js b/ext/wasm/api/pre-js.js index f31dea1794..f959c33072 100644 --- a/ext/wasm/api/pre-js.js +++ b/ext/wasm/api/pre-js.js @@ -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) { +#ifdef 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 diff --git a/ext/wasm/api/sqlite3-api-opfs.js b/ext/wasm/api/sqlite3-api-opfs.js index 86285df1d3..3cb5c8a3eb 100644 --- a/ext/wasm/api/sqlite3-api-opfs.js +++ b/ext/wasm/api/sqlite3-api-opfs.js @@ -166,7 +166,12 @@ const installOpfsVfs = function callee(options){ opfsVfs.dispose(); return promiseReject_(err); }; - const W = new Worker(options.proxyUri); + const W = +#ifdef 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 @@ -566,7 +571,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(""); }; /** @@ -1155,7 +1160,7 @@ const installOpfsVfs = function callee(options){ be set here. */ //"pragma cache_size=-8388608;" - ].join('') + ].join("") ); } diff --git a/manifest b/manifest index 3942c53535..d07080d368 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Globally\sreplace\s''\swith\s""\sfor\sempty\sJS\sstrings\sto\splease\sC\spreprocessor. -D 2022-11-03T21:21:10.223 +C Experimentally\suse\sclang's\sC\spreprocessor\sto\sfilter\s#ifdef's\sout\sof\sthe\sgenerated\ssqlite3-api.js,\sthe\sgoal\sbeing\sto\ssee\sif\swe\scan\sfeasibly\suse\scpp\sto\sinclude\sES6\smodule-specific\scode\sin\sthe\smain\scode\sbase\sand\sconditionally\sfilter\sit\sout. +D 2022-11-03T22:14:47.036 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -487,7 +487,7 @@ 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 78b64d110e66e18709c11437a74c29bc7855d85a713217db99127c7f304a332f +F ext/wasm/GNUmakefile 5f04a4703fa1cbf3b8a031b9875828326c120fd98f08b68e889938f006e26973 F ext/wasm/README-dist.txt 2d670b426fc7c613b90a7d2f2b05b433088fe65181abead970980f0a4a75ea20 F ext/wasm/README.md ef39861aa21632fdbca0bdd469f78f0096f6449a720f3f39642594af503030e9 F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api 9120c2f8f51fa85f46dcf4dcb6b12f4a807d428f6089b99cdb08d8ddfcfd88b2 @@ -497,11 +497,11 @@ F ext/wasm/api/extern-post-js.js f3dc4219a2a1f183d98452dcbd55a0c5351b759eccca754 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 9620327120abb15b3af96f72ef9efbcf69e78d90e501328521108b93547a8eb8 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 cdcbb57acc66f4569ac9e18f9d13d5a3657d8aae195725c6324943da56c1005d +F ext/wasm/api/sqlite3-api-opfs.js 700c1b68c324fb783bb13588f972671e51fbc4b063402768be962df8be7e5142 F ext/wasm/api/sqlite3-api-prologue.js 952ba908cc5ee42728c5c09dd549af32ef0c3cc15ab7b919a8007c5684f69320 F ext/wasm/api/sqlite3-api-worker1.js e94ba98e44afccfa482874cd9acb325883ade50ed1f9f9526beb9de1711f182f F ext/wasm/api/sqlite3-license-version-header.js a661182fc93fc2cf212dfd0b987f8e138a3ac98f850b1112e29b5fbdaecc87c3 @@ -2054,8 +2054,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 70ee6ee014bc4e2c1daa9b4a8909cf76ccecf32de1eb39055f20d3d0b1daa1bd -R eaaf8a7ca25305cffe9e8f7e734e3ee8 +P e92e1f42bef94a1df29f66b4111ebfde93eba3759bc5d5a9c95f714508851346 +R c242b13966143759c21878706550a242 +T *branch * js-cpp +T *sym-js-cpp * +T -sym-trunk * Cancelled\sby\sbranch. U stephan -Z b98d96f31e9b735c045c03e6eefd13f8 +Z c0f79930e1701e29aa37bb40d0432abe # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 8348da8467..125d8b2c56 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e92e1f42bef94a1df29f66b4111ebfde93eba3759bc5d5a9c95f714508851346 \ No newline at end of file +718a6d371e61359d73c8f80afdb248e3d9b4d8df4c4e5c122ac884344e31035b \ No newline at end of file