What scripting languages are similar to ECMA? - javascript

I am looking for an ECMAScript alternative to work as a scripting language for custom application logic. There are things I like about ECMA, especially the syntax for the newer spec(like AS3).
Unfortunately, the AS3 engine is not open source to be able to integrate in a project. I would like a scripting language that was designed for object oriented use.
Specifically, is there a language that has:
Statically typed variables(optional)
Classes, including public/private members
Inheritance, including Interfaces
Packages(optional)
Clean syntax
Must be able to interface as an internal scripting language for an application(like Javascript for a browser), can not be an external system call.
Things I would rather do without
The messy ECMA prototype object
What languages that you know about fit this profile? I've had difficulty finding a quality scripting language that was designed for good object oriented design.

In Java the best ECMAScript (Javascript) option is to embed Rhino. I don't find the prototype-based inheritance a deal killer, as with a bit of discipline you can make it behave almost like class-based inheritance.
If you want something that interoperates very closely with Java, go with Groovy. If you need JVM compatibility, also look into Jython (python on the JVM), Jruby (Ruby on the JVM) and Scala (a functional language on the JVM). If you don't care about Java and JVM compatibility, look at Ruby, Python, and Erlang. Clojure is a dialect of Lisp on the JVM.
Going further afield, TCL (Tool Command Language) lets you embed an interpreter in C/C++ code, there are many embeddable Lisp and Scheme interpreters, etc.

If you want a scripting language that works like ECMAScript, why not use ECMAScript? There are many Open Source implementations, just take a look at the list on Wikipedia.

I'd recommend either Python or Ruby. Neither are like ECMA, but I learned them after JavaScript, and they were a snap to pick up. Plus, they are more powerful languages, making it a better alternative to using a JavaScript engine inside of your application (Rhino for Java).
Python
Forces clean syntax (almost like English while is not False:)
Multiple inheritance (no interfaces)
Interpreter can be extended using C/C++ (possibly used for your adapters, if needed)
Ruby
Syntax is supposed to be close to English (unless conditional, until loop)
Everything is an object
Only supports single inheritance, but uses Mixins to add functionality
Both
Classes
Can be embedded in another application
Private members
Packages

Lua - everything you want and more in ~100KB
See this page for comparison betwen Lua and other mentioned languages.

Haxe on Neko looks like the exact thing you want. I don't know how embeddable nekovm is, but it is opensource so you can fiddle with it. http://haxe.org

The Ruby interpreter can also be embedded within C programs, and may be considered by some to be more object-oriented than Python.

We use ECMAscript as an extension language for the software product I work on and it works quite well. Being a standard (and popular) language, it's nice to be able to point our users to the copious off the shelf documentation for the language. We built our own ECMAscript compiler that translates into bytecode for the platform we are running on, but if I had it to do over again, I'd seriously consider embedding Google's V8 ECMAscript engine (in fact, I'd even consider building our entire app on it).

I'd go with something fairly mainstream to simplify things. Having read your requirements, I'd recommend Python. It doesn't really have interfaces in the Java/C# way, but it doesn't really need them, either. Other than that, it should be a good fit.

QtScript is ECMAScript. You don't mention what the main part of your application is written in, but I suppose it's not Qt, or you'd already know about QtScript.

Related

Is Javascript any better than before as mentioned in the O'Reilly Javascript Patterns book for closures and anonymous functions?

