2012-10-22 14:22:58 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * 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.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// OWNER 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.
|
|
|
|
|
|
|
|
#ifndef V8_JSON_STRINGIFIER_H_
|
|
|
|
#define V8_JSON_STRINGIFIER_H_
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "v8utils.h"
|
|
|
|
#include "v8conversions.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class BasicJsonStringifier BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
explicit BasicJsonStringifier(Isolate* isolate);
|
|
|
|
|
|
|
|
MaybeObject* Stringify(Handle<Object> object);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kInitialPartLength = 32;
|
|
|
|
static const int kMaxPartLength = 16 * 1024;
|
|
|
|
static const int kPartLengthGrowthFactor = 2;
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW };
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
void Extend();
|
2012-10-22 14:22:58 +00:00
|
|
|
|
|
|
|
void ChangeEncoding();
|
|
|
|
|
|
|
|
void ShrinkCurrentPart();
|
|
|
|
|
|
|
|
template <bool is_ascii, typename Char>
|
|
|
|
INLINE(void Append_(Char c));
|
|
|
|
|
|
|
|
template <bool is_ascii, typename Char>
|
|
|
|
INLINE(void Append_(const Char* chars));
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
INLINE(void Append(uint8_t c)) {
|
2012-10-22 14:22:58 +00:00
|
|
|
if (is_ascii_) {
|
|
|
|
Append_<true>(c);
|
|
|
|
} else {
|
|
|
|
Append_<false>(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
INLINE(void AppendAscii(const char* chars)) {
|
2012-10-22 14:22:58 +00:00
|
|
|
if (is_ascii_) {
|
2013-01-09 15:47:53 +00:00
|
|
|
Append_<true>(reinterpret_cast<const uint8_t*>(chars));
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
2013-01-09 15:47:53 +00:00
|
|
|
Append_<false>(reinterpret_cast<const uint8_t*>(chars));
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
Handle<Object> ApplyToJsonFunction(Handle<Object> object,
|
|
|
|
Handle<Object> key);
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
Result SerializeGeneric(Handle<Object> object,
|
|
|
|
Handle<Object> key,
|
|
|
|
bool deferred_comma,
|
|
|
|
bool deferred_key);
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
// Entry point to serialize the object.
|
|
|
|
INLINE(Result SerializeObject(Handle<Object> obj)) {
|
2012-11-05 10:53:56 +00:00
|
|
|
return Serialize_<false>(obj, false, factory_->empty_string());
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
// Serialize an array element.
|
|
|
|
// The index may serve as argument for the toJSON function.
|
|
|
|
INLINE(Result SerializeElement(Handle<Object> object, int i)) {
|
|
|
|
return Serialize_<false>(object, false, Handle<Object>(Smi::FromInt(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize a object property.
|
|
|
|
// The key may or may not be serialized depending on the property.
|
|
|
|
// The key may also serve as argument for the toJSON function.
|
|
|
|
INLINE(Result SerializeProperty(Handle<Object> object,
|
|
|
|
bool deferred_comma,
|
|
|
|
Handle<String> deferred_key)) {
|
2012-10-22 14:22:58 +00:00
|
|
|
ASSERT(!deferred_key.is_null());
|
|
|
|
return Serialize_<true>(object, deferred_comma, deferred_key);
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
template <bool deferred_string_key>
|
|
|
|
Result Serialize_(Handle<Object> object, bool comma, Handle<Object> key);
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
void SerializeDeferredKey(bool deferred_comma, Handle<Object> deferred_key) {
|
2012-10-22 14:22:58 +00:00
|
|
|
if (deferred_comma) Append(',');
|
2012-11-02 14:46:57 +00:00
|
|
|
SerializeString(Handle<String>::cast(deferred_key));
|
2012-10-22 14:22:58 +00:00
|
|
|
Append(':');
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:18:24 +00:00
|
|
|
Result SerializeSmi(Smi* object);
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-10-25 12:18:24 +00:00
|
|
|
Result SerializeDouble(double number);
|
2012-10-22 14:22:58 +00:00
|
|
|
INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) {
|
|
|
|
return SerializeDouble(object->value());
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
Result SerializeJSValue(Handle<JSValue> object);
|
|
|
|
|
|
|
|
INLINE(Result SerializeJSArray(Handle<JSArray> object));
|
|
|
|
INLINE(Result SerializeJSObject(Handle<JSObject> object));
|
|
|
|
|
|
|
|
Result SerializeJSArraySlow(Handle<JSArray> object, int length);
|
2012-10-22 14:22:58 +00:00
|
|
|
|
|
|
|
void SerializeString(Handle<String> object);
|
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
template <typename SrcChar, typename DestChar>
|
|
|
|
INLINE(void SerializeStringUnchecked_(const SrcChar* src,
|
|
|
|
DestChar* dest,
|
|
|
|
int length));
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
template <bool is_ascii, typename Char>
|
2012-12-03 16:23:30 +00:00
|
|
|
INLINE(void SerializeString_(Handle<String> string));
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
template <typename Char>
|
|
|
|
INLINE(bool DoNotEscape(Char c));
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
template <typename Char>
|
|
|
|
INLINE(Vector<const Char> GetCharVector(Handle<String> string));
|
|
|
|
|
2012-10-25 12:18:24 +00:00
|
|
|
Result StackPush(Handle<Object> object);
|
|
|
|
void StackPop();
|
2012-10-22 14:22:58 +00:00
|
|
|
|
|
|
|
INLINE(Handle<String> accumulator()) {
|
2012-11-05 10:53:56 +00:00
|
|
|
return Handle<String>(String::cast(accumulator_store_->value()), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INLINE(void set_accumulator(Handle<String> string)) {
|
|
|
|
return accumulator_store_->set_value(*string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
2012-11-05 10:53:56 +00:00
|
|
|
Factory* factory_;
|
2012-10-22 14:22:58 +00:00
|
|
|
// We use a value wrapper for the string accumulator to keep the
|
|
|
|
// (indirect) handle to it in the outermost handle scope.
|
|
|
|
Handle<JSValue> accumulator_store_;
|
|
|
|
Handle<String> current_part_;
|
|
|
|
Handle<String> tojson_symbol_;
|
|
|
|
Handle<JSArray> stack_;
|
|
|
|
int current_index_;
|
|
|
|
int part_length_;
|
|
|
|
bool is_ascii_;
|
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
static const int kJsonEscapeTableEntrySize = 8;
|
|
|
|
static const char* const JsonEscapeTable;
|
2012-10-22 14:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
// Translation table to escape ASCII characters.
|
|
|
|
// Table entries start at a multiple of 8 and are null-terminated.
|
|
|
|
const char* const BasicJsonStringifier::JsonEscapeTable =
|
2012-10-22 14:22:58 +00:00
|
|
|
"\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 "
|
|
|
|
"\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 "
|
|
|
|
"\\b\0 \\t\0 \\n\0 \\u000b\0 "
|
|
|
|
"\\f\0 \\r\0 \\u000e\0 \\u000f\0 "
|
|
|
|
"\\u0010\0 \\u0011\0 \\u0012\0 \\u0013\0 "
|
|
|
|
"\\u0014\0 \\u0015\0 \\u0016\0 \\u0017\0 "
|
|
|
|
"\\u0018\0 \\u0019\0 \\u001a\0 \\u001b\0 "
|
|
|
|
"\\u001c\0 \\u001d\0 \\u001e\0 \\u001f\0 "
|
|
|
|
" \0 !\0 \\\"\0 #\0 "
|
|
|
|
"$\0 %\0 &\0 '\0 "
|
|
|
|
"(\0 )\0 *\0 +\0 "
|
|
|
|
",\0 -\0 .\0 /\0 "
|
|
|
|
"0\0 1\0 2\0 3\0 "
|
|
|
|
"4\0 5\0 6\0 7\0 "
|
|
|
|
"8\0 9\0 :\0 ;\0 "
|
|
|
|
"<\0 =\0 >\0 ?\0 "
|
|
|
|
"@\0 A\0 B\0 C\0 "
|
|
|
|
"D\0 E\0 F\0 G\0 "
|
|
|
|
"H\0 I\0 J\0 K\0 "
|
|
|
|
"L\0 M\0 N\0 O\0 "
|
|
|
|
"P\0 Q\0 R\0 S\0 "
|
|
|
|
"T\0 U\0 V\0 W\0 "
|
|
|
|
"X\0 Y\0 Z\0 [\0 "
|
|
|
|
"\\\\\0 ]\0 ^\0 _\0 "
|
|
|
|
"`\0 a\0 b\0 c\0 "
|
|
|
|
"d\0 e\0 f\0 g\0 "
|
|
|
|
"h\0 i\0 j\0 k\0 "
|
|
|
|
"l\0 m\0 n\0 o\0 "
|
|
|
|
"p\0 q\0 r\0 s\0 "
|
|
|
|
"t\0 u\0 v\0 w\0 "
|
|
|
|
"x\0 y\0 z\0 {\0 "
|
|
|
|
"|\0 }\0 ~\0 \177\0 ";
|
|
|
|
|
|
|
|
|
|
|
|
BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
|
|
|
|
: isolate_(isolate), current_index_(0), is_ascii_(true) {
|
2012-11-05 10:53:56 +00:00
|
|
|
factory_ = isolate_->factory();
|
2012-10-22 14:22:58 +00:00
|
|
|
accumulator_store_ = Handle<JSValue>::cast(
|
2012-11-05 10:53:56 +00:00
|
|
|
factory_->ToObject(factory_->empty_string()));
|
2012-10-22 14:22:58 +00:00
|
|
|
part_length_ = kInitialPartLength;
|
2012-11-15 13:31:27 +00:00
|
|
|
current_part_ = factory_->NewRawOneByteString(kInitialPartLength);
|
2012-12-17 15:56:16 +00:00
|
|
|
tojson_symbol_ =
|
|
|
|
factory_->LookupOneByteSymbol(STATIC_ASCII_VECTOR("toJSON"));
|
2012-11-05 10:53:56 +00:00
|
|
|
stack_ = factory_->NewJSArray(8);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) {
|
2012-11-02 14:46:57 +00:00
|
|
|
switch (SerializeObject(object)) {
|
|
|
|
case UNCHANGED:
|
|
|
|
return isolate_->heap()->undefined_value();
|
2012-10-22 14:22:58 +00:00
|
|
|
case SUCCESS:
|
|
|
|
ShrinkCurrentPart();
|
2012-11-05 10:53:56 +00:00
|
|
|
return *factory_->NewConsString(accumulator(), current_part_);
|
2012-10-22 14:22:58 +00:00
|
|
|
case CIRCULAR:
|
2012-11-05 10:53:56 +00:00
|
|
|
return isolate_->Throw(*factory_->NewTypeError(
|
2012-10-22 14:22:58 +00:00
|
|
|
"circular_structure", HandleVector<Object>(NULL, 0)));
|
|
|
|
case STACK_OVERFLOW:
|
|
|
|
return isolate_->StackOverflow();
|
|
|
|
default:
|
2012-11-02 14:46:57 +00:00
|
|
|
return Failure::Exception();
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <bool is_ascii, typename Char>
|
|
|
|
void BasicJsonStringifier::Append_(Char c) {
|
|
|
|
if (is_ascii) {
|
2012-11-15 13:31:27 +00:00
|
|
|
SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet(
|
2012-10-22 14:22:58 +00:00
|
|
|
current_index_++, c);
|
|
|
|
} else {
|
|
|
|
SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet(
|
|
|
|
current_index_++, c);
|
|
|
|
}
|
2012-11-05 10:53:56 +00:00
|
|
|
if (current_index_ == part_length_) Extend();
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <bool is_ascii, typename Char>
|
|
|
|
void BasicJsonStringifier::Append_(const Char* chars) {
|
|
|
|
for ( ; *chars != '\0'; chars++) Append_<is_ascii, Char>(*chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
Handle<Object> BasicJsonStringifier::ApplyToJsonFunction(
|
|
|
|
Handle<Object> object, Handle<Object> key) {
|
2012-10-22 14:22:58 +00:00
|
|
|
LookupResult lookup(isolate_);
|
2012-11-02 14:46:57 +00:00
|
|
|
JSObject::cast(*object)->LookupRealNamedProperty(*tojson_symbol_, &lookup);
|
|
|
|
if (!lookup.IsProperty()) return object;
|
|
|
|
PropertyAttributes attr;
|
|
|
|
Handle<Object> fun =
|
|
|
|
Object::GetProperty(object, object, &lookup, tojson_symbol_, &attr);
|
|
|
|
if (!fun->IsJSFunction()) return object;
|
|
|
|
|
|
|
|
// Call toJSON function.
|
2012-11-05 10:53:56 +00:00
|
|
|
if (key->IsSmi()) key = factory_->NumberToString(key);
|
2012-11-02 14:46:57 +00:00
|
|
|
Handle<Object> argv[] = { key };
|
|
|
|
bool has_exception = false;
|
|
|
|
HandleScope scope(isolate_);
|
|
|
|
object = Execution::Call(fun, object, 1, argv, &has_exception);
|
|
|
|
// Return empty handle to signal an exception.
|
|
|
|
if (has_exception) return Handle<Object>::null();
|
|
|
|
return scope.CloseAndEscape(object);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
|
|
|
|
Handle<Object> object) {
|
2012-10-25 12:18:24 +00:00
|
|
|
StackLimitCheck check(isolate_);
|
|
|
|
if (check.HasOverflowed()) return STACK_OVERFLOW;
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
int length = Smi::cast(stack_->length())->value();
|
|
|
|
FixedArray* elements = FixedArray::cast(stack_->elements());
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (elements->get(i) == *object) {
|
|
|
|
return CIRCULAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stack_->EnsureSize(length + 1);
|
|
|
|
FixedArray::cast(stack_->elements())->set(length, *object);
|
|
|
|
stack_->set_length(Smi::FromInt(length + 1));
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicJsonStringifier::StackPop() {
|
|
|
|
int length = Smi::cast(stack_->length())->value();
|
|
|
|
stack_->set_length(Smi::FromInt(length - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
template <bool deferred_string_key>
|
2012-10-22 14:22:58 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::Serialize_(
|
2012-11-02 14:46:57 +00:00
|
|
|
Handle<Object> object, bool comma, Handle<Object> key) {
|
2012-10-22 14:22:58 +00:00
|
|
|
if (object->IsJSObject()) {
|
2012-11-02 14:46:57 +00:00
|
|
|
object = ApplyToJsonFunction(object, key);
|
|
|
|
if (object.is_null()) return EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
if (object->IsSmi()) {
|
2012-11-02 14:46:57 +00:00
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2012-11-05 10:53:56 +00:00
|
|
|
return SerializeSmi(Smi::cast(*object));
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
switch (HeapObject::cast(*object)->map()->instance_type()) {
|
|
|
|
case HEAP_NUMBER_TYPE:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
|
|
|
return SerializeHeapNumber(Handle<HeapNumber>::cast(object));
|
|
|
|
case ODDBALL_TYPE:
|
|
|
|
switch (Oddball::cast(*object)->kind()) {
|
|
|
|
case Oddball::kFalse:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("false");
|
2012-11-05 10:53:56 +00:00
|
|
|
return SUCCESS;
|
|
|
|
case Oddball::kTrue:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("true");
|
2012-11-05 10:53:56 +00:00
|
|
|
return SUCCESS;
|
|
|
|
case Oddball::kNull:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("null");
|
2012-11-05 10:53:56 +00:00
|
|
|
return SUCCESS;
|
|
|
|
default:
|
|
|
|
return UNCHANGED;
|
|
|
|
}
|
|
|
|
case JS_ARRAY_TYPE:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
|
|
|
return SerializeJSArray(Handle<JSArray>::cast(object));
|
|
|
|
case JS_VALUE_TYPE:
|
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
|
|
|
return SerializeJSValue(Handle<JSValue>::cast(object));
|
|
|
|
case JS_FUNCTION_TYPE:
|
|
|
|
return UNCHANGED;
|
|
|
|
default:
|
|
|
|
if (object->IsString()) {
|
2012-11-02 14:46:57 +00:00
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2012-11-05 10:53:56 +00:00
|
|
|
SerializeString(Handle<String>::cast(object));
|
2012-10-22 14:22:58 +00:00
|
|
|
return SUCCESS;
|
2012-11-05 10:53:56 +00:00
|
|
|
} else if (object->IsJSObject()) {
|
2012-11-02 14:46:57 +00:00
|
|
|
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
2012-11-05 10:53:56 +00:00
|
|
|
return SerializeJSObject(Handle<JSObject>::cast(object));
|
|
|
|
} else {
|
|
|
|
return SerializeGeneric(object, key, comma, deferred_string_key);
|
|
|
|
}
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
2012-11-05 10:53:56 +00:00
|
|
|
}
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric(
|
|
|
|
Handle<Object> object,
|
|
|
|
Handle<Object> key,
|
|
|
|
bool deferred_comma,
|
|
|
|
bool deferred_key) {
|
|
|
|
Handle<JSObject> builtins(isolate_->native_context()->builtins());
|
2012-11-23 12:32:24 +00:00
|
|
|
Handle<JSFunction> builtin =
|
|
|
|
Handle<JSFunction>::cast(GetProperty(builtins, "JSONSerializeAdapter"));
|
2012-11-05 10:53:56 +00:00
|
|
|
|
|
|
|
Handle<Object> argv[] = { key, object };
|
|
|
|
bool has_exception = false;
|
|
|
|
Handle<Object> result =
|
|
|
|
Execution::Call(builtin, object, 2, argv, &has_exception);
|
|
|
|
if (has_exception) return EXCEPTION;
|
|
|
|
if (result->IsUndefined()) return UNCHANGED;
|
|
|
|
if (deferred_key) {
|
|
|
|
if (key->IsSmi()) key = factory_->NumberToString(key);
|
|
|
|
SerializeDeferredKey(deferred_comma, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> result_string = Handle<String>::cast(result);
|
|
|
|
// Shrink current part, attach it to the accumulator, also attach the result
|
|
|
|
// string to the accumulator, and allocate a new part.
|
|
|
|
ShrinkCurrentPart(); // Shrink.
|
|
|
|
part_length_ = kInitialPartLength; // Allocate conservatively.
|
|
|
|
Extend(); // Attach current part and allocate new part.
|
|
|
|
// Attach result string to the accumulator.
|
|
|
|
set_accumulator(factory_->NewConsString(accumulator(), result_string));
|
|
|
|
return SUCCESS;
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue(
|
|
|
|
Handle<JSValue> object) {
|
|
|
|
bool has_exception = false;
|
|
|
|
String* class_name = object->class_name();
|
|
|
|
if (class_name == isolate_->heap()->String_symbol()) {
|
|
|
|
Handle<Object> value = Execution::ToString(object, &has_exception);
|
|
|
|
if (has_exception) return EXCEPTION;
|
|
|
|
SerializeString(Handle<String>::cast(value));
|
|
|
|
} else if (class_name == isolate_->heap()->Number_symbol()) {
|
|
|
|
Handle<Object> value = Execution::ToNumber(object, &has_exception);
|
|
|
|
if (has_exception) return EXCEPTION;
|
|
|
|
if (value->IsSmi()) return SerializeSmi(Smi::cast(*value));
|
|
|
|
SerializeHeapNumber(Handle<HeapNumber>::cast(value));
|
|
|
|
} else {
|
|
|
|
ASSERT(class_name == isolate_->heap()->Boolean_symbol());
|
|
|
|
Object* value = JSValue::cast(*object)->value();
|
|
|
|
ASSERT(value->IsBoolean());
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii(value->IsTrue() ? "true" : "false");
|
2012-11-02 14:46:57 +00:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
|
|
|
|
static const int kBufferSize = 100;
|
|
|
|
char chars[kBufferSize];
|
|
|
|
Vector<char> buffer(chars, kBufferSize);
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii(IntToCString(object->value(), buffer));
|
2012-10-22 14:22:58 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
|
|
|
|
double number) {
|
|
|
|
if (isinf(number) || isnan(number)) {
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("null");
|
2012-10-22 14:22:58 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
static const int kBufferSize = 100;
|
|
|
|
char chars[kBufferSize];
|
|
|
|
Vector<char> buffer(chars, kBufferSize);
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii(DoubleToCString(number, buffer));
|
2012-10-22 14:22:58 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
|
2012-10-22 14:22:58 +00:00
|
|
|
Handle<JSArray> object) {
|
|
|
|
HandleScope handle_scope(isolate_);
|
2012-10-24 15:49:59 +00:00
|
|
|
Result stack_push = StackPush(object);
|
|
|
|
if (stack_push != SUCCESS) return stack_push;
|
2012-10-22 14:22:58 +00:00
|
|
|
int length = Smi::cast(object->length())->value();
|
|
|
|
Append('[');
|
|
|
|
switch (object->GetElementsKind()) {
|
|
|
|
case FAST_SMI_ELEMENTS: {
|
2012-11-05 10:53:56 +00:00
|
|
|
Handle<FixedArray> elements(
|
|
|
|
FixedArray::cast(object->elements()), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (i > 0) Append(',');
|
|
|
|
SerializeSmi(Smi::cast(elements->get(i)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FAST_DOUBLE_ELEMENTS: {
|
2012-11-02 14:46:57 +00:00
|
|
|
Handle<FixedDoubleArray> elements(
|
2012-11-05 10:53:56 +00:00
|
|
|
FixedDoubleArray::cast(object->elements()), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (i > 0) Append(',');
|
|
|
|
SerializeDouble(elements->get_scalar(i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FAST_ELEMENTS: {
|
2012-11-05 10:53:56 +00:00
|
|
|
Handle<FixedArray> elements(
|
|
|
|
FixedArray::cast(object->elements()), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (i > 0) Append(',');
|
2012-11-05 10:53:56 +00:00
|
|
|
Result result =
|
|
|
|
SerializeElement(Handle<Object>(elements->get(i), isolate_), i);
|
2012-10-22 14:22:58 +00:00
|
|
|
if (result == SUCCESS) continue;
|
|
|
|
if (result == UNCHANGED) {
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("null");
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-11-02 14:46:57 +00:00
|
|
|
// TODO(yangguo): The FAST_HOLEY_* cases could be handled in a faster way.
|
|
|
|
// They resemble the non-holey cases except that a prototype chain lookup
|
|
|
|
// is necessary for holes.
|
|
|
|
default: {
|
|
|
|
Result result = SerializeJSArraySlow(object, length);
|
|
|
|
if (result != SUCCESS) return result;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
Append(']');
|
|
|
|
StackPop();
|
|
|
|
current_part_ = handle_scope.CloseAndEscape(current_part_);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow(
|
|
|
|
Handle<JSArray> object, int length) {
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (i > 0) Append(',');
|
|
|
|
Handle<Object> element = Object::GetElement(object, i);
|
|
|
|
if (element->IsUndefined()) {
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("null");
|
2012-11-02 14:46:57 +00:00
|
|
|
} else {
|
|
|
|
Result result = SerializeElement(element, i);
|
|
|
|
if (result == SUCCESS) continue;
|
|
|
|
if (result == UNCHANGED) {
|
2013-01-09 15:47:53 +00:00
|
|
|
AppendAscii("null");
|
2012-11-02 14:46:57 +00:00
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
|
2012-10-22 14:22:58 +00:00
|
|
|
Handle<JSObject> object) {
|
|
|
|
HandleScope handle_scope(isolate_);
|
|
|
|
Result stack_push = StackPush(object);
|
|
|
|
if (stack_push != SUCCESS) return stack_push;
|
2012-11-02 14:46:57 +00:00
|
|
|
if (object->IsJSGlobalProxy()) {
|
2012-11-05 10:53:56 +00:00
|
|
|
object = Handle<JSObject>(
|
|
|
|
JSObject::cast(object->GetPrototype()), isolate_);
|
2012-11-02 14:46:57 +00:00
|
|
|
ASSERT(object->IsGlobalObject());
|
|
|
|
}
|
2012-11-05 12:59:35 +00:00
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
Append('{');
|
|
|
|
bool comma = false;
|
2012-11-05 12:59:35 +00:00
|
|
|
|
|
|
|
if (object->HasFastProperties() &&
|
|
|
|
!object->HasIndexedInterceptor() &&
|
|
|
|
!object->HasNamedInterceptor() &&
|
2012-11-05 15:01:51 +00:00
|
|
|
object->elements()->length() == 0) {
|
2012-11-05 12:59:35 +00:00
|
|
|
Handle<Map> map(object->map());
|
2012-11-05 18:39:59 +00:00
|
|
|
for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
|
2012-11-05 15:01:51 +00:00
|
|
|
Handle<String> key(map->instance_descriptors()->GetKey(i), isolate_);
|
|
|
|
PropertyDetails details = map->instance_descriptors()->GetDetails(i);
|
2012-11-05 12:59:35 +00:00
|
|
|
if (details.IsDontEnum() || details.IsDeleted()) continue;
|
|
|
|
Handle<Object> property;
|
2012-11-05 15:01:51 +00:00
|
|
|
if (details.type() == FIELD && *map == object->map()) {
|
2012-11-05 12:59:35 +00:00
|
|
|
property = Handle<Object>(
|
2012-11-05 15:01:51 +00:00
|
|
|
object->FastPropertyAt(
|
|
|
|
map->instance_descriptors()->GetFieldIndex(i)),
|
|
|
|
isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
2012-11-05 12:59:35 +00:00
|
|
|
property = GetProperty(object, key);
|
2012-11-05 15:01:51 +00:00
|
|
|
if (property.is_null()) return EXCEPTION;
|
2012-11-05 12:59:35 +00:00
|
|
|
}
|
|
|
|
Result result = SerializeProperty(property, comma, key);
|
|
|
|
if (!comma && result == SUCCESS) comma = true;
|
|
|
|
if (result >= EXCEPTION) return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool has_exception = false;
|
|
|
|
Handle<FixedArray> contents =
|
|
|
|
GetKeysInFixedArrayFor(object, LOCAL_ONLY, &has_exception);
|
|
|
|
if (has_exception) return EXCEPTION;
|
|
|
|
|
|
|
|
for (int i = 0; i < contents->length(); i++) {
|
|
|
|
Object* key = contents->get(i);
|
|
|
|
Handle<String> key_handle;
|
|
|
|
Handle<Object> property;
|
|
|
|
if (key->IsString()) {
|
|
|
|
key_handle = Handle<String>(String::cast(key), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
property = GetProperty(object, key_handle);
|
2012-11-05 12:59:35 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(key->IsNumber());
|
|
|
|
key_handle = factory_->NumberToString(Handle<Object>(key, isolate_));
|
|
|
|
uint32_t index;
|
|
|
|
if (key->IsSmi()) {
|
|
|
|
property = Object::GetElement(object, Smi::cast(key)->value());
|
|
|
|
} else if (key_handle->AsArrayIndex(&index)) {
|
|
|
|
property = Object::GetElement(object, index);
|
|
|
|
} else {
|
|
|
|
property = GetProperty(object, key_handle);
|
|
|
|
}
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
2012-11-05 12:59:35 +00:00
|
|
|
if (property.is_null()) return EXCEPTION;
|
|
|
|
Result result = SerializeProperty(property, comma, key_handle);
|
|
|
|
if (!comma && result == SUCCESS) comma = true;
|
|
|
|
if (result >= EXCEPTION) return result;
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 12:59:35 +00:00
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
Append('}');
|
|
|
|
StackPop();
|
|
|
|
current_part_ = handle_scope.CloseAndEscape(current_part_);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicJsonStringifier::ShrinkCurrentPart() {
|
|
|
|
ASSERT(current_index_ < part_length_);
|
2012-12-05 15:49:22 +00:00
|
|
|
current_part_ = Handle<String>(
|
|
|
|
SeqString::cast(*current_part_)->Truncate(current_index_), isolate_);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicJsonStringifier::Extend() {
|
2012-11-05 10:53:56 +00:00
|
|
|
set_accumulator(factory_->NewConsString(accumulator(), current_part_));
|
2012-10-22 14:22:58 +00:00
|
|
|
if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) {
|
|
|
|
part_length_ *= kPartLengthGrowthFactor;
|
|
|
|
}
|
2012-11-05 10:53:56 +00:00
|
|
|
if (is_ascii_) {
|
2012-11-15 13:31:27 +00:00
|
|
|
current_part_ = factory_->NewRawOneByteString(part_length_);
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
2012-11-05 10:53:56 +00:00
|
|
|
current_part_ = factory_->NewRawTwoByteString(part_length_);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
current_index_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicJsonStringifier::ChangeEncoding() {
|
|
|
|
ShrinkCurrentPart();
|
2012-11-05 10:53:56 +00:00
|
|
|
set_accumulator(factory_->NewConsString(accumulator(), current_part_));
|
|
|
|
current_part_ = factory_->NewRawTwoByteString(part_length_);
|
2012-10-22 14:22:58 +00:00
|
|
|
current_index_ = 0;
|
|
|
|
is_ascii_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
template <typename SrcChar, typename DestChar>
|
|
|
|
void BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src,
|
|
|
|
DestChar* dest,
|
|
|
|
int length) {
|
|
|
|
dest += current_index_;
|
|
|
|
DestChar* dest_start = dest;
|
|
|
|
|
2012-10-23 09:39:19 +00:00
|
|
|
// Assert that uc16 character is not truncated down to 8 bit.
|
|
|
|
// The <uc16, char> version of this method must not be called.
|
|
|
|
ASSERT(sizeof(*dest) >= sizeof(*src));
|
|
|
|
|
2012-10-23 09:05:56 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
SrcChar c = src[i];
|
|
|
|
if (DoNotEscape(c)) {
|
2012-10-23 09:39:19 +00:00
|
|
|
*(dest++) = static_cast<DestChar>(c);
|
2012-10-23 09:05:56 +00:00
|
|
|
} else {
|
|
|
|
const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize];
|
|
|
|
while (*chars != '\0') *(dest++) = *(chars++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:00:42 +00:00
|
|
|
current_index_ += static_cast<int>(dest - dest_start);
|
2012-10-23 09:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
template <bool is_ascii, typename Char>
|
2012-12-03 16:23:30 +00:00
|
|
|
void BasicJsonStringifier::SerializeString_(Handle<String> string) {
|
|
|
|
int length = string->length();
|
2012-11-12 10:20:07 +00:00
|
|
|
Append_<is_ascii, char>('"');
|
2012-10-23 09:05:56 +00:00
|
|
|
// We make a rough estimate to find out if the current string can be
|
|
|
|
// serialized without allocating a new string part. The worst case length of
|
2012-11-12 10:20:07 +00:00
|
|
|
// an escaped character is 6. Shifting the remainin string length right by 3
|
|
|
|
// is a more pessimistic estimate, but faster to calculate.
|
|
|
|
|
|
|
|
if (((part_length_ - current_index_) >> 3) > length) {
|
2012-12-03 16:23:30 +00:00
|
|
|
AssertNoAllocation no_allocation;
|
|
|
|
Vector<const Char> vector = GetCharVector<Char>(string);
|
2012-10-23 09:05:56 +00:00
|
|
|
if (is_ascii) {
|
|
|
|
SerializeStringUnchecked_(
|
|
|
|
vector.start(),
|
2012-11-15 13:31:27 +00:00
|
|
|
SeqOneByteString::cast(*current_part_)->GetChars(),
|
2012-10-23 09:05:56 +00:00
|
|
|
length);
|
|
|
|
} else {
|
|
|
|
SerializeStringUnchecked_(
|
|
|
|
vector.start(),
|
|
|
|
SeqTwoByteString::cast(*current_part_)->GetChars(),
|
|
|
|
length);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String* string_location = *string;
|
2012-12-03 16:23:30 +00:00
|
|
|
Vector<const Char> vector = GetCharVector<Char>(string);
|
2012-10-22 14:22:58 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Char c = vector[i];
|
2012-10-23 09:05:56 +00:00
|
|
|
if (DoNotEscape(c)) {
|
2012-10-22 14:22:58 +00:00
|
|
|
Append_<is_ascii, Char>(c);
|
|
|
|
} else {
|
2013-01-09 15:47:53 +00:00
|
|
|
Append_<is_ascii, uint8_t>(
|
|
|
|
reinterpret_cast<const uint8_t*>(
|
|
|
|
&JsonEscapeTable[c * kJsonEscapeTableEntrySize]));
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
// If GC moved the string, we need to refresh the vector.
|
|
|
|
if (*string != string_location) {
|
|
|
|
vector = GetCharVector<Char>(string);
|
|
|
|
string_location = *string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-12 10:20:07 +00:00
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
Append_<is_ascii, uint8_t>('"');
|
2012-10-23 09:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
template <typename Char>
|
|
|
|
bool BasicJsonStringifier::DoNotEscape(Char c) {
|
2012-10-23 09:05:56 +00:00
|
|
|
return (c >= 0x80) || (c >= '#' && c <= '~' && c != '\\');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-22 14:22:58 +00:00
|
|
|
template <>
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<const uint8_t> BasicJsonStringifier::GetCharVector(
|
|
|
|
Handle<String> string) {
|
2012-10-22 14:22:58 +00:00
|
|
|
String::FlatContent flat = string->GetFlatContent();
|
|
|
|
ASSERT(flat.IsAscii());
|
2013-01-09 15:47:53 +00:00
|
|
|
return flat.ToOneByteVector();
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
Vector<const uc16> BasicJsonStringifier::GetCharVector(Handle<String> string) {
|
|
|
|
String::FlatContent flat = string->GetFlatContent();
|
|
|
|
ASSERT(flat.IsTwoByte());
|
|
|
|
return flat.ToUC16Vector();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicJsonStringifier::SerializeString(Handle<String> object) {
|
|
|
|
FlattenString(object);
|
|
|
|
String::FlatContent flat = object->GetFlatContent();
|
|
|
|
if (is_ascii_) {
|
|
|
|
if (flat.IsAscii()) {
|
2013-01-09 15:47:53 +00:00
|
|
|
SerializeString_<true, uint8_t>(object);
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
|
|
|
ChangeEncoding();
|
|
|
|
SerializeString(object);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (flat.IsAscii()) {
|
2013-01-09 15:47:53 +00:00
|
|
|
SerializeString_<false, uint8_t>(object);
|
2012-10-22 14:22:58 +00:00
|
|
|
} else {
|
2012-12-03 16:23:30 +00:00
|
|
|
SerializeString_<false, uc16>(object);
|
2012-10-22 14:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_JSON_STRINGIFIER_H_
|