KEMBAR78
Challenges of Transpiling Smalltalk to JavaScript | PDF
Challenges of
Transpiling Smalltalk to JavaScript
Noury Bouraqadi & Dave Mason
International Workshop on Smalltalk Technologies Gdansk, Poland; July 1st to 4th, 2025
Why Transpile Smalltalk to JavaScript?
2
Why Transpile Smalltalk to JavaScript?
ST
Simple & clean semantics
Rich IDE & core libraries
3
JS
Ubiquitous deployment
Performance
Large ecosystem
Why Transpile Smalltalk to JavaScript?
ST
Simple & clean semantics
Rich IDE & core libraries
4
Wednesday
July 2nd 2025
JS
Ubiquitous deployment
Performance
Large ecosystem
● ST + JS = Best of both worlds
● Smalltalk community interest in the Web/JavaScript
○ ESUG Main Track Talk Smalltalk for the Web
5
Transpilation = TRANSlation & comPILATION
Transpiler
100%
Smalltalk
Program
100%
JavaScript
Program
6
Transpilation = TRANSlation & comPILATION
Transpiler
100%
Smalltalk
Program
100%
JavaScript
Program
Valid Valid
7
Transpilation = TRANSlation & comPILATION
Transpiler
100%
Smalltalk
Program
100%
JavaScript
Program
Semantically
Equivalent
Valid Valid
Output
Output
Input Input
8
Semantic Equivalence
Transpiler
100%
Smalltalk
Program
100%
JavaScript
Program
9
How to Transpile Smalltalk to JavaScript?
○ Map Smalltalk’s syntax to JavaScript?
○ Map Smalltalk’s reflective kernel to JavaScript?
○ Handle Smalltalk dependencies with the runtime?
10
How to Transpile Smalltalk to JavaScript?
○ Map Smalltalk’s syntax to JavaScript?
○ Map Smalltalk’s reflective kernel to JavaScript?
○ Handle Smalltalk dependencies with the runtime?
11
How to Transpile Smalltalk to JavaScript?
Empirical Study
based on
10 Year
experience with
○ Map Smalltalk’s syntax to JavaScript?
○ Map Smalltalk’s reflective kernel to JavaScript?
○ Handle Smalltalk dependencies with the runtime?
12
How to Transpile Smalltalk to JavaScript?
Challenges Catalog
1. Primitive Types &
Literals
2. Messages
3. Block Closures
4. Classes
5. Reflection
Empirical Study
based on
10 Year
experience with
13
Challenges Catalog
● JS 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑 and 𝑛𝑢𝑙𝑙 are Not Objects.
● JS has an Impoverished Numeric Stack.
● ST Automatically Converts
○ Between Small Integers & Large Ones.
● ST Supports Fixed-Point Arithmetic.
● ST has Literal Symbols.
14
Challenges: Primitive Types and Literals
● JS has a 𝑆𝑦𝑚𝑏𝑜𝑙 class, but:
○ No literal symbols
■ Simple to fix
● Map ST symbols to instances of JS Symbol
○ JS Symbol class is not related to String!
■ Complex to fix
● Make Symbol subclass of String
○ Side-effects?
● Override some String methods
15
Example: ST Literal Symbols
● Non-Alphanumeric Characters in ST Message Selectors
● JS Math-Like Message Priorities
● ST Message Cascading
16
Challenges: Messages
● Simple to fix:
○ Replace non-alphanumeric characters with their Ascii code
○ ST keyword selectors: 1 string concatenating keywords
● Complex to fix: None
17
Example: Non-Alphanumeric Characters in Selectors
Smalltalk Blocks:
● Always Bind the Outer Context
● Always Answer Some Value
● Support Non-Local Returns
18
Challenges: Block Closures
● Simple to fix: ST blocks always answer some value
○ ST result := [123]
○ JS result = () =>{return 123}
● Complex to fix: ST blocks support non-local returns
○ ST condition ifTrue: [^123]
○ JS rely on exceptions
19
Example: Block Returns
● Class Variables
● Pool Variables
● Class Extensions
● Stateful Traits
20
Challenges: ST Classes - 1
● Class Initialization & Startup/Shutdown Lists
● Methods Always Have a Return Value
● Methods can Have Pragmas
○ Primitive Pragmas Refer to the Virtual Machine
21
Challenges: ST Classes - 2
● Simple to fix: Class Variables
○ JS Classes are objects with attributes
○ JS encapsulation is optional
class A{
anInstanceMethod(){
x = A.someClassVariable + 42; }
}
A.someClassVariable = 37;
22
Examples - 1
● Complex to fix: Pragmas such as #primitive:
○ Implement the primitive behavior
23
Examples - 2
● ST Reifies Messages Upon Handling Type Errors
● ST Reifies Execution Contexts
● ST and JS have different solutions for Intercepting
Method Evaluation
● Pharo ST Reifies Slots
● Pharo ST classes define Object Format/Layout
24
Challenges: Reflection
● Simple to fix: ST DNU Reifies Messages
○ Extend JS Object with
■ doesNotUnderstand() method
■ default methods for every sent message in ST Code
Object.prototype.zork = function(arg1, arg2){
return this.doesNotUnderstand(“zork”, arguments)}
25
Examples - 1
● Complex to fix: ST Reifies Execution Contexts
○ ST thisContext
26
Examples
Smalltalk & JavaScript
100% semantic equivalence
via transpilation is
difficult if not impossible!
27
Conclusion
● Some challenges are “easy” to address
○ Primitive Types & Literals,
○ Messages,
○ Block Closures
● Several ST capabilities are complex to implement in JS
○ Primitives,
○ thisContext,
○ Reified Slots…
28
Conclusion: Not all challenges are equal
● Beyond Transpilation
○ ST and JS Run-time interoperability (Production)
○ Live-coding with JS objects from the ST (Development)
○ Reuse JS libraries (code + globals) in code transcribed from ST
○ Transpile ST to produce JS libraries for 3rd parties.
● Methodology?
○ Generalizable to other language pairs? Smalltalk & Python?
29
Future Work
Develop in Pharo, Run on JavaScript
PharoJS.org
Kindly Supported by
Thanks to all the contributors!
M
IT
License
Challenges of
Transpiling Smalltalk to JavaScript
Noury Bouraqadi & Dave Mason
International Workshop on Smalltalk Technologies Gdansk, Poland; July 1st to 4th, 2025

