| 1 | class CustomFunction : protected DataConverter { |
| 2 | public: |
| 3 | |
| 4 | explicit CustomFunction( |
| 5 | Napi::Env env, |
| 6 | Database* db, |
| 7 | const char* name, |
| 8 | Napi::Function fn, |
| 9 | bool safe_ints |
| 10 | ) : |
| 11 | name(name), |
| 12 | db(db), |
| 13 | env(env), |
| 14 | fn(Napi::Persistent(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 | napi_value args_fast[4]; |
| 27 | napi_value* args = NULL; |
| 28 | if (argc != 0) { |
| 29 | args = argc <= 4 ? args_fast : ALLOC_ARRAY<napi_value>(argc); |
| 30 | Data::GetArgumentsJS(env, args, argv, argc, self->safe_ints); |
| 31 | } |
| 32 | |
| 33 | Napi::Value returnValue = SafeCall(env, self->fn.Value(), env.Undefined(), argc, args); |
| 34 | if (args != args_fast) delete[] args; |
| 35 | |
| 36 | if (env.IsExceptionPending()) self->PropagateJSError(invocation); |
| 37 | else Data::ResultValueFromJS(env, invocation, returnValue, self); |
| 38 | } |
| 39 | |
| 40 | protected: |
| 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 | |
| 52 | private: |
| 53 | const std::string name; |
| 54 | Database* const db; |
| 55 | protected: |
| 56 | const Napi::Env env; |
| 57 | const Napi::FunctionReference fn; |
| 58 | const bool safe_ints; |
| 59 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected