Commit cb68e2cd authored by jochen@chromium.org's avatar jochen@chromium.org

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
parent 47c3a081
......@@ -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 ---
......
......@@ -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 {
......
......@@ -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()) {
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment