summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Runtime/Value.cpp
blob: 6e5d11c3ba3edaf87b9f2a68d8a5a9c241526462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <AK/FlyString.h>
#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/NumberObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/Value.h>
#include <math.h>

namespace JS {

bool Value::is_array() const
{
    return is_object() && as_object().is_array();
}

String Value::to_string() const
{
    if (is_boolean())
        return as_bool() ? "true" : "false";

    if (is_null())
        return "null";

    if (is_undefined())
        return "undefined";

    if (is_number()) {
        if (is_nan())
            return "NaN";

        if (is_infinity())
            return as_double() < 0 ? "-Infinity" : "Infinity";

        // FIXME: This needs improvement.
        if ((double)to_i32() == as_double())
            return String::number(to_i32());
        return String::format("%f", as_double());
    }

    if (is_object())
        return as_object().to_primitive(Object::PreferredType::String).to_string();

    if (is_string())
        return m_value.as_string->string();

    ASSERT_NOT_REACHED();
}

bool Value::to_boolean() const
{
    switch (m_type) {
    case Type::Boolean:
        return m_value.as_bool;
    case Type::Number:
        if (is_nan()) {
            return false;
        }
        return !(m_value.as_double == 0 || m_value.as_double == -0);
    case Type::Null:
    case Type::Undefined:
        return false;
    case Type::String:
        return !as_string()->string().is_empty();
    case Type::Object:
        return true;
    default:
        ASSERT_NOT_REACHED();
    }
}

Object* Value::to_object(Heap& heap) const
{
    if (is_object())
        return &const_cast<Object&>(as_object());

    if (is_string())
        return heap.allocate<StringObject>(m_value.as_string);

    if (is_number())
        return heap.allocate<NumberObject>(m_value.as_double);

    if (is_null() || is_undefined()) {
        heap.interpreter().throw_exception<Error>("TypeError", "ToObject on null or undefined.");
        return nullptr;
    }

    dbg() << "Dying because I can't to_object() on " << *this;
    ASSERT_NOT_REACHED();
}

Value Value::to_number() const
{
    switch (m_type) {
    case Type::Empty:
        ASSERT_NOT_REACHED();
        return {};
    case Type::Boolean:
        return Value(m_value.as_bool ? 1 : 0);
    case Type::Number:
        return Value(m_value.as_double);
    case Type::Null:
        return Value(0);
    case Type::String: {
        auto& string = as_string()->string();
        if (string.is_empty())
            return Value(0);
        bool ok;
        //FIXME: Parse in a better way
        auto parsed_int = string.to_int(ok);
        if (ok)
            return Value(parsed_int);

        return js_nan();
    }
    case Type::Undefined:
        return js_nan();
    case Type::Object:
        if (m_value.as_object->is_array()) {
            auto& array = *static_cast<Array*>(m_value.as_object);
            if (array.length() == 0)
                return Value(0);
            if (array.length() > 1)
                return js_nan();
            return array.elements()[0].to_number();
        } else {
            return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number();
        }
    }

    ASSERT_NOT_REACHED();
}

i32 Value::to_i32() const
{
    return static_cast<i32>(to_number().as_double());
}

Value greater_than(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() > rhs.to_number().as_double());
}

Value greater_than_equals(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() >= rhs.to_number().as_double());
}

Value less_than(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() < rhs.to_number().as_double());
}

Value less_than_equals(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() <= rhs.to_number().as_double());
}

Value bitwise_and(Value lhs, Value rhs)
{
    return Value((i32)lhs.to_number().as_double() & (i32)rhs.to_number().as_double());
}

Value bitwise_or(Value lhs, Value rhs)
{
    bool lhs_invalid = lhs.is_undefined() || lhs.is_null() || lhs.is_nan() || lhs.is_infinity();
    bool rhs_invalid = rhs.is_undefined() || rhs.is_null() || rhs.is_nan() || rhs.is_infinity();

    if (lhs_invalid && rhs_invalid)
        return Value(0);

    if (lhs_invalid || rhs_invalid)
        return lhs_invalid ? rhs.to_number() : lhs.to_number();

    if (!rhs.is_number() && !lhs.is_number())
        return Value(0);

    return Value((i32)lhs.to_number().as_double() | (i32)rhs.to_number().as_double());
}

