Revision: 16048
Author: [email protected]
Date: Mon Aug 5 04:14:46 2013
Log: Expose JSON parser through V8 API
BUG=v8:2821
TEST=cctest/test-api/JSONParse
[email protected]
Review URL: https://codereview.chromium.org/21959003
http://code.google.com/p/v8/source/detail?r=16048
Modified:
/branches/bleeding_edge/include/v8.h
/branches/bleeding_edge/src/api.cc
/branches/bleeding_edge/src/runtime.cc
/branches/bleeding_edge/test/cctest/test-api.cc
=======================================
--- /branches/bleeding_edge/include/v8.h Mon Aug 5 02:46:23 2013
+++ /branches/bleeding_edge/include/v8.h Mon Aug 5 04:14:46 2013
@@ -1254,6 +1254,22 @@
};
+/**
+ * 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 ---
=======================================
--- /branches/bleeding_edge/src/api.cc Mon Aug 5 00:17:08 2013
+++ /branches/bleeding_edge/src/api.cc Mon Aug 5 04:14:46 2013
@@ -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"
@@ -2605,6 +2606,29 @@
i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor");
return is_constructor->IsTrue();
}
+
+
+// --- 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 ---
=======================================
--- /branches/bleeding_edge/src/runtime.cc Fri Aug 2 06:03:06 2013
+++ /branches/bleeding_edge/src/runtime.cc Mon Aug 5 04:14:46 2013
@@ -9483,7 +9483,7 @@
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()) {
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Tue Jul 30 10:05:50 2013
+++ /branches/bleeding_edge/test/cctest/test-api.cc Mon Aug 5 04:14:46 2013
@@ -19861,6 +19861,16 @@
CHECK(!function.IsEmpty());
CHECK(function->IsFunction());
}
+
+
+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
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.