TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Design Issues for Foreign Function Interfaces (2004)

43 pointsby metagameover 3 years ago

2 comments

metagameover 3 years ago
Previous summary of this article, from a thread with otherwise no activity and with an author that no longer posts on HN, said more elegantly than I could dream of saying: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1047559" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1047559</a>
评论 #29390924 未加载
spacechild1over 3 years ago
Very interesting article!<p>Unfortunately, it doesn&#x27;t mention Lua, which in my opinion has one of the most elegant C APIs that I have seen. It is entirely stack based, which means you only need to work with primitive types, such as numbers, C strings and user provided opaque pointers. As a consequence, you never have to care about memory management because Lua doesn&#x27;t even let you access the actual Lua objects.<p>You want to create a table (= Lua&#x27;s dictionary&#x2F;array hybrid) and set a field &quot;foo&quot; to 5? lua_newtable() creates a new table and pushes it onto the stack. Then you push &quot;foo&quot; with lua_pushstring() and 5 with lua_pushnumber(). Finally you call lua_settable(), which pops the key and value from the stack, checks if the top of the stack contains a table, and if yes, sets the given field to the given value. The actual table structure is never exposed!<p>This kind of stack manipulation might seem unusual and a bit unweildy, but what you get is <i>safety</i>. If you mess up the stack or perform illegal operations, Lua will call an error handler, but the VM should never crash. The stack API can be seen as the fundamental layer upon which people can create nice abstractions for their host language of choice. Examples are &quot;sol2&quot; for C++ (<a href="https:&#x2F;&#x2F;github.com&#x2F;ThePhD&#x2F;sol2" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ThePhD&#x2F;sol2</a>) or &quot;lupa&quot; for Python (<a href="https:&#x2F;&#x2F;github.com&#x2F;scoder&#x2F;lupa" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;scoder&#x2F;lupa</a>)<p>The public API is contained in &quot;lua.h&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;lua&#x2F;lua&#x2F;blob&#x2F;master&#x2F;lua.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lua&#x2F;lua&#x2F;blob&#x2F;master&#x2F;lua.h</a>. &quot;lauxlib.h&quot; offers some useful helper functions: <a href="https:&#x2F;&#x2F;github.com&#x2F;lua&#x2F;lua&#x2F;blob&#x2F;master&#x2F;lauxlib.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lua&#x2F;lua&#x2F;blob&#x2F;master&#x2F;lauxlib.h</a><p>For comparison, this is Python&#x27;s &quot;Limited&quot; C API: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;c-api&#x2F;stable.html#stable" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;c-api&#x2F;stable.html#stable</a><p>If you want to learn more about Lua&#x27;s C API, have a look at section 4 in <a href="https:&#x2F;&#x2F;www.lua.org&#x2F;manual&#x2F;5.4&#x2F;manual.html" rel="nofollow">https:&#x2F;&#x2F;www.lua.org&#x2F;manual&#x2F;5.4&#x2F;manual.html</a>
评论 #29400384 未加载