BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage Articles A Technical Overview of Moscrif – MObile SCRipting Framework

A Technical Overview of Moscrif – MObile SCRipting Framework

Moscrif is a cross-platform solution for mobile application development. It lets you create native applications and games for smartphones, tablets and desktops.

Currently, iPhone, iPad, Android smartphones and tablets are supported and the support of new devices are added gradually. Desktop operating systems such as Windows or Mac OS are coming soon so even the old-school PC users can run Moscrif’ applications. Because Moscrif allows the developer to use only one single codebase, there is no need to port the application for all the desired mobile operating systems. Once the development is done, your app will perform and look just like they were natively written.

Built on the top of a powerful JavaScript engine, Moscrif lets you develop fast while preserving high quality and performance. There is no need to write native code for each platform individually in C, C++, Objective C or Java. The knowledge of popular JavaScript is sufficient and the rest is all taken care of by Moscrif.

Benefits

Only one programmer is needed to program in Moscrif, and he will automatically cover all supported platforms. This means that one programmer can generate more applications in shorter time without a diminution of quality. This results in savings and in human resources, funding and time since maintaining only one codebase is less expensive when compared to supporting many platforms. With the current support, you can reach up to 80% of mobile market allowing you to reach quite a large mobile audience.

Moscrif can be used to build interactive 2D games with business applications support on the schedule. The framework offers support for graphics, audio and music, network, cryptographic, local databases, access to file system, using of sensors and comes with a sophisticated 2D game library (Box2D).

Moscrif SDK includes a standalone integrated environment (IDE) that doesn't require the installation of an additional framework or SDK such as Android Developer Tools with Eclipse, or any 3rd party tools.
Moscrif helps to be more effective, productive and to target all mobile operating systems by allowing developers to focus on development, not on porting and maintaining many forks.

Moscrif Core

Moscrif contains a compiler, a parser, an interpreter and a virtual machine. The compiler and the parser are written from scratch in C and C++. The compiler compiles input source code and generates bytecode. The bytecode and the virtual machine are designed to run on less powerful mobile devices. The compilation is done just once during the publishing process. The virtual machine consists mainly of an interpreter with a garbage collector. There is no JIT compilation in Moscrif, one of the reasons being the fact that writable and executable memory regions in iOS are disabled. Moscrif’s garbage collector uses a stop-and-copy technique. The interpreter and VM are natively written in C/C++ for each supported platform.

The language used for coding is JavaScript, modified and extended with additional features from objective programming such as:

      ●    Classes with inheritance
           class Parent {
                 // constructor
                 function this(defaultVal = “default”) {
                         this._prop1 = defaultVal;
                 }
                 // methods
                 function reset() {
                         this._prop1 = “”;
                 }
                 // properties
                 property prop1(v) {
                         get { return this._prop1; }
                         set { this._prop1 = v; }
                 }
           }
           class Child : Parent {
                // you can overwrite members
                function this() {
                         super(“childDefaultVal”);
                         this._prop2 = “test”;
                }
                function reset() {
                         super.reset();
                         this._prop2 = “”;
                }
                // etc…
           }
      ●    Class constructors
           class GameMenu : Scene {
                function this() {
                }
           }
      ●    Read/write, read-only, write-only class properties (getter and setters)
           class Foo {
                function this() {
                         this._prop1 = 123;
                }
                // read/write property
                property prop1(v) {
                         get { return this._prop1; }
                         set { this._prop1 = v; }
                }
                // read-only
                property prop2(v) {
                         get return (this._prop1|| “”) + “.postfix”;
                }
                // write-only property
                property prop3(v) {
                         set {
                               if (v !== null)
                                        this._prop1 = v;
                         }
                }
           }
      ●    Lambda functions
           function doOperation(operation, arg1, arg2) {
              return operation(arg1, arg2);
           }

           // regular function
           var add = function(a, b) {
              return a + b;
           }
           console << doOperation(add, 10, 20) << "\n";
           // output: 30

           // anonymous lambda
           console << doOperation(function(a, b) { return a - b; }, 100, 10) << "\n";                  
           // output: 90
 
           // lambda function block
           console << doOperation(:a, b { return a * b; }, 10, 10) << "\n";
           // output: 100

           // lambda statement function block
           console << doOperation(: a, b : a / b, 33, 3) << "\n";
           // output: 11

      ●    Namespaces
      ●    Support for Integers and Reals
      var r1 = 10 / 3;
      console << "type=" << typeof(r1) << ", value=" << r1 << "\n";
      // output type=integer, value=3

      var r2 = 10 % 3;
      console << "type=" << typeof(r2) << ", value=" << r2 << "\n";
      // output type=integer, value=1

      var r3 = 10 / 3.;
      console << "type=" << typeof(r3) << ", value=" << r3 << "\n";
      // output type=float, value=3.333333

      ●    Optional function arguments
           function func(arg, arg2 = "abc", arg3 = 123)
           {
             console << arg << ", " << arg2 << ", " << arg3 << "\n";
           }

           //func();
           // output: Error: Wrong number of arguments

           func("param1");
           // output: param1, abc, 123

           func("param1", "def");
           // output: param1, def, 123

           func("param1", "def", 456);
           // output: param1, def, 456
      ●    Iterators and decorators
           // decorator '@returns' - verification of returned value of the function
           function @returns(func, return_type)
           {
             return function(params..)
           {
              var rv = func.apply(this,params);
              if( typeof rv != return_type )
                  throw String.printf("expected to return %s but got %s\n", return_type, typeof rv);
              return rv;
            }
           }

           @returns #integer
           function add(a, b)
           {
             return a + b;
           }

           console << add(10, 20) << "\n";
           // output: 30

           console << add(10, "20") << "\n";
           // output (exception): expected to return integer but got string
      ●    Others, see Moscrif Basics

