5

I took it upon myself recently to work towards making a general-purpose Translator to automatically translate js programs into C, but I got stuck when thinking of asynchronous behaviour. Using ANSI / POSIX C, and only allowing yourself to use a library for a threadpool implementation, how would you go about implementing asynchronous function calls in C. The methodology must be thread-safe. When I came up with my approach, I was inspired by this source's use of what they call the Asynchronous Dispatcher pattern: https://doanduyhai.wordpress.com/2012/08/04/design-pattern-the-asynchronous-dispatcher/

  • 2
    What is your approach? I don't see it listed here. – World Engineer Apr 26 '15 at 21:39
  • I purposely omitted my approach to not prejudice any answers, but my approach is an extension of that Asynchronous Dispatcher pattern. I posted the question because I am curious to see what people think could be a better implementation. – Francesco Gramano Apr 26 '15 at 21:42
  • Really the question is "How do I improve upon the Asynchronous Dispatcher pattern?". What is wrong with that approach or your current one? – World Engineer Apr 26 '15 at 21:45
  • Well, translating the pattern into C is a bit of a grey area because Java obscures the low-level details that would need to be specified with C, so I haven't actually been able to implement the pattern for thorough testing. So, I am asking to see if there might be an easier, still thread-safe, way to implement this. – Francesco Gramano Apr 26 '15 at 21:52
  • 1
    You want to translate to Java or Javascript? Because the question says Javascript (JS) but you just said Java. – World Engineer Apr 26 '15 at 22:19
9

Are you aiming at making a general-purpose Javascript to C automatic translator (that is, a compiler from Javascript to C)?

This is quite challenging and will take you several years of work, in particular if you want to make an efficient Javascript to C compiler (something which won't be much slower than most current Javascript implementations)

Notice that semantically Javascript has a lot of common features with Scheme (dynamic typing, garbage collection, closures, and perhaps run-time evaluation), even if the syntax is very different. Its prototype object model is indeed not in Scheme, but you could find some implementations of prototype object models in Scheme quite similar to the Javascript model. So I definitely recommend studing Scheme and its formal semantics, and looking inside several Scheme to C translators (Bigloo, Chicken, Stalin, ...). In contrast to Scheme, Javascript was initially ill-defined (its first implementation was a few weeks hack!) and has some unpleasant peculiarities (result of [] + {}, etc...) that are painful to handle in a Javascript to C translator.

Then I strongly recommend reading C.Queinnec Lisp In Small Pieces book. Read also Scott's programming language pragmatics book.

The asynchronous aspect of Javascript is similar to coroutines and to continuation passing style (a.k.a. CPS). Look into Continuation Passing C, it is translating a program in CPC (an extended dialect of C, with yield, spawn, wait, ... primitives usable for asynchronous computations) into plain C using continuation passing style techniques.

If your goal is not a general-purpose translator of Javascript to C, but simply to manually rewrite a specific Javascript program into C, coroutines, callbacks, closures, and CPS are still useful concepts to assess. And you could even generate or write code for CPC then use the CPC compiler to get ordinary C.

You might also implement your asynchronous computations using the deprecated setcontext(3) routine, but I don't recommend using that since it is deprecated, very low level (basically it is setting all the machine registers), somehow machine specific, and difficult to debug.

Of course you could also consider multi-threading, e.g. POSIX pthreads or C11 threads.

  • I think it's probably safe to assume that the OP isn't attempting to write a generalized JS to C converter. The only sentence in your answer that appears relevant to the question he asked is the last one. – Robert Harvey Apr 27 '15 at 8:52
  • 1
    @RobertHarvey: The OP's original question is ambiguous and unclear. Its first sentence suggests on the contrary that he wants to write a general-purpose JS to C compiler. – Basile Starynkevitch Apr 27 '15 at 9:17
  • Alright, but he's not asking about the general case. – Robert Harvey Apr 27 '15 at 9:19
  • 1
    @RobertHarvey: are you sure of that? Apparently he is coding a JS to C translator, at least for some restricted subset of JS. – Basile Starynkevitch Apr 27 '15 at 9:26
  • "...but I got stuck when thinking of asynchronous behaviour. Using ANSI / POSIX C, and only allowing yourself to use a library for a threadpool implementation, how would you go about implementing asynchronous function calls in C?" – Robert Harvey Apr 27 '15 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.