pipeline_script/plugin/
map.rs

1use crate::core::builtin::{
2    hashmap, hashmap_clear, hashmap_contains_key, hashmap_free, hashmap_get, hashmap_insert,
3    hashmap_len, hashmap_remove,
4};
5use crate::core::engine::Engine;
6use crate::plugin::Plugin;
7use crate::register_external_fn;
8
9pub struct MapPlugin;
10impl Plugin for MapPlugin {
11    fn apply(self, e: &mut Engine) {
12        // 使用宏来注册外部函数
13        register_external_fn!(e, "HashMap", hashmap);
14        register_external_fn!(e, "hashmapInsert", hashmap_insert);
15        register_external_fn!(e, "hashmapGet", hashmap_get);
16        register_external_fn!(e, "hashmapRemove", hashmap_remove);
17        register_external_fn!(e, "hashmapContainsKey", hashmap_contains_key);
18        register_external_fn!(e, "hashmapLen", hashmap_len);
19        register_external_fn!(e, "hashmapClear", hashmap_clear);
20        register_external_fn!(e, "hashmapFree", hashmap_free);
21
22        e.register_precluded_scripts(&["
23            extern fn HashMap() -> Pointer<Any>
24            extern fn hashmapInsert(map: Pointer<Any>, key: String, value: String)
25            extern fn hashmapGet(map: Pointer<Any>, key: String) -> String
26            extern fn hashmapRemove(map: Pointer<Any>, key: String) -> Bool
27            extern fn hashmapContainsKey(map: Pointer<Any>, key: String) -> Bool
28            extern fn hashmapLen(map: Pointer<Any>) -> Int64
29            extern fn hashmapClear(map: Pointer<Any>)
30            extern fn hashmapFree(map: Pointer<Any>)
31            struct Map{
32                inner:Pointer<Any>
33            }
34
35            fn Map()->Map{
36                return Map{
37                    inner:HashMap()
38                }
39            }
40            fn Map.insert(self,key:String,value:String){
41                hashmapInsert(self,key,value)
42            }
43            fn Map.get(self,key:String)->String{
44                return hashmapGet(self,key)
45            }
46            fn Map.remove(self,key:String){
47                hashmapRemove(self,key)
48            }
49            fn Map.contains(self,key:String)->Bool{
50                return hashmapContainsKey(self,key)
51            }
52            fn Map.len(self)->Int64{
53                return hashmapLen(self)
54            }
55            fn Map.clear(self){
56                hashmapClear(self)
57            }
58            fn Map.free(self){
59                hashmapFree(self)
60            }
61            "]);
62    }
63}