summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testJSON.cpp
blob: 5c33e863680fa2023a35f5a6b0eacd00fb9104c5 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <limits>
#include <string.h>

#include "js/Array.h"  // JS::IsArrayObject
#include "js/Exception.h"
#include "js/friend/ErrorMessages.h"  // JSMSG_*
#include "js/JSON.h"
#include "js/MemoryFunctions.h"
#include "js/Printf.h"
#include "js/PropertyAndElement.h"  // JS_GetProperty
#include "jsapi-tests/tests.h"

using namespace JS;

static bool Cmp(const char16_t* buffer, uint32_t length,
                const char16_t* expected) {
  uint32_t i = 0;
  while (*expected) {
    if (i >= length || *expected != *buffer) {
      return false;
    }
    ++expected;
    ++buffer;
    ++i;
  }
  return i == length;
}

static bool Callback(const char16_t* buffer, uint32_t length, void* data) {
  const char16_t** data_ = static_cast<const char16_t**>(data);
  const char16_t* expected = *data_;
  *data_ = nullptr;
  return Cmp(buffer, length, expected);
}

BEGIN_TEST(testToJSON_same) {
  // Primitives
  Rooted<Value> input(cx);
  input = JS::TrueValue();
  CHECK(TryToJSON(cx, input, u"true"));

  input = JS::FalseValue();
  CHECK(TryToJSON(cx, input, u"false"));

  input = JS::NullValue();
  CHECK(TryToJSON(cx, input, u"null"));

  input.setInt32(0);
  CHECK(TryToJSON(cx, input, u"0"));

  input.setInt32(1);
  CHECK(TryToJSON(cx, input, u"1"));

  input.setInt32(-1);
  CHECK(TryToJSON(cx, input, u"-1"));

  input.setDouble(1);
  CHECK(TryToJSON(cx, input, u"1"));

  input.setDouble(1.75);
  CHECK(TryToJSON(cx, input, u"1.75"));

  input.setDouble(9e9);
  CHECK(TryToJSON(cx, input, u"9000000000"));

  input.setDouble(std::numeric_limits<double>::infinity());
  CHECK(TryToJSON(cx, input, u"null"));
  return true;
}

bool TryToJSON(JSContext* cx, Handle<Value> value, const char16_t* expected) {
  {
    Rooted<Value> v(cx, value);
    const char16_t* expected_ = expected;
    CHECK(JS_Stringify(cx, &v, nullptr, JS::NullHandleValue, Callback,
                       &expected_));
    CHECK_NULL(expected_);
  }
  {
    const char16_t* expected_ = expected;
    CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, Callback,
                     &expected_));
    CHECK_NULL(expected_);
  }
  return true;
}
END_TEST(testToJSON_same)

BEGIN_TEST(testToJSON_different) {
  Rooted<Symbol*> symbol(cx, NewSymbol(cx, nullptr));
  Rooted<Value> value(cx, SymbolValue(symbol));

  CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, UnreachedCallback,
                   nullptr));
  const char16_t* expected = u"null";
  CHECK(JS_Stringify(cx, &value, nullptr, JS::NullHandleValue, Callback,
                     &expected));
  CHECK_NULL(expected);
  return true;
}

static bool UnreachedCallback(const char16_t*, uint32_t, void*) {
  MOZ_CRASH("Should not call the callback");
}

END_TEST(testToJSON_different)