v icon indicating copy to clipboard operation
v copied to clipboard

vweb attempts to create non required _str function

Open qtc-de opened this issue 1 year ago • 1 comments

Describe the bug

I try to save a C struct within the App structure that holds the vweb.Context. This works basically, but I'm required to declare the C structure in v, because during compilation, v attempts to build a _str function for the structure and needs to know the field names for this.

In the case of the minimal example below, this does not hurt. However, when storing a C structure defined in some library, that contains other C structures that contain C structures ... it gets a pain.

Expected Behavior

I expected that it is not necessary to re-declare the structure, as no field access is attempted.

Current Behavior

v attempts to generate a _str function for fields within the App structure, which requires to re-declare C structures.

Reproduction Steps

v code: example.v

import vweb

#flag -I c
#include "example.h"

struct App {
    vweb.Context
    state C.Example
}

fn main() {
    vweb.run(&App{}, 8080)
}

c code: c/example.h

struct Example
{
    int field1;
    int field2;
};

Compilation results is:

cgen error: could not generate string method `Example_str` for type `Example`

After modifying the v code to:

import vweb

#flag -I c
#include "example.h"

struct C.Example {
    field1 int
    field2 int
}

struct App {
    vweb.Context
    state C.Example
}

fn main() {
    vweb.run(&App{}, 8080)
}

It compiles without any issues.

Possible Solution

All comes down to the generation of the _str function. If this is actually required, it is fine and this issue is probably no bug. However, if it is not required, it should not be done. Re-declaring all the C structures for larger type constructs can be quite some work.

Obviously, there could be the third option that I'm using vweb incorrectly and App should only hold the web context itself :)

Additional Information/Context

No response

V version

V full version: V 0.3.3 d60ceb4

Environment details (OS name and version, etc.)

OS: linux, "Manjaro Linux" (VM)

qtc-de avatar Apr 16 '23 20:04 qtc-de