MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / CustomFunction

Class CustomFunction

src/util/custom-function.cpp:1–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class CustomFunction : protected DataConverter {
2public:
3
4 explicit CustomFunction(
5 v8::Isolate* isolate,
6 Database* db,
7 const char* name,
8 v8::Local<v8::Function> fn,
9 bool safe_ints
10 ) :
11 name(name),
12 db(db),
13 isolate(isolate),
14 fn(isolate, fn),
15 safe_ints(safe_ints) {}
16
17 virtual ~CustomFunction() {}
18
19 static void xDestroy(void* self) {
20 delete static_cast<CustomFunction*>(self);
21 }
22
23 static void xFunc(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
24 FUNCTION_START();
25
26 v8::Local<v8::Value> args_fast[4];
27 v8::Local<v8::Value>* args = NULL;
28 if (argc != 0) {
29 args = argc <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(argc);
30 Data::GetArgumentsJS(isolate, args, argv, argc, self->safe_ints);
31 }
32
33 v8::MaybeLocal<v8::Value> maybeReturnValue = self->fn.Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), argc, args);
34 if (args != args_fast) delete[] args;
35
36 if (maybeReturnValue.IsEmpty()) self->PropagateJSError(invocation);
37 else Data::ResultValueFromJS(isolate, invocation, maybeReturnValue.ToLocalChecked(), self);
38 }
39
40protected:
41
42 void PropagateJSError(sqlite3_context* invocation) {
43 assert(db->GetState()->was_js_error == false);
44 db->GetState()->was_js_error = true;
45 sqlite3_result_error(invocation, "", 0);
46 }
47
48 std::string GetDataErrorPrefix() {
49 return std::string("User-defined function ") + name + "() returned";
50 }
51
52private:
53 const std::string name;
54 Database* const db;
55protected:
56 v8::Isolate* const isolate;
57 const v8::Global<v8::Function> fn;
58 const bool safe_ints;
59};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected