moduforge-rules-engine 0.5.0

moduforge 引擎规则
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! # 自定义函数监听器模块
//!
//! 本模块实现了CustomListener,用于在JavaScript运行时环境中注册和管理自定义函数。
//!
//! ## 主要功能
//!
//! - **函数注册**: 在运行时启动时自动将Rust自定义函数注册到JavaScript的md命名空间
//! - **命名空间管理**: 创建和管理md作用域,避免全局命名冲突
//! - **类型转换**: 处理Rust和JavaScript之间的数据类型转换
//! - **异步支持**: 提供异步函数调用支持,确保不阻塞JavaScript执行
//! - **错误处理**: 完善的错误捕获和处理机制
//!
//! ## 使用场景
//!
//! 该监听器主要用于规则引擎中,允许在规则表达式中通过`md.functionName()`的形式
//! 调用预定义的Rust函数,从而扩展JavaScript运行时的功能。
//!
//! ## 架构说明
//!
//! ```text
//! MfFunctionRegistry → CustomListener → JavaScript Runtime (md namespace)
//!        ↓                      ↓                    ↓
//!    函数定义存储           函数注册处理          md.functionName() 调用执行
//! ```

use std::future::Future;
use std::pin::Pin;
use crate::handler::function::error::{FunctionResult, ResultExt};
use crate::handler::function::listener::{RuntimeEvent, RuntimeListener};
use crate::handler::function::module::export_default;
use crate::handler::function::serde::JsValue;
use mf_expression::{MfFunctionRegistry, functions::arguments::Arguments};
use rquickjs::module::{Declarations, Exports, ModuleDef};
use rquickjs::prelude::{Async, Func};
use rquickjs::{CatchResultExt, Ctx};

/// 自定义函数监听器
///
/// 该监听器负责在JavaScript运行时启动时,将所有注册的自定义函数
/// 绑定到JavaScript的md命名空间中,使得这些函数可以在规则表达式中通过
/// `md.functionName()`的形式被调用
///
/// # 工作流程
/// 1. 监听运行时启动事件
/// 2. 创建或获取md命名空间对象
/// 3. 从CustomFunctionRegistry获取所有已注册的函数
/// 4. 将每个函数包装为异步JavaScript函数
/// 5. 注册到JavaScript的md命名空间中
pub struct ModuforgeListener {
    // 目前为空结构体,后续可以添加配置或状态字段
}

