Skip to content

Commit ecfc5ad

Browse files
committed
Change build process to create both an extension-functions version and a normal sql.js without the additional functions
1 parent 84da2b4 commit ecfc5ad

16 files changed

+498856
-239182
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ node_modules/
33

44
# LLVM bitcode
55
c/sqlite3.bc
6+
c/extension-functions.bc
67
# Intermediary js files
78
js/api.js
9+
js/api-extension-functions.js
810
js/sql-noopt-raw.js.map
911
js/sql-optimized.js
12+
js/sql-extension-functions-optimized.js
13+
js/worker.js
1014
js/sql-raw.js.map

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@ EMCC=$(EMSCRIPTEN)/emcc
66

77
CFLAGS=-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_INT64_TYPE="long long int" -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
88

9-
all: js/sql.js js/sql-debug.js js/sql-worker.js
9+
all: js/sql.js js/sql-debug.js js/worker.sql.js js/sql-extension-functions.js js/sql-extension-functions-debug.js js/worker.sql-extension-functions.js
1010

1111
# RESERVED_FUNCTION_POINTERS setting is used for registering custom functions
1212
debug: EMFLAGS= -O1 -g -s INLINING_LIMIT=10 -s RESERVED_FUNCTION_POINTERS=64
13-
debug: js/sql-debug.js
13+
debug: js/sql-debug.js js/sql-extension-functions-debug.js
1414

1515
optimized: EMFLAGS= --memory-init-file 0 --closure 1 -O3 -s INLINING_LIMIT=50 -s RESERVED_FUNCTION_POINTERS=64
16-
optimized: js/sql-optimized.js
16+
optimized: js/sql-optimized.js js/sql-extension-functions-optimized.js
1717

1818
js/sql.js: optimized
1919
cp js/sql-optimized.js js/sql.js
2020

21+
js/sql-extension-functions.js: optimized
22+
cp js/sql-extension-functions-optimized.js js/sql-extension-functions.js
23+
2124
js/sql%.js: js/shell-pre.js js/sql%-raw.js js/shell-post.js
2225
cat $^ > $@
2326

24-
js/sql%-raw.js: c/sqlite3.bc js/api.js exported_functions c/extension-functions.bc
25-
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@exported_functions c/extension-functions.bc c/sqlite3.bc --post-js js/api.js -o $@
27+
js/sql%-raw.js: c/sqlite3.bc js/api.js exported_functions extension_exported_functions c/extension-functions.bc js/api-extension-functions.js
28+
if [ "$(findstring extension-functions,$@)" = "extension-functions" ]; then \
29+
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@extension_exported_functions c/extension-functions.bc c/sqlite3.bc --post-js js/api-extension-functions.js -o $@ ;\
30+
else \
31+
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@exported_functions c/sqlite3.bc --post-js js/api.js -o $@ ;\
32+
fi
2633

2734
js/api.js: coffee/api.coffee coffee/exports.coffee coffee/api-data.coffee
2835
cat $^ | coffee --bare --compile --stdio > $@
2936

37+
js/api-extension-functions.js: coffee/api.coffee coffee/extension-functions-exports.coffee coffee/api-data.coffee
38+
cat $^ | coffee --bare --compile --stdio > $@
39+
3040
# Web worker API
3141
worker: js/worker.sql.js
3242
js/worker.js: coffee/worker.coffee
@@ -35,6 +45,9 @@ js/worker.js: coffee/worker.coffee
3545
js/worker.sql.js: js/sql.js js/worker.js
3646
cat $^ > $@
3747

48+
js/worker.sql-extension-functions.js: js/sql-extension-functions.js js/worker.js
49+
cat $^ > $@
50+
3851
c/sqlite3.bc: c/sqlite3.c
3952
# Generate llvm bitcode
4053
$(EMCC) $(CFLAGS) c/sqlite3.c -o c/sqlite3.bc
@@ -43,7 +56,7 @@ module.tar.gz: test package.json AUTHORS README.md js/sql.js
4356
tar --create --gzip $^ > $@
4457

