|
DynamicLanguagesOnMobileDevices
Simplified development on mobile devices using dynamic languages
IntroductionDeveloping mobile applications can be a rough ride. The number of platforms is staggering and the programming langauges and development tools are often quite demanding to learn and master. The use of dynamic languages like Lua and JavaScript can change this picture. MoSync is a C/C++ cross-platform mobile development tool, which produces binaries for a wide range of mobile operating systems. The nice thing with C/C++ is the ability to port interpeters for dynamic languages to mobile platforms. In the case of MoSync, this means that an interpreter that runs on MoSync will run on every platform MoSync supports. MobileLua is a port of the Lua programming language Lua that runs on MoSync. This enables using a fully featured dynamic language to script mobile applications. MotivationOne reason for using a dynamic language is the ability to evaluate code at runtime. With this comes a lot of interesting applications, for example:
Another reason is simplified syntax and high-level language constructs, things that can benefit both beginning programmers and professional developers. To create mobile applications you typically need to download and install the development tools for the platforms you want to develop on (e.g. Android, Symbian, BlackBerry, iPhone), and usually code in a static language such as Java. Developing in a static language usually involves some kind of build process; edit code, compile, launch the application, and test the application. With a dynamic language this process can be simplified, since new code can be dynamically loaded into a running application. With an application that embeds an interpreter and the ability to dynamically load code, programming could be done by publishing scripts on the Internet (using any text editor and upload tool) and download them to the device. No need for complex development environments. Support for debugging is of cource needed, and there are ways of doing that. Restrictions on iPhone/iPadThe catch on iPhone/iPad is that you are not allowed to dynamically download and run interpreted code. But it seems to be fine with Apple to use a scripting language internally in an app, see e.g.:
This means that you can benefit from using a dynamic language for development and then package your program as a native application and deploy it on the AppStore. Example programThe following is an example of a very simple painting program in Lua: -- MOBILELUA BEGIN
-- PaintDemoBasic.lua
-- Author: Mikael Kindborg
-- Date: 2011-02-23
-- Description: Simple paint program demo. Supports multi-touch!
-- Fill background.
Screen.setColor(255, 255, 255)
Screen.fillRect(0, 0, Screen.getWidth(), Screen.getHeight())
Screen.update()
function Paint(x, y, touchId)
if touchId == 0 then
Screen.setColor(0, 0, 0)
else
Screen.setColor(0, 200, 0)
end
Screen.fillRect(x - 20, y - 20, 40, 40)
Screen.update()
end
System.onTouchDown(Paint)
System.onTouchDrag(Paint)
-- MOBILELUA ENDThe Paint function is used as an event callback function which is called when the user touches the screen to paint. Screenshot of this program in action:
Overview of the APIThe API MobileLua uses to access MoSync functionality is currently under development. Documentation is underway, and you are most welcome give feedback and participate in the MobileLua project. NotesMoSync supports a subset of full C/C++, and exception handling is not (yet) supported. The port of Lua therefore uses conditional code to handle some special cases, especially for error handling. Compile with flag MOSYNC set (compiler switch -DMOSYNC). This is already set in the MoSync project Eclipse settings included with the download. ContactYour feedback is most welcome, please contact us if you have questions and/or suggestions. Email: mikael.kindborg@mosync.com MoSync home page: http://www.mosync.com |