Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(513)

Unified Diff: test/unittests/value-serializer-unittest.cc

Issue 2265603002: Blink-compatible serialization of Boolean, Number and String objects. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@vs-date
Patch Set: remove HasSpecificClassOf Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/value-serializer.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/unittests/value-serializer-unittest.cc
diff --git a/test/unittests/value-serializer-unittest.cc b/test/unittests/value-serializer-unittest.cc
index 41fbbdab5e7d9462d41492d249af4b78ac9dfd11..96aba398a74a96b369c216ec317d519389d8bbb8 100644
--- a/test/unittests/value-serializer-unittest.cc
+++ b/test/unittests/value-serializer-unittest.cc
@@ -83,6 +83,10 @@ class ValueSerializerTest : public TestWithIsolate {
functor(try_catch.Message());
}
+ void InvalidEncodeTest(const char* source) {
+ InvalidEncodeTest(source, [](Local<Message>) {});
+ }
+
template <typename OutputFunctor>
void DecodeTest(const std::vector<uint8_t>& data,
const OutputFunctor& output_functor) {
@@ -1126,5 +1130,138 @@ TEST_F(ValueSerializerTest, DecodeDate) {
});
}
+TEST_F(ValueSerializerTest, RoundTripValueObjects) {
+ RoundTripTest("new Boolean(true)", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Boolean.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === true"));
+ });
+ RoundTripTest("new Boolean(false)", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Boolean.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === false"));
+ });
+ RoundTripTest(
+ "({ a: new Boolean(true), get b() { return this.a; }})",
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Boolean"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+ RoundTripTest("new Number(-42)", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Number.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === -42"));
+ });
+ RoundTripTest("new Number(NaN)", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Number.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("Number.isNaN(result.valueOf())"));
+ });
+ RoundTripTest(
+ "({ a: new Number(6), get b() { return this.a; }})",
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Number"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+ RoundTripTest("new String('Qu\\xe9bec')", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === String.prototype"));
+ EXPECT_TRUE(
+ EvaluateScriptForResultBool("result.valueOf() === 'Qu\\xe9bec'"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 6"));
+ });
+ RoundTripTest("new String('\\ud83d\\udc4a')", [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === String.prototype"));
+ EXPECT_TRUE(
+ EvaluateScriptForResultBool("result.valueOf() === '\\ud83d\\udc4a'"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 2"));
+ });
+ RoundTripTest(
+ "({ a: new String(), get b() { return this.a; }})",
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof String"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+}
+
+TEST_F(ValueSerializerTest, RejectsOtherValueObjects) {
+ // This is a roundabout way of getting an instance of Symbol.
+ InvalidEncodeTest("Object.valueOf.apply(Symbol())");
+}
+
+TEST_F(ValueSerializerTest, DecodeValueObjects) {
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x79, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Boolean.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === true"));
+ });
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x78, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Boolean.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === false"));
+ });
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f, 0x01,
+ 0x79, 0x3f, 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Boolean"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45,
+ 0xc0, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Number.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === -42"));
+ });
+ DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xf8, 0x7f, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === Number.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Number.isNaN(result.valueOf())"));
+ });
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f,
+ 0x01, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x40, 0x3f,
+ 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Number"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+ DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x73, 0x07, 0x51, 0x75, 0xc3, 0xa9, 0x62,
+ 0x65, 0x63, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === String.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "result.valueOf() === 'Qu\\xe9bec'"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 6"));
+ });
+ DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x73, 0x04, 0xf0, 0x9f, 0x91, 0x8a},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "Object.getPrototypeOf(result) === String.prototype"));
+ EXPECT_TRUE(EvaluateScriptForResultBool(
+ "result.valueOf() === '\\ud83d\\udc4a'"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 2"));
+ });
+ DecodeTest(
+ {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01,
+ 0x61, 0x3f, 0x01, 0x73, 0x00, 0x3f, 0x02, 0x53, 0x01,
+ 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02, 0x00},
+ [this](Local<Value> value) {
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof String"));
+ EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
+ });
+}
+
} // namespace
} // namespace v8
« no previous file with comments | « src/value-serializer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698