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

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: 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 3993 matching lines...) Expand 10 before | Expand all | Expand 10 after
4004 CHECK_EQ(4, a4->Length()); 4004 CHECK_EQ(4, a4->Length());
4005 CHECK_EQ(17, a4->Get(0)->Int32Value()); 4005 CHECK_EQ(17, a4->Get(0)->Int32Value());
4006 CHECK_EQ(18, a4->Get(1)->Int32Value()); 4006 CHECK_EQ(18, a4->Get(1)->Int32Value());
4007 CHECK_EQ(19, a4->Get(2)->Int32Value()); 4007 CHECK_EQ(19, a4->Get(2)->Int32Value());
4008 CHECK_EQ(20, a4->Get(3)->Int32Value()); 4008 CHECK_EQ(20, a4->Get(3)->Int32Value());
4009 } 4009 }
4010 4010
4011 4011
4012 THREADED_TEST(FunctionCall) { 4012 THREADED_TEST(FunctionCall) {
4013 LocalContext context; 4013 LocalContext context;
4014 v8::HandleScope scope(context->GetIsolate()); 4014 v8::Isolate* isolate = context->GetIsolate();
4015 v8::HandleScope scope(isolate);
4015 CompileRun( 4016 CompileRun(
4016 "function Foo() {" 4017 "function Foo() {"
4017 " var result = [];" 4018 " var result = [];"
4018 " for (var i = 0; i < arguments.length; i++) {" 4019 " for (var i = 0; i < arguments.length; i++) {"
4019 " result.push(arguments[i]);" 4020 " result.push(arguments[i]);"
4020 " }" 4021 " }"
4021 " return result;" 4022 " return result;"
4023 "}"
4024 "function ReturnThisSloppy() {"
4025 " return this;"
4026 "}"
4027 "function ReturnThisStrict() {"
4028 " 'use strict';"
4029 " return this;"
4022 "}"); 4030 "}");
4023 Local<Function> Foo = 4031 Local<Function> Foo =
4024 Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); 4032 Local<Function>::Cast(context->Global()->Get(v8_str("Foo")));
4033 Local<Function> ReturnThisSloppy =
4034 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisSloppy")));
4035 Local<Function> ReturnThisStrict =
4036 Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisStrict")));
4025 4037
4026 v8::Handle<Value>* args0 = NULL; 4038 v8::Handle<Value>* args0 = NULL;
4027 Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0)); 4039 Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0));
4028 CHECK_EQ(0, a0->Length()); 4040 CHECK_EQ(0, a0->Length());
4029 4041
4030 v8::Handle<Value> args1[] = { v8_num(1.1) }; 4042 v8::Handle<Value> args1[] = { v8_num(1.1) };
4031 Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1)); 4043 Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1));
4032 CHECK_EQ(1, a1->Length()); 4044 CHECK_EQ(1, a1->Length());
4033 CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); 4045 CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue());
4034 4046
(...skipping 16 matching lines...) Expand all
4051 v8::Handle<Value> args4[] = { v8_num(7.7), 4063 v8::Handle<Value> args4[] = { v8_num(7.7),
4052 v8_num(8.8), 4064 v8_num(8.8),
4053 v8_num(9.9), 4065 v8_num(9.9),
4054 v8_num(10.11) }; 4066 v8_num(10.11) };
4055 Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4)); 4067 Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4));
4056 CHECK_EQ(4, a4->Length()); 4068 CHECK_EQ(4, a4->Length());
4057 CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); 4069 CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue());
4058 CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); 4070 CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue());
4059 CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); 4071 CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue());
4060 CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue()); 4072 CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue());
4073
4074 Local<v8::Value> r1 = ReturnThisSloppy->Call(v8::Undefined(isolate), 0, NULL);
4075 CHECK(r1->StrictEquals(context->Global()));
4076 Local<v8::Value> r2 = ReturnThisSloppy->Call(v8::Null(isolate), 0, NULL);
4077 CHECK(r2->StrictEquals(context->Global()));
4078 Local<v8::Value> r3 = ReturnThisSloppy->Call(v8_num(42), 0, NULL);
4079 CHECK(r3->IsNumberObject());
4080 CHECK_EQ(42.0, r3.As<v8::NumberObject>()->ValueOf());
4081 Local<v8::Value> r4 = ReturnThisSloppy->Call(v8_str("hello"), 0, NULL);
4082 CHECK(r4->IsStringObject());
4083 CHECK(r4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello")));
4084 Local<v8::Value> r5 = ReturnThisSloppy->Call(v8::True(isolate), 0, NULL);
4085 CHECK(r5->IsBooleanObject());
4086 CHECK(r5.As<v8::BooleanObject>()->ValueOf());
4087
4088 Local<v8::Value> r6 = ReturnThisStrict->Call(v8::Undefined(isolate), 0, NULL);
4089 CHECK(r6->IsUndefined());
4090 Local<v8::Value> r7 = ReturnThisStrict->Call(v8::Null(isolate), 0, NULL);
4091 CHECK(r7->IsNull());
4092 Local<v8::Value> r8 = ReturnThisStrict->Call(v8_num(42), 0, NULL);
4093 CHECK(r8->StrictEquals(v8_num(42)));
4094 Local<v8::Value> r9 = ReturnThisStrict->Call(v8_str("hello"), 0, NULL);
4095 CHECK(r9->StrictEquals(v8_str("hello")));
4096 Local<v8::Value> r10 = ReturnThisStrict->Call(v8::True(isolate), 0, NULL);
4097 CHECK(r10->StrictEquals(v8::True(isolate)));
4061 } 4098 }
4062 4099
4063 4100
4064 static const char* js_code_causing_out_of_memory = 4101 static const char* js_code_causing_out_of_memory =
4065 "var a = new Array(); while(true) a.push(a);"; 4102 "var a = new Array(); while(true) a.push(a);";
4066 4103
4067 4104
4068 // These tests run for a long time and prevent us from running tests 4105 // These tests run for a long time and prevent us from running tests
4069 // that come after them so they cannot run in parallel. 4106 // that come after them so they cannot run in parallel.
4070 TEST(OutOfMemory) { 4107 TEST(OutOfMemory) {
(...skipping 6098 matching lines...) Expand 10 before | Expand all | Expand 10 after
10169 if (args.IsConstructCall()) { 10206 if (args.IsConstructCall()) {
10170 if (args[0]->IsInt32()) { 10207 if (args[0]->IsInt32()) {
10171 args.GetReturnValue().Set(v8_num(-args[0]->Int32Value())); 10208 args.GetReturnValue().Set(v8_num(-args[0]->Int32Value()));
10172 return; 10209 return;
10173 } 10210 }
10174 } 10211 }
10175 10212
10176 args.GetReturnValue().Set(args[0]); 10213 args.GetReturnValue().Set(args[0]);
10177 } 10214 }
10178 10215
10216 static void ReturnThis(const v8::FunctionCallbackInfo<v8::Value>& args) {
10217 args.GetReturnValue().Set(args.This());
10218 }
10219
10179 10220
10180 // Test that a call handler can be set for objects which will allow 10221 // Test that a call handler can be set for objects which will allow
10181 // non-function objects created through the API to be called as 10222 // non-function objects created through the API to be called as
10182 // functions. 10223 // functions.
10183 THREADED_TEST(CallAsFunction) { 10224 THREADED_TEST(CallAsFunction) {
10184 LocalContext context; 10225 LocalContext context;
10185 v8::HandleScope scope(context->GetIsolate()); 10226 v8::HandleScope scope(context->GetIsolate());
10186 10227
10187 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); 10228 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
10188 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); 10229 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
10282 CHECK_EQ("22", *exception_value1); 10323 CHECK_EQ("22", *exception_value1);
10283 try_catch.Reset(); 10324 try_catch.Reset();
10284 10325
10285 v8::Handle<Value> args[] = { v8_num(23) }; 10326 v8::Handle<Value> args[] = { v8_num(23) };
10286 value = instance->CallAsFunction(instance, 1, args); 10327 value = instance->CallAsFunction(instance, 1, args);
10287 CHECK(try_catch.HasCaught()); 10328 CHECK(try_catch.HasCaught());
10288 String::Utf8Value exception_value2(try_catch.Exception()); 10329 String::Utf8Value exception_value2(try_catch.Exception());
10289 CHECK_EQ("23", *exception_value2); 10330 CHECK_EQ("23", *exception_value2);
10290 try_catch.Reset(); 10331 try_catch.Reset();
10291 } 10332 }
10333
10334 { v8::Isolate* isolate = context->GetIsolate();
10335 Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
10336 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
10337 instance_template->SetCallAsFunctionHandler(ReturnThis);
10338 Local<v8::Object> instance = t->GetFunction()->NewInstance();
10339
10340 Local<v8::Value> a1 =
10341 instance->CallAsFunction(v8::Undefined(isolate), 0, NULL);
10342 CHECK(a1->StrictEquals(instance));
10343 Local<v8::Value> a2 =
10344 instance->CallAsFunction(v8::Null(isolate), 0, NULL);
10345 CHECK(a2->StrictEquals(instance));
10346 Local<v8::Value> a3 =
10347 instance->CallAsFunction(v8_num(42), 0, NULL);
10348 CHECK(a3->StrictEquals(instance));
10349 Local<v8::Value> a4 =
10350 instance->CallAsFunction(v8_str("hello"), 0, NULL);
10351 CHECK(a4->StrictEquals(instance));
10352 Local<v8::Value> a5 =
10353 instance->CallAsFunction(v8::True(isolate), 0, NULL);
10354 CHECK(a5->StrictEquals(instance));
10355 }
10356
10357 { v8::Isolate* isolate = context->GetIsolate();
10358 CompileRun(
10359 "function ReturnThisSloppy() {"
10360 " return this;"
10361 "}"
10362 "function ReturnThisStrict() {"
10363 " 'use strict';"
10364 " return this;"
10365 "}");
10366 Local<Function> ReturnThisSloppy =
10367 Local<Function>::Cast(
10368 context->Global()->Get(v8_str("ReturnThisSloppy")));
10369 Local<Function> ReturnThisStrict =
10370 Local<Function>::Cast(
10371 context->Global()->Get(v8_str("ReturnThisStrict")));
10372
10373 Local<v8::Value> a1 =
10374 ReturnThisSloppy->CallAsFunction(v8::Undefined(isolate), 0, NULL);
10375 CHECK(a1->StrictEquals(context->Global()));
10376 Local<v8::Value> a2 =
10377 ReturnThisSloppy->CallAsFunction(v8::Null(isolate), 0, NULL);
10378 CHECK(a2->StrictEquals(context->Global()));
10379 Local<v8::Value> a3 =
10380 ReturnThisSloppy->CallAsFunction(v8_num(42), 0, NULL);
10381 CHECK(a3->IsNumberObject());
10382 CHECK_EQ(42.0, a3.As<v8::NumberObject>()->ValueOf());
10383 Local<v8::Value> a4 =
10384 ReturnThisSloppy->CallAsFunction(v8_str("hello"), 0, NULL);
10385 CHECK(a4->IsStringObject());
10386 CHECK(a4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello")));
10387 Local<v8::Value> a5 =
10388 ReturnThisSloppy->CallAsFunction(v8::True(isolate), 0, NULL);
10389 CHECK(a5->IsBooleanObject());
10390 CHECK(a5.As<v8::BooleanObject>()->ValueOf());
10391
10392 Local<v8::Value> a6 =
10393 ReturnThisStrict->CallAsFunction(v8::Undefined(isolate), 0, NULL);
10394 CHECK(a6->IsUndefined());
10395 Local<v8::Value> a7 =
10396 ReturnThisStrict->CallAsFunction(v8::Null(isolate), 0, NULL);
10397 CHECK(a7->IsNull());
10398 Local<v8::Value> a8 =
10399 ReturnThisStrict->CallAsFunction(v8_num(42), 0, NULL);
10400 CHECK(a8->StrictEquals(v8_num(42)));
10401 Local<v8::Value> a9 =
10402 ReturnThisStrict->CallAsFunction(v8_str("hello"), 0, NULL);
10403 CHECK(a9->StrictEquals(v8_str("hello")));
10404 Local<v8::Value> a10 =
10405 ReturnThisStrict->CallAsFunction(v8::True(isolate), 0, NULL);
10406 CHECK(a10->StrictEquals(v8::True(isolate)));
10407 }
10292 } 10408 }
10293 10409
10294 10410
10295 // Check whether a non-function object is callable. 10411 // Check whether a non-function object is callable.
10296 THREADED_TEST(CallableObject) { 10412 THREADED_TEST(CallableObject) {
10297 LocalContext context; 10413 LocalContext context;
10298 v8::HandleScope scope(context->GetIsolate()); 10414 v8::HandleScope scope(context->GetIsolate());
10299 10415
10300 { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); 10416 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
10301 instance_template->SetCallAsFunctionHandler(call_as_function); 10417 instance_template->SetCallAsFunctionHandler(call_as_function);
(...skipping 10307 matching lines...) Expand 10 before | Expand all | Expand 10 after
20609 } 20725 }
20610 for (int i = 0; i < runs; i++) { 20726 for (int i = 0; i < runs; i++) {
20611 Local<String> expected; 20727 Local<String> expected;
20612 if (i != 0) { 20728 if (i != 0) {
20613 CHECK_EQ(v8_str("escape value"), values[i]); 20729 CHECK_EQ(v8_str("escape value"), values[i]);
20614 } else { 20730 } else {
20615 CHECK(values[i].IsEmpty()); 20731 CHECK(values[i].IsEmpty());
20616 } 20732 }
20617 } 20733 }
20618 } 20734 }
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