impl RuntimeListener for ModuforgeListener {
    /// 处理运行时事件的核心方法
    ///
    /// # 参数
    /// - `ctx`: QuickJS上下文,用于操作JavaScript环境
    /// - `event`: 运行时事件类型
    ///
    /// # 返回值
    /// 返回一个异步Future,包含操作结果
    fn on_event<'js>(
        &self,
        ctx: Ctx<'js>,
        event: RuntimeEvent,
    ) -> Pin<Box<dyn Future<Output = FunctionResult> + 'js>> {
        Box::pin(async move {
            // 只在运行时启动事件时执行函数注册
            if event != RuntimeEvent::Startup {
                return Ok(());
            };

            // 设置全局函数及变量
            // 创建或获取 md 命名空间对象
            let md_namespace = if ctx.globals().contains_key("md")? {
                // 如果 md 已存在,获取它
                ctx.globals().get("md")?
            } else {
                // 如果 md 不存在,创建一个新的空对象
                let md_obj = rquickjs::Object::new(ctx.clone())?;
                ctx.globals().set("md", md_obj.clone())?;
                md_obj
            };

            // 从自定义函数注册表中获取所有函数名称
            let functions_keys = MfFunctionRegistry::list_functions();

            // 遍历每个注册的函数
            for function_key in functions_keys {
                // 根据函数名获取函数定义
                let function_definition =
                    MfFunctionRegistry::get_definition(&function_key);

                if let Some(function_definition) = function_definition {
                    // 将Rust函数包装为JavaScript异步函数并注册到md命名空间下

                    let function_definition = function_definition.clone();
                    let parameters = function_definition.required_parameters();
                    match parameters {
                        0 => {
                            md_namespace
                                .set(
                                    function_key, // 函数名作为md对象的属性名
                                    Func::from(Async(move |ctx: Ctx<'js>| {
                                        // 克隆函数定义以避免生命周期问题
                                        let function_definition =
                                            function_definition.clone();

                                        async move {
                                            // 调用Rust函数,传入JavaScript参数
                                            let response = function_definition
                                                .call(Arguments(&[]))
                                                .or_throw(&ctx)?;

                                            // 将Rust函数的返回值序列化为JSON,再转换为JavaScript值
                                            let k =
                                                serde_json::to_value(response)
                                                    .or_throw(&ctx)?
                                                    .into();

                                            return rquickjs::Result::Ok(
                                                JsValue(k),
                                            );
                                        }
                                    })),
                                )
                                .catch(&ctx)?; // 捕获并处理可能的JavaScript异常
                        },
                        1 => {
                            md_namespace
                            .set(
                                function_key, // 函数名作为md对象的属性名
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, context: JsValue| {
                                        // 克隆函数定义以避免生命周期问题
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            // 调用Rust函数,传入JavaScript参数
                                            let response = function_definition
                                                .call(Arguments(&[context.0]))
                                                .or_throw(&ctx)?;
                                            // 将Rust函数的返回值序列化为JSON,再转换为JavaScript值
                                            let k = serde_json::to_value(response)
                                                .or_throw(&ctx)?
                                                .into();
                                            return rquickjs::Result::Ok(JsValue(
                                                k,
                                            ));
                                        }
                                    },
                                )),
                            )
                            .catch(&ctx)?; // 捕获并处理可能的JavaScript异常
                        },
                        2 => {
                            md_namespace
                            .set(
                                function_key, // 函数名作为md对象的属性名
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, context: JsValue,context2: JsValue| {
                                        // 克隆函数定义以避免生命周期问题
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            // 调用Rust函数,传入JavaScript参数
                                            let response = function_definition
                                                .call(Arguments(&[context.0,context2.0]))
                                                .or_throw(&ctx)?;
                                            // 将Rust函数的返回值序列化为JSON,再转换为JavaScript值
                                            let k = serde_json::to_value(response)
                                                .or_throw(&ctx)?
                                                .into();
                                            return rquickjs::Result::Ok(JsValue(
                                                k,
                                            ));
                                        }
                                    },
                                )),
                            )
                            .catch(&ctx)?; // 捕获并处理可能的JavaScript异常
                        },
                        3 => {
                            md_namespace
                            .set(
                                function_key, // 函数名作为md对象的属性名
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, context: JsValue,context2: JsValue,context3: JsValue| {
                                        // 克隆函数定义以避免生命周期问题
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            // 调用Rust函数,传入JavaScript参数
                                            let response = function_definition
                                                .call(Arguments(&[context.0,context2.0,context3.0]))
                                                .or_throw(&ctx)?;
                                            // 将Rust函数的返回值序列化为JSON,再转换为JavaScript值
                                            let k: mf_expression::Variable = serde_json::to_value(response)
                                                .or_throw(&ctx)?
                                                .into();
                                            return rquickjs::Result::Ok(JsValue(
                                                k,
                                            ));
                                        }
                                    },
                                )),
                            )
                            .catch(&ctx)?; // 捕获并处理可能的JavaScript异常
                        },
                        _ => {
                            md_namespace
                            .set(
                                function_key, // 函数名作为md对象的属性名
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, context: Vec<JsValue>| {
                                        // 克隆函数定义以避免生命周期问题
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            // 调用Rust函数,传入JavaScript参数
                                            let response = function_definition
                                                .call(Arguments(&context.iter().map(|arg| arg.0.clone()).collect::<Vec<_>>()))
                                                .or_throw(&ctx)?;
                                            // 将Rust函数的返回值序列化为JSON,再转换为JavaScript值
                                            let k = serde_json::to_value(response)
                                                .or_throw(&ctx)?
                                                .into();
                                            return rquickjs::Result::Ok(JsValue(
                                                k,
                                            ));
                                        }
                                    },
                                )),
                            )
                            .catch(&ctx)?; // 捕获并处理可能的JavaScript异常
                        },
                    }
                }
            }

            Ok(()) // 成功完成函数注册
        })
    }
}

