UniSet 2.45.1
JSHelpers.h
1/*
2 * Copyright (c) 2025 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// --------------------------------------------------------------------------
17#ifndef JSEngine_H_
18// --------------------------------------------------------------------------
19#include <string>
20#include <vector>
21#include <unordered_map>
22#include <set>
23#include <functional>
24#include "quickjs/quickjs.h"
25// --------------------------------------------------------------------------
26namespace uniset
27{
28 // ----------------------------------------------------------------------
29 namespace jshelper
30 {
31 struct IOProp
32 {
33 std::string name;
34 std::string sensor;
35 };
36
37 IOProp convert_js_to_io_prop(JSContext* ctx, JSValueConst obj_val );
38 void dump_exception_details( JSContext* ctx );
39
40 void safe_function_call(JSContext* ctx, JSValueConst obj, JSValueConst func,
41 int argc = 0, JSValueConst* argv = nullptr );
42 void safe_function_call_by_name(JSContext* ctx, const char* func_name,
43 int argc = 0, JSValue* argv = nullptr );
44
45 void debug_function_call( JSContext* ctx, const char* func_name );
46
48 {
49 std::vector<std::string> search_paths;
50 };
51
52 const std::string js_search_paths_object = "__load_search_paths";
53 // ----------------------------------------------------------------------
54 struct JsArgs
55 {
56 JSContext* ctx;
57 std::vector<JSValue > vals;
58
59 explicit JsArgs(JSContext* c) : ctx(c) {}
60 ~JsArgs()
61 {
62 for (auto& v : vals)
63 JS_FreeValue(ctx, v);
64 }
65
66 // Добавить аргументы
67 JsArgs& i64(int64_t x)
68 {
69 vals.push_back(JS_NewInt64(ctx, x));
70 return *this;
71 }
72
73 JsArgs& str(const char* s)
74 {
75 vals.push_back(JS_NewString(ctx, s));
76 return *this;
77 }
78
79 int size() const
80 {
81 return (int)vals.size();
82 }
83
84 // Важно: возвращаем JSValueConst* !
85 JSValueConst* data()
86 {
87 return vals.data();
88 }
89 };
90
91 // ----------------------------------------------------------------------
92 JSModuleDef* module_loader_with_path( JSContext* ctx, const char* module_name, void* opaque );
93 JSValue js_load_file_with_data( JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv );
94 JSValue js_read_text_file( JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv );
95 std::string find_file( const std::string filename, const std::vector<std::string>& search_paths );
96 JSModuleDef* qjs_module_loader(JSContext* ctx, const char* module_name, void* opaque);
97 char* qjs_module_normalize(JSContext* ctx, const char* base_name, const char* name, void* opaque);
98 // ----------------------------------------------------------------------
99 class JSValueGuard
100 {
101 JSContext* ctx;
102 JSValue value;
103 public:
104 JSValueGuard(JSContext* ctx, JSValue val) : ctx(ctx), value(val) {}
105 ~JSValueGuard()
106 {
107 if( ctx && !JS_IsUndefined(value) && !JS_IsNull(value) )
108 JS_FreeValue(ctx, value);
109 }
110 JSValue get() const
111 {
112 return value;
113 }
114
115 JSValueGuard(const JSValueGuard&) = delete;
116 JSValueGuard& operator=(const JSValueGuard&) = delete;
117 };
118
119 // ----------------------------------------------------------------------
120 template<typename T>
121 std::vector<T> js_array_to_vector(JSContext* ctx, JSValueConst array_val,
122 std::function<T(JSContext*, JSValue)> converter)
123 {
124 std::vector<T> result;
125
126 if (!JS_IsArray(ctx, array_val))
127 {
128 return result;
129 }
130
131 JSValue length_val = JS_GetPropertyStr(ctx, array_val, "length");
132 int length;
133 JS_ToInt32(ctx, &length, length_val);
134 JS_FreeValue(ctx, length_val);
135
136 for( int i = 0; i < length; i++ )
137 {
138 JSValue item_val = JS_GetPropertyUint32(ctx, array_val, i);
139 T item = converter(ctx, item_val);
140 result.push_back(item);
141 JS_FreeValue(ctx, item_val);
142 }
143
144 return result;
145 }
146 // ----------------------------------------------------------------------
148 {
149 private:
150 static std::unordered_map<JSRuntime*, std::set<std::string>> loaded_modules;
151
152 public:
153 static void mark_module_loaded(JSRuntime* rt, const std::string& module_name);
154 static bool is_module_loaded(JSRuntime* rt, const std::string& module_name);
155 static std::vector<std::string> get_loaded_modules(JSRuntime* rt);
156 };
157
158 bool is_module_loaded(JSContext* ctx, const std::string& module_name);
159
160 }
161 // ----------------------------------------------------------------------
162} // end of namespace uniset
163// --------------------------------------------------------------------------
164#endif
Определения JSHelpers.h:148
Определения Calibration.h:27
Определения JSHelpers.h:32
Определения JSHelpers.h:48