Challenges of Transpiling Smalltalk to JavaScript

  • 1.
    Challenges of Transpiling Smalltalkto JavaScript Noury Bouraqadi & Dave Mason International Workshop on Smalltalk Technologies Gdansk, Poland; July 1st to 4th, 2025
  • 2.
    Why Transpile Smalltalkto JavaScript? 2
  • 3.
    Why Transpile Smalltalkto JavaScript? ST Simple & clean semantics Rich IDE & core libraries 3 JS Ubiquitous deployment Performance Large ecosystem
  • 4.
    Why Transpile Smalltalkto JavaScript? ST Simple & clean semantics Rich IDE & core libraries 4 Wednesday July 2nd 2025 JS Ubiquitous deployment Performance Large ecosystem ● ST + JS = Best of both worlds ● Smalltalk community interest in the Web/JavaScript ○ ESUG Main Track Talk Smalltalk for the Web
  • 5.
    5 Transpilation = TRANSlation& comPILATION Transpiler 100% Smalltalk Program 100% JavaScript Program
  • 6.
    6 Transpilation = TRANSlation& comPILATION Transpiler 100% Smalltalk Program 100% JavaScript Program Valid Valid
  • 7.
    7 Transpilation = TRANSlation& comPILATION Transpiler 100% Smalltalk Program 100% JavaScript Program Semantically Equivalent Valid Valid
  • 8.
  • 9.
    9 How to TranspileSmalltalk to JavaScript?
  • 10.
    ○ Map Smalltalk’ssyntax to JavaScript? ○ Map Smalltalk’s reflective kernel to JavaScript? ○ Handle Smalltalk dependencies with the runtime? 10 How to Transpile Smalltalk to JavaScript?
  • 11.
    ○ Map Smalltalk’ssyntax to JavaScript? ○ Map Smalltalk’s reflective kernel to JavaScript? ○ Handle Smalltalk dependencies with the runtime? 11 How to Transpile Smalltalk to JavaScript? Empirical Study based on 10 Year experience with
  • 12.
    ○ Map Smalltalk’ssyntax to JavaScript? ○ Map Smalltalk’s reflective kernel to JavaScript? ○ Handle Smalltalk dependencies with the runtime? 12 How to Transpile Smalltalk to JavaScript? Challenges Catalog 1. Primitive Types & Literals 2. Messages 3. Block Closures 4. Classes 5. Reflection Empirical Study based on 10 Year experience with
  • 13.
  • 14.
    ● JS 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑and 𝑛𝑢𝑙𝑙 are Not Objects. ● JS has an Impoverished Numeric Stack. ● ST Automatically Converts ○ Between Small Integers & Large Ones. ● ST Supports Fixed-Point Arithmetic. ● ST has Literal Symbols. 14 Challenges: Primitive Types and Literals
  • 15.
    ● JS hasa 𝑆𝑦𝑚𝑏𝑜𝑙 class, but: ○ No literal symbols ■ Simple to fix ● Map ST symbols to instances of JS Symbol ○ JS Symbol class is not related to String! ■ Complex to fix ● Make Symbol subclass of String ○ Side-effects? ● Override some String methods 15 Example: ST Literal Symbols
  • 16.
    ● Non-Alphanumeric Charactersin ST Message Selectors ● JS Math-Like Message Priorities ● ST Message Cascading 16 Challenges: Messages
  • 17.
    ● Simple tofix: ○ Replace non-alphanumeric characters with their Ascii code ○ ST keyword selectors: 1 string concatenating keywords ● Complex to fix: None 17 Example: Non-Alphanumeric Characters in Selectors
  • 18.
    Smalltalk Blocks: ● AlwaysBind the Outer Context ● Always Answer Some Value ● Support Non-Local Returns 18 Challenges: Block Closures
  • 19.
    ● Simple tofix: ST blocks always answer some value ○ ST result := [123] ○ JS result = () =>{return 123} ● Complex to fix: ST blocks support non-local returns ○ ST condition ifTrue: [^123] ○ JS rely on exceptions 19 Example: Block Returns
  • 20.
    ● Class Variables ●Pool Variables ● Class Extensions ● Stateful Traits 20 Challenges: ST Classes - 1
  • 21.
    ● Class Initialization& Startup/Shutdown Lists ● Methods Always Have a Return Value ● Methods can Have Pragmas ○ Primitive Pragmas Refer to the Virtual Machine 21 Challenges: ST Classes - 2
  • 22.
    ● Simple tofix: Class Variables ○ JS Classes are objects with attributes ○ JS encapsulation is optional class A{ anInstanceMethod(){ x = A.someClassVariable + 42; } } A.someClassVariable = 37; 22 Examples - 1
  • 23.
    ● Complex tofix: Pragmas such as #primitive: ○ Implement the primitive behavior 23 Examples - 2
  • 24.
    ● ST ReifiesMessages Upon Handling Type Errors ● ST Reifies Execution Contexts ● ST and JS have different solutions for Intercepting Method Evaluation ● Pharo ST Reifies Slots ● Pharo ST classes define Object Format/Layout 24 Challenges: Reflection
  • 25.
    ● Simple tofix: ST DNU Reifies Messages ○ Extend JS Object with ■ doesNotUnderstand() method ■ default methods for every sent message in ST Code Object.prototype.zork = function(arg1, arg2){ return this.doesNotUnderstand(“zork”, arguments)} 25 Examples - 1
  • 26.
    ● Complex tofix: ST Reifies Execution Contexts ○ ST thisContext 26 Examples
  • 27.
    Smalltalk & JavaScript 100%semantic equivalence via transpilation is difficult if not impossible! 27 Conclusion
  • 28.
    ● Some challengesare “easy” to address ○ Primitive Types & Literals, ○ Messages, ○ Block Closures ● Several ST capabilities are complex to implement in JS ○ Primitives, ○ thisContext, ○ Reified Slots… 28 Conclusion: Not all challenges are equal
  • 29.
    ● Beyond Transpilation ○ST and JS Run-time interoperability (Production) ○ Live-coding with JS objects from the ST (Development) ○ Reuse JS libraries (code + globals) in code transcribed from ST ○ Transpile ST to produce JS libraries for 3rd parties. ● Methodology? ○ Generalizable to other language pairs? Smalltalk & Python? 29 Future Work
  • 30.
    Develop in Pharo,Run on JavaScript PharoJS.org Kindly Supported by Thanks to all the contributors! M IT License
  • 31.
    Challenges of Transpiling Smalltalkto JavaScript Noury Bouraqadi & Dave Mason International Workshop on Smalltalk Technologies Gdansk, Poland; July 1st to 4th, 2025