Value bitwise_xor(Value lhs, Value rhs)
{
    return Value((i32)lhs.to_number().as_double() ^ (i32)rhs.to_number().as_double());
}

Value bitwise_not(Value lhs)
{
    return Value(~(i32)lhs.to_number().as_double());
}

Value unary_plus(Value lhs)
{
    return lhs.to_number();
}

Value unary_minus(Value lhs)
{
    if (lhs.to_number().is_nan())
        return js_nan();
    return Value(-lhs.to_number().as_double());
}

Value left_shift(Value lhs, Value rhs)
{
    return Value((i32)lhs.to_number().as_double() << (i32)rhs.to_number().as_double());
}

Value right_shift(Value lhs, Value rhs)
{
    return Value((i32)lhs.to_number().as_double() >> (i32)rhs.to_number().as_double());
}

Value add(Value lhs, Value rhs)
{
    if (lhs.is_string() || rhs.is_string())
        return js_string((lhs.is_string() ? lhs : rhs).as_string()->heap(), String::format("%s%s", lhs.to_string().characters(), rhs.to_string().characters()));

    return Value(lhs.to_number().as_double() + rhs.to_number().as_double());
}

Value sub(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() - rhs.to_number().as_double());
}

Value mul(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() * rhs.to_number().as_double());
}

Value div(Value lhs, Value rhs)
{
    return Value(lhs.to_number().as_double() / rhs.to_number().as_double());
}

Value mod(Value lhs, Value rhs)
{
    if (lhs.to_number().is_nan() || rhs.to_number().is_nan())
        return js_nan();

    double index = lhs.to_number().as_double();
    double period = rhs.to_number().as_double();
    double trunc = (double)(i32) (index / period);

    return Value(index - trunc * period);
}

Value exp(Value lhs, Value rhs)
{
    return Value(pow(lhs.to_number().as_double(), rhs.to_number().as_double()));
}

Value typed_eq(Value lhs, Value rhs)
{
    if (rhs.type() != lhs.type())
        return Value(false);

    switch (lhs.type()) {
    case Value::Type::Empty:
        ASSERT_NOT_REACHED();
        return {};
    case Value::Type::Undefined:
        return Value(true);
    case Value::Type::Null:
        return Value(true);
    case Value::Type::Number:
        return Value(lhs.as_double() == rhs.as_double());
    case Value::Type::String:
        return Value(lhs.as_string()->string() == rhs.as_string()->string());
    case Value::Type::Boolean:
        return Value(lhs.as_bool() == rhs.as_bool());
    case Value::Type::Object:
        return Value(&lhs.as_object() == &rhs.as_object());
    }

    ASSERT_NOT_REACHED();
}

Value eq(Value lhs, Value rhs)
{
    if (lhs.type() == rhs.type())
        return typed_eq(lhs, rhs);

    if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
        return Value(true);

    if (lhs.is_object() && rhs.is_boolean())
        return eq(lhs.as_object().to_primitive(), rhs.to_number());

    if (lhs.is_boolean() && rhs.is_object())
        return eq(lhs.to_number(), rhs.as_object().to_primitive());

    if (lhs.is_object())
        return eq(lhs.as_object().to_primitive(), rhs);

    if (rhs.is_object())
        return eq(lhs, rhs.as_object().to_primitive());

    if (lhs.is_number() || rhs.is_number())
        return Value(lhs.to_number().as_double() == rhs.to_number().as_double());

    if ((lhs.is_string() && rhs.is_boolean()) || (lhs.is_string() && rhs.is_boolean()))
        return Value(lhs.to_number().as_double() == rhs.to_number().as_double());

    return Value(false);
}

Value instance_of(Value lhs, Value rhs)
{
    if (!lhs.is_object() || !rhs.is_object())
        return Value(false);

    auto constructor_prototype_property = rhs.as_object().get("prototype");
    if (!constructor_prototype_property.has_value() || !constructor_prototype_property.value().is_object())
        return Value(false);

    return Value(lhs.as_object().has_prototype(&constructor_prototype_property.value().as_object()));
}

const LogStream& operator<<(const LogStream& stream, const Value& value)
{
    return stream << value.to_string();
}

}