Skip to content
brett hartshorn edited this page Sep 15, 2015 · 17 revisions

regression test results

gotchas

In general, the basic limitations of the JavaScript backend are constrained and differ from regular Python in much the same way as RapydScript. Note that Rusthon's JS backend and RapydScript can be used together, and work well when mixed together, see this example

see also: https://github.com/rusthon/Rusthon/blob/master/examples/javascript_gotchas.md

  • the only form of string replacement that is supported is "%s" %n. The string.format method is not supported. If you want fancy string replacement, then you can directly use sprintf.js
  • a list and tuple are the same type, both are translated into regular javascript arrays.
  • a tuple can not be used as a dictionary key.
  • the keys in a dictionary literal are always treated as string literals, unless you wrap them in brackets [mykey] which is the new standard in ES6. Otherwise, if you need to use variables as keys, then you need to construct an empty dict first, and then set keys on it with mydict[myvar]=n. more info
  • classes you define in Rusthon can be constructed without using new, but classes defined in an external library need to be wrapped with new like this: a = new( THREE.Object3D() )
  • methods can not be directly passed as callback functions, because in javascript you need to worry about the calling-context-of-this. To properly pass a method as a callback use bind like this: F( ob.mymethod.bind(ob) ) where F is a function that takes a callback.
  • dictionary (object) keys are always strings, however the builtin method mydict.keys() will return an array of numbers if all the keys are string integers, this allows you to do things like max( mydict.keys() ) to get the key of the highest number.
  • class attributes must be accessed using their class names like this MyClass.somevar, you can not use this form self.somevar.
  • when using external javascript libraries, you must use new SomeClass() to create instances.
  • when calling functions in external javascript, you can use named keyword parameters in function calls only if the external function takes an object as its last argument, often this is the case because there is an informal standard of passing an options object as the last argument.
  • python is a very type safe language, while javascript is not. In regular python 1+"2" will raise TypeError: unsupported operand type(s) for +: 'int' and 'str'. While in javascript 1+"2" causes no error at all, and instead returns "12". The transpiler currently does not check for these types of mistakes, so it is up to you to not make them. As a workaround you can use static typing, and trap type errors when calling functions.

workarounds

  • bad javascript type safety

javascript allows you to create c which becomes "12" and is an error, a runtime error will be raised when c is passed to somefunc because it requires an integer.

def somefunc( x:int ):
  ...
a = 1
b = "2"
c = a + b
somefunc( c )
  • using variables as dict keys

in regular python this is perfectly valid:

something = ['foo', 'bar']
objects   = []
for KEY in something:
   ob = {KEY: 1}
   objects.append( ob )

above objects will be: [{'foo':1}, {'bar':1}], and this correct in regular Python. however, this is not what happens when translated to JavaScript, instead objects would be: [{'KEY':1}, {'KEY':1}], this is bad! The workaround is this:

something = ['foo', 'bar']
objects   = []
for KEY in something:
   ob = {}
   ob[ KEY ] = 1
   objects.append( ob )

above objects will be: [{'foo':1}, {'bar':1}]

  • class attributes that refer to other class attributes

the following is valid python, but will fail in Rusthon.

class MyClass:
  A = 1
  B = A + 1

in order for above to work in Rusthon the class name must be used to refer to A, like this:

class MyClass:
  A = 1
  B = MyClass.A + 1

specialized syntax

features

  • multiple inheritance
  • operator overloading (explicit)
  • function and class decorators
  • getter/setter function decorators
  • list comprehensions
  • regular and lambda functions
  • function calls with *args and **kwargs

Language Keywords

  • global, nonlocal
  • while, for, continue, break
  • if, elif, else,
  • switch, case
  • try, except, raise
  • def, lambda
  • new, class
  • from, import, as
  • pass, assert
  • and, or, is, in, not
  • return, yield

HTML DOM Iterables

for item in iterable

  • NodeList
  • FileList
  • ClientRectList
  • DOMSettableTokenList
  • DOMStringList
  • DataTransferItemList
  • HTMLCollection
  • HTMLAllCollection
  • SVGElementInstanceList
  • SVGNumberList
  • SVGTransformList

Operator Overloading

TODO

  • add
  • mul

builtins

  • dir
  • type
  • hasattr
  • getattr
  • setattr
  • issubclass
  • isinstance
  • dict
  • list
  • tuple
  • int
  • float
  • str
  • round
  • range
  • sum
  • len
  • map
  • filter
  • min
  • max
  • abs
  • ord
  • chr
  • open (nodejs only)

List

  • list.append
  • list.extend
  • list.remove
  • list.insert
  • list.index
  • list.count
  • list.pop
  • list.len
  • list.contains
  • list.getitem
  • list.setitem
  • list.iter
  • list.getslice

Set

  • set.bisect
  • set.difference
  • set.intersection
  • set.issubset

String

  • str.split
  • str.splitlines
  • str.strip
  • str.startswith
  • str.endswith
  • str.join
  • str.upper
  • str.lower
  • str.index
  • str.find
  • str.isdigit
  • str.format
  • str.getitem
  • str.len
  • str.getslice

Dict

  • dict.copy
  • dict.clear
  • dict.has_key
  • dict.update
  • dict.items
  • dict.keys
  • dict.get
  • dict.set
  • dict.pop
  • dict.values
  • dict.contains
  • dict.iter
  • dict.len
  • dict.getitem
  • dict.setitem