Comments (12)
@pwaller your wish is granted. I also re-generated my Nuklear package for UI which also benefits a lot from the automatic null termination approach.
GENERATOR:
Options:
SafeStrings: true
happy hacking! 🍻
from c-for-go.
I agree. I will add this as a global preference of generator.
from c-for-go.
I wrote this issue on the assumption that c-for-go expects you to null terminate the strings you pass to the generated API. I actually can't find any advice on that front, but observe that all sorts of things don't work unless you null-terminate the strings you feed into the API. So maybe it's supposed to work and I'm encountering a bug, instead.
from c-for-go.
Hi!
Yes that's a common mistake. I usually address that when creating a wrapper for the generated code,
when I want to simplify the API and introduce objects. As an example see String
https://github.com/xlab/pocketsphinx-go/blob/master/sphinx/helpers.go#L42 which is used to make
params safe https://github.com/xlab/pocketsphinx-go/blob/master/sphinx/ngram.go#L316 so user will
working with sphinx.String
and know that it's safe.
I wrote this issue on the assumption that c-for-go expects you to null terminate the strings you
pass to the generated API.
Yes, this is so by design. The point there is that c-for-go generates its binding on the API
interface level, not the data level. I other worlds, it does rely on types and method signatures but
doesn't go into data context. It doesn't modify or copy data except when unavoidable.
Passing raw things like string
or []byte
is a 0-allocation operation, memory directly referenced
from Go to C.
This is mainly because I wanted to avoid any overhead and hidden effects. Many C functions out there
are waiting for non-null terminated strings, knowing their length. And if they require the null-termination, that's up to user to decide how to properly do that. Otherwise you will introduce an
extra allocation in the generated code, what I tried to avoid.
It's about safety VS performance, I chose the latter to be the base layer, and the safety can be enforced
later as an additional layer written by humans, for humans.
from c-for-go.
Yes that's a common mistake.
It might be worth documenting it clearly somewhere. I scoured the docs a few times and didn't find anything.
The problem though is that even knowing what is correct I still make the mistake.
It's about safety VS performance, I chose the latter to be the base layer, and the safety can be enforced
later as an additional layer written by humans, for humans.
I understand your motivation. However, for strings specifically, this means that the "additional layer" would have to be almost as big as the first layer. Which seems to me to defeat the idea of doing code generation in the first place. See for instance the python C-API. It's huge, and very many functions take strings, all of which must be 0-terminated. https://docs.python.org/3/c-api/index.html
I'm not asking you to change the default, I'm asking if there is room in the generator to automate this with a new PtrType. Any thoughts on that? It would eliminate a lot of manual and error-prone work.
from c-for-go.
Great. Though I hope it is still possible to override it in specific cases where performance is a concern, or for specific cases which require null terminated strings.
from c-for-go.
@pwaller the override can be achieved by taking out the code into a separate file and building the hand-made optimisations upon generated code.
I'm not making this feature into the tip system, because it's very easy to create a mess of strings that are null-terminated automatically and other cases that require manual null-termination. So in order to use API safely, the user will be forced to look into guts of binding code (not the interface, but cgo_helpers.go
) and study whether the strings are terminated in some particular functions. This is wrong.
from c-for-go.
How about introducing a new type:
type CString string
Then it would be obvious at the point of use that you're doing something strange:
cpkg.Function(cpkg.CString(s))
The type cast should be "free" but annotates what is happening in a fairly obvious manner.
from c-for-go.
(and is impossible to accidentally miss because the type-checker will complain)
from c-for-go.
Type-cast won't guarantee safeness, this doesn't contribute to solution but simply makes the resulting API harder to use. I want to use string
, it's a part of the language.
We don't need annotations there, its either safe strings across all package, or not.
from c-for-go.
Looks great, thanks :)
from c-for-go.
your wish is granted.
Thanks for adding this. Took a long time to find this issue - but I'm pleased to see that I don't have to NUL-terminate my Go strings!
from c-for-go.
Related Issues (20)
- define pointer to function HOT 1
- type C.struct_ in strut and func HOT 1
- How to handle a function returning a struct as value
- Type translation does not respect systems data model HOT 1
- Verbose argument to print more logs
- Avoid lock / unlock for some function call
- Wiki need to be updated
- panic: unknown type _Float128 HOT 2
- Non int return function pointer not recognized
- Generator uses underlying type of typedef when used as a parameter
- Website down HOT 3
- It would be nice to at least have an example of the YML required in the readme since the website does not work HOT 1
- build HOT 5
- [OS X / Ubuntu ARM64] include file not found: <string> HOT 3
- Extracting SAL annotations from declarations HOT 2
- Example where C const get translated into Go typed const
- Callback generation HOT 2
- THIS IS NOT AN ISSUE!!!
- Avoid symbol lookup error
- Passing in an array to a C function and reading the data - incorrect autogenerated Go code
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 c-for-go.