albertodemichelis / squirrel

Official repository for the programming language Squirrel
http://www.squirrel-lang.org
MIT License
913 stars 156 forks source link

Add userdata pointer to functions #199

Closed frabert closed 7 months ago

frabert commented 4 years ago

Adds an additional argument to C functions in order to pass an additional pointer to arbitrary data, which allows - for example - to implement closures from C. sq_newclosure is augmented in order to reflect this. A new function sq_newudclosure is added in order to support this.

This is a breaking change - existing codebases will refuse to compile. Maybe an auxiliary way can be found to render the additional parameter optional.

VasiliyRyabtsev commented 4 years ago

Why not use free variables?

frabert commented 4 years ago

Mainly ease of use and conciseness, no need to push and pop stuff from the stack.

frabert commented 4 years ago

I made the API backwards compatible by wrapping existing SQFUNCTIONs with an auxiliary function.

zeromus commented 4 years ago

Seems like a cool idea to me even though there's a workaround. With the new function added, the only possible objection would be an extra pointer stored with the closure, maybe not used by some people. This is a very small objection (literally) but maybe larger if the data is wasted on pure squirrel closures. (just thinking out loud here)

frabert commented 4 years ago

@zeromus If it turns out it's a significant issue, I guess it could be hidden behind some ugly preprocessor flag to render it optional