Skip to content

Commit 2323cf2

Browse files
committed
Minor internal reorganization
1 parent a462da1 commit 2323cf2

File tree

3 files changed

+24
-61
lines changed

3 files changed

+24
-61
lines changed

src/sqlitejs.c

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ static JSValue sqlite_value_to_js (JSContext *ctx, sqlite3_value *value);
5858

5959
#define SAFE_STRCMP(a,b) (((a) != (b)) && ((a) == NULL || (b) == NULL || strcmp((a), (b)) != 0))
6060

61-
#define SQLITEJS_VERSION "1.1.0"
62-
static char gversion[128];
63-
6461
// MARK: - RowSet -
6562

6663
typedef struct {
@@ -305,53 +302,6 @@ static void functionjs_free (functionjs_context *fctx, bool complete) {
305302
}
306303
}
307304

308-
static void compute_version_string (void) {
309-
const char *s1 = SQLITEJS_VERSION;
310-
const char *s2 = JS_GetVersion();
311-
312-
const char *p1 = s1;
313-
const char *p2 = s2;
314-
char *res_ptr = gversion;
315-
bool first_component = true;
316-
317-
while (*p1 != 0 || *p2 != 0) {
318-
// extract a numeric component from each string
319-
int num1 = 0, num2 = 0;
320-
321-
// parse number from s1
322-
while (*p1 != '\0' && isdigit((unsigned char)*p1)) {
323-
num1 = num1 * 10 + (*p1 - '0');
324-
p1++;
325-
}
326-
327-
// parse number from s2
328-
while (*p2 != '\0' && isdigit((unsigned char)*p2)) {
329-
num2 = num2 * 10 + (*p2 - '0');
330-
p2++;
331-
}
332-
333-
// add the numbers
334-
int sum = num1 + num2;
335-
336-
// add a delimiter if this isn't the first component
337-
if (!first_component) {
338-
*res_ptr++ = '.';
339-
} else {
340-
first_component = false;
341-
}
342-
343-
// Convert sum to string and append to result
344-
char temp[32];
345-
sprintf(temp, "%d", sum);
346-
strcpy(res_ptr, temp);
347-
res_ptr += strlen(temp);
348-
349-
// skip delimiters
350-
if (*p1 == '.') p1++;
351-
if (*p2 == '.') p2++;
352-
}
353-
}
354-
355305
// MARK: - Utils -
356306

357307
static JSValue js_sqlite_exec (JSContext *ctx, sqlite3 *db, const char *sql, int argc, JSValueConst *argv) {
@@ -785,8 +735,17 @@ static void js_execute_cleanup (void *xdata) {
785735

786736
// MARK: - Functions -
787737

788-
void js_version (sqlite3_context *context, int argc, sqlite3_value **argv) {
789-
sqlite3_result_text(context, gversion, -1, NULL);
738+
void js_version (sqlite3_context *context, bool internal_engine) {
739+
sqlite3_result_text(context, (internal_engine) ? quickjs_version() : sqlitejs_version(), -1, NULL);
740+
}
741+
742+
void js_version1 (sqlite3_context *context, int argc, sqlite3_value **argv) {
743+
bool internal_engine = (sqlite3_value_int(argv[0]) != 0);
744+
js_version(context, internal_engine);
745+
}
746+
747+
void js_version0 (sqlite3_context *context, int argc, sqlite3_value **argv) {
748+
js_version(context, false);
790749
}
791750

792751
bool js_add_to_table (sqlite3_context *context, const char *type, const char *name, const char *init_code, const char *step_code, const char *final_code, const char *value_code, const char *inverse_code) {
@@ -1098,8 +1057,11 @@ void js_init_table0 (sqlite3_context *context, int argc, sqlite3_value **argv) {
10981057
// MARK: -
10991058

11001059
const char *sqlitejs_version (void) {
1101-
if (gversion[0] == 0) compute_version_string();
1102-
return gversion;
1060+
return SQLITE_JS_VERSION;
1061+
}
1062+
1063+
const char *quickjs_version (void) {
1064+
return JS_GetVersion();
11031065
}
11041066

11051067
APIEXPORT int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
@@ -1110,9 +1072,9 @@ APIEXPORT int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_r
11101072
globaljs_context *data = globaljs_init(db);
11111073
if (!data) return SQLITE_NOMEM;
11121074

1113-
const char *f_name[] = {"js_version", "js_create_scalar", "js_create_aggregate", "js_create_window", "js_create_collation", "js_eval", "js_load_text", "js_load_blob", "js_init_table", "js_init_table"};
1114-
const void *f_ptr[] = {js_version, js_create_scalar, js_create_aggregate, js_create_window, js_create_collation, js_eval, js_load_text, js_load_blob, js_init_table0, js_init_table1};
1115-
int f_arg[] = {0, 2, 4, 6, 2, 1, 1, 1, 0, 1};
1075+
const char *f_name[] = {"js_version", "js_version", "js_create_scalar", "js_create_aggregate", "js_create_window", "js_create_collation", "js_eval", "js_load_text", "js_load_blob", "js_init_table", "js_init_table"};
1076+
const void *f_ptr[] = {js_version0, js_version1, js_create_scalar, js_create_aggregate, js_create_window, js_create_collation, js_eval, js_load_text, js_load_blob, js_init_table0, js_init_table1};
1077+
int f_arg[] = {0, 1, 2, 4, 6, 2, 1, 1, 1, 0, 1};
11161078

11171079
size_t f_count = sizeof(f_name) / sizeof(const char *);
11181080
for (size_t i=0; i<f_count; ++i) {

src/sqlitejs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Created by Marco Bambini on 31/03/25.
66
//
77

8-
#define SQLITE_JS_VERSION "1.1.0"
9-
108
#ifndef __SQLITEJS__
119
#define __SQLITEJS__
1210

@@ -18,7 +16,10 @@
1816
#include "sqlite3.h"
1917
#endif
2018

19+
#define SQLITE_JS_VERSION "1.1.1"
20+
2121
int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
2222
const char *sqlitejs_version (void);
23+
const char *quickjs_version (void);
2324

2425
#endif

test/sqlitejs/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ int test_execution (void) {
158158
// MARK: -
159159

160160
int main (void) {
161-
printf("SQLite-JS version: %s\n\n", sqlitejs_version());
161+
printf("SQLite-JS version: %s (engine: %s)\n\n", sqlitejs_version(), quickjs_version());
162162

163-
int rc = 0;//test_execution();
163+
int rc = test_execution();
164164

165165
rc = test_serialization(DB_PATH, false, 1); // create and execute original implementations
166166
rc = test_serialization(DB_PATH, false, 2); // update functions previously registered in the js_functions table

0 commit comments

Comments
 (0)