Commit 14b3ee5f authored by Kunshan Wang's avatar Kunshan Wang

[API-GEN]: Tests, CStr, comments.

Added the "localtest" directory for testing the API without touching
other modules. (TODO: It could be moved somewhere else, but should not
use src as an extern crate, or it will depend on the entire crate.)

Use CStr for incoming strings from C. CString is for C-style strings
**owned by Rust**.

Added comments at the beginning of api_c.rs and api_bridge.rs to
indicate some parts of the files are generated.
parent 20b2980c
/*!
* Stubs for api_impl.rs
*
* The muapi2rustapi.py script will always generate these stubs. Copy them into api_impl.rs as the
* template to the implementation.
* Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not edit
* those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py script
* to generate the desired code.
*
* To implement these functions, copy the generated stubs to api_impl.rs and add the function body.
*/
#![allow(non_camel_case_types)]
#![allow(dead_code)]
use std::os::raw::*;
use api_c::*;
......@@ -690,7 +690,7 @@ impl MuIRBuilder {
panic!("Not implemented")
}
pub fn new_atomicrmw(&mut self, id: MuID, result_id: MuID, is_ptr: bool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, refTy: MuID, loc: MuID, opnd: MuID, exc_clause: Option<MuID>) {
pub fn new_atomicrmw(&mut self, id: MuID, result_id: MuID, is_ptr: bool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, ref_ty: MuID, loc: MuID, opnd: MuID, exc_clause: Option<MuID>) {
panic!("Not implemented")
}
......
#![allow(non_snake_case)] // It's generated code.
#![allow(dead_code)] // Seems Rust do not consider "taking the function pointer" as "use"
/**
* This file contains the bridge between the C interface and the Rust implementation.
*
* This file contains the the functions in the C-API's `MuVM`, `MuCtx` and `MuIRBuilder` structs.
* These functions will convert the low-level C-style argument types to the high-level Rust-level
* argument types, and attempt to call the methods of the same name on the corresponding Rust
* structs in api_impl.
*
* NOTE: Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not
* edit those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py
* script to generate the desired code.
*/
use std::ptr;
use std::os::raw::*;
use std::ffi::CString;
use std::ffi::CStr;
use std::slice;
use super::api_c::*;
......@@ -54,11 +69,11 @@ fn from_MuName(cname: CMuName) -> MuName {
#[inline(always)]
fn from_MuCString(cstring: CMuCString) -> String {
debug_assert!(!cstring.is_null());
let ffi_cstring = unsafe {
CString::from_raw(cstring)
let ffi_cstr = unsafe {
CStr::from_ptr(cstring)
};
ffi_cstring.into_string().unwrap()
ffi_cstr.to_string_lossy().into_owned()
}
#[inline(always)]
......@@ -1891,19 +1906,19 @@ extern fn _forwarder__MuIRBuilder__new_cmpxchg(b: *mut CMuIRBuilder, id: CMuID,
};
}
extern fn _forwarder__MuIRBuilder__new_atomicrmw(b: *mut CMuIRBuilder, id: CMuID, result_id: CMuID, is_ptr: CMuBool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, refTy: CMuTypeNode, loc: CMuVarNode, opnd: CMuVarNode, exc_clause: CMuExcClause) {
extern fn _forwarder__MuIRBuilder__new_atomicrmw(b: *mut CMuIRBuilder, id: CMuID, result_id: CMuID, is_ptr: CMuBool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, ref_ty: CMuTypeNode, loc: CMuVarNode, opnd: CMuVarNode, exc_clause: CMuExcClause) {
let mut _arg_b = from_MuIRBuilder_ptr(b);
let mut _arg_id = from_MuID(id);
let mut _arg_result_id = from_MuID(result_id);
let mut _arg_is_ptr = from_MuBool(is_ptr);
let mut _arg_ord = ord;
let mut _arg_optr = optr;
let mut _arg_refTy = from_MuID(refTy);
let mut _arg_ref_ty = from_MuID(ref_ty);
let mut _arg_loc = from_MuID(loc);
let mut _arg_opnd = from_MuID(opnd);
let mut _arg_exc_clause = from_MuID_optional(exc_clause);
unsafe {
(*_arg_b).new_atomicrmw(_arg_id, _arg_result_id, _arg_is_ptr, _arg_ord, _arg_optr, _arg_refTy, _arg_loc, _arg_opnd, _arg_exc_clause)
(*_arg_b).new_atomicrmw(_arg_id, _arg_result_id, _arg_is_ptr, _arg_ord, _arg_optr, _arg_ref_ty, _arg_loc, _arg_opnd, _arg_exc_clause)
};
}
......
/*
#![allow(non_camel_case_types)] // Generated code
#![allow(dead_code)] // This is a public module that mirrors muapi.h
/**
* This file contains the C-facing interfaces.
*
* It is basically the muapi.h header written in Rust. It does not contain any
* implementation-specific code. Most codes are simply generated from muapi.h.
*
* NOTE: Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not
* edit those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py
* script to generate the desired code.
*/
// This file is for interfacing with C, so it is not idiomatic Rust code.
#![allow(non_camel_case_types)]
#![allow(dead_code)]
use std::os::raw::*;
......
RUSTC=rustc
MUAPI_H_DIR=..
INCLUDE_DIR=$(MUAPI_H_DIR)
rustpart.a: *.rs
$(RUSTC) --crate-type=staticlib -o rustpart.a mock.rs
cpart.o: cpart.c cpart.h
$(CC) -c -I $(INCLUDE_DIR) -o $@ $<
cclient: cclient.c cpart.h rustpart.a cpart.o ../muapi.h
$(CC) -I $(INCLUDE_DIR) -o $@ cclient.c cpart.o rustpart.a
The purpose of this directory is to test the generated stubs in the
`src/vm/api` module without touching other modules.
Invoke `make` to build the test program.
../api_bridge.rs
\ No newline at end of file
../api_c.rs
\ No newline at end of file
#![allow(unused_variables)] // stubs
#![allow(dead_code)] // stubs
use std::os::raw::*;
use std::ffi::CStr;
use api_c::*;
use api_bridge::*;
use deps::*;
#[no_mangle]
pub extern fn new_mock_micro_vm(name: CMuCString) -> *mut CMuVM {
println!("name: {:?}", name);
let rust_name = unsafe {
CStr::from_ptr(name)
}.to_string_lossy().into_owned();
println!("The client asked to create a micro VM: {}", rust_name);
let muvm = Box::new(MuVM {
my_name: rust_name,
contexts: Vec::new(),
});
let muvm_ptr = Box::into_raw(muvm);
println!("The header address: {:?}", muvm_ptr);
let cmuvm = make_new_MuVM(muvm_ptr as *mut c_void);
println!("The C-visible CMuVM struct address: {:?}", cmuvm);
cmuvm
}
#[no_mangle]
pub extern fn free_mock_micro_vm(cmvm: *mut CMuVM) {
println!("The client asked to deallocate a micro VM: {:?}", cmvm);
let cmvm_box = unsafe { Box::from_raw(cmvm) };
let mvm_ptr = cmvm_box.header as *mut MuVM;
println!("The MuVM pointer is {:?}", mvm_ptr);
let mvm_box = unsafe { Box::from_raw(mvm_ptr) };
println!("All structures re-boxed and ready for deallocation. By the way, is it really possible to destroy a micro VM in a productional setting?");
}
pub struct MuVM {
my_name: String,
contexts: Vec<MuCtx>,
}
pub struct MuCtx {
// Stub
}
pub struct MuIRBuilder {
// Stub
}
impl MuVM {
pub fn new_context(&mut self) -> *mut CMuCtx {
panic!("Not implemented")
}
pub fn id_of(&mut self, name: MuName) -> MuID {
if name == "@truth" {
42
} else {
panic!("I don't know the id of it")
}
}
pub fn name_of(&mut self, id: MuID) -> CMuCString {
panic!("Not implemented")
}
pub fn set_trap_handler(&mut self, trap_handler: CMuTrapHandler, userdata: CMuCPtr) {
panic!("Not implemented")
}
}
impl MuCtx {
pub fn id_of(&mut self, name: MuName) -> MuID {
panic!("Not implemented")
}
pub fn name_of(&mut self, id: MuID) -> CMuCString {
panic!("Not implemented")
}
pub fn close_context(&mut self) {
panic!("Not implemented")
}
pub fn load_bundle(&mut self, buf: &[c_char]) {
panic!("Not implemented")
}
pub fn load_hail(&mut self, buf: &[c_char]) {
panic!("Not implemented")
}
pub fn handle_from_sint8(&mut self, num: i8, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_uint8(&mut self, num: u8, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_sint16(&mut self, num: i16, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_uint16(&mut self, num: u16, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_sint32(&mut self, num: i32, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_uint32(&mut self, num: u32, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_sint64(&mut self, num: i64, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_uint64(&mut self, num: u64, len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_uint64s(&mut self, nums: &[u64], len: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_float(&mut self, num: f32) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_double(&mut self, num: f64) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_ptr(&mut self, mu_type: MuID, ptr: CMuCPtr) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_fp(&mut self, mu_type: MuID, fp: CMuCFP) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_to_sint8(&mut self, opnd: &APIMuValue) -> i8 {
panic!("Not implemented")
}
pub fn handle_to_uint8(&mut self, opnd: &APIMuValue) -> u8 {
panic!("Not implemented")
}
pub fn handle_to_sint16(&mut self, opnd: &APIMuValue) -> i16 {
panic!("Not implemented")
}
pub fn handle_to_uint16(&mut self, opnd: &APIMuValue) -> u16 {
panic!("Not implemented")
}
pub fn handle_to_sint32(&mut self, opnd: &APIMuValue) -> i32 {
panic!("Not implemented")
}
pub fn handle_to_uint32(&mut self, opnd: &APIMuValue) -> u32 {
panic!("Not implemented")
}
pub fn handle_to_sint64(&mut self, opnd: &APIMuValue) -> i64 {
panic!("Not implemented")
}
pub fn handle_to_uint64(&mut self, opnd: &APIMuValue) -> u64 {
panic!("Not implemented")
}
pub fn handle_to_float(&mut self, opnd: &APIMuValue) -> f32 {
panic!("Not implemented")
}
pub fn handle_to_double(&mut self, opnd: &APIMuValue) -> f64 {
panic!("Not implemented")
}
pub fn handle_to_ptr(&mut self, opnd: &APIMuValue) -> CMuCPtr {
panic!("Not implemented")
}
pub fn handle_to_fp(&mut self, opnd: &APIMuValue) -> CMuCFP {
panic!("Not implemented")
}
pub fn handle_from_const(&mut self, id: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_global(&mut self, id: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_func(&mut self, id: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn handle_from_expose(&mut self, id: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn delete_value(&mut self, opnd: &APIMuValue) {
panic!("Not implemented")
}
pub fn ref_eq(&mut self, lhs: &APIMuValue, rhs: &APIMuValue) -> bool {
panic!("Not implemented")
}
pub fn ref_ult(&mut self, lhs: &APIMuValue, rhs: &APIMuValue) -> bool {
panic!("Not implemented")
}
pub fn extract_value(&mut self, str: &APIMuValue, index: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn insert_value(&mut self, str: &APIMuValue, index: c_int, newval: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn extract_element(&mut self, str: &APIMuValue, index: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn insert_element(&mut self, str: &APIMuValue, index: &APIMuValue, newval: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn new_fixed(&mut self, mu_type: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn new_hybrid(&mut self, mu_type: MuID, length: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn refcast(&mut self, opnd: &APIMuValue, new_type: MuID) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn get_iref(&mut self, opnd: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn get_field_iref(&mut self, opnd: &APIMuValue, field: c_int) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn get_elem_iref(&mut self, opnd: &APIMuValue, index: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn shift_iref(&mut self, opnd: &APIMuValue, offset: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn get_var_part_iref(&mut self, opnd: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn load(&mut self, ord: CMuMemOrd, loc: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn store(&mut self, ord: CMuMemOrd, loc: &APIMuValue, newval: &APIMuValue) {
panic!("Not implemented")
}
pub fn cmpxchg(&mut self, ord_succ: CMuMemOrd, ord_fail: CMuMemOrd, weak: bool, loc: &APIMuValue, expected: &APIMuValue, desired: &APIMuValue, is_succ: *mut CMuBool) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn atomicrmw(&mut self, ord: CMuMemOrd, op: CMuAtomicRMWOptr, loc: &APIMuValue, opnd: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn fence(&mut self, ord: CMuMemOrd) {
panic!("Not implemented")
}
pub fn new_stack(&mut self, func: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn new_thread_nor(&mut self, stack: &APIMuValue, threadlocal: Option<&APIMuValue>, vals: Vec<&APIMuValue>) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn new_thread_exc(&mut self, stack: &APIMuValue, threadlocal: Option<&APIMuValue>, exc: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn kill_stack(&mut self, stack: &APIMuValue) {
panic!("Not implemented")
}
pub fn set_threadlocal(&mut self, thread: &APIMuValue, threadlocal: &APIMuValue) {
panic!("Not implemented")
}
pub fn get_threadlocal(&mut self, thread: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn new_cursor(&mut self, stack: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn next_frame(&mut self, cursor: &APIMuValue) {
panic!("Not implemented")
}
pub fn copy_cursor(&mut self, cursor: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn close_cursor(&mut self, cursor: &APIMuValue) {
panic!("Not implemented")
}
pub fn cur_func(&mut self, cursor: &APIMuValue) -> MuID {
panic!("Not implemented")
}
pub fn cur_func_ver(&mut self, cursor: &APIMuValue) -> MuID {
panic!("Not implemented")
}
pub fn cur_inst(&mut self, cursor: &APIMuValue) -> MuID {
panic!("Not implemented")
}
pub fn dump_keepalives(&mut self, cursor: &APIMuValue, results: *mut CMuValue) {
panic!("Not implemented")
}
pub fn pop_frames_to(&mut self, cursor: &APIMuValue) {
panic!("Not implemented")
}
pub fn push_frame(&mut self, stack: &APIMuValue, func: &APIMuValue) {
panic!("Not implemented")
}
pub fn tr64_is_fp(&mut self, value: &APIMuValue) -> bool {
panic!("Not implemented")
}
pub fn tr64_is_int(&mut self, value: &APIMuValue) -> bool {
panic!("Not implemented")
}
pub fn tr64_is_ref(&mut self, value: &APIMuValue) -> bool {
panic!("Not implemented")
}
pub fn tr64_to_fp(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_to_int(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_to_ref(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_to_tag(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_from_fp(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_from_int(&mut self, value: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn tr64_from_ref(&mut self, reff: &APIMuValue, tag: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn enable_watchpoint(&mut self, wpid: CMuWPID) {
panic!("Not implemented")
}
pub fn disable_watchpoint(&mut self, wpid: CMuWPID) {
panic!("Not implemented")
}
pub fn pin(&mut self, loc: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn unpin(&mut self, loc: &APIMuValue) {
panic!("Not implemented")
}
pub fn get_addr(&mut self, loc: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn expose(&mut self, func: &APIMuValue, call_conv: CMuCallConv, cookie: &APIMuValue) -> *mut APIMuValue {
panic!("Not implemented")
}
pub fn unexpose(&mut self, call_conv: CMuCallConv, value: &APIMuValue) {
panic!("Not implemented")
}
pub fn new_ir_builder(&mut self) -> *mut CMuIRBuilder {
panic!("Not implemented")
}
pub fn make_boot_image(&mut self, whitelist: Vec<MuID>, primordial_func: Option<&APIMuValue>, primordial_stack: Option<&APIMuValue>, primordial_threadlocal: Option<&APIMuValue>, sym_fields: Vec<&APIMuValue>, sym_strings: Vec<String>, reloc_fields: Vec<&APIMuValue>, reloc_strings: Vec<String>, output_file: String) {
panic!("Not implemented")
}
}
impl MuIRBuilder {
pub fn load(&mut self) {
panic!("Not implemented")
}
pub fn abort(&mut self) {
panic!("Not implemented")
}
pub fn gen_sym(&mut self, name: Option<String>) -> MuID {
panic!("Not implemented")
}
pub fn new_type_int(&mut self, id: MuID, len: c_int) {
panic!("Not implemented")
}
pub fn new_type_float(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_double(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_uptr(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_type_ufuncptr(&mut self, id: MuID, sig: MuID) {
panic!("Not implemented")
}
pub fn new_type_struct(&mut self, id: MuID, fieldtys: Vec<MuID>) {
panic!("Not implemented")
}
pub fn new_type_hybrid(&mut self, id: MuID, fixedtys: Vec<MuID>, varty: MuID) {
panic!("Not implemented")
}
pub fn new_type_array(&mut self, id: MuID, elemty: MuID, len: u64) {
panic!("Not implemented")
}
pub fn new_type_vector(&mut self, id: MuID, elemty: MuID, len: u64) {
panic!("Not implemented")
}
pub fn new_type_void(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_ref(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_type_iref(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_type_weakref(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_type_funcref(&mut self, id: MuID, sig: MuID) {
panic!("Not implemented")
}
pub fn new_type_tagref64(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_threadref(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_stackref(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_framecursorref(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_type_irbuilderref(&mut self, id: MuID) {
panic!("Not implemented")
}
pub fn new_funcsig(&mut self, id: MuID, paramtys: Vec<MuID>, rettys: Vec<MuID>) {
panic!("Not implemented")
}
pub fn new_const_int(&mut self, id: MuID, ty: MuID, value: u64) {
panic!("Not implemented")
}
pub fn new_const_int_ex(&mut self, id: MuID, ty: MuID, values: &[u64]) {
panic!("Not implemented")
}
pub fn new_const_float(&mut self, id: MuID, ty: MuID, value: f32) {
panic!("Not implemented")
}
pub fn new_const_double(&mut self, id: MuID, ty: MuID, value: f64) {
panic!("Not implemented")
}
pub fn new_const_null(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_const_seq(&mut self, id: MuID, ty: MuID, elems: Vec<MuID>) {
panic!("Not implemented")
}
pub fn new_const_extern(&mut self, id: MuID, ty: MuID, symbol: String) {
panic!("Not implemented")
}
pub fn new_global_cell(&mut self, id: MuID, ty: MuID) {
panic!("Not implemented")
}
pub fn new_func(&mut self, id: MuID, sig: MuID) {