This is a quote from the O'Reilly Javascript Patterns book:
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed. Interestingly, languages
such as Java and PHP started adding features such as closures and
anonymous functions, which JavaScript developers have been enjoying
and taking for granted for a while.
and that's it. I really don't understand how Javascript was considered "deficient" before, and now is not, because other languages like Java or PHP has added closures and anonymous functions? Aren't they just generic computing concepts? Aren't they available in other languages like Ruby? So I don't really know how Javascript is now not "deficient" because Java and PHP added closures and anonymous functions as their features? Why is that?
I think what it's referring to is that in the past many developers considered JavaScript as a 'toy' language and only used it to do quick Web UI tasks like validation etc. without bothering to understand how the language really worked.
In recent years the 'hidden' features of JavaScript such as closures, prototypal inheritance etc. have come to the fore and people are now taking JavaScript much more seriously as a 'real' language.
So JavaScript was never really "deficient" but people may have thought that it was due to their misconceptions about the language.
I personally think it's a poor editing job.
The paragraph should have read (bold addition is mine and is just a suggestion on how
to read it):
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed due to a better and more uniform browser support, extensive work done by various ECMA editions and evolution of various JavaScript frameworks. All, or much, of that change is a direct result of ever-expanding movement of software products to the Web and growing demand for a lightweight language for mobile applications (this one can be somewhat argued).
Interestingly, languages such as Java and PHP started adding features
such as closures and anonymous functions, which JavaScript developers
have been enjoying and taking for granted for a while.
For years, JavaScript has been known to only be a browser language, for (simple) features that required more dynamic and/or flexibility than offered by HTML and CSS.
For several years now, JavaScript evolved as a language and as platform, offering powerful libraries, which also removed certain cross-browser incompatibilities. With rise of these libraries, community actually only started to learn how to use some powerful concepts of JavaScript, such as closures and prototypal inheritance.
They have not been widely used nor known even to JavaScript developers before, since JavaScript is in its basics very simple language, and most developers didn't even have to learn it as you'd learn Python, C or Java - all they had to look at were tutorials and many of them didn't include these language features.
Duo to the increase in popularity, JavaScript (officially ECMAScript) has received several 'editions', where features have been added to the language itself. Latest edition (still under development) will introduce some radical novelties (considered are classes, module system, iterators, algebraic types among others), so JS will become even better, more structured and more easily maintainable language.
JavaScript is raising in popularity on server side also (node.js), and proves to be easy, fast and enough powerful language to do (almost) any kind of work. Node widely 'abuses' probably its best feature - asynchronous nature of JavaScript. That is not new, many languages have async libraries, but as Ryan Dahl said in one of his earliest speeches on node, JS is perfect for it. And mainly because of the closures and other concepts built into it.
So final answer would be the author of the book you're quoting has poorly expressed his thoughts or is simply wrong. It was a toy language and didn't mean to be used for such variety of use cases as it is today. But it evolved, just as Java or PHP did (don't forget PHP didn't have OOP support that seems so obvious now until v5). Other languages are just picking up goodies that JavaScript proved to be more efficient for certain use cases. Overall, my thoughts are that JavaScript hasn't been used properly from the start, and that's the case it had been called 'deficient'. Now developers got chance to explore its magnificence.

Are there any prototype-based languages with a whole development cycle?

