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::file::File;
  6use crate::gitroot::{get, with_plugin, with_plugin_reporter};
  7use crate::ptr::{ptr_to_string, string_to_ptr};
  8use crate::{Call, CallRes, Commit, ConfPlugin, Report};
  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    conf_has_changed_ptr: u32,
 17    conf_has_changed_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 conf_has_changed = ptr_to_string(conf_has_changed_ptr, conf_has_changed_size) == "true";
 23    let conf = ptr_to_string(conf_ptr, conf_size);
 24    with_plugin(|plugin| plugin.init(repo_name, conf_has_changed, conf));
 25}
 26
 27#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "startCommit"))]
 28pub fn start_commit(json: &str) {
 29    let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
 30    with_plugin(|plugin| plugin.start_commit(commit));
 31}
 32
 33#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "addFile"))]
 34pub fn add_file(json: &str) {
 35    let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
 36    with_plugin(|plugin| plugin.add_file(file));
 37}
 38
 39#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "modFile"))]
 40pub fn mod_file(json: &str) {
 41    let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
 42    with_plugin(|plugin| plugin.mod_file(file));
 43}
 44
 45#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "delFile"))]
 46pub fn del_file(json: &str) {
 47    let file: File = serde_json::from_str(json).expect("JSON was not well-formatted");
 48    with_plugin(|plugin| plugin.del_file(file));
 49}
 50
 51#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "endCommit"))]
 52pub fn end_commit(json: &str) {
 53    let commit: Commit = serde_json::from_str(json).expect("JSON was not well-formatted");
 54    with_plugin(|plugin| plugin.end_commit(commit));
 55}
 56
 57#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "finish"))]
 58pub fn finish() {
 59    with_plugin(|plugin| plugin.finish());
 60}
 61
 62#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "report"))]
 63pub fn report(json: &str) {
 64    let report: Report = serde_json::from_str(json).expect("JSON was not well-formatted");
 65    with_plugin_reporter(|plugin| plugin.report(report));
 66}
 67
 68#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "call"))]
 69pub fn call(json: &str) -> u64 {
 70    let c: Call = serde_json::from_str(json).expect("JSON was not well-formatted");
 71    let call_res = match get().call_internal_func(c) {
 72        Ok(map) => CallRes {
 73            res: map,
 74            err: String::new(), // Pas d'erreur, on met une chaรฎne vide
 75        },
 76        Err(e) => CallRes {
 77            res: HashMap::new(), // Erreur, on renvoie une map vide
 78            err: e,
 79        },
 80    };
 81    let call_res_string = serde_json::to_string(&call_res).unwrap();
 82    let (ptr, len) = string_to_ptr(&call_res_string);
 83    // Note: This changes ownership of the pointer to the external caller. If
 84    // we didn't call forget, the caller would read back a corrupt value. Since
 85    // we call forget, the caller must deallocate externally to prevent leaks.
 86    std::mem::forget(call_res_string);
 87    ((ptr as u64) << 32) | len as u64
 88}
 89
 90/// # Safety
 91///
 92/// Called by host
 93#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "pluginConf"))]
 94pub unsafe extern "C" fn plugin_conf() -> u64 {
 95    let conf_plugin = ConfPlugin {
 96        default_run: get().run.clone(),
 97        sdk_version: String::from("0.4.0"),
 98        sdk_type: String::from("tinygo"),
 99    };
100    let conf = serde_json::to_string(&conf_plugin).unwrap();
101    let (ptr, len) = string_to_ptr(&conf);
102    // Note: This changes ownership of the pointer to the external caller. If
103    // we didn't call forget, the caller would read back a corrupt value. Since
104    // we call forget, the caller must deallocate externally to prevent leaks.
105    std::mem::forget(conf);
106    ((ptr as u64) << 32) | len as u64
107}
108
109#[unsafe(no_mangle)]
110extern "C" fn gitrootAlloc(size: u32) -> *mut u8 {
111    // Allocate the amount of bytes needed.
112    let vec: Vec<MaybeUninit<u8>> = vec![MaybeUninit::uninit(); size as usize];
113    // into_raw leaks the memory to the caller.
114    Box::into_raw(vec.into_boxed_slice()) as *mut u8
115}
116
117#[unsafe(no_mangle)]
118extern "C" fn gitrootFree(ptr: u32, size: u32) {
119    unsafe {
120        let _ = Vec::from_raw_parts(ptr as *mut u8, 0, size as usize);
121    }
122}