Web Pages Via Forth

Forth. You either love it or you hate it. If you have struggled to work on tiny microcontrollers, you probably are in the first camp. After all, bringing up a minimal Forth system is pretty simple and requires very little resources on the CPU. Once you have such an environment it is then easy to extend Forth in Forth. [Remko] decided he wanted to build a Forth compiler that uses WebAssembly and runs in your browser. Why? We’ve learned not to think about that question too much.

The world has changed a lot since the first introduction of the WorldWideWeb browser in 1990. What started out as a way to show text documents over the network has become — for better or worse — an application platform. JavaScript won the browser scripting language wars and security concerns pretty much killed Java applets and Flash. But JavaScript isn’t always fast. Sure, there are ways to do just in time compiling, such as Google’s V8 engine. But that compile step takes time, too. Enter WebAssembly (or Wasm).

Just as a C programmer might write certain parts of her code in assembler for performance, a JavaScript developer can build speed-critical code in Wasm. Wasm defines a text format and a binary format that is easy for the browser to digest and execute. The Forth compiler uses mostly the text format with some JavaScript for a few functions. Of course, a lot of it is also written in Forth.

If you hate Forth, you’ve probably already quit reading by now. We’ve enjoyed using Forth on the BluePill which makes for a dirt cheap development system. We’ve also seen Forth in JavaScript before. It would be fun to benchmark the JavaScript and Wasm versions and see how Wasm fares.

16 thoughts on “Web Pages Via Forth

  1. There are so many plugin compiled languages for a browser now.

    There are also some interpreters and VMs as well.

    JavaScript itself is slow, chunky and resource hungry for the asynchronous tasks we see more of in modern web apps.

    LUA is probably a better browser plugin language as it’s elegant for asynchronous tasks and if you have some experience with JavaScript then LUA is not that hard to learn.

    LUA is a little like Lisp in some regards as even the data structures can first class. That is a function can be data and data can be a function.

    LUA has a lot of nice surprises about what you “can do” as opposed to other languages that have nasty surprises about what you “can’t do”.

    1. “JavaScript itself is slow, chunky and resource hungry for the asynchronous tasks we see more of in modern web apps”

      The problem isn’t Javascript, which is actually incredibly fast for a scripting language. What makes it slow is that simple-looking web pages often include multiple huge libraries and drag in megabytes of fonts, GIFs, etc. If you could snap your fingers and change history so that Netscape pushed Lua instead of javascript, most web apps would still be slow.

        1. That depends on who you talk to.

          In the case of JavaScript – older people remember the pain of the browser wars and JavaScript that would only run in the browser you wrote the code in until extensive modification. There were no libraries commonly used then. It was custom code.

          JavaScript is much more cross browser compatible now so younger people are more comfortable with it.

          Also everything JavaScript now uses a libraries so younger coders have a much better organized and sensible code namespace and that makes things easier as well.

          I can’t comment on Java today as I haven’t used it for a long time but the most common complaint I hear about Java can be summarized by the following pseudocode –

          While (1)

  2. Hi Folks. Reading about Forth is certainly a trip backwards to younger and finer days. I started learning Forth on a IIgs then a //c and finally a ][+ then and finally on a monochrome PC. Heady days certainly, DOS, CP/M, USCD Pascal on one PC! It was just too much for a high school kid to absorb in one day, so I spent the rest of my years doing so. Forth, Cobol, RPG, asm’s, Forth, Pascal, and so many others of all subjects, machines, opsyses,hardware. Finally Linux.

    I learned how to think analytically from Charles, Albert, Niklaus, and so many other men who were the luminaries of though and programming in those days.

    Forth was my first true love in any language, written or spoken. It was small, fast, light, portable, and simply confoundingly complex.

    I didn’t truly begin to comprehend it until I began to try to write my own Forth engine. I preferred indirect threading in 32bits on Linux using Fasm. Took more than a few interations to figer out the cause of the faulting, but I did. It was a mental coup for me.

    Then I found Lisp. Hated the parens, but after learning it too, I discovered more similarities between the two languages than differences. So much as to be able to easily port concepts and functions! That was fun… Cons, car, and cdr in forth certainly did simplify alot of code. Actually made Factoring easier in Forth! Designing words too became easier, and allowed more flexibility in the headers too.

    My 32bit, x86 code had separated code and data segs, as any normal ELF object. All code addresses were stored in dataseg pointer vars, just as in C, and reg swapping ESP and EBP didn’t cause any problems at all.

    After a few simple words were coded in asm, I began writing the high level ones that were easy, then assembling, until I had a decent incomplete dictionary.

    Connecting to studio needed a little pondering because I didn’t want to use the libs/. Eventually, I did figer that out too.

    And so it went Wirth’s successive refinement, and Liberal incrementalism! LoL.

    It’s been some years, but I was quite fond of saying back then that I have forgotten more knowledge from more subjects than most people learn in one lifetime. It’s still true, more so now. Swiss cheese ram.

    Stay well and remember that Simple is always good. flexible code is always better so that User can do what you don’t expect them to do. They will do the unexpected with code that Coders write.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.