A complete JavaScript (ECMAScript 5.1) parser and interpreter written in Delphi/Object Pascal.
JavaScript4D enables Delphi applications to parse and execute JavaScript code natively. It's designed to allow LLMs to write JavaScript functions that can be executed by Delphi code, bridging the gap between AI-generated scripts and native Delphi applications.
- Complete ES5.1 Support: Full ECMAScript 5.1 specification compliance
- Pure Delphi: No external dependencies or DLLs required
- Recursive Descent Parser: Clean AST-based architecture
- Built-in Objects: Array, String, Number, Object, Math, JSON, Date, RegExp support
- Higher-Order Functions: map, filter, reduce, forEach, find, some, every
- Closures: Full support for lexical scoping and closures
- Error Handling: Try/catch/finally with TypeError, RangeError, SyntaxError, etc.
- Native Binding: Easy integration between Delphi and JavaScript
uses
JS4D.Engine;
var
Engine: TJSEngine;
begin
Engine := TJSEngine.Create;
try
// Simple expression
var Result := Engine.Evaluate('1 + 2 * 3');
WriteLn(Result.ToString); // Output: 7
// Variables and functions
Engine.Execute('var x = 10;');
Engine.Execute('function double(n) { return n * 2; }');
Result := Engine.Evaluate('double(x)');
WriteLn(Result.ToString); // Output: 20
// Array operations
Engine.Execute('var nums = [1, 2, 3, 4, 5];');
Result := Engine.Evaluate('nums.map(function(x) { return x * 2; }).join(",")');
WriteLn(Result.ToString); // Output: 2,4,6,8,10
// Object literals
Engine.Execute('var person = { name: "John", age: 30 };');
Result := Engine.Evaluate('JSON.stringify(person)');
WriteLn(Result.ToString); // Output: {"name":"John","age":30}
finally
Engine.Free;
end;
end;- Variables (
var) - Functions (declaration and expression)
- Control flow (
if,else,for,while,do-while,switch) - Operators (arithmetic, comparison, logical, bitwise)
- Object literals and property access
- Array literals and indexing
- Try/catch/finally
Array Methods
push,pop,shift,unshiftindexOf,lastIndexOf,includesjoin,slice,splice,concat,reversemap,filter,reduce,reduceRightforEach,find,findIndexsome,every,sortfill,copyWithin,flat
String Methods
charAt,charCodeAtindexOf,lastIndexOf,includessubstring,substr,slicesplit,trim,trimStart,trimEndtoLowerCase,toUpperCasereplace,concat,repeatstartsWith,endsWithpadStart,padEnd
Object Methods
Object.keys,Object.values,Object.entriesObject.assign,Object.createhasOwnProperty
JSON
JSON.parse,JSON.stringify
Math
- All standard Math functions (
abs,floor,ceil,round,sqrt,pow,min,max,random,sin,cos,tan, etc.)
Global Functions
parseInt,parseFloatisNaN,isFiniteArray.isArray,Array.fromDate.now,Date.parse
JavaScript Source Code
│
▼
┌─────────────────┐
│ Lexer │ → Tokens
└─────────────────┘
│
▼
┌─────────────────┐
│ Parser │ → Abstract Syntax Tree (AST)
└─────────────────┘
│
▼
┌─────────────────┐
│ Interpreter │ → Execution Result
└─────────────────┘
Javascript4D/
├── Source/
│ ├── API/ # Public Engine API
│ ├── Core/ # Types, Errors
│ ├── Lexer/ # Tokenizer
│ ├── Parser/ # AST Builder
│ └── Runtime/ # Interpreter, Built-ins
├── Demo/ # VCL demo application
├── Tests/ # DUnitX unit tests
└── CLAUDE.md # Claude Code instructions
Requires Delphi 12.3 (RAD Studio Athens) or later.
Open the project in RAD Studio IDE or use MSBuild:
msbuild Tests/JS4D.Tests.dproj /p:Configuration=Release /p:Platform=Win64
msbuild Demo/JS4D.Demo.dproj /p:Configuration=Release /p:Platform=Win64Run the compiled test executable JS4D.Tests.exe from your build output directory.
MIT License
Contributions are welcome! Please feel free to submit pull requests.