summaryrefslogtreecommitdiff
path: root/src/napi-sqlite.c
blob: df3b042088a6bd82b6178b1c106ff56fa10b94a7 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>

#include <node/node_api.h>

/*
FIXME
static const napi_type_tag SQLITE_DB_TYPE_TAG = {
	0x0e9614d459f746cc, 0x88b814a5dc5c4cf7
};
*/

static napi_value
myfn(napi_env env, napi_callback_info info) {
	napi_value ret = NULL;

	napi_status status;
	size_t argc = 1;
	int number;
	napi_value argv[1];
	napi_value my_number;

	status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
	if (status != napi_ok) {
		napi_throw_error(env, NULL, "Failed to parse arguments FIXME i18n");
		goto out;
	}

	status = napi_get_value_int32(env, argv[0], &number);
	if (status != napi_ok) {
		napi_throw_error(env, NULL, "Invalid number was passed as argument FIXME i18n");
		goto out;
	}
	number = number * 2;

	status = napi_create_int32(env, number, &my_number);
	if (status != napi_ok) {
		napi_throw_error(env, NULL, "Unable to create return value FIXME i18n");
		goto out;
	}
	ret = my_number;

out:
	return ret;
}

static napi_value
open(napi_env env, napi_callback_info info) {
	(void)env;
	(void)info;
	return NULL;
}

static napi_value
close(napi_env env, napi_callback_info info) {
	(void)env;
	(void)info;
	return NULL;
}

static const struct {
	const char *label;
	napi_value(*const handle)(napi_env env, napi_callback_info info);
} fns[] = {
	{ .label = "myfn",  .handle = myfn,  },
	{ .label = "open",  .handle = open,  },
	{ .label = "close", .handle = close, },
	{ NULL, NULL },
};

static napi_value
init(napi_env env, napi_value exports) {
	napi_value ret = exports;

	napi_status status;

	for (size_t i = 0; fns[i].label && fns[i].handle; i++) {
		napi_value fn;
		status = napi_create_function(
			env,
			fns[i].label,
			NAPI_AUTO_LENGTH,
			fns[i].handle,
			"xucrutes",
			&fn
		);
		if (status != napi_ok) {
			ret = NULL;
			napi_throw_error(
				env,
				"SQLITE_FN_CREATE",
				"Unable to wrap native function FIXME i18n"
			);
			goto out;
		}

		status = napi_set_named_property(
			env,
			exports,
			fns[i].label,
			fn
		);
		if (status != napi_ok) {
			ret = NULL;
			napi_throw_error(
				env,
				"SQLITE_FN_SETNAME",
				"Unable to populate exports FIXME i18n"
			);
			goto out;
		}
	}

out:
	return ret;
}


NAPI_MODULE_INIT() {
	return init(env, exports);
}