symisc / unqlite

An Embedded NoSQL, Transactional Database Engine
https://unqlite.symisc.net
Other
2.11k stars 164 forks source link

Passing a callable to a foreign function #128

Closed mdorier closed 3 years ago

mdorier commented 3 years ago

I could not find any documentation on this: how can I pass a callable from jx9 to a foreign function written in C, and call this callable on some data? I tried looking at how db_fetch_all is implemented, but it seems to be using a function that is not in the API (here).

symisc commented 3 years ago

Passing a callable to a foreign function is done via an internal Jx9 API hence jx9VmCallUserFunction()which is not publicly exported for obvious security reasons.

mdorier commented 3 years ago

I'm not sure I understand the security reasons, could you give me an example of a situation where such a functionality would be unsafe?

symisc commented 3 years ago

Well, Jx9 was designed to run on restricted embedded devices such as your home router or set top box where memory and stack is limited. Suppose if a broken callable enter in an endless recursive calls/loop on your router...

mdorier commented 3 years ago

How would that be different from executing the same callable from the jx9 script itself?

symisc commented 3 years ago

Jx9 internal callable are certified not to broke such as calling a recursive function or entering an infinite loop unlike passing a user defined callable to the VM which cannot be controlled. Nevertheless, making callable public shoud be straightforward by simply writing a wrapper around jx9VmCallUserFunction().