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.

Bonsai-c: C to asm.js compilation for humans

56 pointsby fdbalmost 10 years ago

4 comments

azakaialmost 10 years ago
I like this a lot. It&#x27;s similar to lljs [1] in allowing small amounts of asm.js to be generated, using a familiar syntax (C). That&#x27;s an important use case and indeed Emscripten doesn&#x27;t really support it.<p>Two comments on this bit:<p>&gt; Wait, what&#x27;s wrong with Emscripten? [..] It&#x27;s perfect if you&#x27;ve got a great big POSIX C + SDL codebase that you want to run in its own self-contained environment in a browser, but it&#x27;s not very helpful for writing idiomatic Javascript code that just needs to drop into optimised asm.js occasionally for some heavy lifting.<p>First, I think this correctly describes some important use cases. But more generally, I think the main use cases I see in this space are<p>1. Complete C or C++ programs, like a 3D game engine. Emscripten mainly focuses on this.<p>2. A C or C++ library that people want to use from JS, like Box2D or Bullet. Emscripten focuses on this quite a bit as well. You can disable filesystem support and other unnecessary features for such pure computational code, and then for a library the size of Box2D or Bullet, Emscripten&#x27;s runtime isn&#x27;t a significant code size issue. And it provides tools to make C++ objects look like JS objects, so using such libraries is convenient (see ammo.js and box2d.js for examples).<p>3. Small amounts of C that are to be &quot;embedded&quot; in JS. lljs and Bonsai-c focus on this.<p>A second comment: The third use case here is easy to get wrong. There is often a cost to calling from JS to asm.js and back. Writing a little asm.js function that adds two numbers and calling that in a loop will generally not be efficient, both because such functions are often optimized to asm.js-like speed anyhow, and because of the JS&lt;-&gt;asm.js overhead. To make it worth it, the asm.js needs to do a more serious amount of work. Just mentioning this because it seems like a common pitfall.<p>[1] <a href="http:&#x2F;&#x2F;lljs.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;lljs.org&#x2F;</a>
yoklovalmost 10 years ago
As someone who has worked extensively with emscripten, and asm.js, something like this is great and needed.<p>Very promising, however it could easily become a crappy version of emscripten by adding e.g. C++ support or full C standard library support.
overgardalmost 10 years ago
&gt; It&#x27;s perfect if you&#x27;ve got a great big POSIX C + SDL codebase that you want to run in its own self-contained environment in a browser, but it&#x27;s not very helpful for writing idiomatic Javascript code that just needs to drop into optimised asm.js occasionally for some heavy lifting.<p>That&#x27;s not really true -- it&#x27;s pretty easy to call into an Emscripten module from javascript. You have to do some data marshalling, but that&#x27;s true of any dynamic language -&gt; c binding.
KenanSulaymanalmost 10 years ago
Really great, like it. I&#x27;d also love to see the examples get more complex as the engine gets more capable. I mean the engine is pretty cool already: <a href="https:&#x2F;&#x2F;github.com&#x2F;gasman&#x2F;bonsai-c&#x2F;blob&#x2F;master&#x2F;examples&#x2F;add.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gasman&#x2F;bonsai-c&#x2F;blob&#x2F;master&#x2F;examples&#x2F;add....</a> - but it doesn&#x27;t give any impression about what it currently is capable of. (e.g. in the README it tells about the lack of string support, et al.)<p>Will follow the progress of this project!