UniSet @VERSION@
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 std::string find_file( const std::string filename, const std::vector<std::string>& search_paths );
95 JSModuleDef* qjs_module_loader(JSContext* ctx, const char* module_name, void* opaque);
96 char* qjs_module_normalize(JSContext* ctx, const char* base_name, const char* name, void* opaque);
97 // ----------------------------------------------------------------------
99 {
100 JSContext* ctx;
101 JSValue value;
102 public:
103 JSValueGuard(JSContext* ctx, JSValue val) : ctx(ctx), value(val) {}
105 {
106 if( ctx && !JS_IsUndefined(value) && !JS_IsNull(value) )
107 JS_FreeValue(ctx, value);
108 }
109 JSValue get() const
110 {
111 return value;
112 }
113
114 JSValueGuard(const JSValueGuard&) = delete;
115 JSValueGuard& operator=(const JSValueGuard&) = delete;
116 };
117
118 // ----------------------------------------------------------------------
119 template<typename T>
120 std::vector<T> js_array_to_vector(JSContext* ctx, JSValueConst array_val,
121 std::function<T(JSContext*, JSValue)> converter)
122 {
123 std::vector<T> result;
124
125 if (!JS_IsArray(ctx, array_val))
126 {
127 return result;
128 }
129
130 JSValue length_val = JS_GetPropertyStr(ctx, array_val, "length");
131 int length;
132 JS_ToInt32(ctx, &length, length_val);
133 JS_FreeValue(ctx, length_val);
134
135 for( int i = 0; i < length; i++ )
136 {
137 JSValue item_val = JS_GetPropertyUint32(ctx, array_val, i);
138 T item = converter(ctx, item_val);
139 result.push_back(item);
140 JS_FreeValue(ctx, item_val);
141 }
142
143 return result;
144 }
145 // ----------------------------------------------------------------------
147 {
148 private:
149 static std::unordered_map<JSRuntime*, std::set<std::string>> loaded_modules;
150
151 public:
152 static void mark_module_loaded(JSRuntime* rt, const std::string& module_name);
153 static bool is_module_loaded(JSRuntime* rt, const std::string& module_name);
154 static std::vector<std::string> get_loaded_modules(JSRuntime* rt);
155 };
156
157 bool is_module_loaded(JSContext* ctx, const std::string& module_name);
158
159 }
160 // ----------------------------------------------------------------------
161} // end of namespace uniset
162// --------------------------------------------------------------------------
163#endif
Definition JSHelpers.h:99
Definition JSHelpers.h:147
Definition AccessConfig.h:30
Definition JSHelpers.h:32
Definition JSHelpers.h:48
Definition JSHelpers.h:55