Run ClojureScript on Android

Source: Internet
Author: User
Tags clojurescript

Run ClojureScript on Android

In the past few days, I have one of the most meaningful experiences in my development career and want to share with you here.

Now we have enabled ClojureScript to run on Android. Instead of using something like Cordova in a WebView, it actually runs in an embedded JavaScript Engine. What happened?

Recently, we have done some work to create an iOS ClojureScript REPL application, Replete. The whole process looks cool, and the corresponding Android Application is barely created. Unfortunately, my Android technology is average and cannot be used immediately.

However, the Tahmid Sadik technology is still on the stage.

Tahmid can concatenate the UI and instantiate Rhino and calculate JavaScript statements. However, it is a challenge for Tahmid to run the guided ClojureScript.

But before that, he had to boot ClojureScript in his app. I specifically pointed out that he needs to use Google's Closure to rely on the management system to start ClojureScript runtime, without JavaScript or other optimizations (for example, none mode ), you can define the CLOSURE_IMPORT_SCRIPT environment variable as needed. If you want to include a REPL, it is important to use this method to introduce runtime. This also provides support for source code-level namespaces in your REPL.

In fact, Replete also needs to do this and uses a function of Ambly. Of course I know that Replete is an independent REPL and does not require Ambly. However, I used a trick to reuse this function to enable ClojureScript to run in Replete.

By the way, I initially planned to use React Native to implement Replete. The results show that it is still too early to support ClojureScript using React Native, And the ClojureScript that can be guided is indeed only an initial function. It is still necessary to do a lot of work to make it practical. Therefore, for Replete, I should keep it simple, even Goby will not be used.

For the above results, the good thing is that there is no need for React Native dependencies (Android has not been officially released), and there is no Goby dependency (only supports iOS ). The built-in ClojureScript of Replete is very concise and uses the traditional iOS UI. It turns out that this is very helpful for Tahmid to complete the Android version of the same function.

Let's get back to the beginning of the story: essentially, Tahmid copies the bootstrap logic of Ambly and executes the JavaScript statements in Rhino one by one in order. However, from time to time, he may encounter some strange problems. In my memory, I encountered similar problems a few months ago. After studying the Ambly code, I gave some suggestions.

Then, it can basically guide the successful ClosureScriptle.

Cljs. core. apply. call (null, cljs. core. inc, new cljs. core. persistentVector (null, 1, 5, cljs. core. persistentVector. EMPTY_NODE, [1], null ))

This line is the JavaScript code to be bound (apply inc [1 ]).

Then Tahmid finally has 2.0. Awesome! This should be the first time in history to run ClojureScript on Android-based embedded Rhino.

Next, try reader, analyzer, and compiler. Now let's try it out. Just use the JavaScript in Replete to let the Android app execute the read_eval_print function of Replete. The parameter is a string (+ 1 2). If it works properly, congratulations! ClojureScript has been started successfully.

Replete. core. read_eval_print.call (null, '(+ 1 2 )')

Slow down. A problem occurs when goog. require ('replete. core') is executed in Transit. It should be related to randomUUID. Tragedy, it seems that there are still many places to deal.

But it doesn't matter. Replete has been trying to load the analysis cache in different ways. Thanks to Karl Mikkelsen, we have an available version that only uses pure JavaScript without any dependencies. After this is used up, get the print callback ready (this way can work like println), and then the Tahmid notifies me through Slack:

I got 3
(+ 1 2) = 3

... Keep bothering me and blog without authorization. In this way, the Guided ClojureScript REPL was born on Android!

Tahmid encapsulates some things on the interface and fixes some minor issues in JavaScript/ClojureScript integration, and then releases Replicator.

It's a storm!

Now, Tahmid is replacing Rhino with JavaScriptCore, which is not so fast.

I think this will increase the speed. This provides an important foundation for using JavaScriptCore for Local switching on Android.

From the big picture, I really think ClojureScript runs very fast on Android. In particular, use JavaScriptCore. To solve this problem, let's take a look at the differences in the startup speed of Bocko's port to Android's Vladimir iakovlevev.

  • Clojure ~ 14 seconds
  • Clojure/Skummet ~ 11 seconds
  • ClojureScript ~ 2 seconds

The above is the result of running on the simulator, but I still think it shows that ClojureScript truly implements the promise of reducing computing latency on mobile devices. I think it is time to make ClojureScript active to develop applications for mobile devices!

ClojureScript on Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.