Operating Modes

DWScript is a versatile language that can run in different environments. Understanding the operating mode is crucial as it affects available features and libraries.

1. Native Script Mode

In this mode, DWScript runs directly on the host machine (Windows) via the CLI or an embedded host application.

  • Execution: Parsed and compiled into an executable abstract syntax tree (with optional partial Just-In-Time compilation).
  • Capabilities: Full access to the file system, network, and external libraries (DLLs).
  • Memory Management: Automatic Reference Counting (ARC) and Garbage Collection (depending on configuration).
  • Use Case: Automation scripts, build tools, backend processing.

2. Web Server Mode (Server Pages)

This is the mode used to serve this website. DWScript files (.dws) are executed by the web server to generate dynamic HTML content.

  • Syntax: Usually employs the HTML Filter (PHP-style) to mix code and markup.
  • Context: Includes a global WebRequest and WebResponse object.
  • Output: Print and PrintLn functions write directly to the HTTP response stream.
  • Lifecycle: Scripts are stateless; they run once per request. Standard global variables are reset for every request.
  • Persistence: For sharing data across requests, see the Global & Private State page.
  • Use Case: Web applications, API endpoints.

3. JavaScript Transpilation

DWScript can be compiled into JavaScript to run in a web browser or Node.js environment. This allows you to write the frontend code in the same language as your backend.

  • Compilation: The Pascal source is translated into equivalent JavaScript code.
  • Integration: You can interact with the DOM, use JS libraries, and write asm blocks containing raw JavaScript.
  • Differences:
    • Integers: JavaScript numbers are doubles. Integer behaves like a JS number (53-bit precision safe).
    • Memory: Relies on the JavaScript engine's Garbage Collector.
    • File System: No direct access to the client's file system (browser security sandbox).
  • Use Case: Single Page Applications (SPAs), complex client-side logic, game logic sharing.

Syntax & Compilation Units

Beyond the execution environment, DWScript supports two primary ways of structuring source code: Script Mode and Unit Mode.

Script Mode (Mixed Code & Declarations)

This is the most flexible mode, where you can mix variables, types, and procedures directly with executable code. It is ideal for small tasks and web server pages.

  • Top-down execution: Code is executed from the first line.
  • Inline Declarations: You can declare a var or type anywhere before its first use.
  • Interspersed Logic: You can have PrintLn calls between type and procedure definitions.
PrintLn('Starting script');

type TUser = record Name: String; end;
var u: TUser;
u.Name := 'Alice';

procedure SayHello; 
begin 
  PrintLn('Hello ' + u.Name); 
end;

SayHello;
Result
Starting script
Hello Alice

Unit / Program Mode (Structured)

This mode follows the classic Object Pascal structure and is used for building reusable modules or larger applications. It enforces a strict separation between declarations and implementation.

  • Keyword: Must start with unit or program.
  • Sections: Uses interface and implementation blocks.
  • Type Blocks: Types and variables must be grouped into their respective sections (e.g., after the type keyword).
unit MyUtils;

interface

type
  TPoint = record
    X, Y: Integer;
  end;
  
implementation

// Code logic goes here...

end.

Choosing a Mode

Web server .dws files always start in Script Mode. If you want to use Units, you create separate .pas files and include them via uses.

On this page