Tuesday, November 28, 2017

Mobile Smalltalk

I've been announcing the Open Beta release of my game HexSolve for Android and iOS.  HexSolve was written in Smalltalk and people have been asking me how it's possible to run Smalltalk on mobile devices.  I thought I'd write up a little description of the technology behind it.



I've wanted to be able to run Smalltalk on Android and iOS for many years.  I finally decided to do something about it.  I chose VisualWorks for my development environment.  Unfortunately iOS doesn't support dynamic compilation so getting the regular VisualWorks VM to run on iOS is virtually impossible.  I decided to skirt around the issue by writing my own VM.

My VM is written in C.  It interprets the same bytecodes as those generated by VisualWorks so I can use the VisualWorks compiler to compile all of the code for the mobile Smalltalk.  Being a pure interpreter, I get around the problem of dynamic compilation on iOS.  Apple is okay with an interpreter so long as all of the code it's interpreting is delivered with the product and not downloaded dynamically.

Development of the Smalltalk code begins in VisualWorks.  I have my own Object class (in my own namespace) as well as my own copies of the other base classes (True, False, UndefinedObject, etc.).  I did this because I had better control of the implementation than I would if I just used the VisualWorks base code.  If I used the regular VisualWorks classes, then I would need to bring in more and more of the base code to get those classes to work.  There are just too many "strings attached" to do this effectively.



Methods beginning with an underscore ($_) will have the underscore removed when moving the image to the mobile device.  It allows me to have one version for Mobile Smalltalk and a different version for VisualWorks.  You can imagine that it would be bad if I re-defined at:put: in VisualWorks.

I have an ImageBuilder utility that takes all of the code in the packages "SimTalk Remote Core" (base Smalltalk code) and "SimTalk HexSolve" (the game itself), packages them into a special 64-bit image format and writes the image into a file.  I can then use AndroidStudio or XCode to include this image as an asset to deliver to the device along with the interpreter.

The interpreter itself is a 64 bit image.  It uses immediate objects (tagged objects that fit within the object pointer) for SmallInteger, Character and Float.  This allows me to do faster operations on these objects without allocating memory.

The game uses Smalltalk primitives to interface to OpenGL ES which runs on both Android and iOS. For text, I interface to Freetype which is compiled and delivered with the game.  Freetype will create images for the characters which I can collect and cache in a Smalltalk Image object.  I then render these images as OpenGL textures.

My mobile Smalltalk is single-threaded - there's no such thing as fork, a Process or a ProcessManager.  In fact, Smalltalk runs completely as an OS callback.  When the OS determines that something happened (a touch event, a button or a timeout), it runs a callback in my VM.  The VM creates an Event object, drops it into a known spot in the image and runs the interpreter.  The interpreter then reads the event, dispatches it, re-renders the screen if necessary then calls suspend which returns from the OS callback.  Nothing more happens until the next OS callback.

Some people have asked me when I'll release this technology so they can use it.  I'm sorry to say, but I don't intend to do that.  This is for internal use for me to write Smalltalk code for Android and iOS.  I have several problems if I try to commercialize the development technology:

  1. You can't develop code in this environment without a VisualWorks license.  I have such a license with Cincom but it's hard to tell other developers that they need a license for VisualWorks plus a license for my mobile development environment and it's hard to enforce.
  2. I've implemented interfaces to API's I need in the OS but not to everything.  Other developers would need probably access to accelerometers, GPS, Internet, cameras and other things.  I can't have a good commercial product without those interfaces and it's too much work to write these interfaces on speculation.
  3. Too many companies have died trying to provide tools for developers to enhance Smalltalk environments.  The market isn't big enough to justify the effort.
  4. To be honest, software developers make lousy customers.  They tend to be very demanding on everything they need and usually don't like paying money for what they get.  They're used to software development environments being free and open source and don't want to pay for development and maintenance of their toolset.
  5. I'd rather create fun profitable games than creating development environments for other people to use to create fun profitable games.
The graphics for HexSolve were all rendered using POV-Ray.

I hope that answers most of your questions.  Happy Smalltalking.

9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. You just earned your spot on the list of coolest smalltalkers! Frankly I never thought such an interpreter would be a manageable project but you just proved me wrong. Looking forward to lots of VW-based apps :)

    1-4 = you're preaching to the choir. Mobile support is a problem for Cincom to solve when they see enough potential revenue. Frankly I develop for iOS and watchOS almost exclusively these days and I'm pretty happy with Swift and XCode. The value proposition to bring Smalltalk to iOS is limited unless you're prepared for a substantial feature set (including debugging the app "on device" and providing access to iOS frameworks).

    Anyway, very cool stuff and best of luck to you!

    ReplyDelete
    Replies
    1. Thanks, David. I've already had a few questions asking when I'll release this technology for others to use. I'm glad you agree that it's a difficult thing to do.

      Delete
  3. Very cool!

    Good point about devs not wanting to pay for software.

    If someone wants to use Smalltalk on iOS/Android without developing their own interpreter, check out Squeak. It's not trivial to make an app for mobile with it, but certainly easier than creating an interpreter from scratch. E.g. "Pyonkee" is an app in Apple's store that uses the Squeak VM.

    ReplyDelete
  4. We use S8 to build iOS and android native applications with Smalltalk
    Take a look at S8 Smalltalk platforms and U8 tools.
    We do not use a smalltalk VM, nor bytecodes (our intermediate code
    is javascript source code in iOS and android) more freedom on where
    to run; 100% MIT licensed and social development.

    For iOS native applications, 100% of application is written
    in S8 smalltalk, using and creating Objective-C classes
    dynamically on demmand; implementing the methods
    in Smalltalk (of new classes and additions/replacements
    of native classes).

    For Android we use something similar to use and interact
    with Java; and also implement prototype based interfaces
    when required (of course, again, 100% S8 smalltalk).

    In short we use smalltalk, without the old parts; e.g. VM
    and forced encapsulation of contents, that prevents the
    wide use of smalltalk systems in modern execution contexts.

    References: (links to start a tour on S8 and social software dev. with Smalltalk)
    http://u8.smalltalling.net - Main service portal for Social software development with Smalltalk
    http://aleReimondo.no-ip.org/U8 - main community swiki for S8 smalltalk and tools (U8)
    http://aleReimondo.no-ip.org/s8media - media swiki for collaborative development and testing
    https://www.facebook.com/groups/s8smalltalk/ - S8 Smalltalk Facebook group

    cheers,
    Ale
    p.d.: I tried to send this text as an email through squeak-dev list
    but the list refuses to post my email.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    infinite logo design
    logo design uk
    professional app development

    ReplyDelete