summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs/valuewriter.cpp
blob: 0a81b5133b624f6a647c351a96de2eeb212d9b08 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/scripting/mozjs/valuewriter.h"

#include <js/Conversions.h>

#include "mongo/base/error_codes.h"
#include "mongo/platform/decimal128.h"
#include "mongo/scripting/mozjs/exception.h"
#include "mongo/scripting/mozjs/implscope.h"
#include "mongo/scripting/mozjs/jsstringwrapper.h"
#include "mongo/scripting/mozjs/objectwrapper.h"
#include "mongo/scripting/mozjs/valuereader.h"
#include "mongo/util/base64.h"
#include "mongo/util/represent_as.h"

namespace mongo {
namespace mozjs {

ValueWriter::ValueWriter(JSContext* cx, JS::HandleValue value)
    : _context(cx), _value(value), _originalParent(nullptr) {}

void ValueWriter::setOriginalBSON(BSONObj* obj) {
    _originalParent = obj;
}

int ValueWriter::type() {
    if (_value.isNull())
        return BSONType::jstNULL;
    if (_value.isUndefined())
        return BSONType::Undefined;
    if (_value.isString())
        return BSONType::String;

    bool isArray;

    if (!JS_IsArrayObject(_context, _value, &isArray)) {
        uasserted(ErrorCodes::BadValue, "unable to check if type is an array");
    }
    if (isArray) {
        return BSONType::Array;
    }

    if (_value.isBoolean()) {
        return BSONType::Bool;
    }

    // We could do something more sophisticated here by checking to see if we
    // round trip through int32_t, int64_t and double and picking a type that
    // way, for now just always come back as double for numbers though (it's
    // what we did for v8)
    if (_value.isNumber()) {
        return BSONType::NumberDouble;
    }

    if (_value.isObject()) {
        JS::RootedObject obj(_context, _value.toObjectOrNull());

        bool isDate;
        if (!JS_ObjectIsDate(_context, obj, &isDate)) {
            uasserted(ErrorCodes::BadValue, "unable to check if type is a date");
        }
        if (isDate) {
            return BSONType::Date;
        }

        bool isRegExp;
        if (!JS_ObjectIsRegExp(_context, obj, &isRegExp)) {
            uasserted(ErrorCodes::BadValue, "unable to check if type is a regexp");
        }
        if (isRegExp) {
            return BSONType::RegEx;
        }

        if (JS_ObjectIsFunction(_context, obj)) {
            return BSONType::Code;
        }

        if (auto jsClass = JS_GetClass(obj)) {
            auto scope = getScope(_context);
            if (scope->getProto<NumberIntInfo>().getJSClass() == jsClass) {
                return BSONType::NumberInt;
            } else if (scope->getProto<NumberLongInfo>().getJSClass() == jsClass) {
                return BSONType::NumberLong;
            } else if (scope->getProto<NumberDecimalInfo>().getJSClass() == jsClass) {
                return BSONType::NumberDecimal;
            } else if (scope->getProto<OIDInfo>().getJSClass() == jsClass) {
                return BSONType::jstOID;
            } else if (scope->getProto<BinDataInfo>().getJSClass() == jsClass) {
                return BSONType::BinData;
            } else if (scope->getProto<TimestampInfo>().getJSClass() == jsClass) {
                return BSONType::bsonTimestamp;
            } else if (scope->getProto<MinKeyInfo>().getJSClass() == jsClass) {
                return BSONType::MinKey;
            } else if (scope->getProto<MaxKeyInfo>().getJSClass() == jsClass) {
                return BSONType::MaxKey;
            }
        }

        return BSONType::Object;
    }

    uasserted(ErrorCodes::BadValue, "unable to get type");
}

std::string ValueWriter::typeAsString() {
    if (_value.isNull())
        return "null";
    if (_value.isUndefined())
        return "undefined";
    if (_value.isString())
        return "string";

    bool isArray;

    if (!JS_IsArrayObject(_context, _value, &isArray)) {
        uasserted(ErrorCodes::BadValue, "unable to check if type is an array");
    }

    if (isArray)
        return "array";
    if (_value.isBoolean())
        return "boolean";
    if (_value.isNumber())
        return "number";

    if (_value.isObject()) {
        JS::RootedObject obj(_context, _value.toObjectOrNull());

        if (!JS_IsArrayObject(_context, obj, &isArray)) {
            uasserted(ErrorCodes::BadValue, "unable to check if type is an array");
        }
        if (isArray)
            return "array";

        bool isDate;

        if (!JS_ObjectIsDate(_context, obj, &isDate)) {
            uasserted(ErrorCodes::BadValue, "unable to check if type is a date");
        }
        if (isDate)
            return "date";

        if (JS_ObjectIsFunction(_context, obj))
            return "function";

        return ObjectWrapper(_context, _value).getClassName();
    }

    uasserted(ErrorCodes::BadValue, "unable to get type");
}

BSONObj ValueWriter::toBSON() {
    if (!_value.isObject())
        return BSONObj();

    JS::RootedObject obj(_context, _value.toObjectOrNull());

    return ObjectWrapper(_context, obj).toBSON();
}

std::string ValueWriter::toString() {
    JSStringWrapper jsstr;
    return toStringData(&jsstr).toString();
}

StringData ValueWriter::toStringData(JSStringWrapper* jsstr) {
    *jsstr = JSStringWrapper(_context, JS::ToString(_context, _value));
    return jsstr->toStringData();
}

double ValueWriter::toNumber() {
    double out;
    if (JS::ToNumber(_context, _value, &out))
        return out;

    throwCurrentJSException(_context, ErrorCodes::BadValue, "Failure to convert value to number");
}

bool ValueWriter::toBoolean() {
    return JS::ToBoolean(_value);
}

int32_t ValueWriter::toInt32() {
    int32_t out;
    if (JS::ToInt32(_context, _value, &out))
        return out;

    throwCurrentJSException(_context, ErrorCodes::BadValue, "Failure to convert value to number");
}

int64_t ValueWriter::toInt64() {
    int64_t out;
    if (getScope(_context)->getProto<NumberLongInfo>().instanceOf(_value))
        return NumberLongInfo::ToNumberLong(_context, _value);

    if (JS::ToInt64(_context, _value, &out))
        return out;

    throwCurrentJSException(_context, ErrorCodes::BadValue, "Failure to convert value to number");
}

Decimal128 ValueWriter::toDecimal128() {
    std::uint32_t signalingFlags = 0;
    if (_value.isNumber()) {
        return Decimal128(toNumber(), Decimal128::kRoundTo15Digits);
    }
    if (getScope(_context)->getProto<NumberIntInfo>().instanceOf(_value))
        return Decimal128(NumberIntInfo::ToNumberInt(_context, _value));

    if (getScope(_context)->getProto<NumberLongInfo>().instanceOf(_value))
        return Decimal128(static_cast<int64_t>(NumberLongInfo::ToNumberLong(_context, _value)));

    if (getScope(_context)->getProto<NumberDecimalInfo>().instanceOf(_value))
        return NumberDecimalInfo::ToNumberDecimal(_context, _value);

    if (_value.isString()) {
        std::string input = toString();
        Decimal128 decimal = Decimal128(input, &signalingFlags);
        uassert(ErrorCodes::BadValue,
                str::stream() << "Input is not a valid Decimal128 value.",
                !Decimal128::hasFlag(signalingFlags, Decimal128::SignalingFlag::kInvalid));
        uassert(ErrorCodes::BadValue,
                str::stream() << "Input out of range of Decimal128 value (inexact).",
                !Decimal128::hasFlag(signalingFlags, Decimal128::SignalingFlag::kInexact));
        uassert(ErrorCodes::BadValue,
                str::stream() << "Input out of range of Decimal128 value (underflow).",
                !Decimal128::hasFlag(signalingFlags, Decimal128::SignalingFlag::kUnderflow));
        uassert(ErrorCodes::BadValue,
                str::stream() << "Input out of range of Decimal128 value (overflow).",
                !Decimal128::hasFlag(signalingFlags, Decimal128::SignalingFlag::kOverflow));

        return decimal;
    }
    uasserted(ErrorCodes::BadValue, str::stream() << "Unable to write Decimal128 value.");
}

OID ValueWriter::toOID() {
    if (getScope(_context)->getProto<OIDInfo>().instanceOf(_value)) {
        return OIDInfo::getOID(_context, _value);
    }

    throwCurrentJSException(_context, ErrorCodes::BadValue, "Unable to write ObjectId value.");
}

void ValueWriter::toBinData(std::function<void(const BSONBinData&)> withBinData) {
    if (!getScope(_context)->getProto<BinDataInfo>().instanceOf(_value)) {
        throwCurrentJSException(_context, ErrorCodes::BadValue, "Unable to write BinData value.");
    }

    JS::RootedObject obj(_context, _value.toObjectOrNull());
    ObjectWrapper wrapper(_context, obj);
    auto subType = wrapper.getNumber(InternedString::type);
    uassert(6123400, "BinData sub type must be between 0 and 255", subType >= 0 && subType <= 255);

    auto binDataStr = static_cast<std::string*>(JS_GetPrivate(obj));
    uassert(ErrorCodes::BadValue, "Cannot call getter on BinData prototype", binDataStr);

    auto binData = base64::decode(*binDataStr);
    withBinData(BSONBinData(binData.c_str(),
                            binData.size(),
                            static_cast<mongo::BinDataType>(static_cast<int>(subType))));
}

Timestamp ValueWriter::toTimestamp() {
    JS::RootedObject obj(_context, _value.toObjectOrNull());
    ObjectWrapper wrapper(_context, obj);

    uassert(ErrorCodes::BadValue,
            "Unable to write Timestamp value.",
            getScope(_context)->getProto<TimestampInfo>().getJSClass() == JS_GetClass(obj));

    return Timestamp(wrapper.getNumber("t"), wrapper.getNumber("i"));
}

JSRegEx ValueWriter::toRegEx() {
    std::string regexStr = toString();
    uassert(6123401, "Empty regular expression", regexStr.size() > 0);
    uassert(6123402, "Invalid regular expression", regexStr[0] == '/');

    return JSRegEx(regexStr.substr(1, regexStr.rfind('/')),
                   regexStr.substr(regexStr.rfind('/') + 1));
}

void ValueWriter::writeThis(BSONObjBuilder* b,
                            StringData sd,
                            ObjectWrapper::WriteFieldRecursionFrames* frames) {
    uassert(17279,
            str::stream() << "Exceeded depth limit of " << ObjectWrapper::kMaxWriteFieldDepth
                          << " when converting js object to BSON. Do you have a cycle?",
            frames->size() < ObjectWrapper::kMaxWriteFieldDepth);

    // Null char should be at the end, not in the string
    uassert(16985,
            str::stream() << "JavaScript property (name) contains a null char "
                          << "which is not allowed in BSON. "
                          << (_originalParent ? _originalParent->jsonString() : ""),
            (std::string::npos == sd.find('\0')));

    if (_value.isString()) {
        JSStringWrapper jsstr;
        b->append(sd, toStringData(&jsstr));
    } else if (_value.isNumber()) {
        double val = toNumber();

        // if previous type was integer, attempt to represent 'val' as an integer
        auto intval = representAs<int>(val);

        if (intval && _originalParent) {
            // This makes copying an object of numbers O(n**2) :(
            BSONElement elmt = _originalParent->getField(sd);
            if (elmt.type() == mongo::NumberInt) {
                b->append(sd, *intval);
                return;
            }
        }

        b->append(sd, val);
    } else if (_value.isObject()) {
        _writeObject(b, sd, frames);
    } else if (_value.isBoolean()) {
        b->appendBool(sd, _value.toBoolean());
    } else if (_value.isUndefined()) {
        b->appendUndefined(sd);
    } else if (_value.isNull()) {
        b->appendNull(sd);
    } else {
        uasserted(16662,
                  str::stream() << "unable to convert JavaScript property to mongo element " << sd);
    }
}

void ValueWriter::_writeObject(BSONObjBuilder* b,
                               StringData sd,
                               ObjectWrapper::WriteFieldRecursionFrames* frames) {
    auto scope = getScope(_context);

    // We open a block here because it's important that the two rooting types
    // we need (obj and o) go out of scope before we actually open a
    // new WriteFieldFrame (in the emplace at the bottom of the function). If
    // we don't do this, we'll destroy the local roots in this function body
    // before the frame we added, which will break the gc rooting list.
    {
        JS::RootedObject obj(_context, _value.toObjectOrNull());
        ObjectWrapper o(_context, obj);

        auto jsclass = JS_GetClass(obj);

        if (jsclass) {
            if (scope->getProto<OIDInfo>().getJSClass() == jsclass) {
                b->append(sd, OIDInfo::getOID(_context, obj));

                return;
            }

            if (scope->getProto<NumberLongInfo>().getJSClass() == jsclass) {
                long long out = NumberLongInfo::ToNumberLong(_context, obj);
                b->append(sd, out);

                return;
            }

            if (scope->getProto<NumberIntInfo>().getJSClass() == jsclass) {
                b->append(sd, NumberIntInfo::ToNumberInt(_context, obj));

                return;
            }

            if (scope->getProto<CodeInfo>().getJSClass() == jsclass) {
                if (o.hasOwnField(InternedString::scope)  // CodeWScope
                    && o.type(InternedString::scope) == mongo::Object) {
                    if (o.type(InternedString::code) != mongo::String) {
                        uasserted(ErrorCodes::BadValue, "code must be a string");
                    }

                    b->appendCodeWScope(
                        sd, o.getString(InternedString::code), o.getObject(InternedString::scope));
                } else {  // Code
                    if (o.type(InternedString::code) != mongo::String) {
                        uasserted(ErrorCodes::BadValue, "code must be a string");
                    }

                    b->appendCode(sd, o.getString(InternedString::code));
                }
                return;
            }

            if (scope->getProto<NumberDecimalInfo>().getJSClass() == jsclass) {
                b->append(sd, NumberDecimalInfo::ToNumberDecimal(_context, obj));

                return;
            }

            if (scope->getProto<DBPointerInfo>().getJSClass() == jsclass) {
                uassert(ErrorCodes::BadValue,
                        "can't serialize DBPointer prototype",
                        scope->getProto<DBPointerInfo>().getProto() != obj);

                JS::RootedValue id(_context);
                o.getValue("id", &id);

                b->appendDBRef(sd, o.getString("ns"), OIDInfo::getOID(_context, id));

                return;
            }

            if (scope->getProto<BinDataInfo>().getJSClass() == jsclass) {
                auto str = static_cast<std::string*>(JS_GetPrivate(obj));

                uassert(ErrorCodes::BadValue, "Cannot call getter on BinData prototype", str);

                auto binData = base64::decode(*str);

                b->appendBinData(sd,
                                 binData.size(),
                                 static_cast<mongo::BinDataType>(
                                     static_cast<int>(o.getNumber(InternedString::type))),
                                 binData.c_str());

                return;
            }

            if (scope->getProto<TimestampInfo>().getJSClass() == jsclass) {
                Timestamp ot(o.getNumber("t"), o.getNumber("i"));
                b->append(sd, ot);

                return;
            }

            if (scope->getProto<MinKeyInfo>().getJSClass() == jsclass) {
                b->appendMinKey(sd);

                return;
            }

            if (scope->getProto<MaxKeyInfo>().getJSClass() == jsclass) {
                b->appendMaxKey(sd);

                return;
            }
        }

        auto protoKey = JS::IdentifyStandardInstanceOrPrototype(obj);

        switch (protoKey) {
            case JSProto_Function: {
                uassert(16716,
                        "cannot convert native function to BSON",
                        !scope->getProto<NativeFunctionInfo>().instanceOf(obj));
                JSStringWrapper jsstr;
                b->appendCode(sd, ValueWriter(_context, _value).toStringData(&jsstr));
                return;
            }
            case JSProto_RegExp: {
                JS::RootedValue v(_context);
                v.setObjectOrNull(obj);

                std::string regex = ValueWriter(_context, v).toString();
                regex = regex.substr(1);
                std::string r = regex.substr(0, regex.rfind('/'));
                std::string o = regex.substr(regex.rfind('/') + 1);

                b->appendRegex(sd, r, o);

                return;
            }
            case JSProto_Date: {
                Date_t d;
                if (JS::IdentifyStandardPrototype(obj) == JSProto_Date) {
                    d = Date_t::fromMillisSinceEpoch(0);
                } else {
                    JS::RootedValue dateval(_context);
                    o.callMethod("getTime", &dateval);
                    d = Date_t::fromMillisSinceEpoch(ValueWriter(_context, dateval).toInt64());
                }

                b->appendDate(sd, d);

                return;
            }
            default:
                break;
        }
    }

    // nested object or array

    // This emplace is effectively a recursive function call, as this code path
    // unwinds back to ObjectWrapper::toBSON. In that function we'll actually
    // write the child we've just pushed onto the frames stack.
    frames->emplace(_context, _value.toObjectOrNull(), b, sd);
}

}  // namespace mozjs
}  // namespace mongo