GitRoot
Craft your forge, Build your project, Grow your community freely
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::model::{Call, CallRes, Commit, ConfPlugin, File, InitKind, PluginSdkType, Report};
7use crate::ptr::{ptr_to_string, string_to_ptr};
8use std::collections::HashMap;
9use std::mem::MaybeUninit;
10
11#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "init"))]
12pub fn init(
13 repo_name_ptr: u32,
14 repo_name_size: u32,
15 init_kind_ptr: u32,
16 init_kind_size: u32,
17 conf_ptr: u32,
18 conf_size: u32,
19) {
20 let repo_name = ptr_to_string(repo_name_ptr, repo_name_size);
21 let init_kind_str = ptr_to_string(init_kind_ptr, init_kind_size);
22 let init_kind = match init_kind_str.as_str() {
23 "confhaschange" => InitKind::ConfHasChange,
24 "diff" => InitKind::Diff,
25 "schedule" => InitKind::Schedule,
26 _ => return,
27 };
28 let conf = ptr_to_string(conf_ptr, conf_size);
29 with_plugin(|plugin| plugin.init(repo_name, init_kind, conf));
30}
31
32#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "startCommit"))]
33pub fn start_commit(json: &str) {
34 let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
35 with_plugin(|plugin| plugin.start_commit(commit));
36}
37
38#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "addFile"))]
39pub fn add_file(json: &str) {
40 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
41 with_plugin(|plugin| plugin.add_file(file));
42}
43
44#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "modFile"))]
45pub fn mod_file(json: &str) {
46 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
47 with_plugin(|plugin| plugin.mod_file(file));
48}
49
50#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "delFile"))]
51pub fn del_file(json: &str) {
52 let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
53 with_plugin(|plugin| plugin.del_file(file));
54}
55
56#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "endCommit"))]
57pub fn end_commit(json: &str) {
58 let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
59 with_plugin(|plugin| plugin.end_commit(commit));
60}
61
62#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "finish"))]
63pub fn finish() {
64 with_plugin(|plugin| plugin.finish());
65}
66
67#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "report"))]
68pub fn report(json: &str) {
69 let report: Report = serde_json::from_str(json).expect("JSON was not well-formatted");
70 with_plugin_reporter(|plugin| plugin.report(report));
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}