Expose JSON parser through V8 API

BUG=v8:2821
TEST=cctest/test-api/JSONParse
R=yangguo@chromium.org

Review URL: https://codereview.chromium.org/21959003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16048 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
jochen@chromium.org 2013-08-05 11:14:46 +00:00
parent 47c3a081f1
commit cb68e2cd9b
4 changed files with 51 additions and 1 deletions

View File

@ -1254,6 +1254,22 @@ class V8EXPORT StackFrame {
};
/**
* A JSON Parser.
*/
class V8EXPORT JSON {
public:
/**
* Tries to parse the string |json_string| and returns it as object if
* successful.
*
* \param json_string The string to parse.
* \return The corresponding object if successfully parsed.
*/
static Local<Object> Parse(Local<String> json_string);
};
// --- Value ---

View File

@ -46,6 +46,7 @@
#include "heap-profiler.h"
#include "heap-snapshot-generator-inl.h"
#include "icu_util.h"
#include "json-parser.h"
#include "messages.h"
#ifdef COMPRESS_STARTUP_DATA_BZ2
#include "natives.h"
@ -2607,6 +2608,29 @@ bool StackFrame::IsConstructor() const {
}
// --- J S O N ---
Local<Object> JSON::Parse(Local<String> json_string) {
i::Isolate* isolate = i::Isolate::Current();
EnsureInitializedForIsolate(isolate, "v8::JSON::Parse");
ENTER_V8(isolate);
i::HandleScope scope(isolate);
i::Handle<i::String> source = i::Handle<i::String>(
FlattenGetString(Utils::OpenHandle(*json_string)));
EXCEPTION_PREAMBLE(isolate);
i::Handle<i::Object> result;
if (source->IsSeqOneByteString()) {
result = i::JsonParser<true>::Parse(source);
} else {
result = i::JsonParser<false>::Parse(source);
}
has_pending_exception = result.is_null();
EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
return Utils::ToLocal(
i::Handle<i::JSObject>::cast(scope.CloseAndEscape(result)));
}
// --- D a t a ---
bool Value::FullIsUndefined() const {

View File

@ -9483,7 +9483,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) {
ASSERT_EQ(1, args.length());
CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
source = Handle<String>(source->TryFlattenGetString());
source = Handle<String>(FlattenGetString(source));
// Optimized fast case where we only have ASCII characters.
Handle<Object> result;
if (source->IsSeqOneByteString()) {

View File

@ -19863,6 +19863,16 @@ THREADED_TEST(Regress260106) {
}
THREADED_TEST(JSONParse) {
LocalContext context;
HandleScope scope(context->GetIsolate());
Local<Object> obj = v8::JSON::Parse(v8_str("{\"x\":42}"));
Handle<Object> global = context->Global();
global->Set(v8_str("obj"), obj);
ExpectString("JSON.stringify(obj)", "{\"x\":42}");
}
#ifndef WIN32
class ThreadInterruptTest {
public: