OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "include/v8.h" | 10 #include "include/v8.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 template <typename MessageFunctor> | 77 template <typename MessageFunctor> |
78 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) { | 78 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) { |
79 Context::Scope scope(serialization_context()); | 79 Context::Scope scope(serialization_context()); |
80 TryCatch try_catch(isolate()); | 80 TryCatch try_catch(isolate()); |
81 Local<Value> input_value = EvaluateScriptForInput(source); | 81 Local<Value> input_value = EvaluateScriptForInput(source); |
82 ASSERT_TRUE(DoEncode(input_value).IsNothing()); | 82 ASSERT_TRUE(DoEncode(input_value).IsNothing()); |
83 functor(try_catch.Message()); | 83 functor(try_catch.Message()); |
84 } | 84 } |
85 | 85 |
| 86 void InvalidEncodeTest(const char* source) { |
| 87 InvalidEncodeTest(source, [](Local<Message>) {}); |
| 88 } |
| 89 |
86 template <typename OutputFunctor> | 90 template <typename OutputFunctor> |
87 void DecodeTest(const std::vector<uint8_t>& data, | 91 void DecodeTest(const std::vector<uint8_t>& data, |
88 const OutputFunctor& output_functor) { | 92 const OutputFunctor& output_functor) { |
89 Context::Scope scope(deserialization_context()); | 93 Context::Scope scope(deserialization_context()); |
90 TryCatch try_catch(isolate()); | 94 TryCatch try_catch(isolate()); |
91 // TODO(jbroman): Use the public API once it exists. | 95 // TODO(jbroman): Use the public API once it exists. |
92 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); | 96 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |
93 i::HandleScope handle_scope(internal_isolate); | 97 i::HandleScope handle_scope(internal_isolate); |
94 i::ValueDeserializer deserializer( | 98 i::ValueDeserializer deserializer( |
95 internal_isolate, | 99 internal_isolate, |
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 DecodeTest( | 1123 DecodeTest( |
1120 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f, | 1124 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f, |
1121 0x01, 0x44, 0x00, 0x20, 0x39, 0x50, 0x37, 0x6a, 0x75, 0x42, 0x3f, | 1125 0x01, 0x44, 0x00, 0x20, 0x39, 0x50, 0x37, 0x6a, 0x75, 0x42, 0x3f, |
1122 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02}, | 1126 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02}, |
1123 [this](Local<Value> value) { | 1127 [this](Local<Value> value) { |
1124 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Date")); | 1128 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Date")); |
1125 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 1129 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
1126 }); | 1130 }); |
1127 } | 1131 } |
1128 | 1132 |
| 1133 TEST_F(ValueSerializerTest, RoundTripValueObjects) { |
| 1134 RoundTripTest("new Boolean(true)", [this](Local<Value> value) { |
| 1135 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1136 "Object.getPrototypeOf(result) === Boolean.prototype")); |
| 1137 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === true")); |
| 1138 }); |
| 1139 RoundTripTest("new Boolean(false)", [this](Local<Value> value) { |
| 1140 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1141 "Object.getPrototypeOf(result) === Boolean.prototype")); |
| 1142 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === false")); |
| 1143 }); |
| 1144 RoundTripTest( |
| 1145 "({ a: new Boolean(true), get b() { return this.a; }})", |
| 1146 [this](Local<Value> value) { |
| 1147 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Boolean")); |
| 1148 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1149 }); |
| 1150 RoundTripTest("new Number(-42)", [this](Local<Value> value) { |
| 1151 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1152 "Object.getPrototypeOf(result) === Number.prototype")); |
| 1153 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === -42")); |
| 1154 }); |
| 1155 RoundTripTest("new Number(NaN)", [this](Local<Value> value) { |
| 1156 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1157 "Object.getPrototypeOf(result) === Number.prototype")); |
| 1158 EXPECT_TRUE(EvaluateScriptForResultBool("Number.isNaN(result.valueOf())")); |
| 1159 }); |
| 1160 RoundTripTest( |
| 1161 "({ a: new Number(6), get b() { return this.a; }})", |
| 1162 [this](Local<Value> value) { |
| 1163 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Number")); |
| 1164 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1165 }); |
| 1166 RoundTripTest("new String('Qu\\xe9bec')", [this](Local<Value> value) { |
| 1167 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1168 "Object.getPrototypeOf(result) === String.prototype")); |
| 1169 EXPECT_TRUE( |
| 1170 EvaluateScriptForResultBool("result.valueOf() === 'Qu\\xe9bec'")); |
| 1171 EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 6")); |
| 1172 }); |
| 1173 RoundTripTest("new String('\\ud83d\\udc4a')", [this](Local<Value> value) { |
| 1174 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1175 "Object.getPrototypeOf(result) === String.prototype")); |
| 1176 EXPECT_TRUE( |
| 1177 EvaluateScriptForResultBool("result.valueOf() === '\\ud83d\\udc4a'")); |
| 1178 EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 2")); |
| 1179 }); |
| 1180 RoundTripTest( |
| 1181 "({ a: new String(), get b() { return this.a; }})", |
| 1182 [this](Local<Value> value) { |
| 1183 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof String")); |
| 1184 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1185 }); |
| 1186 } |
| 1187 |
| 1188 TEST_F(ValueSerializerTest, RejectsOtherValueObjects) { |
| 1189 // This is a roundabout way of getting an instance of Symbol. |
| 1190 InvalidEncodeTest("Object.valueOf.apply(Symbol())"); |
| 1191 } |
| 1192 |
| 1193 TEST_F(ValueSerializerTest, DecodeValueObjects) { |
| 1194 DecodeTest( |
| 1195 {0xff, 0x09, 0x3f, 0x00, 0x79, 0x00}, |
| 1196 [this](Local<Value> value) { |
| 1197 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1198 "Object.getPrototypeOf(result) === Boolean.prototype")); |
| 1199 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === true")); |
| 1200 }); |
| 1201 DecodeTest( |
| 1202 {0xff, 0x09, 0x3f, 0x00, 0x78, 0x00}, |
| 1203 [this](Local<Value> value) { |
| 1204 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1205 "Object.getPrototypeOf(result) === Boolean.prototype")); |
| 1206 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === false")); |
| 1207 }); |
| 1208 DecodeTest( |
| 1209 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f, 0x01, |
| 1210 0x79, 0x3f, 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02}, |
| 1211 [this](Local<Value> value) { |
| 1212 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Boolean")); |
| 1213 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1214 }); |
| 1215 DecodeTest( |
| 1216 {0xff, 0x09, 0x3f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, |
| 1217 0xc0, 0x00}, |
| 1218 [this](Local<Value> value) { |
| 1219 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1220 "Object.getPrototypeOf(result) === Number.prototype")); |
| 1221 EXPECT_TRUE(EvaluateScriptForResultBool("result.valueOf() === -42")); |
| 1222 }); |
| 1223 DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1224 0xf8, 0x7f, 0x00}, |
| 1225 [this](Local<Value> value) { |
| 1226 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1227 "Object.getPrototypeOf(result) === Number.prototype")); |
| 1228 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1229 "Number.isNaN(result.valueOf())")); |
| 1230 }); |
| 1231 DecodeTest( |
| 1232 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, 0x61, 0x3f, |
| 1233 0x01, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x40, 0x3f, |
| 1234 0x02, 0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02}, |
| 1235 [this](Local<Value> value) { |
| 1236 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof Number")); |
| 1237 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1238 }); |
| 1239 DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x73, 0x07, 0x51, 0x75, 0xc3, 0xa9, 0x62, |
| 1240 0x65, 0x63, 0x00}, |
| 1241 [this](Local<Value> value) { |
| 1242 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1243 "Object.getPrototypeOf(result) === String.prototype")); |
| 1244 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1245 "result.valueOf() === 'Qu\\xe9bec'")); |
| 1246 EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 6")); |
| 1247 }); |
| 1248 DecodeTest({0xff, 0x09, 0x3f, 0x00, 0x73, 0x04, 0xf0, 0x9f, 0x91, 0x8a}, |
| 1249 [this](Local<Value> value) { |
| 1250 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1251 "Object.getPrototypeOf(result) === String.prototype")); |
| 1252 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1253 "result.valueOf() === '\\ud83d\\udc4a'")); |
| 1254 EXPECT_TRUE(EvaluateScriptForResultBool("result.length === 2")); |
| 1255 }); |
| 1256 DecodeTest( |
| 1257 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x01, |
| 1258 0x61, 0x3f, 0x01, 0x73, 0x00, 0x3f, 0x02, 0x53, 0x01, |
| 1259 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02, 0x00}, |
| 1260 [this](Local<Value> value) { |
| 1261 EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof String")); |
| 1262 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1263 }); |
| 1264 } |
| 1265 |
1129 } // namespace | 1266 } // namespace |
1130 } // namespace v8 | 1267 } // namespace v8 |
OLD | NEW |