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

Update string representation to not char* #665

Merged
merged 2 commits into from
Sep 12, 2023
Merged
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
24 changes: 18 additions & 6 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,35 @@ impl WorldGenerator for C {
StringEncoding::CompactUTF16 => unimplemented!(),
};
let ty = self.char_type();
let c_string_ty = match self.opts.string_encoding {
StringEncoding::UTF8 => "char",
StringEncoding::UTF16 => "char16_t",
StringEncoding::CompactUTF16 => panic!("Compact UTF16 unsupported"),
};
uwrite!(
self.src.h_helpers,
"
void {snake}_string_set({snake}_string_t *ret, const {ty} *s);
void {snake}_string_dup({snake}_string_t *ret, const {ty} *s);
// Transfers ownership of `s` into the string `ret`
void {snake}_string_set({snake}_string_t *ret, {c_string_ty} *s);

// Creates a copy of the input nul-terminate string `s` and
// stores it into the component model string `ret`.
void {snake}_string_dup({snake}_string_t *ret, const {c_string_ty} *s);

// Deallocates the string pointed to by `ret`, deallocating
// the memory behind the string.
void {snake}_string_free({snake}_string_t *ret);\
",
);
uwrite!(
self.src.c_helpers,
"
void {snake}_string_set({snake}_string_t *ret, const {ty} *s) {{
void {snake}_string_set({snake}_string_t *ret, {c_string_ty} *s) {{
ret->ptr = ({ty}*) s;
ret->len = {strlen};
}}

void {snake}_string_dup({snake}_string_t *ret, const {ty} *s) {{
void {snake}_string_dup({snake}_string_t *ret, const {c_string_ty} *s) {{
ret->len = {strlen};
ret->ptr = cabi_realloc(NULL, 0, {size}, ret->len * {size});
memcpy(ret->ptr, s, ret->len * {size});
Expand Down Expand Up @@ -440,8 +452,8 @@ impl C {

fn char_type(&self) -> &'static str {
match self.opts.string_encoding {
StringEncoding::UTF8 => "char",
StringEncoding::UTF16 => "char16_t",
StringEncoding::UTF8 => "uint8_t",
StringEncoding::UTF16 => "uint16_t",
StringEncoding::CompactUTF16 => panic!("Compact UTF16 unsupported"),
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
uwriteln!(self.lower_src, "{lower_name} := {param}",);
}
Type::String => {
self.interface.gen.needs_import_unsafe = true;
uwriteln!(
self.lower_src,
"var {lower_name} {value}",
Expand All @@ -1721,7 +1722,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
uwriteln!(
self.lower_src,
"
{lower_name}.ptr = C.CString({param})
// use unsafe.Pointer to avoid copy
{lower_name}.ptr = (*uint8)(unsafe.Pointer(C.CString({param})))
{lower_name}.len = C.size_t(len({param}))"
);
}
Expand Down Expand Up @@ -1887,10 +1889,11 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
uwriteln!(self.lift_src, "{lift_name} := {param}");
}
Type::String => {
self.interface.gen.needs_import_unsafe = true;
uwriteln!(
self.lift_src,
"var {name} {value}
{lift_name} = C.GoStringN({param}.ptr, C.int({param}.len))",
{lift_name} = C.GoStringN((*C.char)(unsafe.Pointer({param}.ptr)), C.int({param}.len))",
name = lift_name,
value = self.interface.get_ty(ty),
);
Expand Down