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

docs: strings #20

Open
konsumer opened this issue Jun 8, 2024 · 1 comment
Open

docs: strings #20

konsumer opened this issue Jun 8, 2024 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@konsumer
Copy link

konsumer commented Jun 8, 2024

I am playing with cpp host, and I noticed the README is off, and also the unit-tests do not have an example of what I am trying to do.

Imagine a single trace function, wasm-side:

EXTISM_IMPORT("extism:host/user", "trace")
extern void _null0_trace_real(ExtismHandle);

// Log a string (using printf-style)
void trace(const char* format, ...) {
  char null0_traceBuffer[NULL0_TRACE_SIZE];
  va_list args;
  va_start(args, format);
  vsnprintf(null0_traceBuffer, NULL0_TRACE_SIZE, format, args);
  va_end(args);

  ExtismHandle handle = extism_alloc(NULL0_TRACE_SIZE);
  extism_store_to_handle(handle, 0, null0_traceBuffer, NULL0_TRACE_SIZE);
  _null0_trace_real(handle);
}

This will perform the string-formatting, wasm-side, and just send a simple string to host to be logged.

After a bit of fighting, and asking in disocrd (thanks @bhelx !) I worked out in the host, you can expose it like this:

auto trace_in = std::vector<extism::ValType>{extism::ValType::ExtismValType_I64};
auto null0_host_trace = extism::Function(
    "trace",
    trace_in,
    {},
    [](extism::CurrentPlugin plugin, void* user_data) {
      cout << plugin.inputStringView(0) << endl;
    },
    NULL,
    NULL);

Plugin plugin(bytes, true, {null0_host_trace});

But the README makes no mention of inputStringView and there were no unit-tests about this. The closest I saw was the keyval example, which uses a few functions that are undefined.

@G4Vi G4Vi added the documentation Improvements or additions to documentation label Jun 10, 2024
@G4Vi
Copy link
Contributor

G4Vi commented Jun 10, 2024

Thanks for the report, looks like the docs were never cleaned up after I added the std::string_view optimizations, testing it is a good idea too. Contributions welcome, otherwise I'll eventually get around to fixing this.

Since it was mentioned in discord, just to clarify, a std::string_view is just a slice or pointer and length combo. The idea is that std::string_view can be very cheaply constructed from existing data and is lightweight to pass around and copy (as it doesn't include the actual data). It was a much needed improvement to the C++ standard library as it reduces cases where using raw pointers is tempting or the need to promote everything to std::string to for passing std::string& .

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

No branches or pull requests

2 participants