Comments (4)
I think this is the problem:
> let str = "hello 🌎"
undefined
> str.length
8
> Buffer.byteLength(str)
10
String#length
in javascript counts the characters and not the bytes.
I think you need to change this:
var offs = currentPlugin.memoryAlloc(helloMessage.length)
to this:
var offs = currentPlugin.memoryAlloc(Buffer.byteLength(helloMessage))
Could you try that?
Did you get this from an example? We probably should change this in other places or start thinking about some abstractions that can help prevent these kinds of footguns.
from extism.
Buffer.byteLength(helloMessage)
does the job 👍 thank you 🙇
My sample is a mix between the doc and a chat in the Discord https://discord.com/channels/1011124058408112148/1050088035691270204/1066403189211017257
I think we can close the issue
from extism.
I agree with @bhelx Emojis are stored in Unicode code points, and some are comprised of multiple Unicode points. So when working with emojis in strings and setting byte arrays to the length of said string, the only safe way to do so with Buffer.byteLength()
from extism.
Right, I think we should always use byteLength regardless of what encoding we think the string is because it works for all encodings. This is an easy mistake to make though, and we made it when writing this library: #82
Next steps would be to agree on a slightly higher level API for host functions to help prevent these kind of footguns. Perhaps we could have a method kind of like this:
class Plugin {
allocBytes(data: Uint8Array | String): number {
let offs = this.memoryAlloc(Buffer.byteLength(data))
this.memory(offs).write(data)
return offs
}
}
We'd like to take some time and come up with something consistent for all the SDKs though, so for now best to just try to document this better.
from extism.
Related Issues (20)
- feat: Add ability to dump memory to a file when an error occurs
- Rust SDK Example has an Error HOT 1
- Static linking extism with c and c++ sdks HOT 1
- Plugin Errors Propagating as segfault HOT 2
- Regression: Can't cancel multiple calls for the same plugin instance HOT 1
- Implement Java PDK HOT 1
- The runtime only returns a generic error when calling `extism_plugin_error` HOT 1
- Improve MSVC support HOT 5
- via proto conversation: refine extism error message when plugin calls fail
- via proto conversation: add example of using extism+tracing to rust runtime HOT 1
- Build linux libextism on manylinux/musllinux containers HOT 8
- Add extism_load_v128/extism_store_v128
- Double mutable borrow issue for `host_fn` with Rust SDK HOT 4
- Error when passing an empty String from Plugin to Runtime via a host function or a logging macro HOT 3
- Add building and installing cmake config mode package
- [Question] How to reload plugin with new wasm file? HOT 6
- extism_length returns garbage values when an ExtismPointer is not aligned with an alloc HOT 1
- feature: Give users a way to manage timeouts from inside host functions HOT 3
- [Question] nodejs call rust wasm function failed. HOT 2
- [Question] C plugin example build failed HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extism.