4558
clean:
46-
rm -rf js/sql*.js js/api.js js/sql*-raw.js c/sqlite3.bc c/extension-functions.bc
59+
rm -rf js/sql*.js js/api*.js js/sql*-raw.js js/worker.sql*.js js/worker.js c/sqlite3.bc c/extension-functions.bc
4760

4861
c/extension-functions.bc: c/extension-functions.c
4962
$(EMCC) $(CFLAGS) -s LINKABLE=1 c/extension-functions.c -o c/extension-functions.bc

coffee/api.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class Database
243243
if data? then FS.createDataFile '/', @filename, data, true, true
244244
@handleError sqlite3_open @filename, apiTemp
245245
@db = getValue(apiTemp, 'i32')
246-
RegisterExtensionFunctions @db
246+
RegisterExtensionFunctions(@db) if typeof(RegisterExtensionFunctions) == "function"
247247
@statements = {} # A list of all prepared statements of the database
248248

249249
### Execute an SQL query, ignoring the rows it returns.

coffee/exports.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ sqlite3_value_double = Module['cwrap'] 'sqlite3_value_double', 'number', ['numbe
5252
sqlite3_result_double = Module['cwrap'] 'sqlite3_result_double', '', ['number', 'number']
5353
sqlite3_result_null = Module['cwrap'] 'sqlite3_result_null', '', ['number']
5454
sqlite3_result_text = Module['cwrap'] 'sqlite3_result_text', '', ['number', 'string', 'number', 'number']
55-
RegisterExtensionFunctions = Module['cwrap'] 'RegisterExtensionFunctions', 'number', ['number']
5655

5756
# Export the API
5857
this['SQL'] = {'Database':Database}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
sqlite3_open = Module['cwrap'] 'sqlite3_open', 'number', ['string', 'number']
2+
sqlite3_close_v2 = Module['cwrap'] 'sqlite3_close_v2', 'number', ['number']
3+
sqlite3_exec = Module['cwrap'] 'sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number']
4+
sqlite3_free = Module['cwrap'] 'sqlite3_free', '', ['number']
5+
6+
# Prepared statements
7+
## prepare
8+
sqlite3_prepare_v2 = Module['cwrap'] 'sqlite3_prepare_v2', 'number', ['number', 'string', 'number', 'number', 'number']
9+
# Version of sqlite3_prepare_v2 to which a pointer to a string that is already
10+
# in memory is passed.
11+
sqlite3_prepare_v2_sqlptr = Module['cwrap'] 'sqlite3_prepare_v2', 'number', ['number', 'number', 'number', 'number', 'number']
12+
## Bind parameters
13+
14+
#int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
15+
# We declare const char* as a number, because we will manually allocate the memory and pass a pointer to the function
16+
sqlite3_bind_text = Module['cwrap'] 'sqlite3_bind_text', 'number', ['number', 'number', 'number', 'number', 'number']
17+
sqlite3_bind_blob = Module['cwrap'] 'sqlite3_bind_blob', 'number', ['number', 'number', 'number', 'number', 'number']
18+
#int sqlite3_bind_double(sqlite3_stmt*, int, double);
19+
sqlite3_bind_double = Module['cwrap'] 'sqlite3_bind_double', 'number', ['number', 'number', 'number']
20+
#int sqlite3_bind_double(sqlite3_stmt*, int, int);
21+
sqlite3_bind_int = Module['cwrap'] 'sqlite3_bind_int', 'number', ['number', 'number', 'number']
22+
#int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
23+
sqlite3_bind_parameter_index = Module['cwrap'] 'sqlite3_bind_parameter_index', 'number', ['number', 'string']
24+
25+
## Get values
26+
# int sqlite3_step(sqlite3_stmt*)
27+
sqlite3_step = Module['cwrap'] 'sqlite3_step', 'number', ['number']
28+
sqlite3_errmsg = Module['cwrap'] 'sqlite3_errmsg', 'string', ['number']
29+
# int sqlite3_data_count(sqlite3_stmt *pStmt);
30+
sqlite3_data_count = Module['cwrap'] 'sqlite3_data_count', 'number', ['number']
31+
sqlite3_column_double = Module['cwrap'] 'sqlite3_column_double', 'number', ['number', 'number']
32+
sqlite3_column_text = Module['cwrap'] 'sqlite3_column_text', 'string', ['number', 'number']
33+
sqlite3_column_blob = Module['cwrap'] 'sqlite3_column_blob', 'number', ['number', 'number']
34+
sqlite3_column_bytes = Module['cwrap'] 'sqlite3_column_bytes', 'number', ['number', 'number']
35+
sqlite3_column_type = Module['cwrap'] 'sqlite3_column_type', 'number', ['number', 'number']
36+
#const char *sqlite3_column_name(sqlite3_stmt*, int N);
37+
sqlite3_column_name = Module['cwrap'] 'sqlite3_column_name', 'string', ['number', 'number']
38+
# int sqlite3_reset(sqlite3_stmt *pStmt);
39+
sqlite3_reset = Module['cwrap'] 'sqlite3_reset', 'number', ['number']
40+
sqlite3_clear_bindings = Module['cwrap'] 'sqlite3_clear_bindings', 'number', ['number']
41+
# int sqlite3_finalize(sqlite3_stmt *pStmt);
42+
sqlite3_finalize = Module['cwrap'] 'sqlite3_finalize', 'number', ['number']
43+
44+
## Create custom functions
45+
sqlite3_create_function_v2 = Module['cwrap'] 'sqlite3_create_function_v2', 'number', ['number', 'string', 'number', 'number', 'number', 'number', 'number', 'number', 'number']
46+
sqlite3_value_type = Module['cwrap'] 'sqlite3_value_type', 'number', ['number']
47+
sqlite3_value_bytes = Module['cwrap'] 'sqlite3_value_bytes', 'number', ['number']
48+
sqlite3_value_text = Module['cwrap'] 'sqlite3_value_text', 'string', ['number']
49+
sqlite3_value_int = Module['cwrap'] 'sqlite3_value_int', 'number', ['number']
50+
sqlite3_value_blob = Module['cwrap'] 'sqlite3_value_blob', 'number', ['number']
51+
sqlite3_value_double = Module['cwrap'] 'sqlite3_value_double', 'number', ['number']
52+
sqlite3_result_double = Module['cwrap'] 'sqlite3_result_double', '', ['number', 'number']
53+
sqlite3_result_null = Module['cwrap'] 'sqlite3_result_null', '', ['number']
54+
sqlite3_result_text = Module['cwrap'] 'sqlite3_result_text', '', ['number', 'string', 'number', 'number']
55+
RegisterExtensionFunctions = Module['cwrap'] 'RegisterExtensionFunctions', 'number', ['number']
56+
57+
# Export the API
58+
this['SQL'] = {'Database':Database}
59+
Module[i] = this['SQL'][i] for i of this['SQL']

