1 /**
2 This module implements how arrays are internally handled. There is no reason to use this instead of constructing a
3 ScriptAny with a D array or using toValue!(ScriptAny[]) on a ScriptAny that stores an array. 
4 
5 ────────────────────────────────────────────────────────────────────────────────
6 
7 Copyright (C) 2021 pillager86.rf.gd
8 
9 This program is free software: you can redistribute it and/or modify it under 
10 the terms of the GNU General Public License as published by the Free Software 
11 Foundation, either version 3 of the License, or (at your option) any later 
12 version.
13 
14 This program is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License along with 
19 this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21 module mildew.types.array;
22 
23 import mildew.types.object: ScriptObject;
24 
25 /**
26  * Implements arrays of ScriptAny values
27  */
28 class ScriptArray : ScriptObject
29 {
30     import mildew.types.any: ScriptAny;
31     import mildew.interpreter: Interpreter;
32 public:
33 
34     /**
35      * Takes a D array of ScriptAnys
36      */
37     this(ScriptAny[] values)
38     {
39         import mildew.types.bindings: getArrayPrototype;
40         super("array", getArrayPrototype, null);
41         _array = values;
42     }
43 
44     /**
45      * Returns the length of the array
46      */
47     size_t length() const { return _array.length; }
48 
49     /**
50      * Returns a string representation of the array, which is [] surrounding a comma separated
51      * list of elements.
52      */
53     override string toString() const 
54     {
55         auto str = "[";
56         for(size_t i = 0; i < _array.length; ++i)
57         {
58             str ~= _array[i].toString();
59             if(i < _array.length - 1) // @suppress(dscanner.suspicious.length_subtraction)
60                 str ~= ", ";
61         }
62         str ~= "]";
63         return str;
64     }
65 
66     /// The actual array
67     ref ScriptAny[] array() { return _array; }
68     /// ditto
69     ref ScriptAny[] array(ScriptAny[] ar) { return _array = ar; } 
70 
71 private:
72 
73     ScriptAny[] _array;
74 }
75