Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(c-api) Implement wasm_exporttype_delete #1685

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/c-api/src/wasm_c_api/types/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use wasmer::ExportType;
pub struct wasm_exporttype_t {
name: NonNull<wasm_name_t>,
extern_type: NonNull<wasm_externtype_t>,

/// If `true`, `name` and `extern_type` will be dropped by
/// `wasm_exporttype_t::drop`.
owns_fields: bool,
}

wasm_declare_boxed_vec!(exporttype);
Expand All @@ -15,7 +19,11 @@ pub extern "C" fn wasm_exporttype_new(
name: NonNull<wasm_name_t>,
extern_type: NonNull<wasm_externtype_t>,
) -> Box<wasm_exporttype_t> {
Box::new(wasm_exporttype_t { name, extern_type })
Box::new(wasm_exporttype_t {
name,
extern_type,
owns_fields: false,
})
}

#[no_mangle]
Expand All @@ -30,6 +38,23 @@ pub extern "C" fn wasm_exporttype_type(
unsafe { et.extern_type.as_ref() }
}

#[no_mangle]
pub extern "C" fn wasm_exporttype_delete(_exporttype: Option<Box<wasm_exporttype_t>>) {}

impl Drop for wasm_exporttype_t {
fn drop(&mut self) {
if self.owns_fields {
// SAFETY: `owns_fields` is set to `true` only in
// `wasm_exporttype_t::from(&ExportType)`, where the data
// are leaked properly and won't be freed somewhere else.
unsafe {
let _ = Box::from_raw(self.name.as_ptr());
let _ = Box::from_raw(self.extern_type.as_ptr());
}
}
}
}

impl From<ExportType> for wasm_exporttype_t {
fn from(other: ExportType) -> Self {
(&other).into()
Expand All @@ -56,6 +81,10 @@ impl From<&ExportType> for wasm_exporttype_t {
unsafe { NonNull::new_unchecked(Box::into_raw(Box::new(extern_type))) }
};

wasm_exporttype_t { name, extern_type }
wasm_exporttype_t {
name,
extern_type,
owns_fields: true,
}
}
}