Javascript4D
A native JavaScript (ECMAScript 5.1) parser and interpreter written in Delphi/Object Pascal. Execute JavaScript code directly from your Delphi applications.
Install / Use
/learn @GDKsoftware/Javascript4DREADME
JavaScript4D
A complete JavaScript (ECMAScript 5.1) parser and interpreter written in Delphi/Object Pascal.
Overview
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.
Features
- 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
Quick Start
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;
Supported JavaScript Features
Core Language
- 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
Built-in Objects
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
Architecture
JavaScript Source Code
│
▼
┌─────────────────┐
│ Lexer │ → Tokens
└─────────────────┘
│
▼
┌─────────────────┐
│ Parser │ → Abstract Syntax Tree (AST)
└─────────────────┘
│
▼
┌─────────────────┐
│ Interpreter │ → Execution Result
└─────────────────┘
Project Structure
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
Building
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=Win64
Running Tests
Run the compiled test executable JS4D.Tests.exe from your build output directory.
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit pull requests.
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
