FFI string corrupted?
Is there anything i'm doing wrong here? I'm confused on why it's crashing on setVerisonUE5 in particular. The code without FFI in C++ works fine
12 Replies
uint32_t UsmapLength, const char* UsmapFilePath
but idk if that's the way to go without leaking memoryYou at least don't seem to be null-terminating your strings. That's going to be an issue since then C++ won't know when the strings end and odd things happen.
Giving the length explicitly will then fix that issue since now C++ can rely on the length parameter to stop reading the string and doesn't need to look for a null byte.
How do I pass a null terminated string to C++ from Deno?
do i just add a null byte at the end myself?
Yeah, easiest and probably fastest is to do
alr ty will try
If you know you're dealing with ASCII strings then you could also use
encodeInto
with a buffer you create yourself with its length being one longer than the length of your string. The extra byte is then your null byte. That could lead to a faster call, but it could also be slower since the buffer is initially zeroes which is not needed in the above version.seems like this worked
i never expected to deal with pointers in javascript lmfao
but it's pretty nice
Yeah 🙂 FFI pretty much drew me into Deno in the first place, and by god it's been fun to deal with bytes and pointers and whatnot in JS.
It feels so dirty but oh so fun 🙂
Just in case you haven't seen this, do check out Denonomicon: https://denonomicon.deno.dev/introduction
I've written up at least some of the internals and intricacies of FFI there.
never heard about the denomicon
thank you!
Also in YouTube we did a three part podcasty video series on FFI with Andy. It's on Deno's channel with the name "WTF is FFI"
last question, the "V8 Fast API" in the denomicon is just used internally within Deno and is not usable via the FFI APIs right?
Yes. It gets used automatically with all FFI calls except calls that use struct-by-value parameters or return values (
{ struct: [...] }
)
And feel free to ask as many questions as you feel like, I'm happy to see FFI being used and enjoyed! 🙂