1. exec

Executes an object immediately.

1.1. Syntax

any exec → –

1.2. Stack Effects

Table 1. Before
Level Object

0

any (object to execute)

Table 2. After
Level Object

(varies)

Results depend on object executed

1.3. Description

exec pushes the operand on the execution stack, executing it immediately.

The effect depends on the object’s type and literal/executable attribute:

  • Literal objects: Pushed back on operand stack

  • Executable arrays (procedures): Elements executed sequentially

  • Executable names: Looked up and executed

  • Operators: Executed

  • Files/strings: Interpreted as PostScript code

1.4. PostScript Level

Level 1 and later

1.5. Examples

Executing a string
(3 2 add) cvx exec  % Returns 5
Executing a name
/add cvx exec  % Error: stackunderflow (no operands)
3 2 /add cvx exec  % Returns 5
Executing a procedure
{ 1 2 add } exec  % Returns 3
Literal vs. executable
/add exec       % Pushes /add (literal)
/add cvx exec   % Executes add operator

1.6. Common Use Cases

1.6.1. Dynamic Code Execution

/operation (add) def
operation cvn cvx exec  % Executes add

1.6.2. Executing Stored Procedures

/handlers 5 dict def
handlers /onClick { handleClick } put

/onClick handlers exch get exec

1.6.3. String Evaluation

(10 20 mul) cvx exec  % Evaluates string: 200

1.7. Common Pitfalls

Literal Objects Not Executed - Literal objects are just pushed back.
123 exec       % Returns 123 (literal integer)
/name exec     % Returns /name (literal name)
{ code } cvlit exec  % Returns { code } (literal array)
Must Use cvx for Strings - Strings must be executable to interpret as code.
(1 2 add) exec      % Pushes string (literal)
(1 2 add) cvx exec  % Evaluates: 3
Security Risk - Executing arbitrary strings can be dangerous.
userInput cvx exec  % Dangerous if userInput malicious!
Check Executable Attribute - Use xcheck to verify before execution.

1.8. Error Conditions

Error Condition

[stackunderflow]

No operand on stack

(various)

Depends on what is executed

1.9. Implementation Notes

  • Pushes object onto execution stack

  • Interpreter then processes it normally

  • No special overhead (same as normal execution)

  • Can execute any PostScript object

1.10. Execution Behavior by Type

Object Type Literal Executable

Integer/Real

Pushed on stack

Pushed on stack

Boolean/Null

Pushed on stack

Pushed on stack

String

Pushed on stack

Interpreted as code

Name

Pushed on stack

Looked up and executed

Array

Pushed on stack

Elements executed

Operator

Pushed on stack

Executed

Dictionary

Pushed on stack

Pushed on stack

1.11. See Also

  • cvx - Convert to executable

  • cvlit - Convert to literal

  • xcheck - Check if executable

  • stopped - Execute with error catching

  • if / ifelse - Conditional execution


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.