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

Add annotation support for string parameters #507

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

electrum
Copy link
Collaborator

@electrum electrum commented Sep 2, 2024

The @Buffer annotation assumes a POINTER_LENGTH layout, which seems common. If needed, we can add an enum to support a LENGTH_POINTER layout, with the default being the former.

The @Buffer annotation could also be extended to support byte[], additional character sets for strings, etc.

While there is lots more that we could do with layouts, this seems like a good step to handle a common case. It makes the logging for WASI significantly more useable, as we now have strings instead of pointers. This would allow us to add automatic logging/tracing support to the binding generator.

Copy link
Collaborator

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

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

I like where this PR is going, at the same time I don't know how we can identify and generate the code for this "specialization".
A note on using Random in tests and LGTM, thanks!

@WasmExport
public void randomGet(Memory memory, int ptr, int len) {
byte[] data = new byte[len];
new SecureRandom().nextBytes(data);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: instead of generating random data we can stick with a predefined constant to avoid flakiness and make the test reproducible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This code is never actually called -- we only verify the generated module source code. But I'll change this to have a Random instance as a constructor argument, as it's likely that host modules will have instance fields in practice.

@electrum
Copy link
Collaborator Author

electrum commented Sep 5, 2024

I don't know how we can identify and generate the code for this "specialization"

Not sure what you mean, can you elaborate?

@andreaTP
Copy link
Collaborator

andreaTP commented Sep 6, 2024

Not sure what you mean, can you elaborate?

Sure, sorry, I forgot to give context.
I see those changes useful in general, but, if I understand it correctly, they are not yet covering real-world use-cases:

  • Are we assuming that the memory is always already allocated?
  • How do you easily "free" a String allocated memory? use-case
  • How does this play with explicit "malloc" calls? use-case

@electrum electrum merged commit bc67751 into dylibso:main Sep 6, 2024
13 checks passed
@electrum electrum deleted the wasi-string branch September 6, 2024 18:55
@electrum
Copy link
Collaborator Author

electrum commented Sep 6, 2024

Ah, I understand what you mean. I don't think those are an issue for arguments to host functions, since the memory will be already allocated. Returning strings from a host function should be the same problem as passing strings to an export function.

We would need to know

  • how to allocate and free memory
  • if memory should be freed (i.e. is the passed or returned value owned by the function or the caller)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants