Google Groups

Woot! Hello world.


Mike Acton May 6, 2012 2:32 AM
Posted in group: Native-Client-Discuss
In my quixotic quest to make porting stuff to NaCl more straightforward and the API (or app's use of it) more sensible, I decided to begin at the beginning.

I wanted to get this to work:
#include <stdio.h>

int main( int argc, char** argv )
{
  printf("Hello, World!\n");
  return (0);
}

Surely, that couldn't be asking too much? 

We'll here you go. It's not much to look at but I have to say that I'm pretty excited to see it! :)

Inline image 1

So what had do be done?
  • main() is already taken by NaCl, so after the file is compiled objcopy is used to rename main to something else.
  • I made a quick library (nativeblue) that does the work (hello.c just links with this)
    • it provides all the entry points that the NaCl SDK needs.
    • it spawns a "main" thread and in that thread's entry point calls the renamed main function.
    • write() is wrapped. (a la /naclports/src/experimental/libav/photo/)
    • write() copies fd+buffer to a queue to be processed later; write can be called from whatever thread.
    • Note: printf eventually calls write with fileno(stdout).
    • In the render loop in the NaCl main thread, anything in that write queue is flushed out and sent to the browser using MessagingPost
  • I borrowed hterm from the hterm/ssh project: http://git.chromium.org/gitweb/?p=chromiumos/platform/assets.git;a=blob;f=chromeapps/hterm/doc/faq.txt;h=f0d3007f9fc54c7331f68293b7fae5f5b71214ff;hb=HEAD
  • finally, javascript recieves the message and if the dev in the message is "/dev/stdout" (which fileno(stdout) is changed to) it's sent to the terminal output.
Lots of work left to make it more generally useful, cleanup, etc. But definitely takes me the first big step to porting normal "headless" apps and perhaps using the same source both client and server side.

If you want to check it out: https://github.com/macton/nativecolors
And you can see that hello.c is exactly what you'd expect: https://github.com/macton/nativecolors/blob/master/examples/hello_nativeblue_hterm/hello.c

Mike.