InfoQ HomepageArticlesTranscrypt: Anatomy of a Python to JavaScript Compiler
Transcrypt: Anatomy of a Python to JavaScript Compiler
Mar 08, 201717min read
reviewed by
Write for InfoQ
Feed your curiosity.Help 550k+ globalsenior developers
each month stay ahead.Get in touch
Key Takeaways
- Languages that run in the browser should precompile to JavaScript isomorphically for compactness, execution speed and development speed
- For efficient cooperation on a large web application, module boundaries should coincide with team boundaries
- Modules may have dynamic typing on the inside, but should have static typing on the outside
- Having the same technology on the client and on the server promotes scalability
- The future of Python in the browser is tied to the future of Python in general, not so much to a particular implementation
Featuring a diversity of programming languages, backend technology offers the right tool for any kind of job. At the frontend, however, it's one size fits all: JavaScript. Someone with only a hammer will have to treat anything like a nail. One attempt to break open this restricted world is represented by the growing set of source to source compilers that target JavaScript. Such compilers are available for languages as diverse as Scala, C++, Ruby, and Python. The Transcrypt Python to JavaScript compiler is a relatively new open source project, aiming at executing Python 3.6 at JavaScript speed, with comparable file sizes.
For a tool like this to offer an attractive alternative to everyday web development in JavaScript, at least the following three demands have to be met:
- From a user point of view, web sites and web applications created with it should be indistinguishable with regard to look and feel, page load time, page startup time and sustained speed
- From a developer point of view, it should allow seamless access to any JavaScript library, efficient debugging and the opportunity to capitalize on existing skills
- From a business point of view, it should offer continuity, availability of a large pool of professionally trained developers, a good ratio of created functionality to invested hours and a resulting application open to changing needs
To be successful, all aspects of these three requirements have to be met. Different compilers strike a different balance between them, but no viable compiler for every day production use can neglect any of them. ForTranscrypt, each of the above three points has led to certain design decisions.
Architecting Intelligent Applications with GenAI: A Hands-On Guide
Related Sponsored Content
Related Sponsor
%2ffilters%3ano_upscale()%2fsponsorship%2ftopic%2fe8c439e9-1abf-40b5-97cc-4b161b1c209e%2fMicrosoftAzureLogoRSB-1727169232499.png&f=jpg&w=240)
Free services for AI apps. Are you ready to experiment with cutting-edge technologies? You can start building intelligent apps with free Azure app, data, and AI services to minimize upfront costs.Learn More.
Demand 1:
Look and feel of web sites and web applications are directly connected to the underlying JavaScript libraries used, so to have exactly the same look and feel, a site or application should use exactly the same libraries.
Although fast connections may hide the differences, achieving the same page load time, even on mobile devices running on public networks, mandates having roughly the same code size. This rules out downloading a compiler, virtual machine or large runtime at each new page load.
Achieving the same startup time as pages utilizing native JavaScript is only possible if the code is statically precompiled to JavaScript on the server. The larger the amount of code needed for a certain page, the more obvious the difference becomes.
To have the same sustained speed, the generated JavaScript must be efficient. Since JavaScript virtual machines are highly optimized for common coding patterns, the generated JavaScript should be similar to handwritten JavaScript, rather than emulating a stack machine or any other low level abstraction.
Demand 2:
To allow seamless access to any JavaScript library, Python and JavaScript have to use unified data formats, a unified calling model, and a unified object model. The latter requires the JavaScript prototype based single inheritance mechanism to somehow gel with Python’s class based multiple inheritance. Note that the recent addition of the keyword 'class' to JavaScript has no impact on the need to bridge this fundamental difference.
To enable efficient debugging, things like setting breakpoints and single stepping through code have to be done on the source level. In other words: source maps are necessary. Whenever a problem is encountered it must be possible to inspect and comprehend the generated JavaScript to pinpoint exactly what's going on. To this end, the generated JavaScript should be isomorphic to the Python source code.
The ability to capitalize on existing skills means that the source code has to be pure Python, not some syntactic variation. A robust way to achieve this is to use Python's native parser. The same holds for semantics, a requirement that poses practical problems and requires introduction of compiler directives to maintain runtime efficiency.
Demand 3:
Continuity is needed to protect investments in client side Python code, requiring continued availability of client side Python compilers with both good conformance and good performance. Striking the right balance between these two is the most critical part of designing a compiler.
Continued availability of trained Python developers is sufficiently warranted by the fact thatPython has been the number 1 language taught in introductory computer science courses for three consecutive years now. On the backend it is used for every conceivable branch of computing. All these developers, used to designing large, long lived systems rather than insulated, short lived pieces of frontend script code, become available to browser programming if it is done in Python.
With regard to productivity, many developers that have made the switch from a different programming language to Python agree that it has significantly increased their output while retaining runtime performance. The latter is due to the fact that libraries used by Python applications for time critical operations like numerical processing and 3D graphics usually compile to native machine code.
The last point – openness to changed needs – means that modularity and flexibility have to be supported at every level. The presence, right from the start, of class-based OO with multiple inheritance and a sophisticated module and package mechanism has contributed to this. In addition, the possibility to use named and default parameters allows developers to change call signatures in a late stage without breaking existing code.
Conformance versus performance: language convergence to the rescue
Many Python constructs closely match JavaScript constructs, especially when translating to newer versions of JavaScript. There's a clear convergence between both languages. Specifically, more and more elements of Python make their way into #"img://fotoberg.JPG" alt="" data-src="articles/transcrypt-python-javascript-compiler/en/resources/fotoberg.JPG" src="https://imgopt.infoq.com/fit-in/3000x4000/filters:quality(85)/filters:no_upscale()/articles/transcrypt-python-javascript-compiler/en/resources/fotoberg.JPG" rel="share">Jacques de Hooge MSc is a C++/Python developer living in Rotterdam, the Netherlands. After graduating from the Delft University of Technology, department of Information Theory, he started his own company, GEATEC engineering, specializing in Realtime Controls, Scientific Computation, Oil and Gas Prospecting and Medical Imaging. He is a part-time teacher at the Rotterdam University of Applied Sciences, where he teaches C++, Python, Image Processing, Artificial Intelligence, Robotics, Realtime Embedded Systems and Linear Algebra. Currently he's developing cardiological research software for the Erasmus University in Rotterdam. Also he is the initiator and the lead designer of the Transcrypt open source project.
This content is in theWeb Development topic
Related Topics:
Related Editorial
Popular across InfoQ
Ultra-Fast In-Memory Database Applications with Java
Beyond the Gang of Four: Practical Design Patterns for Modern AI Systems
AWS Lambda Introduces Tiered Pricing for CloudWatch Logs and Expands Logging Destinations
How Allegro Does Automated Code Migrations for over 2000 Microservices
Windsurf Launches SWE-1 Family of Models for Software Engineering
How developer platforms, Wasm and sovereign cloud can help build a more effective organization
The InfoQ Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers.View an example