pub struct ModuforgeModule;

impl ModuleDef for ModuforgeModule {
    fn declare<'js>(decl: &Declarations<'js>) -> rquickjs::Result<()> {
        // 声明所有可用的函数
        for function_key in MfFunctionRegistry::list_functions() {
            decl.declare(function_key.as_str())?;
        }
        decl.declare("default")?;
        Ok(())
    }

    fn evaluate<'js>(
        ctx: &Ctx<'js>,
        exports: &Exports<'js>,
    ) -> rquickjs::Result<()> {
        export_default(ctx, exports, |default| {
            // 为每个函数创建对应的异步函数
            for function_key in MfFunctionRegistry::list_functions() {
                if let Some(function_definition) =
                    MfFunctionRegistry::get_definition(&function_key)
                {
                    let function_definition = function_definition.clone();
                    let parameters = function_definition.required_parameters();
                    match parameters {
                        0 => {
                            default.set(
                                &function_key,
                                Func::from(Async(move |ctx: Ctx<'js>| {
                                    let function_definition =
                                        function_definition.clone();
                                    async move {
                                        let response = function_definition
                                            .call(Arguments(&[]))
                                            .or_throw(&ctx)?;

                                        let result =
                                            serde_json::to_value(response)
                                                .or_throw(&ctx)?
                                                .into();

                                        Ok::<JsValue, rquickjs::Error>(JsValue(
                                            result,
                                        ))
                                    }
                                })),
                            )?;
                        },
                        1 => {
                            //只有一个参数
                            default.set(
                                &function_key,
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, args: JsValue| {
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            let response = function_definition
                                                .call(Arguments(&[args.0]))
                                                .or_throw(&ctx)?;

                                            let result =
                                                serde_json::to_value(response)
                                                    .or_throw(&ctx)?
                                                    .into();

                                            Ok::<JsValue, rquickjs::Error>(
                                                JsValue(result),
                                            )
                                        }
                                    },
                                )),
                            )?;
                        },
                        2 => {
                            //有两个参数
                            default.set(
                                &function_key,
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, args: JsValue,args2: JsValue| {
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            let response = function_definition
                                                .call(Arguments(&[args.0,args2.0]))
                                                .or_throw(&ctx)?;

                                            let result =
                                                serde_json::to_value(response)
                                                    .or_throw(&ctx)?
                                                    .into();

                                            Ok::<JsValue, rquickjs::Error>(
                                                JsValue(result),
                                            )
                                        }
                                    },
                                )),
                            )?;
                        },
                        3 => {
                            //有三个参数
                            default.set(
                                &function_key,
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, args: JsValue,args2: JsValue,args3: JsValue| {
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            let response = function_definition
                                                .call(Arguments(&[args.0,args2.0,args3.0]))
                                                .or_throw(&ctx)?;

                                            let result =
                                                serde_json::to_value(response)
                                                    .or_throw(&ctx)?
                                                    .into();

                                            Ok::<JsValue, rquickjs::Error>(
                                                JsValue(result),
                                            )
                                        }
                                    },
                                )),
                            )?;
                        },
                        _ => {
                            //4个以上参数 的参数必须以数组的形式传入
                            default.set(
                                &function_key,
                                Func::from(Async(
                                    move |ctx: Ctx<'js>, args: Vec<JsValue>| {
                                        let function_definition =
                                            function_definition.clone();
                                        async move {
                                            let args_vec = args
                                                .iter()
                                                .map(|arg| arg.0.clone())
                                                .collect::<Vec<_>>();
                                            let response = function_definition
                                                .call(Arguments(&args_vec))
                                                .or_throw(&ctx)?;

                                            let result =
                                                serde_json::to_value(response)
                                                    .or_throw(&ctx)?
                                                    .into();

                                            Ok::<JsValue, rquickjs::Error>(
                                                JsValue(result),
                                            )
                                        }
                                    },
                                )),
                            )?;
                        },
                    }
                }
            }

            Ok(())
        })
    }
}