203 lines
5.4 KiB
Markdown
203 lines
5.4 KiB
Markdown
# Host files
|
|
File path: `out/dir/api.rs`
|
|
```rust
|
|
// Host code
|
|
/* Rust API */
|
|
#[derive(Debug)]
|
|
pub enum Commands {
|
|
#[allow(non_camel_case_types)]
|
|
print { message: String },
|
|
Trinitrix(trinitrix::Trinitrix),
|
|
}
|
|
pub mod trinitrix {
|
|
#[derive(Debug)]
|
|
pub enum Trinitrix {
|
|
#[allow(non_camel_case_types)]
|
|
hi { trixy_output: trixy::oneshot::Sender<trixy::types::String>, name: String },
|
|
}
|
|
}
|
|
/* C API */
|
|
#[no_mangle]
|
|
pub extern "C" fn print(message: String) -> core::ffi::c_int {
|
|
callback_function(print);
|
|
return 1;
|
|
}
|
|
pub mod trinitrix_c {}
|
|
#[no_mangle]
|
|
pub extern "C" fn trinitrix_hi(
|
|
output: *mut trixy::types::String,
|
|
name: String,
|
|
) -> core::ffi::c_int {
|
|
let output_val: trixy::types::String = {
|
|
let (tx, rx) = trixy::oneshot::channel();
|
|
callback_function(trinitrix_hi);
|
|
let recv = rx
|
|
.recv()
|
|
.expect("The channel should not be closed until this value is received");
|
|
recv.into()
|
|
};
|
|
unsafe {
|
|
std::ptr::write(output, output_val);
|
|
}
|
|
return 1;
|
|
}
|
|
// vim: filetype=rust
|
|
```
|
|
|
|
# Auxiliary files
|
|
File path: `dist/interface.h`
|
|
```c
|
|
#if !defined TRIXY_MAIN_HEADER
|
|
#define TRIXY_MAIN_HEADER
|
|
|
|
#include "errno.h"
|
|
#include "string.h"
|
|
#include "vec.h"
|
|
|
|
extern int print (const char *message);
|
|
|
|
extern int trinitrix_hi (const char **trixy_output, const char *name);
|
|
|
|
struct trinitrix
|
|
{
|
|
int (*hi) (const char **, const char *);
|
|
};
|
|
|
|
const struct trinitrix trinitrix = {
|
|
.hi = trinitrix_hi,
|
|
};
|
|
|
|
#endif // if !defined TRIXY_MAIN_HEADER
|
|
// vim: filetype=c
|
|
```
|
|
|
|
File path: `dist/errno.h`
|
|
```c
|
|
/*
|
|
* Copyright (C) 2023 - 2024:
|
|
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
*
|
|
* This file is part of the Trixy crate for Trinitrix.
|
|
*
|
|
* Trixy is free software: you can redistribute it and/or modify
|
|
* it under the terms of the Lesser GNU General Public License as
|
|
* published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* and the Lesser GNU General Public License along with this program.
|
|
* If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef TRIXY_ERRNO_H
|
|
#define TRIXY_ERRNO_H
|
|
#include <stdint.h>
|
|
|
|
/** Calculate the number of bytes in the last error's error message **not**
|
|
* including any trailing `null` characters.
|
|
*/
|
|
extern int last_error_length ();
|
|
|
|
/** Write the most recent error message into a caller-provided buffer as a
|
|
* UTF-8 string, returning the number of bytes written.
|
|
*
|
|
* # Note
|
|
*
|
|
* This writes a **UTF-8** string into the buffer. Windows users may need to
|
|
* convert it to a UTF-16 “Unicode” afterwards.
|
|
*
|
|
* If there are no recent errors then this returns `0` (because we wrote 0
|
|
* bytes). `-1` is returned if there are any errors, for example when passed a
|
|
* null pointer or a buffer of insufficient size.
|
|
*/
|
|
extern int last_error_message (char *buffer, uint64_t length);
|
|
|
|
#endif // TRIXY_ERRNO_H
|
|
```
|
|
|
|
File path: `dist/string.h`
|
|
```c
|
|
/*
|
|
* Copyright (C) 2023 - 2024:
|
|
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
*
|
|
* This file is part of the Trixy crate for Trinitrix.
|
|
*
|
|
* Trixy is free software: you can redistribute it and/or modify
|
|
* it under the terms of the Lesser GNU General Public License as
|
|
* published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* and the Lesser GNU General Public License along with this program.
|
|
* If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef TRIXY_STRING_H
|
|
#define TRIXY_STRING_H
|
|
|
|
/**
|
|
* @brief Frees a rust-allocated string.
|
|
*/
|
|
extern int string_free (const char *string);
|
|
|
|
#endif // TRIXY_STRING_H
|
|
```
|
|
|
|
File path: `dist/vec.h`
|
|
```c
|
|
/*
|
|
* Copyright (C) 2023 - 2024:
|
|
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
*
|
|
* This file is part of the Trixy crate for Trinitrix.
|
|
*
|
|
* Trixy is free software: you can redistribute it and/or modify
|
|
* it under the terms of the Lesser GNU General Public License as
|
|
* published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* and the Lesser GNU General Public License along with this program.
|
|
* If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef TRIXY_VEC_H
|
|
#define TRIXY_VEC_H
|
|
#include <stdlib.h>
|
|
|
|
/**
|
|
* @brief A read-only vector from rust.
|
|
*
|
|
* @detail
|
|
* You are must not free it by calling c's `free`. Use `vec_free`
|
|
* instead.
|
|
*/
|
|
struct vec
|
|
{
|
|
void *data;
|
|
size_t length;
|
|
};
|
|
|
|
extern int vec_free (struct vec vector);
|
|
|
|
#endif // TRIXY_VEC_H
|
|
```
|
|
|
|
|