| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include <limits> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "js/Array.h" // JS::IsArrayObject |
| 11 |
#include "js/Exception.h" |
| 12 |
#include "js/friend/ErrorMessages.h" // JSMSG_* |
| 13 |
#include "js/JSON.h" |
| 14 |
#include "js/MemoryFunctions.h" |
| 15 |
#include "js/Printf.h" |
| 16 |
#include "js/PropertyAndElement.h" // JS_GetProperty |
| 17 |
#include "jsapi-tests/tests.h" |
| 18 |
|
| 19 |
using namespace JS; |
| 20 |
|
| 21 |
static bool Cmp(const char16_t* buffer, uint32_t length, |
| 22 |
const char16_t* expected) { |
| 23 |
uint32_t i = 0; |
| 24 |
while (*expected) { |
| 25 |
if (i >= length || *expected != *buffer) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
++expected; |
| 29 |
++buffer; |
| 30 |
++i; |
| 31 |
} |
| 32 |
return i == length; |
| 33 |
} |
| 34 |
|
| 35 |
static bool Callback(const char16_t* buffer, uint32_t length, void* data) { |
| 36 |
const char16_t** data_ = static_cast<const char16_t**>(data); |
| 37 |
const char16_t* expected = *data_; |
| 38 |
*data_ = nullptr; |
| 39 |
return Cmp(buffer, length, expected); |
| 40 |
} |
| 41 |
|
| 42 |
BEGIN_TEST(testToJSON_same) { |
| 43 |
// Primitives |
| 44 |
Rooted<Value> input(cx); |
| 45 |
input = JS::TrueValue(); |
| 46 |
CHECK(TryToJSON(cx, input, u"true")); |
| 47 |
|
| 48 |
input = JS::FalseValue(); |
| 49 |
CHECK(TryToJSON(cx, input, u"false")); |
| 50 |
|
| 51 |
input = JS::NullValue(); |
| 52 |
CHECK(TryToJSON(cx, input, u"null")); |
| 53 |
|
| 54 |
input.setInt32(0); |
| 55 |
CHECK(TryToJSON(cx, input, u"0")); |
| 56 |
|
| 57 |
input.setInt32(1); |
| 58 |
CHECK(TryToJSON(cx, input, u"1")); |
| 59 |
|
| 60 |
input.setInt32(-1); |
| 61 |
CHECK(TryToJSON(cx, input, u"-1")); |
| 62 |
|
| 63 |
input.setDouble(1); |
| 64 |
CHECK(TryToJSON(cx, input, u"1")); |
| 65 |
|
| 66 |
input.setDouble(1.75); |
| 67 |
CHECK(TryToJSON(cx, input, u"1.75")); |
| 68 |
|
| 69 |
input.setDouble(9e9); |
| 70 |
CHECK(TryToJSON(cx, input, u"9000000000")); |
| 71 |
|
| 72 |
input.setDouble(std::numeric_limits<double>::infinity()); |
| 73 |
CHECK(TryToJSON(cx, input, u"null")); |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
bool TryToJSON(JSContext* cx, Handle<Value> value, const char16_t* expected) { |
| 78 |
{ |
| 79 |
Rooted<Value> v(cx, value); |
| 80 |
const char16_t* expected_ = expected; |
| 81 |
CHECK(JS_Stringify(cx, &v, nullptr, JS::NullHandleValue, Callback, |
| 82 |
&expected_)); |
| 83 |
CHECK_NULL(expected_); |
| 84 |
} |
| 85 |
{ |
| 86 |
const char16_t* expected_ = expected; |
| 87 |
CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, Callback, |
| 88 |
&expected_)); |
| 89 |
CHECK_NULL(expected_); |
| 90 |
} |
| 91 |
return true; |
| 92 |
} |
| 93 |
END_TEST(testToJSON_same) |
| 94 |
|
| 95 |
BEGIN_TEST(testToJSON_different) { |
| 96 |
Rooted<Symbol*> symbol(cx, NewSymbol(cx, nullptr)); |
| 97 |
Rooted<Value> value(cx, SymbolValue(symbol)); |
| 98 |
|
| 99 |
CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, UnreachedCallback, |
| 100 |
nullptr)); |
| 101 |
const char16_t* expected = u"null"; |
| 102 |
CHECK(JS_Stringify(cx, &value, nullptr, JS::NullHandleValue, Callback, |
| 103 |
&expected)); |
| 104 |
CHECK_NULL(expected); |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
static bool UnreachedCallback(const char16_t*, uint32_t, void*) { |
| 109 |
MOZ_CRASH("Should not call the callback"); |
| 110 |
} |
| 111 |
|
| 112 |
END_TEST(testToJSON_different) |