From 36dc7faf2cafd45f094e39b78616803bd106fcbb Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 8 Feb 2023 12:52:00 +0200 Subject: [PATCH] net.openssl: add manual .str() methods for C.SSL and C.SSL_CTX, fix a bug in the V auto str generation as well. --- vlib/net/openssl/c.v | 12 ++++++++++++ vlib/net/openssl/openssl_compiles_test.v | 11 +++++++++++ vlib/v/gen/c/auto_str_methods.v | 1 - vlib/v/gen/c/fn.v | 3 +++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/vlib/net/openssl/c.v b/vlib/net/openssl/c.v index 546e3d32ac..b02f2bb622 100644 --- a/vlib/net/openssl/c.v +++ b/vlib/net/openssl/c.v @@ -42,6 +42,18 @@ pub struct C.SSL { pub struct C.SSL_CTX { } +// The above C structs, have incomplete declarations in the OpenSSL headers. +// For this reason, we have to prevent the automatic str() generation for them, +// by adding manual implementations of their .str() methods, that are defined on +// pointers to them: +fn (s &C.SSL) str() string { + return 'C.SSL(0x${voidptr(s)})' +} + +fn (c &C.SSL_CTX) str() string { + return 'C.SSL_CTX(0x${voidptr(c)})' +} + fn C.BIO_new_ssl_connect(ctx &C.SSL_CTX) &C.BIO fn C.BIO_set_conn_hostname(b &C.BIO, name &char) int diff --git a/vlib/net/openssl/openssl_compiles_test.v b/vlib/net/openssl/openssl_compiles_test.v index a9dc5e06b8..aac82c34e7 100644 --- a/vlib/net/openssl/openssl_compiles_test.v +++ b/vlib/net/openssl/openssl_compiles_test.v @@ -1,5 +1,16 @@ import net.openssl +struct Abc { + x &C.SSL_CTX +} + +fn test_printing_struct_with_reference_field_of_type_ssl_ctx() { + a := Abc{&C.SSL_CTX(123)} + dump(a) + sa := a.str() + assert sa.contains('&C.SSL_CTX(0x7b)') +} + fn test_openssl_compiles() { assert openssl.is_used == 1 assert true diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 0a3ab0070b..5f05024c4a 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -912,7 +912,6 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, typ_str string, } else { g.get_str_fn(ftyp_noshared) } - // with floats we use always the g representation: if sym.kind !in [.f32, .f64] { fn_body.write_string('{_SLIT("${quote_str}"), ${int(base_fmt)}, {.${data_str(base_fmt)}=') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 6bf8eaf145..ae888d3306 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -435,6 +435,9 @@ fn (mut g Gen) c_fn_name(node &ast.FnDecl) !string { return error('none') } name = g.cc_type(node.receiver.typ, false) + '_' + name + if unwrapped_rec_sym.language == .c { + name = name.replace_once('C__', '') + } } if node.language == .c { name = util.no_dots(name)