Are there any real-world prototype-based programming languages with a whole development cycle?
"A whole development cycle" like Ruby and Python: web frameworks, scripting/interacting with the system, tools for debugging, profiling, etc.
Thank you
A brief note on PBPLs: (let's call these languages PBPL : prototype-based programming language)
There are some PBPLs out there. Some are being widely used like JavaScript (which Node.js may bring it into the field - or may not!). One other language is ActionScript which is also a PBPL but tightly bound to Flash VM (is it correct to say so?).
From less known ones I can speak of Lua which has a strong reputation in game development (mostly spread by WOW) but never took off as a full language. Lua has a table concept which can provide you some sort of prototype based programming facility.
There is also JScript (Windows scripting tool) which is already pointless by the newcomer PowerShell (I have used JScript to manipulate IIS but I never understood what is JScript!).
Others can be named like io (indeed very very neat, you will fall in love with it; absolutely impossible to use) and REBOL (What is this all about? A proprietary scripting tool? You must be kidding!) and newLISP (Which is actually a full language, but no one ever heard about it).
For sure there are much more to list here but either I do not remember or I did not understood them as a real world thing, like Self).
I would argue that JavaScript is a real world language. The main difference is that it tends to be embedded into lots of different environments rather than being a stand alone development environment.
Aside from the obvious use in a browser, JavaScript can be used on the server side using CouchDB (which is becoming very popular as a database both for websites and the desktop) or Node.js - there are lots of others as well. It can also be used to create desktop applications via seed or gjs.
As for debugging tools, these are included in most web browsers and there are extensions such as firebug.
The approach is different in JavaScript - instead of having a core language and using libraries to access different programming environments, it is embedded directly into these environments.
It doesn't look like it. Checking out the Wikipedia list, I can't say any of them are particularly popular for systems-type or standalone programming. And I have a few theories why:
Inertia - people are more familiar with procedural/procedure-class based languages. In the same way procedural languages are more popular than functional, I think we find it easier to mentally keep track of the objects we've specified beforehand
Less error-prone - When I use a class, I can't use things not specified in that class. It's a bit less flexible, but when working on a large project I'm very, very grateful to be able to look at the spec for an object (even if I've written that object) and know how the object will behave. That is, I know the compiler will probably throw an error if I try to do something stupid like use the wrong variable name.
You mention "scripting/interacting with the system". As far as that goes, classes aren't even particularly popular. One-off scripts may use a few functions, or even just straight procedural code. I know if I write a simple little Python script, I'll have a few simple functions and I'll use builtin objects. It's OO, but I'm not writing anything.
No/not many suitable languages. This is I think the biggest one. Lua's great for embedded scripting, but I don't think it can replace Python/Perl scripts in my toolkit.
Prejudice - the sordid past of Javascript has not been good for prototype-based languages, I don't think. Javascript's actually a pretty nice language, once you see code written by somebody that understands it, but there's still a lot of derision - it's seen like a kiddie language. If you say "I'm a Javascript programmer", it's hard to be taken seriously.
EDIT It's either a sign or a symptom, but most PT languages are embedded or application-specific scripting languages (Lua in games or for UIs, Javascript in webpages or for the UI like in Firefox, etc). I'm not sure why this is, but they are either more suited to application customizing or otherwise-embeddedness, or that's what the common implementations are designed for. Python can be easily embedded into a program, and it sometimes is, but it's more common standalone.
This may be more philosophical than you're looking for, and I'm by no means a prototype-based expert, but I've done a fair bit of Javascript and fiddled with Lua. I stand by my answer, though, based on at least personal experience. YMMV.
I see no reason why a PT language as useful as Python couldn't be written, but it doesn't seem like anyone's done it.
I did not found one and the only candidate (JavaScript) is still spending very first steps (like Node.js).
Still one other valid candidate would be JavaScript! There is a .NET based implementation named IronJS which is implemented in F# and is going pretty well. This way one can have whole .NET development cycle at his toolbox. It is based on DLR and can be used in both .NET and Mono!

What “type” of language is JavaScript?

Programming languages are often classified by their structure. To date there are many types, including (but not limited to): Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted (Source). Some programming languages are a mixture of these constructs, while others may fall under only one of the headings listed above.
My question is: What type of language can JavaScript be classified as?
The Javascript Wikipedia entry tells you.
JavaScript (/ˈdʒɑːvəˌskrɪpt/), often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.
Paragraph three goes on to say:
As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles (emphasis added). It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).
Regarding your selection of Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted as "types" of a computer language, Javascript is quite powerful; it is at least (or can be) Procedural, Structured, Object-Oriented, Functional, Event-Driven and Interpreted.
However, as your source says, many languages do not fall into one category only, but can be used in different ways. Even for example Java could be used in a rather strict procedural way.
And although Javascript supports functional programming, it is not the lingua franca of functional programmers - but they could use it, if they wanted to.
Javascript is so versatile, because it is prototype-based and because you can pass functions as parameters to other functions, which really allows for some crazy programming techniques (read: HACKS) combined with dynamic creation of Javascript it can be really powerful and confusing as well :)
You might want to look up http://en.wikipedia.org/wiki/Prototype-based_programming for more explanations.
Mozilla Developer Network
...
JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes (see more about prototypical inheritance and ES2015 classes). JavaScript also supports functional programming — functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
...
JavaScript is an implementation of the ECMAScript language standard and is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites. This enables programmatic access to computational objects within a host environment.

What compilers target JavaScript runtimes?

