Giter Site home page Giter Site logo

Comments (4)

bhelx avatar bhelx commented on May 13, 2024 1

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.

k33g avatar k33g commented on May 13, 2024 1

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.

syke99 avatar syke99 commented on May 13, 2024 1

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.

bhelx avatar bhelx commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.