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