<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fumiya Chiba on Medium]]></title>
        <description><![CDATA[Stories by Fumiya Chiba on Medium]]></description>
        <link>https://medium.com/@fchiba?source=rss-6848c341423------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7_Qe-059CmTM4GMG.</url>
            <title>Stories by Fumiya Chiba on Medium</title>
            <link>https://medium.com/@fchiba?source=rss-6848c341423------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 01:19:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fchiba/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I Learned to Stop Worrying and Handle Concurrency.]]></title>
            <link>https://medium.com/tombo-blog/how-i-learned-to-stop-worrying-and-handle-concurrency-9f2f88cd1dc5?source=rss-6848c341423------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f2f88cd1dc5</guid>
            <category><![CDATA[web-apps]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[emscripten]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Fumiya Chiba]]></dc:creator>
            <pubDate>Tue, 14 Feb 2017 08:02:01 GMT</pubDate>
            <atom:updated>2017-02-14T08:02:01.151Z</atom:updated>
            <content:encoded><![CDATA[<p>Concurrency is an unavoidable subject when creating a GUI application. In particular, heavy processes such as network communication and file I/O are generally handled by a separate thread from the GUI thread. Such threads are created in two ways:</p><ol><li>The application explicitly creates threads.</li><li>The application implicitly creates threads via an API and asynchronously receives results with a callback function.</li></ol><p>While you can use either style in iOS, you can only use the latter for Emscripten purposes. This is because the browser’s Javascript doesn’t support threading.</p><p>Technically, pthread has been implemented experimentally in Javascript. But it depends on SharedArrayBuffer, which in turn is only enabled in Firefox Nightly. So in any case, it’s not widely available yet.</p><p>(We’ve heard that it’s under development for Chrome though, and we’re eagerly awaiting for support by other browser vendors as well!)</p><p>With no pthread support widely available in browsers, we had to come up with workarounds to handle iOS concurrency in Javascript.</p><h3>Case 1: Asynchronous Calls</h3><p>Let’s first consider a simple asynchronous call.</p><p>Grand Central Dispatch (GCD), a framework for handling the queue, is most frequently used on iOS for asynchronous processes. GCD is implemented in the libdispatch library. But since it uses pthread and the iOS kernel API, it can’t be used as is.</p><p>To get around this, we wrote emulation code for key functions such as dispatch_async and dispatch_source (timer). The emulation itself is simple: we delegate queue processing to JavaScript’s event loop using setTimout().</p><h3>Case 2: Explicit Calls</h3><p>Next, let’s consider explicit calls.</p><p>Is it not possible to convert code that explicitly creates new threads? Have no fear! When we looked at the source code of several iOS applications, we found that most explicitly created threads fell into one of these three categories.</p><ol><li>When the task is completed, it terminates as is without notifying the result to other threads.</li><li>When the task is completed, it notifies the main thread with a callback.</li><li>It repeats some process using NSRunLoop (process some sort of task queue).</li></ol><p>In each of these cases, NSThread is typically used.</p><p>So we reimplemented NSThread using GCD. Now, as long as these threads are called in an orderly fashion using NSThread, they are automatically processed using the queue without any code modifications.</p><p>This does mean that processes that would have been handled in the background are now executed in the main thread (postponed by setTimeout()), so there are times when the app lags a bit.</p><p>But if the task is network communication or file I/O, it won’t be a big problem. Network communication uses XHR, so Javascript will execute it in the background. Emscripten processes File I/O on-memory, so it is much faster than the actual on-device I/O.</p><h3>Conclusion</h3><p>We can now support iOS concurrency in our converted web applications. But we’re looking forward to the day when we can throw out all this code and use pthreads instead!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f2f88cd1dc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tombo-blog/how-i-learned-to-stop-worrying-and-handle-concurrency-9f2f88cd1dc5">How I Learned to Stop Worrying and Handle Concurrency.</a> was originally published in <a href="https://medium.com/tombo-blog">Tombo Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extending Emscripten to Support Objective-C — running iOS Apps on the Web]]></title>
            <link>https://medium.com/tombo-blog/extending-emscripten-to-support-objective-c-running-ios-apps-on-the-web-10e54b854671?source=rss-6848c341423------2</link>
            <guid isPermaLink="false">https://medium.com/p/10e54b854671</guid>
            <category><![CDATA[web-apps]]></category>
            <category><![CDATA[emscripten]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Fumiya Chiba]]></dc:creator>
            <pubDate>Tue, 14 Feb 2017 07:46:45 GMT</pubDate>
            <atom:updated>2017-02-16T01:23:30.605Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://kripken.github.io/emscripten-site/">Emscripten</a> is a source-to-source compiler that converts C/C++ to JavaScript. As part of our effort to run iOS applications on web browsers, we have extended Emscripten to compile and run Objective-C.</p><p>In this post, we’ll give an overview of the modifications that were needed to support Objective-C on Emscripten.</p><h3>Inside Objective-C</h3><p>First, let’s look at how Objective-C works internally.</p><p>Let’s consider an Objective-C message call like below.</p><pre>id foo = [dic objectForKey:bar];</pre><p>Internally, this is translated into a function call like below.</p><pre>id foo = objc_msgSend(dic, @selector(objectForKey:), bar);</pre><p>Likewise, metadata such as class declarations are converted to C structures and C function pointer tables. There are held in memory just like global variables. objc_msgSend reads this information, finds the appropriate function from the class inheritance tree, and calls it.</p><p>This mechanism is comprised of several components.</p><p>First, the conversion from Objective-C to C is implemented in the compiler, <strong>clang</strong>. Meanwhile, functions such as objc_msgSend are implemented in <strong>the runtime library</strong> and are dynamically linked at runtime. There are several different runtime libraries, and each library has different conversion requirements (thus clang has several different implementations of the Objective-C to C conversion). Finally, implementations for basic objects such as NSObject and NSString are included in the <strong>Foundation framework</strong> separate from runtime.</p><h3>Changes to clang</h3><p>Emscripten uses clang to convert C/C++ to JavaScript, via LLVM-IR. Therefore, once we extend Emscripten so it can call the currently unsupported clang features needed for Objective-C, much of the work will be complete!</p><p>If that sounds too good to be true, well, you’d be right. Naturally, additional modifications are needed.</p><h4>Metadata</h4><p>For example, Objective-C stores its class metadata in the DATA segment of executable and library files. But Emscripten doesn’t have the concept of segments! So we created a process that gathers all the Objective-C metadata into one ‘special data location’.</p><h4>Variable Length Arguments</h4><p>The objc_msgSend family of functions take variable length arguments in Objective-C, but Emscripten will not let us use function pointers unless we know the signature of the called function. We convert these to functions with fixed length arguments, such as <em>void objc_msgSend_vii(int,int).</em></p><h3>Changes to the Runtime library</h3><p>As mentioned earlier, Objective-C has multiple runtimes. They are all open source. This time we decided to use Apple’s runtime, <a href="https://opensource.apple.com/source/objc4/">objc4</a>, as our starting point.</p><h4>Linking</h4><p>objc4 uses hooks at dynamic link loading to begin operations. While Emscripten does support dynamic linking, its capabilities are limited. Thus, we modified objc4 to work with static linking.</p><h4>Metadata</h4><p>As mentioned earlier, we made changes to clang to store metadata in a ‘special data location’ instead of Objective-C’s DATA segment. Accordingly, we modified the objc4 runtime to read the ‘special data’ output from clang.</p><h4>Environment Dependencies</h4><p>There is a big list of environment dependent parts like #ifdef TARGET_OS_MAC that we rewrote. Luckily for us, objc4 is highly portable and even runs on Windows!</p><h3>Changes to Foundation framework</h3><p>The Foundation framework consists of CoreFoundation (implemented in C) and Foundation, which can (in a narrow sense) be thought of as an Objective-C wrapper around CoreFoundation.</p><h4>Environment Dependencies</h4><p>Like obj4, CoreFoundation also has environment dependencies, such as input/output and directory structures. We made modifications so that these would work on Emscripten.</p><p>CoreFoundation is open sourced by Apple and Foundation is open sourced by Apportable, so we were able to build on top of their work.</p><h3>Conclusion</h3><p>This post gave a big picture overview of the modifications we made to compile and run Objective-C on Emscripten.</p><p>We’ve opted to discuss our general approach and category of modifications rather than the implementation details, since on a granular level, the work has been closer to a very very long list of bug fixes rather than novel architectural breakthroughs. But the work continues to be worthwhile, as we continue to develop technologies for the Open Web.</p><p>To run iOS applications on a browser, further work downstream is required. We’ll be sharing that work in future blog posts.</p><p>A big thanks to all the open source contributors whose work we have been able to leverage, especially Mozilla and Emscripten. We have made some modest contributions to these projects so far, and hope to give back much more in the future.</p><h3>Demo</h3><p>We’ve been working with game companies to convert their iOS games to Web games. You can try some of them out <a href="http://demo.tombo.io/">on our demo site</a>.</p><p>(<a href="http://tombo.io/contact_form/">Feedback Welcome!</a>)</p><p>Enjoy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10e54b854671" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tombo-blog/extending-emscripten-to-support-objective-c-running-ios-apps-on-the-web-10e54b854671">Extending Emscripten to Support Objective-C — running iOS Apps on the Web</a> was originally published in <a href="https://medium.com/tombo-blog">Tombo Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>