1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5use crate::gitroot::{get, with_plugin, with_plugin_reporter};
6use crate::iterator::Iterator;
7use crate::model::{Call, CallRes, Commit, ConfPlugin, File, InitKind, PluginSdkType, Report};
8use crate::ptr::{ptr_to_string, string_to_ptr};
9use std::collections::HashMap;
10use std::mem::MaybeUninit;
11
12#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "init"))]
13pub fn init(
14 repo_name_ptr: u32,
15 repo_name_size: u32,
16 init_kind_ptr: u32,
17 init_kind_size: u32,
18 conf_ptr: u32,
19 conf_size: u32,
20) {
21 let repo_name = ptr_to_string(repo_name_ptr, repo_name_size);
22 let init_kind_str = ptr_to_string(init_kind_ptr, init_kind_size);
23 let init_kind = match init_kind_str.as_str() {
24 "confhaschange" => InitKind::ConfHasChange,
25 "diff" => InitKind::Diff,
26 "schedule" => InitKind::Schedule,
27 _ => return,
28 };
29 let conf = ptr_to_string(conf_ptr, conf_size);
30 with_plugin(|plugin| plugin.init(repo_name, init_kind, conf));
31}
32
33#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "startCommit"))]
34pub fn start_commit(json: &str) {
35 let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
36 with_plugin(|plugin| plugin.start_commit(commit));
37}
38
39#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "addFile"))]
40pub fn add_file(json: &str) {
41 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
42 with_plugin(|plugin| plugin.add_file(file));
43}
44
45#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "modFile"))]
46pub fn mod_file(json: &str) {
47 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
48 with_plugin(|plugin| plugin.mod_file(file));
49}
50
51#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "delFile"))]
52pub fn del_file(json: &str) {
53 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
54 with_plugin(|plugin| plugin.del_file(file));
55}
56
57#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "endCommit"))]
58pub fn end_commit(json: &str) {
59 let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
60 with_plugin(|plugin| plugin.end_commit(commit));
61}
62
63#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "finish"))]
64pub fn finish() {
65 with_plugin(|plugin| plugin.finish());
66}
67
68#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "report"))]
69pub fn report(iterator_name: &str) {
70 with_plugin_reporter(|plugin| plugin.report(&mut Iterator::<Report>::new(iterator_name)));
71}
72
73#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "call"))]
74pub fn call(json: &str) -> u64 {
75 let c: Call = serde_json::from_str(json).expect("JSON was not well-formatted");
76 let call_res = match get().call_internal_func(c) {
77 Ok(map) => CallRes {
78 res: map,
79 err: String::new(),
80 },
81 Err(e) => CallRes {
82 res: HashMap::new(),
83 err: e,
84 },
85 };
86 let call_res_string = serde_json::to_string(&call_res).unwrap();
87 let (ptr, len) = string_to_ptr(&call_res_string);
88 std::mem::forget(call_res_string);
89 ((ptr as u64) << 32) | len as u64
90}
91
92/// # Safety
93///
94/// Called by host
95#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "pluginConf"))]
96pub unsafe extern "C" fn plugin_conf() -> u64 {
97 let conf_plugin = ConfPlugin {
98 default_run: get().run.clone(),
99 sdk_version: String::from("0.5.0"),
100 sdk_type: PluginSdkType::Tinygo,
101 };
102 let conf = serde_json::to_string(&conf_plugin).unwrap();
103 let (ptr, len) = string_to_ptr(&conf);
104 std::mem::forget(conf);
105 ((ptr as u64) << 32) | len as u64
106}
107
108#[unsafe(no_mangle)]
109extern "C" fn gitrootAlloc(size: u32) -> *mut u8 {
110 let vec: Vec<MaybeUninit<u8>> = vec![MaybeUninit::uninit(); size as usize];
111 Box::into_raw(vec.into_boxed_slice()) as *mut u8
112}
113
114#[unsafe(no_mangle)]
115extern "C" fn gitrootFree(ptr: u32, size: u32) {
116 unsafe {
117 let _ = Vec::from_raw_parts(ptr as *mut u8, 0, size as usize);
118 }
119}