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

Side by Side Diff: test/cctest/test-api.cc

Issue 24920003: Function::Call and Object::CallAsFunction APIs should allow v8::Value as a receiver (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Add CallAsFunction Created 7 years, 2 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 unified diff | Download patch
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3995 matching lines...) Expand 10 before | Expand all | Expand 10 after
4006 CHECK_EQ(4, a4->Length()); 4006 CHECK_EQ(4, a4->Length());
4007 CHECK_EQ(17, a4->Get(0)->Int32Value()); 4007 CHECK_EQ(17, a4->Get(0)->Int32Value());
4008 CHECK_EQ(18, a4->Get(1)->Int32Value()); 4008 CHECK_EQ(18, a4->Get(1)->Int32Value());
4009 CHECK_EQ(19, a4->Get(2)->Int32Value()); 4009 CHECK_EQ(19, a4->Get(2)->Int32Value());
4010 CHECK_EQ(20, a4->Get(3)->Int32Value()); 4010 CHECK_EQ(20, a4->Get(3)->Int32Value());
4011 } 4011 }
4012 4012
4013 4013
4014 THREADED_TEST(FunctionCall) { 4014 THREADED_TEST(FunctionCall) {
4015 LocalContext context; 4015 LocalContext context;
4016 v8::HandleScope scope(context->GetIsolate()); 4016 v8::Isolate* isolate = context->GetIsolate();
4017 v8::HandleScope scope(isolate);
4017 CompileRun( 4018 CompileRun(
4018 "function Foo() {" 4019 "function Foo() {"
4019 " var result = [];" 4020 " var result = [];"
4020 " for (var i = 0; i < arguments.length; i++) {" 4021 " for (var i = 0; i < arguments.length; i++) {"
4021 " result.push(arguments[i]);" 4022 " result.push(arguments[i]);"
4022 " }" 4023 " }"
4023 " return result;" 4024 " return result;"
4025 "}"
4026 "function ReturnThisSloppy() {"
4027 " return this;"
4028 "}"
4029 "function ReturnThisStrict() {"
4030 " 'use strict';"
4031 " return this;"
4024 "}"); 4032 "}");
4025 Local<Function> Foo = 4033 Local<Function> Foo =
4026 Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); 4034 Local<Function>::Cast(context->Global()->Get(v8_str("Foo")));
4035 Local<Function> ReturnThisSloppy =
4036 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisSloppy")));
4037 Local<Function> ReturnThisStrict =
4038 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisStrict")));
4027 4039
4028 v8::Handle<Value>* args0 = NULL; 4040 v8::Handle<Value>* args0 = NULL;
4029 Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0)); 4041 Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0));
4030 CHECK_EQ(0, a0->Length()); 4042 CHECK_EQ(0, a0->Length());
4031 4043
4032 v8::Handle<Value> args1[] = { v8_num(1.1) }; 4044 v8::Handle<Value> args1[] = { v8_num(1.1) };
4033 Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1)); 4045 Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1));
4034 CHECK_EQ(1, a1->Length()); 4046 CHECK_EQ(1, a1->Length());
4035 CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); 4047 CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue());
4036 4048
(...skipping 16 matching lines...) Expand all
4053 v8::Handle<Value> args4[] = { v8_num(7.7), 4065 v8::Handle<Value> args4[] = { v8_num(7.7),
4054 v8_num(8.8), 4066 v8_num(8.8),
4055 v8_num(9.9), 4067 v8_num(9.9),
4056 v8_num(10.11) }; 4068 v8_num(10.11) };
4057 Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4)); 4069 Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4));
4058 CHECK_EQ(4, a4->Length()); 4070 CHECK_EQ(4, a4->Length());
4059 CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); 4071 CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue());
4060 CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); 4072 CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue());
4061 CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); 4073 CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue());
4062 CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue()); 4074 CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue());
4075
4076 Local<v8::Value> r1 = ReturnThisSloppy->Call(v8::Undefined(isolate), 0, NULL);
4077 CHECK(r1->StrictEquals(context->Global()));
4078 Local<v8::Value> r2 = ReturnThisSloppy->Call(v8::Null(isolate), 0, NULL);
4079 CHECK(r2->StrictEquals(context->Global()));
4080 Local<v8::Value> r3 = ReturnThisSloppy->Call(v8_num(42), 0, NULL);
4081 CHECK(r3->IsNumberObject());
4082 CHECK_EQ(42.0, r3.As<v8::NumberObject>()->ValueOf());
4083 Local<v8::Value> r4 = ReturnThisSloppy->Call(v8_str("hello"), 0, NULL);
4084 CHECK(r4->IsStringObject());
4085 CHECK(r4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello")));
4086 Local<v8::Value> r5 = ReturnThisSloppy->Call(v8::True(isolate), 0, NULL);
4087 CHECK(r5->IsBooleanObject());
4088 CHECK(r5.As<v8::BooleanObject>()->ValueOf());
4089
4090 Local<v8::Value> r6 = ReturnThisStrict->Call(v8::Undefined(isolate), 0, NULL);
4091 CHECK(r6->IsUndefined());
4092 Local<v8::Value> r7 = ReturnThisStrict->Call(v8::Null(isolate), 0, NULL);
4093 CHECK(r7->IsNull());
4094 Local<v8::Value> r8 = ReturnThisStrict->Call(v8_num(42), 0, NULL);
4095 CHECK(r8->StrictEquals(v8_num(42)));
4096 Local<v8::Value> r9 = ReturnThisStrict->Call(v8_str("hello"), 0, NULL);
4097 CHECK(r9->StrictEquals(v8_str("hello")));
4098 Local<v8::Value> r10 = ReturnThisStrict->Call(v8::True(isolate), 0, NULL);
4099 CHECK(r10->StrictEquals(v8::True(isolate)));
4063 } 4100 }
4064 4101
4065 4102
4066 static const char* js_code_causing_out_of_memory = 4103 static const char* js_code_causing_out_of_memory =
4067 "var a = new Array(); while(true) a.push(a);"; 4104 "var a = new Array(); while(true) a.push(a);";
4068 4105
4069 4106
4070 // These tests run for a long time and prevent us from running tests 4107 // These tests run for a long time and prevent us from running tests
4071 // that come after them so they cannot run in parallel. 4108 // that come after them so they cannot run in parallel.
4072 TEST(OutOfMemory) { 4109 TEST(OutOfMemory) {
(...skipping 6098 matching lines...) Expand 10 before | Expand all | Expand 10 after
10171 if (args.IsConstructCall()) { 10208 if (args.IsConstructCall()) {
10172 if (args[0]->IsInt32()) { 10209 if (args[0]->IsInt32()) {
10173 args.GetReturnValue().Set(v8_num(-args[0]->Int32Value())); 10210 args.GetReturnValue().Set(v8_num(-args[0]->Int32Value()));
10174 return; 10211 return;
10175 } 10212 }
10176 } 10213 }
10177 10214
10178 args.GetReturnValue().Set(args[0]); 10215 args.GetReturnValue().Set(args[0]);
10179 } 10216 }
10180 10217
10218 static void ReturnThis(const v8::FunctionCallbackInfo<v8::Value>& args) {
10219 args.GetReturnValue().Set(args.This());
10220 }
10221
10181 10222
10182 // Test that a call handler can be set for objects which will allow 10223 // Test that a call handler can be set for objects which will allow
10183 // non-function objects created through the API to be called as 10224 // non-function objects created through the API to be called as
10184 // functions. 10225 // functions.
10185 THREADED_TEST(CallAsFunction) { 10226 THREADED_TEST(CallAsFunction) {
10186 LocalContext context; 10227 LocalContext context;
10187 v8::HandleScope scope(context->GetIsolate()); 10228 v8::HandleScope scope(context->GetIsolate());
10188 10229
10189 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); 10230 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
10190 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); 10231 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
10284 CHECK_EQ("22", *exception_value1); 10325 CHECK_EQ("22", *exception_value1);
10285 try_catch.Reset(); 10326 try_catch.Reset();
10286 10327
10287 v8::Handle<Value> args[] = { v8_num(23) }; 10328 v8::Handle<Value> args[] = { v8_num(23) };
10288 value = instance->CallAsFunction(instance, 1, args); 10329 value = instance->CallAsFunction(instance, 1, args);
10289 CHECK(try_catch.HasCaught()); 10330 CHECK(try_catch.HasCaught());
10290 String::Utf8Value exception_value2(try_catch.Exception()); 10331 String::Utf8Value exception_value2(try_catch.Exception());
10291 CHECK_EQ("23", *exception_value2); 10332 CHECK_EQ("23", *exception_value2);
10292 try_catch.Reset(); 10333 try_catch.Reset();
10293 } 10334 }
10335
10336 { v8::Isolate* isolate = context->GetIsolate();
10337 Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
10338 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
10339 instance_template->SetCallAsFunctionHandler(ReturnThis);
10340 Local<v8::Object> instance = t->GetFunction()->NewInstance();
10341
10342 Local<v8::Value> a1 = instance->CallAsFunction(v8::Undefined(isolate), 0, NU LL);
Michael Starzinger 2013/09/30 11:17:41 nit: 80 characters limit throughout this file.
yusukesuzuki 2013/09/30 11:32:19 Done.
10343 CHECK(a1->StrictEquals(instance));
10344 Local<v8::Value> a2 = instance->CallAsFunction(v8::Null(isolate), 0, NULL);
10345 CHECK(a2->StrictEquals(instance));
10346 Local<v8::Value> a3 = instance->CallAsFunction(v8_num(42), 0, NULL);
10347 CHECK(a3->StrictEquals(instance));
10348 Local<v8::Value> a4 = instance->CallAsFunction(v8_str("hello"), 0, NULL);
10349 CHECK(a4->StrictEquals(instance));
10350 Local<v8::Value> a5 = instance->CallAsFunction(v8::True(isolate), 0, NULL);
10351 CHECK(a5->StrictEquals(instance));
10352 }
10353
10354 { v8::Isolate* isolate = context->GetIsolate();
10355 CompileRun(
10356 "function ReturnThisSloppy() {"
10357 " return this;"
10358 "}"
10359 "function ReturnThisStrict() {"
10360 " 'use strict';"
10361 " return this;"
10362 "}");
10363 Local<Function> ReturnThisSloppy =
10364 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisSloppy")) );
10365 Local<Function> ReturnThisStrict =
10366 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisStrict")) );
10367
10368 Local<v8::Value> a1 = ReturnThisSloppy->CallAsFunction(v8::Undefined(isolate ), 0, NULL);
10369 CHECK(a1->StrictEquals(context->Global()));
10370 Local<v8::Value> a2 = ReturnThisSloppy->CallAsFunction(v8::Null(isolate), 0, NULL);
10371 CHECK(a2->StrictEquals(context->Global()));
10372 Local<v8::Value> a3 = ReturnThisSloppy->CallAsFunction(v8_num(42), 0, NULL);
10373 CHECK(a3->IsNumberObject());
10374 CHECK_EQ(42.0, a3.As<v8::NumberObject>()->ValueOf());
10375 Local<v8::Value> a4 = ReturnThisSloppy->CallAsFunction(v8_str("hello"), 0, N ULL);
10376 CHECK(a4->IsStringObject());
10377 CHECK(a4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello")));
10378 Local<v8::Value> a5 = ReturnThisSloppy->CallAsFunction(v8::True(isolate), 0, NULL);
10379 CHECK(a5->IsBooleanObject());
10380 CHECK(a5.As<v8::BooleanObject>()->ValueOf());
10381
10382 Local<v8::Value> a6 = ReturnThisStrict->CallAsFunction(v8::Undefined(isolate ), 0, NULL);
10383 CHECK(a6->IsUndefined());
10384 Local<v8::Value> a7 = ReturnThisStrict->CallAsFunction(v8::Null(isolate), 0, NULL);
10385 CHECK(a7->IsNull());
10386 Local<v8::Value> a8 = ReturnThisStrict->CallAsFunction(v8_num(42), 0, NULL);
10387 CHECK(a8->StrictEquals(v8_num(42)));
10388 Local<v8::Value> a9 = ReturnThisStrict->CallAsFunction(v8_str("hello"), 0, N ULL);
10389 CHECK(a9->StrictEquals(v8_str("hello")));
10390 Local<v8::Value> a10 = ReturnThisStrict->CallAsFunction(v8::True(isolate), 0 , NULL);
10391 CHECK(a10->StrictEquals(v8::True(isolate)));
10392 }
10294 } 10393 }
10295 10394
10296 10395
10297 // Check whether a non-function object is callable. 10396 // Check whether a non-function object is callable.
10298 THREADED_TEST(CallableObject) { 10397 THREADED_TEST(CallableObject) {
10299 LocalContext context; 10398 LocalContext context;
10300 v8::HandleScope scope(context->GetIsolate()); 10399 v8::HandleScope scope(context->GetIsolate());
10301 10400
10302 { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); 10401 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
10303 instance_template->SetCallAsFunctionHandler(call_as_function); 10402 instance_template->SetCallAsFunctionHandler(call_as_function);
(...skipping 10303 matching lines...) Expand 10 before | Expand all | Expand 10 after
20607 } 20706 }
20608 for (int i = 0; i < runs; i++) { 20707 for (int i = 0; i < runs; i++) {
20609 Local<String> expected; 20708 Local<String> expected;
20610 if (i != 0) { 20709 if (i != 0) {
20611 CHECK_EQ(v8_str("escape value"), values[i]); 20710 CHECK_EQ(v8_str("escape value"), values[i]);
20612 } else { 20711 } else {
20613 CHECK(values[i].IsEmpty()); 20712 CHECK(values[i].IsEmpty());
20614 } 20713 }
20615 } 20714 }
20616 } 20715 }
OLDNEW
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698