1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "src/frames-inl.h"
34 #include "src/string-stream.h"
35 #include "test/cctest/cctest.h"
37 using ::v8::ObjectTemplate;
44 using ::v8::Extension;
46 static void handle_property(Local<String> name,
47 const v8::PropertyCallbackInfo<v8::Value>& info) {
48 ApiTestFuzzer::Fuzz();
49 info.GetReturnValue().Set(v8_num(900));
52 static void handle_property_2(Local<String> name,
53 const v8::PropertyCallbackInfo<v8::Value>& info) {
54 ApiTestFuzzer::Fuzz();
55 info.GetReturnValue().Set(v8_num(902));
59 static void handle_property(const v8::FunctionCallbackInfo<v8::Value>& info) {
60 ApiTestFuzzer::Fuzz();
61 CHECK_EQ(0, info.Length());
62 info.GetReturnValue().Set(v8_num(907));
66 THREADED_TEST(PropertyHandler) {
68 v8::Isolate* isolate = env->GetIsolate();
69 v8::HandleScope scope(isolate);
70 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(isolate);
71 fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property);
72 Local<v8::FunctionTemplate> getter_templ =
73 v8::FunctionTemplate::New(isolate, handle_property);
74 getter_templ->SetLength(0);
76 InstanceTemplate()->SetAccessorProperty(v8_str("bar"), getter_templ);
77 fun_templ->InstanceTemplate()->
78 SetNativeDataProperty(v8_str("instance_foo"), handle_property);
79 fun_templ->SetNativeDataProperty(v8_str("object_foo"), handle_property_2);
80 Local<Function> fun = fun_templ->GetFunction();
81 env->Global()->Set(v8_str("Fun"), fun);
84 // check function instance accessors
85 getter = v8_compile("var obj = new Fun(); obj.instance_foo;");
86 CHECK_EQ(900, getter->Run()->Int32Value());
87 setter = v8_compile("obj.instance_foo = 901;");
88 CHECK_EQ(901, setter->Run()->Int32Value());
89 getter = v8_compile("obj.bar;");
90 CHECK_EQ(907, getter->Run()->Int32Value());
91 setter = v8_compile("obj.bar = 908;");
92 CHECK_EQ(908, setter->Run()->Int32Value());
93 // check function static accessors
94 getter = v8_compile("Fun.object_foo;");
95 CHECK_EQ(902, getter->Run()->Int32Value());
96 setter = v8_compile("Fun.object_foo = 903;");
97 CHECK_EQ(903, setter->Run()->Int32Value());
101 static void GetIntValue(Local<String> property,
102 const v8::PropertyCallbackInfo<v8::Value>& info) {
103 ApiTestFuzzer::Fuzz();
105 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value());
106 info.GetReturnValue().Set(v8_num(*value));
110 static void SetIntValue(Local<String> property,
112 const v8::PropertyCallbackInfo<void>& info) {
114 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value());
115 *field = value->Int32Value();
120 THREADED_TEST(GlobalVariableAccess) {
124 v8::Isolate* isolate = CcTest::isolate();
125 v8::HandleScope scope(isolate);
126 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
127 templ->InstanceTemplate()->SetAccessor(
128 v8_str("foo"), GetIntValue, SetIntValue,
129 v8::External::New(isolate, &foo));
130 templ->InstanceTemplate()->SetAccessor(
131 v8_str("bar"), GetIntValue, SetIntValue,
132 v8::External::New(isolate, &bar));
133 templ->InstanceTemplate()->SetAccessor(
134 v8_str("baz"), GetIntValue, SetIntValue,
135 v8::External::New(isolate, &baz));
136 LocalContext env(0, templ->InstanceTemplate());
137 v8_compile("foo = (++bar) + baz")->Run();
143 static int x_register[2] = {0, 0};
144 static v8::Handle<v8::Object> x_receiver;
145 static v8::Handle<v8::Object> x_holder;
148 static void XGetter(const Info& info, int offset) {
149 ApiTestFuzzer::Fuzz();
150 v8::Isolate* isolate = CcTest::isolate();
151 CHECK_EQ(isolate, info.GetIsolate());
152 CHECK_EQ(x_receiver, info.This());
153 info.GetReturnValue().Set(v8_num(x_register[offset]));
157 static void XGetter(Local<String> name,
158 const v8::PropertyCallbackInfo<v8::Value>& info) {
159 CHECK_EQ(x_holder, info.Holder());
164 static void XGetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
165 CHECK_EQ(x_receiver, info.Holder());
171 static void XSetter(Local<Value> value, const Info& info, int offset) {
172 v8::Isolate* isolate = CcTest::isolate();
173 CHECK_EQ(isolate, info.GetIsolate());
174 CHECK_EQ(x_holder, info.This());
175 CHECK_EQ(x_holder, info.Holder());
176 x_register[offset] = value->Int32Value();
177 info.GetReturnValue().Set(v8_num(-1));
181 static void XSetter(Local<String> name,
183 const v8::PropertyCallbackInfo<void>& info) {
184 XSetter(value, info, 0);
188 static void XSetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
189 CHECK_EQ(1, info.Length());
190 XSetter(info[0], info, 1);
194 THREADED_TEST(AccessorIC) {
195 LocalContext context;
196 v8::Isolate* isolate = context->GetIsolate();
197 v8::HandleScope scope(isolate);
198 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
199 obj->SetAccessor(v8_str("x0"), XGetter, XSetter);
200 obj->SetAccessorProperty(v8_str("x1"),
201 v8::FunctionTemplate::New(isolate, XGetter),
202 v8::FunctionTemplate::New(isolate, XSetter));
203 x_holder = obj->NewInstance();
204 context->Global()->Set(v8_str("holder"), x_holder);
205 x_receiver = v8::Object::New(isolate);
206 context->Global()->Set(v8_str("obj"), x_receiver);
207 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun(
208 "obj.__proto__ = holder;"
212 "for (var j = 0; j < 10; j++) {"
214 " result.push(holder.x0 = i);"
215 " result.push(obj.x0);"
216 " result.push(holder.x1 = i + 1);"
217 " result.push(obj.x1);"
218 " result.push(holder[key_0] = i + 2);"
219 " result.push(obj[key_0]);"
220 " result.push(holder[key_1] = i + 3);"
221 " result.push(obj[key_1]);"
224 CHECK_EQ(80, array->Length());
225 for (int i = 0; i < 80; i++) {
226 v8::Handle<Value> entry = array->Get(v8::Integer::New(isolate, i));
227 CHECK_EQ(v8::Integer::New(isolate, i/2), entry);
233 static void HandleAllocatingGetter(
235 const v8::PropertyCallbackInfo<v8::Value>& info) {
236 ApiTestFuzzer::Fuzz();
237 for (int i = 0; i < C; i++)
238 v8::String::NewFromUtf8(info.GetIsolate(), "foo");
239 info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "foo"));
243 THREADED_TEST(HandleScopePop) {
244 LocalContext context;
245 v8::Isolate* isolate = context->GetIsolate();
246 v8::HandleScope scope(isolate);
247 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
248 obj->SetAccessor(v8_str("one"), HandleAllocatingGetter<1>);
249 obj->SetAccessor(v8_str("many"), HandleAllocatingGetter<1024>);
250 v8::Handle<v8::Object> inst = obj->NewInstance();
251 context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
253 i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
255 v8::HandleScope scope(isolate);
257 "for (var i = 0; i < 1000; i++) {"
263 i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
264 CHECK_EQ(count_before, count_after);
267 static void CheckAccessorArgsCorrect(
269 const v8::PropertyCallbackInfo<v8::Value>& info) {
270 CHECK(info.GetIsolate() == CcTest::isolate());
271 CHECK(info.This() == info.Holder());
273 info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
274 ApiTestFuzzer::Fuzz();
275 CHECK(info.GetIsolate() == CcTest::isolate());
276 CHECK(info.This() == info.Holder());
278 info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
279 CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
280 CHECK(info.GetIsolate() == CcTest::isolate());
281 CHECK(info.This() == info.Holder());
283 info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
284 info.GetReturnValue().Set(17);
288 THREADED_TEST(DirectCall) {
289 LocalContext context;
290 v8::Isolate* isolate = context->GetIsolate();
291 v8::HandleScope scope(isolate);
292 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
293 obj->SetAccessor(v8_str("xxx"),
294 CheckAccessorArgsCorrect,
296 v8::String::NewFromUtf8(isolate, "data"));
297 v8::Handle<v8::Object> inst = obj->NewInstance();
298 context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"),
300 Local<Script> scr = v8::Script::Compile(
301 v8::String::NewFromUtf8(isolate, "obj.xxx"));
302 for (int i = 0; i < 10; i++) {
303 Local<Value> result = scr->Run();
304 CHECK(!result.IsEmpty());
305 CHECK_EQ(17, result->Int32Value());
309 static void EmptyGetter(Local<String> name,
310 const v8::PropertyCallbackInfo<v8::Value>& info) {
311 CheckAccessorArgsCorrect(name, info);
312 ApiTestFuzzer::Fuzz();
313 CheckAccessorArgsCorrect(name, info);
314 info.GetReturnValue().Set(v8::Handle<v8::Value>());
318 THREADED_TEST(EmptyResult) {
319 LocalContext context;
320 v8::Isolate* isolate = context->GetIsolate();
321 v8::HandleScope scope(isolate);
322 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
323 obj->SetAccessor(v8_str("xxx"), EmptyGetter, NULL,
324 v8::String::NewFromUtf8(isolate, "data"));
325 v8::Handle<v8::Object> inst = obj->NewInstance();
326 context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
328 v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
329 for (int i = 0; i < 10; i++) {
330 Local<Value> result = scr->Run();
331 CHECK(result == v8::Undefined(isolate));
336 THREADED_TEST(NoReuseRegress) {
337 // Check that the IC generated for the one test doesn't get reused
339 v8::Isolate* isolate = CcTest::isolate();
340 v8::HandleScope scope(isolate);
342 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
343 obj->SetAccessor(v8_str("xxx"), EmptyGetter, NULL,
344 v8::String::NewFromUtf8(isolate, "data"));
345 LocalContext context;
346 v8::Handle<v8::Object> inst = obj->NewInstance();
347 context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
349 v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
350 for (int i = 0; i < 2; i++) {
351 Local<Value> result = scr->Run();
352 CHECK(result == v8::Undefined(isolate));
356 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
357 obj->SetAccessor(v8_str("xxx"),
358 CheckAccessorArgsCorrect,
360 v8::String::NewFromUtf8(isolate, "data"));
361 LocalContext context;
362 v8::Handle<v8::Object> inst = obj->NewInstance();
363 context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
365 v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
366 for (int i = 0; i < 10; i++) {
367 Local<Value> result = scr->Run();
368 CHECK(!result.IsEmpty());
369 CHECK_EQ(17, result->Int32Value());
374 static void ThrowingGetAccessor(
376 const v8::PropertyCallbackInfo<v8::Value>& info) {
377 ApiTestFuzzer::Fuzz();
378 info.GetIsolate()->ThrowException(v8_str("g"));
382 static void ThrowingSetAccessor(Local<String> name,
384 const v8::PropertyCallbackInfo<void>& info) {
385 info.GetIsolate()->ThrowException(value);
389 THREADED_TEST(Regress1054726) {
391 v8::Isolate* isolate = env->GetIsolate();
392 v8::HandleScope scope(isolate);
393 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
394 obj->SetAccessor(v8_str("x"),
399 env->Global()->Set(v8_str("obj"), obj->NewInstance());
401 // Use the throwing property setter/getter in a loop to force
402 // the accessor ICs to be initialized.
403 v8::Handle<Value> result;
404 result = Script::Compile(v8_str(
406 "for (var i = 0; i < 5; i++) {"
407 " try { obj.x; } catch (e) { result += e; }"
408 "}; result"))->Run();
409 CHECK_EQ(v8_str("ggggg"), result);
411 result = Script::Compile(String::NewFromUtf8(
414 "for (var i = 0; i < 5; i++) {"
415 " try { obj.x = i; } catch (e) { result += e; }"
416 "}; result"))->Run();
417 CHECK_EQ(v8_str("01234"), result);
421 static void AllocGetter(Local<String> name,
422 const v8::PropertyCallbackInfo<v8::Value>& info) {
423 ApiTestFuzzer::Fuzz();
424 info.GetReturnValue().Set(v8::Array::New(info.GetIsolate(), 1000));
430 v8::Isolate* isolate = env->GetIsolate();
431 v8::HandleScope scope(isolate);
432 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
433 obj->SetAccessor(v8_str("xxx"), AllocGetter);
434 env->Global()->Set(v8_str("obj"), obj->NewInstance());
435 Script::Compile(String::NewFromUtf8(
438 "for (var i = 0; i < 2048; i++) {"
439 " var result = obj.xxx;"
446 static void StackCheck(Local<String> name,
447 const v8::PropertyCallbackInfo<v8::Value>& info) {
448 i::StackFrameIterator iter(reinterpret_cast<i::Isolate*>(info.GetIsolate()));
449 for (int i = 0; !iter.done(); i++) {
450 i::StackFrame* frame = iter.frame();
451 CHECK(i != 0 || (frame->type() == i::StackFrame::EXIT));
452 i::Code* code = frame->LookupCode();
453 CHECK(code->IsCode());
454 i::Address pc = frame->pc();
455 CHECK(code->contains(pc));
461 THREADED_TEST(StackIteration) {
463 v8::Isolate* isolate = env->GetIsolate();
464 v8::HandleScope scope(isolate);
465 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
466 i::StringStream::ClearMentionedObjectCache(
467 reinterpret_cast<i::Isolate*>(isolate));
468 obj->SetAccessor(v8_str("xxx"), StackCheck);
469 env->Global()->Set(v8_str("obj"), obj->NewInstance());
470 Script::Compile(String::NewFromUtf8(
475 "for (var i = 0; i < 100; i++) {"
481 static void AllocateHandles(Local<String> name,
482 const v8::PropertyCallbackInfo<v8::Value>& info) {
483 for (int i = 0; i < i::kHandleBlockSize + 1; i++) {
484 v8::Local<v8::Value>::New(info.GetIsolate(), name);
486 info.GetReturnValue().Set(v8::Integer::New(info.GetIsolate(), 100));
490 THREADED_TEST(HandleScopeSegment) {
491 // Check that we can return values past popping of handle scope
494 v8::Isolate* isolate = env->GetIsolate();
495 v8::HandleScope scope(isolate);
496 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
497 obj->SetAccessor(v8_str("xxx"), AllocateHandles);
498 env->Global()->Set(v8_str("obj"), obj->NewInstance());
499 v8::Handle<v8::Value> result = Script::Compile(String::NewFromUtf8(
502 "for (var i = 0; i < 4; i++)"
505 CHECK_EQ(100, result->Int32Value());
509 void JSONStringifyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info) {
510 v8::Handle<v8::Array> array = v8::Array::New(info.GetIsolate(), 1);
511 array->Set(0, v8_str("regress"));
512 info.GetReturnValue().Set(array);
516 void JSONStringifyGetter(Local<String> name,
517 const v8::PropertyCallbackInfo<v8::Value>& info) {
518 info.GetReturnValue().Set(v8_str("crbug-161028"));
522 THREADED_TEST(JSONStringifyNamedInterceptorObject) {
524 v8::Isolate* isolate = env->GetIsolate();
525 v8::HandleScope scope(isolate);
527 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
528 obj->SetNamedPropertyHandler(
529 JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator);
530 env->Global()->Set(v8_str("obj"), obj->NewInstance());
531 v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}");
532 CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected));
536 static v8::Local<v8::Context> expected_current_context;
537 static v8::Local<v8::Context> expected_calling_context;
540 static void check_contexts(const v8::FunctionCallbackInfo<v8::Value>& info) {
541 ApiTestFuzzer::Fuzz();
542 CHECK(expected_current_context == info.GetIsolate()->GetCurrentContext());
543 CHECK(expected_calling_context == info.GetIsolate()->GetCallingContext());
547 THREADED_TEST(AccessorPropertyCrossContext) {
549 v8::Isolate* isolate = env->GetIsolate();
550 v8::HandleScope scope(isolate);
551 v8::Handle<v8::Function> fun = v8::Function::New(isolate, check_contexts);
552 LocalContext switch_context;
553 switch_context->Global()->Set(v8_str("fun"), fun);
554 v8::TryCatch try_catch;
555 expected_current_context = env.local();
556 expected_calling_context = switch_context.local();
558 "var o = Object.create(null, { n: { get:fun } });"
559 "for (var i = 0; i < 10; i++) o.n;");
560 CHECK(!try_catch.HasCaught());
564 THREADED_TEST(GlobalObjectAccessor) {
566 v8::Isolate* isolate = env->GetIsolate();
567 v8::HandleScope scope(isolate);
570 "Object.defineProperty(this.__proto__, 'x', {"
571 " get : function() { return this; },"
572 " set : function() { set_value = this; }"
574 "function getter() { return x; }"
575 "function setter() { x = 1; }"
576 "for (var i = 0; i < 4; i++) { getter(); setter(); }");
577 CHECK(v8::Utils::OpenHandle(*CompileRun("getter()"))->IsJSGlobalProxy());
578 CHECK(v8::Utils::OpenHandle(*CompileRun("set_value"))->IsJSGlobalProxy());