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

C API improvements #235

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion c-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "lolhtml"
name = "lol_html_c_api"
version = "1.1.2"
authors = ["Ivan Nikulin <[email protected]>", "Joshua Nelson <[email protected]>"]
edition = "2021"
Expand All @@ -17,4 +17,5 @@ panic = "abort"
lto = true

[lib]
name = "lolhtml"
crate-type = ["staticlib", "cdylib", "rlib"]
2 changes: 1 addition & 1 deletion c-api/c-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false

[dependencies]
lol_html = { path = "../../" }
lolhtml = { path = "../" }
lol_html_c_api = { path = "../" }
libc = "0.2.139"

[build-dependencies]
Expand Down
11 changes: 7 additions & 4 deletions c-api/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::*;

#[no_mangle]
pub extern "C" fn lol_html_comment_text_get(comment: *const Comment) -> Str {
pub unsafe extern "C" fn lol_html_comment_text_get(comment: *const Comment) -> Str {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing this change, let's set #![forbid(unsafe_op_in_unsafe_fn)] for the crate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ready to do that yet, as it will cause merge conflicts for the streaming handlers branch.

Str::new(to_ref!(comment).text())
}

#[no_mangle]
pub extern "C" fn lol_html_comment_text_set(
pub unsafe extern "C" fn lol_html_comment_text_set(
comment: *mut Comment,
text: *const c_char,
text_len: size_t,
Expand Down Expand Up @@ -60,11 +60,14 @@ pub extern "C" fn lol_html_comment_is_removed(comment: *const Comment) -> bool {
}

#[no_mangle]
pub extern "C" fn lol_html_comment_user_data_set(comment: *mut Comment, user_data: *mut c_void) {
pub unsafe extern "C" fn lol_html_comment_user_data_set(
comment: *mut Comment,
user_data: *mut c_void,
) {
to_ref_mut!(comment).set_user_data(user_data);
}

#[no_mangle]
pub extern "C" fn lol_html_comment_user_data_get(comment: *const Comment) -> *mut c_void {
pub unsafe extern "C" fn lol_html_comment_user_data_get(comment: *const Comment) -> *mut c_void {
get_user_data!(comment)
}
13 changes: 8 additions & 5 deletions c-api/src/doctype.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
use super::*;

#[no_mangle]
pub extern "C" fn lol_html_doctype_name_get(doctype: *const Doctype) -> Str {
pub unsafe extern "C" fn lol_html_doctype_name_get(doctype: *const Doctype) -> Str {
Str::from_opt(to_ref!(doctype).name())
}

#[no_mangle]
pub extern "C" fn lol_html_doctype_public_id_get(doctype: *const Doctype) -> Str {
pub unsafe extern "C" fn lol_html_doctype_public_id_get(doctype: *const Doctype) -> Str {
Str::from_opt(to_ref!(doctype).public_id())
}

#[no_mangle]
pub extern "C" fn lol_html_doctype_system_id_get(doctype: *const Doctype) -> Str {
pub unsafe extern "C" fn lol_html_doctype_system_id_get(doctype: *const Doctype) -> Str {
Str::from_opt(to_ref!(doctype).system_id())
}

#[no_mangle]
pub extern "C" fn lol_html_doctype_user_data_set(doctype: *mut Doctype, user_data: *mut c_void) {
pub unsafe extern "C" fn lol_html_doctype_user_data_set(
doctype: *mut Doctype,
user_data: *mut c_void,
) {
to_ref_mut!(doctype).set_user_data(user_data);
}

#[no_mangle]
pub extern "C" fn lol_html_doctype_user_data_get(doctype: *const Doctype) -> *mut c_void {
pub unsafe extern "C" fn lol_html_doctype_user_data_get(doctype: *const Doctype) -> *mut c_void {
get_user_data!(doctype)
}

Expand Down
47 changes: 27 additions & 20 deletions c-api/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ use super::*;
use std::slice::Iter;

#[no_mangle]
pub extern "C" fn lol_html_element_tag_name_get(element: *const Element) -> Str {
pub unsafe extern "C" fn lol_html_element_tag_name_get(element: *const Element) -> Str {
let element = to_ref!(element);

Str::new(element.tag_name())
}

#[no_mangle]
pub extern "C" fn lol_html_element_tag_name_get_preserve_case(element: *const Element) -> Str {
pub unsafe extern "C" fn lol_html_element_tag_name_get_preserve_case(
element: *const Element,
) -> Str {
let element = to_ref!(element);

Str::new(element.tag_name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_element_tag_name_set(
pub unsafe extern "C" fn lol_html_element_tag_name_set(
element: *mut Element,
name: *const c_char,
name_len: size_t,
Expand Down Expand Up @@ -52,7 +54,7 @@ pub extern "C" fn lol_html_element_namespace_uri_get(element: *mut Element) -> *
}

#[no_mangle]
pub extern "C" fn lol_html_attributes_iterator_get<'r, 't>(
pub unsafe extern "C" fn lol_html_attributes_iterator_get<'r, 't>(
element: *const Element<'r, 't>,
) -> *mut Iter<'r, Attribute<'t>> {
let attributes = to_ref!(element).attributes();
Expand All @@ -61,7 +63,7 @@ pub extern "C" fn lol_html_attributes_iterator_get<'r, 't>(
}

#[no_mangle]
pub extern "C" fn lol_html_attributes_iterator_next<'t>(
pub unsafe extern "C" fn lol_html_attributes_iterator_next<'t>(
iterator: *mut Iter<'_, Attribute<'t>>,
) -> *const Attribute<'t> {
let iterator = to_ref_mut!(iterator);
Expand All @@ -73,33 +75,35 @@ pub extern "C" fn lol_html_attributes_iterator_next<'t>(
}

#[no_mangle]
pub extern "C" fn lol_html_attributes_iterator_free(iterator: *mut Iter<Attribute>) {
pub unsafe extern "C" fn lol_html_attributes_iterator_free(iterator: *mut Iter<Attribute>) {
drop(to_box!(iterator));
}

#[no_mangle]
pub extern "C" fn lol_html_attribute_name_get(attribute: *const Attribute) -> Str {
pub unsafe extern "C" fn lol_html_attribute_name_get(attribute: *const Attribute) -> Str {
let attribute = to_ref!(attribute);

Str::new(attribute.name())
}

#[no_mangle]
pub extern "C" fn lol_html_attribute_name_get_preserve_case(attribute: *const Attribute) -> Str {
pub unsafe extern "C" fn lol_html_attribute_name_get_preserve_case(
attribute: *const Attribute,
) -> Str {
let attribute = to_ref!(attribute);

Str::new(attribute.name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_attribute_value_get(attribute: *const Attribute) -> Str {
pub unsafe extern "C" fn lol_html_attribute_value_get(attribute: *const Attribute) -> Str {
let attribute = to_ref!(attribute);

Str::new(attribute.value())
}

#[no_mangle]
pub extern "C" fn lol_html_element_get_attribute(
pub unsafe extern "C" fn lol_html_element_get_attribute(
element: *const Element,
name: *const c_char,
name_len: size_t,
Expand All @@ -111,7 +115,7 @@ pub extern "C" fn lol_html_element_get_attribute(
}

#[no_mangle]
pub extern "C" fn lol_html_element_has_attribute(
pub unsafe extern "C" fn lol_html_element_has_attribute(
element: *const Element,
name: *const c_char,
name_len: size_t,
Expand All @@ -127,7 +131,7 @@ pub extern "C" fn lol_html_element_has_attribute(
}

#[no_mangle]
pub extern "C" fn lol_html_element_set_attribute(
pub unsafe extern "C" fn lol_html_element_set_attribute(
element: *mut Element,
name: *const c_char,
name_len: size_t,
Expand All @@ -144,7 +148,7 @@ pub extern "C" fn lol_html_element_set_attribute(
}

#[no_mangle]
pub extern "C" fn lol_html_element_remove_attribute(
pub unsafe extern "C" fn lol_html_element_remove_attribute(
element: *mut Element,
name: *const c_char,
name_len: size_t,
Expand Down Expand Up @@ -233,19 +237,22 @@ pub extern "C" fn lol_html_element_is_removed(element: *mut Element) -> bool {
}

#[no_mangle]
pub extern "C" fn lol_html_element_user_data_set(element: *mut Element, user_data: *mut c_void) {
pub unsafe extern "C" fn lol_html_element_user_data_set(
element: *mut Element,
user_data: *mut c_void,
) {
to_ref_mut!(element).set_user_data(user_data);
}

#[no_mangle]
pub extern "C" fn lol_html_element_user_data_get(element: *mut Element) -> *mut c_void {
pub unsafe extern "C" fn lol_html_element_user_data_get(element: *mut Element) -> *mut c_void {
get_user_data!(element)
}

type EndTagHandler = unsafe extern "C" fn(*mut EndTag, *mut c_void) -> RewriterDirective;

#[no_mangle]
pub extern "C" fn lol_html_element_add_end_tag_handler(
pub unsafe extern "C" fn lol_html_element_add_end_tag_handler(
element: *mut Element,
handler: EndTagHandler,
user_data: *mut c_void,
Expand All @@ -267,7 +274,7 @@ pub extern "C" fn lol_html_element_add_end_tag_handler(
}

#[no_mangle]
pub extern "C" fn lol_html_element_clear_end_tag_handlers(element: *mut Element) {
pub unsafe extern "C" fn lol_html_element_clear_end_tag_handlers(element: *mut Element) {
let element = to_ref_mut!(element);
if let Some(handlers) = element.end_tag_handlers() {
handlers.clear();
Expand Down Expand Up @@ -300,19 +307,19 @@ pub extern "C" fn lol_html_end_tag_remove(end_tag: *mut EndTag) {
}

#[no_mangle]
pub extern "C" fn lol_html_end_tag_name_get(end_tag: *mut EndTag) -> Str {
pub unsafe extern "C" fn lol_html_end_tag_name_get(end_tag: *mut EndTag) -> Str {
let tag = to_ref_mut!(end_tag);
Str::new(tag.name())
}

#[no_mangle]
pub extern "C" fn lol_html_end_tag_name_get_preserve_case(end_tag: *mut EndTag) -> Str {
pub unsafe extern "C" fn lol_html_end_tag_name_get_preserve_case(end_tag: *mut EndTag) -> Str {
let tag = to_ref_mut!(end_tag);
Str::new(tag.name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_end_tag_name_set(
pub unsafe extern "C" fn lol_html_end_tag_name_set(
end_tag: *mut EndTag,
name: *const c_char,
len: size_t,
Expand Down
10 changes: 5 additions & 5 deletions c-api/src/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl OutputSink for ExternOutputSink {
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_build(
pub unsafe extern "C" fn lol_html_rewriter_build(
builder: *mut HtmlRewriterBuilder,
encoding: *const c_char,
encoding_len: size_t,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub extern "C" fn lol_html_rewriter_build(
}

#[no_mangle]
pub extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags(
pub unsafe extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags(
builder: *mut HtmlRewriterBuilder,
encoding: *const c_char,
encoding_len: size_t,
Expand Down Expand Up @@ -99,7 +99,7 @@ pub extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags(
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_write(
pub unsafe extern "C" fn lol_html_rewriter_write(
rewriter: *mut HtmlRewriter,
chunk: *const c_char,
chunk_len: size_t,
Expand All @@ -116,7 +116,7 @@ pub extern "C" fn lol_html_rewriter_write(
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int {
pub unsafe extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int {
let rewriter = to_ref_mut!(rewriter)
.0
.take() // Using `take()` allows calling `free()` afterwards (it will be a no-op).
Expand All @@ -128,7 +128,7 @@ pub extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int {
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_free(rewriter: *mut HtmlRewriter) {
pub unsafe extern "C" fn lol_html_rewriter_free(rewriter: *mut HtmlRewriter) {
// SAFETY: `to_box` includes a check that `rewriter` is non-null.
// The caller is required to ensure that `rewriter` is aligned and that `free` has not been called before.
// NOTE: if `end()` was called before, it is valid (but not recommended) to call `free()` more than once.
Expand Down
8 changes: 4 additions & 4 deletions c-api/src/rewriter_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ impl HtmlRewriterBuilder {
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_builder_new() -> *mut HtmlRewriterBuilder {
pub unsafe extern "C" fn lol_html_rewriter_builder_new() -> *mut HtmlRewriterBuilder {
to_ptr_mut(HtmlRewriterBuilder::default())
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_builder_add_document_content_handlers(
pub unsafe extern "C" fn lol_html_rewriter_builder_add_document_content_handlers(
builder: *mut HtmlRewriterBuilder,
doctype_handler: Option<DoctypeHandler>,
doctype_handler_user_data: *mut c_void,
Expand All @@ -146,7 +146,7 @@ pub extern "C" fn lol_html_rewriter_builder_add_document_content_handlers(
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_builder_add_element_content_handlers(
pub unsafe extern "C" fn lol_html_rewriter_builder_add_element_content_handlers(
builder: *mut HtmlRewriterBuilder,
selector: *const Selector,
element_handler: Option<ElementHandler>,
Expand All @@ -171,6 +171,6 @@ pub extern "C" fn lol_html_rewriter_builder_add_element_content_handlers(
}

#[no_mangle]
pub extern "C" fn lol_html_rewriter_builder_free(builder: *mut HtmlRewriterBuilder) {
pub unsafe extern "C" fn lol_html_rewriter_builder_free(builder: *mut HtmlRewriterBuilder) {
drop(to_box!(builder));
}
4 changes: 2 additions & 2 deletions c-api/src/selector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[no_mangle]
pub extern "C" fn lol_html_selector_parse(
pub unsafe extern "C" fn lol_html_selector_parse(
selector: *const c_char,
selector_len: size_t,
) -> *mut Selector {
Expand All @@ -12,6 +12,6 @@ pub extern "C" fn lol_html_selector_parse(
}

#[no_mangle]
pub extern "C" fn lol_html_selector_free(selector: *mut Selector) {
pub unsafe extern "C" fn lol_html_selector_free(selector: *mut Selector) {
drop(to_box!(selector));
}
4 changes: 2 additions & 2 deletions c-api/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ impl Str {
}
}

#[inline]
/// Convert an `Option<String>` to a C-style string.
///
/// If `string` is `None`, `data` will be set to `NULL`.
#[inline]
#[must_use]
pub fn from_opt(string: Option<String>) -> Self {
match string {
Expand All @@ -45,6 +45,6 @@ impl Drop for Str {
}

#[no_mangle]
pub extern "C" fn lol_html_str_free(string: Str) {
pub unsafe extern "C" fn lol_html_str_free(string: Str) {
drop(string);
}
Loading
Loading