MySQL 9.5.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 is_idle();
116 bool wait_for_idle();
117
118 size_t id() { return m_id; }
119
120 private:
121 void run();
122 void stop_run_thread();
123
124 Value native_object(poly_value object);
125 Value native_array(poly_value object);
126 Value to_native_object(poly_value object,
127 const std::string &class_name) override;
128 void output_handler(const char *bytes, size_t length) override;
129 void error_handler(const char *bytes, size_t length) override;
130 poly_value from_native_object(const Object_bridge_t &object) const override;
131
134
135 // Every global function exposed to JavaScript requires:
136 // - The function implementation
137 // - The function metadata
138 poly_value synch_return(const std::vector<poly_value> &args);
140 static const constexpr char *name = "synch_return";
141 static const constexpr std::size_t argc = 1;
142 static const constexpr auto callback = &JavaScript::synch_return;
143 };
144
145 poly_value synch_error(const std::vector<poly_value> &args);
146 struct Synch_error {
147 static const constexpr char *name = "synch_error";
148 static const constexpr std::size_t argc = 1;
149 static const constexpr auto callback = &JavaScript::synch_error;
150 };
151
152 void resolve_promise(poly_value promise);
153 shcore::Value get_session(const std::vector<shcore::Value> &args);
154
155 struct Get_session {
156 static const constexpr char *name = "getSession";
157 static const constexpr std::size_t argc = 1;
158 static const constexpr auto callback = &JavaScript::get_session;
159 };
160
161 poly_value get_current_mrs_user_id();
163 static const constexpr char *name = "getCurrentMrsUserId";
164 static const constexpr auto callback = &JavaScript::get_current_mrs_user_id;
165 };
166
167 shcore::Value get_content_set_path(const std::vector<shcore::Value> &args);
169 static const constexpr char *name = "getContentSetPath";
170 static const constexpr std::size_t argc = 1;
171 static const constexpr auto callback = &JavaScript::get_content_set_path;
172 };
173
175
176 // To control the statement execution, the execution thread will be in
177 // wait state until a statement arrives
178 std::unique_ptr<std::thread> m_execution_thread;
179
181
184
187
189 std::shared_ptr<shcore::polyglot::Session> m_session;
190
191 std::optional<ProcessingState> m_processing_state;
192 std::condition_variable m_processing_state_condition;
194 size_t m_id = 0;
195};
196
197} // namespace jit_executor
198
199#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:186
std::unique_ptr< std::thread > m_execution_thread
Definition: jit_executor_javascript.h:178
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:435
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:192
mysql_harness::WaitingMPSCQueue< Result > m_result
Definition: jit_executor_javascript.h:183
bool wait_for_idle()
Definition: jit_executor_javascript.cc:356
std::optional< ProcessingState > m_processing_state
Definition: jit_executor_javascript.h:191
mysql_harness::WaitingMPSCQueue< std::variant< std::monostate, Code > > m_code
Definition: jit_executor_javascript.h:182
std::mutex m_processing_state_mutex
Definition: jit_executor_javascript.h:193
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:394
~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:418
const GlobalCallbacks * m_global_callbacks
Definition: jit_executor_javascript.h:188
std::string execute(const std::string &code, int timeout, ResultType result_type, const GlobalCallbacks &callbacks)
Definition: jit_executor_javascript.cc:534
poly_value get_current_mrs_user_id()
Definition: jit_executor_javascript.cc:661
void error_handler(const char *bytes, size_t length) override
Definition: jit_executor_javascript.cc:472
void resolve_promise(poly_value promise)
Definition: jit_executor_javascript.cc:634
std::shared_ptr< shcore::polyglot::Session > m_session
Definition: jit_executor_javascript.h:189
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:468
poly_value from_native_object(const Object_bridge_t &object) const override
Definition: jit_executor_javascript.cc:476
size_t m_id
Definition: jit_executor_javascript.h:194
ResultType m_result_type
Definition: jit_executor_javascript.h:185
shcore::Value get_session(const std::vector< shcore::Value > &args)
Definition: jit_executor_javascript.cc:644
bool is_idle()
Definition: jit_executor_javascript.cc:390
Dictionary_t m_predefined_globals
Definition: jit_executor_javascript.h:180
poly_value synch_return(const std::vector< poly_value > &args)
Definition: jit_executor_javascript.cc:597
poly_value synch_error(const std::vector< poly_value > &args)
Definition: jit_executor_javascript.cc:622
size_t id()
Definition: jit_executor_javascript.h:118
void set_processing_state(ProcessingState state)
Definition: jit_executor_javascript.cc:344
shcore::Value get_content_set_path(const std::vector< shcore::Value > &args)
Definition: jit_executor_javascript.cc:676
std::string get_parameter_string(const std::vector< Value > &parameters) const
Definition: jit_executor_javascript.cc:506
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:168
static constexpr const char * name
Definition: jit_executor_javascript.h:169
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:170
static constexpr const auto callback
Definition: jit_executor_javascript.h:171
Definition: jit_executor_javascript.h:162
static constexpr const auto callback
Definition: jit_executor_javascript.h:164
static constexpr const char * name
Definition: jit_executor_javascript.h:163
Definition: jit_executor_javascript.h:155
static constexpr const char * name
Definition: jit_executor_javascript.h:156
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:157
static constexpr const auto callback
Definition: jit_executor_javascript.h:158
Definition: jit_executor_javascript.h:146
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:148
static constexpr const char * name
Definition: jit_executor_javascript.h:147
static constexpr const auto callback
Definition: jit_executor_javascript.h:149
Definition: jit_executor_javascript.h:139
static constexpr const std::size_t argc
Definition: jit_executor_javascript.h:141
static constexpr const auto callback
Definition: jit_executor_javascript.h:142
static constexpr const char * name
Definition: jit_executor_javascript.h:140
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