Moscrif uses a modified JavaScript that is more convenient for mobile development. JavaScript has been chosen because it is extremely easy to learn and most developers (especially the web ones) are already familiar with it. To allow the usage of modern OOP approach, JavaScript was extended with basic object oriented features like first tier classes.

IDE & Publishing Tools

The Moscrif SDK also includes an integrated development environment (IDE) and tools for publishing native application installation files which can be uploaded on various application markets or business environments. We have heard many times: why did you decide to develop a custom IDE? What about Eclipse, NetBeans or Visual Studio? We think there are many IDEs for many developers. Many developers love Eclipse, but many others love Visual Studio and dislike Eclipse, making it difficult for such a tool to be accepted by all developers. We also think that mobile development should be faster and simplified, so we decided to develop a simple IDE focused on mobile development. This is how one of the settings panels of the IDE looks like:

(Click on the image to enlarge it)

Moscrif IDE was developed from scratch in .NET and Gtk#. This allows you to run the IDE on all major desktop operating systems, Windows, Mac and Linux. It is necessary to install Mono on Mac OS or Linux. The IDE integrates all basic features starting from the creation of projects, text editing, and ending with the publishing process. After a project’s setup, publishing the installation package is straightforward, without having to install any additional development tools (such as Android Developer Tools) and without sending the project to a build server.

The publishing tools are mostly written in Moscrif and use some core binaries from mobile platforms’ SDK. In order to successfully publish the application to an iOS device, Mac OS is required as well as Organizer from the Xcode.

The publishing tools invoke a compiler in order to compile a source code to its corresponding bytecode. Besides the bytecode, the final distribution package contains additional application resources, such as graphics, sounds, music, texts, etc., and the virtual machine and the interpreter’s binaries. When the package is targeted for a mobile store, the publishing tools will create and sign a single file containing the list of bundled files (their hashes) and will send it to a build server for signing. The code and resources are not sent to the server, they remain on the developer's computer. This process of signing on a Moscrif server is necessary only when a developer wants to publish the application to Google Play, Apple App Store, Samsung Apps etc..

Native Classes & Frameworks

Basic classes and device features classes are written natively in C/C++/Objective-C/Java. They are called “native classes” but they are exposed to a developer as script classes. So, when a developer creates a Sensor class, he is actually creating and using the native class Sensor. There are several native basic and advanced classes:

  • JavaScript like classes (Math, Array, Strings, …)
  • System, File system, Streams
  • SQLite, Crypto, Web, Facebook
  • Rendering (OpenGL) and advanced graphics (Paints, Shaders, Vectors…)
  • Media, Sensors
  • AdMob, iAd, InApp (for Apple Appstore and Google Play)
  • View based set of class for application development (View, ScrollView, TextView,…)
  • etc…

All these classes are natively written. A script developer uses them as regular script classes. It doesn't require knowledge of low level languages like C, C++ or Objective-C. Native classes are the foundation for the classes written in Moscrif's script. This set of classes is called the Framework. For example, the Game2D framework is written in Moscrif's JavaScript and uses built-in native classes like Window (main application window), Bitmap, Canvas, Paint, Shader (for Sprite class and advanced drawings), etc. The most of Game2D classes are written in script. Animations, transitions, particles system is completely written in Moscrif's script. No need to develop natively.

For example: Bitmap is a natively written class. A script programmer can use it as usual:

var bitmap = Bitmap.fromFile(“path_to_bitmap”);

The framework uses a set of native and/or other framework classes to define new and advanced classes. For example,

include "lib://core/log.ms"
include "lib://game2d/game.ms"

// custom script class
class FrameworkSprite
{
  // Bitmap is native class
  var box = Bitmap.fromFile("app://gfx/sprite.png");
  function this(x, y)
  {
     this._x = x;
     this._y = y;
  }
  function draw(canvas)
  {
     canvas.drawBitmap(box, this._x, this._y);
  }
}
class TestGame : Game
{
  function start()
  {
     super.start();
     this._sprite = new FrameworkSprite(100, 100);
  }
  function draw(canvas)
  {
     super.draw(canvas);
     this._sprite.draw(canvas);
  }
}

// create instace and run the game
new TestGame().run();

The application framework uses a set of native classes different from the games framework and this framework is in a beta phase. Because not all the native components are implemented yet, the framework uses skins and themes to mimic native behavior. It means that is possible to have an app that looks like a Windows Phone app (Metro UI or Modern UI) and runs on Android and iOS. Also this way is very suitable for highly branded business apps.

It has been planned to allow the extension of Moscrif with custom natively written classes, meaning that the developers will have the possibility to create own native classes. They will be able to extend predefined native classes and/or create new ones. Also they will have the possibility to create completely native applications or games and use Moscrif in some parts: e.g. to create a native app with native UI controls (separately for Android and iDevice), but to keep the business logic in script (to leverage cross-platform). This will be done through an NDK (Native Development Kit), planned for the near future (Q1 2013). The NDK will also bring native controls to script programmers, allowing the development of apps resembling the underlying system at the expense of cross-platform.

About the Author

Jozef Pridavok is the CEO of Moscrif, the company which created Moscrif SDK - the most efficient solution for mobile development currently on the market. Jozef has more than 10 years of experience in software development. Prior to the establishment of Mothiva, Jozef founded a company called Inlogic Software – a development studio focusing on the creation of mobile applications and games.

Jozef has keen eye and a great passion for mobile technologies and software development, believing that building software for smartphones and tablets can be more appealing, simpler, more fun and enjoyable as it is now.

Rate this Article

Adoption
Style

BT