exported_functions

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
[
2-
"_malloc",
3-
"_free",
4-
"_sqlite3_open",
5-
"_sqlite3_exec",
6-
"_sqlite3_free",
7-
"_sqlite3_errmsg",
8-
"_sqlite3_prepare_v2",
9-
"_sqlite3_bind_text",
10-
"_sqlite3_bind_blob",
11-
"_sqlite3_bind_double",
12-
"_sqlite3_bind_int",
13-
"_sqlite3_bind_parameter_index",
14-
"_sqlite3_step",
15-
"_sqlite3_data_count",
16-
"_sqlite3_column_double",
17-
"_sqlite3_column_text",
18-
"_sqlite3_column_blob",
19-
"_sqlite3_column_bytes",
20-
"_sqlite3_column_type",
21-
"_sqlite3_column_name",
22-
"_sqlite3_reset",
23-
"_sqlite3_clear_bindings",
24-
"_sqlite3_finalize",
25-
"_sqlite3_close_v2",
26-
"_sqlite3_create_function_v2",
27-
"_sqlite3_value_bytes",
28-
"_sqlite3_value_type",
29-
"_sqlite3_value_text",
30-
"_sqlite3_value_int",
31-
"_sqlite3_value_blob",
32-
"_sqlite3_value_double",
33-
"_sqlite3_value_int64",
34-
"_sqlite3_result_double",
35-
"_sqlite3_result_null",
36-
"_sqlite3_result_text",
37-
"_sqlite3_result_error",
38-
"_sqlite3_result_int64",
39-
"_sqlite3_malloc",
40-
"_sqlite3_free",
41-
"_RegisterExtensionFunctions"
42-
]
2+
"_malloc",
3+
"_free",
4+
"_sqlite3_open",
5+
"_sqlite3_exec",
6+
"_sqlite3_free",
7+
"_sqlite3_errmsg",
8+
"_sqlite3_prepare_v2",
9+
"_sqlite3_bind_text",
10+
"_sqlite3_bind_blob",
11+
"_sqlite3_bind_double",
12+
"_sqlite3_bind_int",
13+
"_sqlite3_bind_parameter_index",
14+
"_sqlite3_step",
15+
"_sqlite3_data_count",
16+
"_sqlite3_column_double",
17+
"_sqlite3_column_text",
18+
"_sqlite3_column_blob",
19+
"_sqlite3_column_bytes",
20+
"_sqlite3_column_type",
21+
"_sqlite3_column_name",
22+
"_sqlite3_reset",
23+
"_sqlite3_clear_bindings",
24+
"_sqlite3_finalize",
25+
"_sqlite3_close_v2",
26+
"_sqlite3_create_function_v2",
27+
"_sqlite3_value_bytes",
28+
"_sqlite3_value_type",
29+
"_sqlite3_value_text",
30+
"_sqlite3_value_int",
31+
"_sqlite3_value_blob",
32+
"_sqlite3_value_double",
33+
"_sqlite3_result_double",
34+
"_sqlite3_result_null",
35+
"_sqlite3_result_text"
36+
]

