1. astore
Stores stack elements into an array.
1.2. Stack Effects
| Level | Object |
|---|---|
n |
|
… |
… |
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
astore stores objects from the operand stack into an array:
-
Removes the array from the stack and determines its length n
-
Removes n objects from the stack
-
Stores them into the array (topmost stack element → array[n-1], bottommost → array[0])
-
Pushes the array back onto the stack
The array must have been previously created with the correct length. astore cannot be performed on packed arrays (they are read-only).
1.5. Examples
(a) (bcd) (ef) 3 array astore
% Result: [(a) (bcd) (ef)]
10 20 add % 30
30 40 add % 70
50 60 add % 110
3 array astore
% Result: [30 70 110]
100 200 2 array astore
/point exch def % Store as /point
1.6. Common Use Cases
1.7. Common Pitfalls
| Array Size Must Match - The array must be exactly the right size for the number of objects on the stack. |
1 2 3 4 array astore % Only stores 3 items
% 4th element remains null
| Stack Order - Objects are stored in reverse stack order: topmost stack element → highest array index. |
1 2 3 3 array astore % Result: [1 2 3]
% NOT [3 2 1]
Global/Local VM Restrictions - If the array is in global VM and any stored object is in local VM, [invalidaccess] error occurs.
|
Pre-create Array - Always create the array first with the correct size using array.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Array is in global VM and contains local VM objects, or array is read-only |
[ |
Fewer than n+1 objects on stack |
[ |
Operand is not an array |
1.9. Implementation Notes
-
Cannot be used with packed arrays (they are read-only)
-
Elements are stored by reference (not copied) for composite objects
-
The array is modified in place
-
Most efficient for small to medium-sized arrays
1.10. Comparison with [ ]
% Using astore
1 2 3 3 array astore % [1 2 3]
% Using [ ]
[ 1 2 3 ] % [1 2 3] (more convenient)
The [ ] syntax is usually more convenient for literal arrays, but astore is useful when the array size is computed dynamically.
1.11. See Also
-
aload- Load array elements onto stack -
array- Create new array -
put- Put single value into array -
putinterval- Put subarray into array -
[/] - Array literal syntax