MySQL 9.3.0
Source Code Documentation
jit_executor_javascript.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_JIT_EXECUTOR_SRC_JIT_EXECUTOR_JAVASCRIPT_H_
27#define ROUTER_SRC_JIT_EXECUTOR_SRC_JIT_EXECUTOR_JAVASCRIPT_H_
28
29#include <condition_variable>
30#include <memory> // shared_ptr
31#include <mutex>
32#include <queue>
33#include <string>
34#include <thread>
35#include <unordered_map>
36#include <variant>
37#include <vector>
38
48
49namespace jit_executor {
50
56
57// To be used to determine the actual state of the produced result
59
60// To be used to determine the processing state
62
63struct Result {
64 std::optional<ResultState> state;
65 std::optional<std::string> data;
66
67 void reset() {
68 state.reset();
69 data.reset();
70 }
71};
72
73struct Code {
74 std::string source;
76};
77
78/**
79 * MRS JavaScript Implementation
80 *
81 * Starts the JavaScript engine in a thread for execution of code from the
82 * MRS end points. A threaded version is needed to support the JavaScript
83 * Promise resolution to get the final result.
84 *
85 * To achieve these two global functions are exposed: synch_return and
86 * synch_error, such function would be used on the promise resolution by
87 * executing:
88 * promise.then(value => synch_return(value), error=>synch_error(err0r))
89 */
91 public:
92 using Java_script_interface::Java_script_interface;
93 ~JavaScript() override = default;
94
95 bool start(size_t id, const std::shared_ptr<IFile_system> &fs = {},
96 const Dictionary_t &predefined_globals = {});
97 void stop();
98
99 std::string execute(const std::string &code, int timeout,
100 ResultType result_type, const GlobalCallbacks &callbacks);
101
102 std::string get_parameter_string(const std::vector<Value> &parameters) const;
103
104 /**
105 * Wraps a call to poly_context_eval
106 */
107 int64_t eval(poly_reference source, poly_value *result) const;
108
109 /**
110 * Creates a Source object
111 */
112 poly_value create_source(const std::string &source,
113 const std::string &code_str) const;
114
115 bool wait_for_idle();
116
117 size_t id() { return m_id; }
118
119 private:
120 void run();
121 void stop_run_thread();
122
123 Value native_object(poly_value object);
124 Value native_array(poly_value object);
125 Value to_native_object(poly_value object,
126 const std::string &class_name) override;
127 void output_handler(const char *bytes, size_t length) override;
128 void error_handler(const char *bytes, size_t length) override;
129 poly_value from_native_object(const Object_bridge_t &object) const override;
130
133
134 // Every global function exposed to JavaScript requires:
135 // - The function implementation
136 // - The function metadata
137 poly_value synch_return(const std::vector<poly_value> &args);
139 static const constexpr char *name = "synch_return";
140 static const constexpr std::size_t argc = 1;
141 static const constexpr auto callback = &JavaScript::synch_return;
142 };
143
144 poly_value synch_error(const std::vector<poly_value> &args);
145 struct Synch_error {
146 static const constexpr char *name = "synch_error";
147 static const constexpr std::size_t argc = 1;
148 static const constexpr auto callback = &JavaScript::synch_error;
149 };
150
151 void resolve_promise(poly_value promise);
152 shcore::Value get_session(const std::vector<shcore::Value> &args);
153
154 struct Get_session {
155 static const constexpr char *name = "getSession";
156 static const constexpr std::size_t argc = 1;
157 static const constexpr auto callback = &JavaScript::get_session;
158 };
159
160 poly_value get_current_mrs_user_id();
162 static const constexpr char *name = "getCurrentMrsUserId";
163 static const constexpr auto callback = &JavaScript::get_current_mrs_user_id;
164 };
165
166 shcore::Value get_content_set_path(const std::vector<shcore::Value> &args);
168 static const constexpr char *name = "getContentSetPath";
169 static const constexpr std::size_t argc = 1;
170 static const constexpr auto callback = &JavaScript::get_content_set_path;
171 };
172
174
175 // To control the statement execution, the execution thread will be in
176 // wait state until a statement arrives
177 std::unique_ptr<std::thread> m_execution_thread;
178
180
183
186
188 std::shared_ptr<shcore::polyglot::Session> m_session;
189
190 std::optional<ProcessingState> m_processing_state;
191 std::condition_variable m_processing_state_condition;
193 size_t m_id = 0;
194};
195
196} // namespace jit_executor
197
198#endif // ROUTER_SRC_JIT_EXECUTOR_SRC_JIT_EXECUTOR_JAVASCRIPT_H_
MRS JavaScript Implementation.
Definition: jit_executor_javascript.h:90
poly_value m_promise_resolver
Definition: jit_executor_javascript.h:185
std::unique_ptr< std::thread > m_execution_thread
Definition: jit_executor_javascript.h:177
void stop_run_thread()
Definition: jit_executor_javascript.cc:172
Value to_native_object(poly_value object, const std::string &class_name) override
Converts a guest language object into its C++ representation.
Definition: jit_executor_javascript.cc:410
void stop()
Definition: jit_executor_javascript.cc:177
poly_value create_source(const std::string &source, const std::string &code_str) const
Creates a Source object.
Definition: jit_executor_javascript.cc:189
std::condition_variable m_processing_state_condition
Definition: jit_executor_javascript.h:191
mysql_harness::WaitingMPSCQueue< Result > m_result
Definition: jit_executor_javascript.h:182
bool wait_for_idle()
Definition: jit_executor_javascript.cc:344
std::optional< ProcessingState > m_processing_state
Definition: jit_executor_javascript.h:190
mysql_harness::WaitingMPSCQueue< std::variant< std::monostate, Code > > m_code
Definition: jit_executor_javascript.h:181
std::mutex m_processing_state_mutex
Definition: jit_executor_javascript.h:192
void create_result(const Value &result, ResultState state=ResultState::Ok)
Definition: jit_executor_javascript.cc:60
Value native_array(poly_value object)
Definition: jit_executor_javascript.cc:369
~JavaScript() override=default
bool start(size_t id, const std::shared_ptr< IFile_system > &fs={}, const Dictionary_t &predefined_globals={})
Definition: jit_executor_javascript.cc:152
Value native_object(poly_value object)
Definition: jit_executor_javascript.cc:393
const GlobalCallbacks * m_global_callbacks
Definition: jit_executor_javascript.h:187
std::string execute(const std::string &code, int timeout, ResultType result_type, const GlobalCallbacks &callbacks)
Definition: jit_executor_javascript.cc:509
poly_value get_current_mrs_user_id()
Definition: jit_executor_javascript.cc:628
void error_handler(const char *bytes, size_t length) override
Definition: jit_executor_javascript.cc:447
void resolve_promise(poly_value promise)
Definition: jit_executor_javascript.cc:601
std::shared_ptr< shcore::polyglot::Session > m_session
Definition: jit_executor_javascript.h:188
void run()
Definition: jit_executor_javascript.cc:206
int64_t eval(poly_reference source, poly_value *result) const
Wraps a call to poly_context_eval.
Definition: jit_executor_javascript.cc:185
void output_handler(const char *bytes, size_t length) override
Definition: jit_executor_javascript.cc:443
poly_value from_native_object(const Object_bridge_t &object) const override
Definition: jit_executor_javascript.cc:451
size_t m_id
Definition: jit_executor_javascript.h:193
ResultType m_result_type
Definition: jit_executor_javascript.h:184
shcore::Value get_session(const std::vector< shcore::Value > &args)
Definition: jit_executor_javascript.cc:611
Dictionary_t m_predefined_globals
Definition: jit_executor_javascript.h:179
poly_value synch_return(const std::vector< poly_value > &args)
Definition: jit_executor_javascript.cc:564
poly_value synch_error(const std::vector< poly_value > &args)
Definition: jit_executor_javascript.cc:589
size_t id()
Definition: jit_executor_javascript.h:117
void set_processing_state(ProcessingState state)
Definition: jit_executor_javascript.cc:334
shcore::Value get_content_set_path(const std::vector< shcore::Value > &args)
Definition: jit_executor_javascript.cc:643
std::string get_parameter_string(const std::vector< Value > &parameters) const
Definition: jit_executor_javascript.cc:481
provide waiting pop and push operator to thread-safe queues.
Definition: waiting_queue_adaptor.h:40
Definition: polyglot_file_system.h:185
Definition: polyglot_javascript.h:43
Represents polyglot errors that will be created from information available in the polyglot library st...
Definition: polyglot_error.h:77
ResultType
Type used to differentiate the three cases that can happen when parsing a geometry.
Definition: geometry_extraction.h:42
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: jit_executor_callbacks.h:36
shcore::polyglot::IFile_system IFile_system
Definition: jit_executor_javascript.h:54
ProcessingState
Definition: jit_executor_javascript.h:61
shcore::Dictionary_t Dictionary_t
Definition: jit_executor_javascript.h:52
ResultState
Definition: jit_executor_javascript.h:58
shcore::polyglot::Polyglot_error Polyglot_error
Definition: jit_executor_javascript.h:53
ResultType
Definition: jit_executor_common.h:32
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
std::shared_ptr< Object_bridge > Object_bridge_t
Definition: polyglot_object_bridge.h:89
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:430
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
Definition: jit_executor_javascript.h:73
ResultType result_type
Definition: jit_executor_javascript.h:75
std::string source
Definition: jit_executor_javascript.h:74
Definition: jit_executor_callbacks.h:38
Definition: jit_executor_javascript.h:167
static constexpr const char * name
Definition: jit_executor_javascript.h:168
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:169
static constexpr const auto callback
Definition: jit_executor_javascript.h:170
Definition: jit_executor_javascript.h:161
static constexpr const auto callback
Definition: jit_executor_javascript.h:163
static constexpr const char * name
Definition: jit_executor_javascript.h:162
Definition: jit_executor_javascript.h:154
static constexpr const char * name
Definition: jit_executor_javascript.h:155
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:156
static constexpr const auto callback
Definition: jit_executor_javascript.h:157
Definition: jit_executor_javascript.h:145
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:147
static constexpr const char * name
Definition: jit_executor_javascript.h:146
static constexpr const auto callback
Definition: jit_executor_javascript.h:148
Definition: jit_executor_javascript.h:138
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:140
static constexpr const auto callback
Definition: jit_executor_javascript.h:141
static constexpr const char * name
Definition: jit_executor_javascript.h:139
Definition: jit_executor_javascript.h:63
std::optional< std::string > data
Definition: jit_executor_javascript.h:65
void reset()
Definition: jit_executor_javascript.h:67
std::optional< ResultState > state
Definition: jit_executor_javascript.h:64
Definition: result.h:30
Pointer to a function that may be implemented in any language.
Definition: jit_executor_value.h:130