extension_exported_functions

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
"_malloc",
3+
"_free",
4+
"_sqlite3_open",
5+
"_sqlite3_exec",
6+
"_sqlite3_free",
7+
"_sqlite3_errmsg",
8+
"_sqlite3_prepare_v2",
9+
"_sqlite3_bind_text",
10+
"_sqlite3_bind_blob",
11+
"_sqlite3_bind_double",
12+
"_sqlite3_bind_int",
13+
"_sqlite3_bind_parameter_index",
14+
"_sqlite3_step",
15+
"_sqlite3_data_count",
16+
"_sqlite3_column_double",
17+
"_sqlite3_column_text",
18+
"_sqlite3_column_blob",
19+
"_sqlite3_column_bytes",
20+
"_sqlite3_column_type",
21+
"_sqlite3_column_name",
22+
"_sqlite3_reset",
23+
"_sqlite3_clear_bindings",
24+
"_sqlite3_finalize",
25+
"_sqlite3_close_v2",
26+
"_sqlite3_create_function_v2",
27+
"_sqlite3_value_bytes",
28+
"_sqlite3_value_type",
29+
"_sqlite3_value_text",
30+
"_sqlite3_value_int",
31+
"_sqlite3_value_blob",
32+
"_sqlite3_value_double",
33+
"_sqlite3_value_int64",
34+
"_sqlite3_result_double",
35+
"_sqlite3_result_null",
36+
"_sqlite3_result_text",
37+
"_sqlite3_result_error",
38+
"_sqlite3_result_int64",
39+
"_sqlite3_malloc",
40+
"_sqlite3_free",
41+
"_RegisterExtensionFunctions"
42+
]

0 commit comments

Comments
 (0)