I am using GWT, which includes a Java-to-JavaScript compiler. Before this project, targeting the JavaScript runtime from a different language hadn't occurred to me, and I'm enjoying the GWT experience.
A quick search revealed Java2Script as another Java-to-JavaScript solution. Are there any other mature compilers that target the JavaScript runtime?
List of languages that compile to JS
You also have Haxe. It features static, structural, strong and inferred typing; algebraic data types; lambda expressions with closure support; a module system and can compile not only to JavaScript but also Flash, C++, Neko, PHP. Java support is under development too.
Pyjamas compiles Python to JavaScript.
HotRuby runs Ruby source code under JavaScript and Flash.
Orto is (was?) a JVM implemented in JavaScript. (Original site is down; link is to a blog entry.)
The ZK framework, which likes to compare themselves favorably against GWT, uses Java and XML to generate JavaScript. (They claim that it takes far less code than GWT to do the same stuff.)
And I have heard it well-argued that jQuery is a separate language that happens to be implemented in, and run under, JavaScript. :-)
OpenLaszlo is on open source XML -> Javascript (technically DHTML, also can compile to Flash) compiler. I've played with it a little bit and it seemed interesting, although have never used it seriously (bad IDE support when I tried it).
Objective-j is a similar project that resembles objective-c, although runs as javascript (not sure if it compiles it or has a js interpreter). I don't know much about it, but do see posts about it on the Ajaxian from time to time.
Parenscript is "a translator from a small Lispy language to JavaScript". It's not Common Lisp -- it's mostly just Lisp syntax for Javascript, though for simple things you can write the same code to target both, if you're careful. I'm not sure I'd call it "mature", either.
While there are other products which compile to javascript (noted in the other comments), I believe that GWT is, by far, the most mature one out there, in terms of real-world usage.
The simple fact that a number of Google's core applications use GWT (e.g.: the new adwords GUI, Google wave, etc) means you can have confidence the product is going to be maintained for at least the next few years, it isn't just going to fade away anytime soon.
Another thing to keep in mind is that GWT is far more than a java-to-javascript compiler, it also is an optimizing compiler, it has hosted mode, it is fully interoperable with native javascript, it does image bundling, it does code spliting in the new version, etc.
Script #
JSC
cappuccino (sort of)
The inventors of Clojure (a JVM based Lisp dialect) launched ClojureScript in mid-2011. ClojureScript compiles Clojure source code into JavaScript, making use of the Google Closure compiler and the Closure framework. ClojureScript code can run both in the browser client, or on node.js servers.
Here's a shameless plug for a project I started and am continuing to develop: scxml-js, a Statecharts-to-JavaScript compiler

Non-web Javascript frameworks

Are there any good JavaScript frameworks out there which primary audience is not web programming? Especially frameworks/libraries which improves the object orientation?
The framework should be usable within an desktop application embedding a JavaScript engine (such as Spidermonkey or JavaScriptCore), so no external dependency are allowed.
As far as "improving object orientation" goes, Javascript is already great. You just need to get used to thinking in prototypes instead of classes.
After reading Douglas Crawford's great page on prototypal inheritance I really started to enjoy working with javascript. (He also has a page on class inheritance if you must use classes.)
Edit: If by asking for a framework you also mean, helpful libraries that don't use the DOM, you might be interested in Functional Javascript.
Dojo can be used (and is used) in non-browser environments (e.g., Rhino, Jaxer, SpiderMonkey). It can be easily adapted for other environments too — all DOM-related functions are separated from functions dealing with global language features.
dojo.declare() (more docs) comes in the Dojo Base (as soon as you load dojo.js) and implements full-blown OOP with single- and multiple- inheritance, automatic constructor chaining, and super-calls. In fact it is the cornerstone of many Dojo facilities.
Of course there are more low-level facilities like dojo.mixin() to mix objects together and dojo.extend() to extend a prototype dynamically.
More language-related features can be found in dojox.lang. Following parts of it are thoroughly explained and documented: functional, AOP, recursion combinators.
Dojo comes with other batteries included from string-related algorithms to the date processing. If you are interested in those you can discover them yourself, or contact the Dojo community.
Objective-J(avascript) is one. Is that the kind of thing you are looking for?
The most widely known one is XULRunner from Mozilla. This is framework that FireFox and Thunderbird are built on.
https://developer.mozilla.org/en/XULRunner
Although not strictly javascript only, it incorporates a host of technologies closely tied to web for the purposes of building desktop applications.
Have you looked at Adobe Air? It allows you to use JavaScript/AJAX to write Desktop applications.
http://www.adobe.com/products/air/

Categories

Resources