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

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

Issue 6902108: Implement CallAsConstructor method for Object in the API (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add missing testcase Created 9 years, 7 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 | Annotate | Revision Log
« src/api.cc ('K') | « src/execution.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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 6728 matching lines...) Expand 10 before | Expand all | Expand 10 after
6739 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); 6739 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
6740 templ->SetClassName(v8_str("Fun")); 6740 templ->SetClassName(v8_str("Fun"));
6741 Local<Function> cons = templ->GetFunction(); 6741 Local<Function> cons = templ->GetFunction();
6742 context->Global()->Set(v8_str("Fun"), cons); 6742 context->Global()->Set(v8_str("Fun"), cons);
6743 Local<v8::Object> inst = cons->NewInstance(); 6743 Local<v8::Object> inst = cons->NewInstance();
6744 i::Handle<i::JSObject> obj = v8::Utils::OpenHandle(*inst); 6744 i::Handle<i::JSObject> obj = v8::Utils::OpenHandle(*inst);
6745 Local<Value> value = CompileRun("(new Fun()).constructor === Fun"); 6745 Local<Value> value = CompileRun("(new Fun()).constructor === Fun");
6746 CHECK(value->BooleanValue()); 6746 CHECK(value->BooleanValue());
6747 } 6747 }
6748 6748
6749
6750 static Handle<Value> ConstructorCallback(const Arguments& args) {
6751 ApiTestFuzzer::Fuzz();
6752 Local<Object> This;
6753
6754 if (args.IsConstructCall()) {
6755 Local<Object> Holder = args.Holder();
6756 This = Object::New();
6757 Local<Value> proto = Holder->GetPrototype();
6758 if (proto->IsObject()) {
6759 This->SetPrototype(proto);
6760 }
6761 } else {
6762 This = args.This();
6763 }
6764
6765 This->Set(v8_str("a"), args[0]);
6766 return This;
6767 }
6768
6769
6770 static Handle<Value> FakeConstructorCallback(const Arguments& args) {
6771 ApiTestFuzzer::Fuzz();
6772 return args[0];
6773 }
6774
6775
6776 THREADED_TEST(ConstructorForObject) {
6777 v8::HandleScope handle_scope;
6778 LocalContext context;
6779
6780 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6781 instance_template->SetCallAsFunctionHandler(ConstructorCallback);
6782 Local<Object> instance = instance_template->NewInstance();
6783 context->Global()->Set(v8_str("obj"), instance);
6784 v8::TryCatch try_catch;
6785 Local<Value> value;
6786 CHECK(!try_catch.HasCaught());
6787
6788 // Call the Object's constructor with a 32-bit signed integer.
6789 value = CompileRun("(function() { var o = new obj(28); return o.a; })()");
6790 CHECK(!try_catch.HasCaught());
6791 CHECK(value->IsInt32());
6792 CHECK_EQ(28, value->Int32Value());
6793
6794 Local<Value> args1[] = { v8_num(28) };
6795 Local<Value> value_obj1 = instance->CallAsConstructor(1, args1);
6796 CHECK(value_obj1->IsObject());
6797 Local<Object> object1 = Local<Object>::Cast(value_obj1);
6798 value = object1->Get(v8_str("a"));
6799 CHECK(value->IsInt32());
6800 CHECK(!try_catch.HasCaught());
6801 CHECK_EQ(28, value->Int32Value());
6802
6803 // Call the Object's constructor with a String.
6804 value = CompileRun(
6805 "(function() { var o = new obj('tipli'); return o.a; })()");
6806 CHECK(!try_catch.HasCaught());
6807 CHECK(value->IsString());
6808 String::AsciiValue string_value1(value->ToString());
6809 CHECK_EQ("tipli", *string_value1);
6810
6811 Local<Value> args2[] = { v8_str("tipli") };
6812 Local<Value> value_obj2 = instance->CallAsConstructor(1, args2);
6813 CHECK(value_obj2->IsObject());
6814 Local<Object> object2 = Local<Object>::Cast(value_obj2);
6815 value = object2->Get(v8_str("a"));
6816 CHECK(!try_catch.HasCaught());
6817 CHECK(value->IsString());
6818 String::AsciiValue string_value2(value->ToString());
6819 CHECK_EQ("tipli", *string_value2);
6820
6821 // Call the Object's constructor with a Boolean.
6822 value = CompileRun("(function() { var o = new obj(true); return o.a; })()");
6823 CHECK(!try_catch.HasCaught());
6824 CHECK(value->IsBoolean());
6825 CHECK_EQ(true, value->BooleanValue());
6826
6827 Handle<Value> args3[] = { v8::Boolean::New(true) };
6828 Local<Value> value_obj3 = instance->CallAsConstructor(1, args3);
6829 CHECK(value_obj3->IsObject());
6830 Local<Object> object3 = Local<Object>::Cast(value_obj3);
6831 value = object3->Get(v8_str("a"));
6832 CHECK(!try_catch.HasCaught());
6833 CHECK(value->IsBoolean());
6834 CHECK_EQ(true, value->BooleanValue());
6835
6836 // Call the Object's constructor with undefined.
6837 Handle<Value> args4[] = { v8::Undefined() };
6838 Local<Value> value_obj4 = instance->CallAsConstructor(1, args4);
6839 CHECK(value_obj4->IsObject());
6840 Local<Object> object4 = Local<Object>::Cast(value_obj4);
6841 value = object4->Get(v8_str("a"));
6842 CHECK(!try_catch.HasCaught());
6843 CHECK(value->IsUndefined());
6844
6845 // Call the Object's constructor with null.
6846 Handle<Value> args5[] = { v8::Null() };
6847 Local<Value> value_obj5 = instance->CallAsConstructor(1, args5);
6848 CHECK(value_obj5->IsObject());
6849 Local<Object> object5 = Local<Object>::Cast(value_obj5);
6850 value = object5->Get(v8_str("a"));
6851 CHECK(!try_catch.HasCaught());
6852 CHECK(value->IsNull());
6853 }
6854
6855 // Check exception handling when there is no constructor set for the Object.
6856 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6857 Local<Object> instance = instance_template->NewInstance();
6858 context->Global()->Set(v8_str("obj2"), instance);
6859 v8::TryCatch try_catch;
6860 Local<Value> value;
6861 CHECK(!try_catch.HasCaught());
6862
6863 value = CompileRun("new obj2(28)");
6864 CHECK(try_catch.HasCaught());
6865 String::AsciiValue exception_value1(try_catch.Exception());
6866 CHECK_EQ("TypeError: object is not a function", *exception_value1);
6867 try_catch.Reset();
6868
6869 Local<Value> args[] = { v8_num(29) };
6870 value = instance->CallAsConstructor(1, args);
6871 CHECK(try_catch.HasCaught());
6872 String::AsciiValue exception_value2(try_catch.Exception());
6873 CHECK_EQ("TypeError: #<Object> is not a function", *exception_value2);
6874 try_catch.Reset();
6875 }
6876
6877 // Check the case when constructor throws exception.
6878 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6879 instance_template->SetCallAsFunctionHandler(ThrowValue);
6880 Local<Object> instance = instance_template->NewInstance();
6881 context->Global()->Set(v8_str("obj3"), instance);
6882 v8::TryCatch try_catch;
6883 Local<Value> value;
6884 CHECK(!try_catch.HasCaught());
6885
6886 value = CompileRun("new obj3(22)");
6887 CHECK(try_catch.HasCaught());
6888 String::AsciiValue exception_value1(try_catch.Exception());
6889 CHECK_EQ("22", *exception_value1);
6890 try_catch.Reset();
6891
6892 Local<Value> args[] = { v8_num(23) };
6893 value = instance->CallAsConstructor(1, args);
6894 CHECK(try_catch.HasCaught());
6895 String::AsciiValue exception_value2(try_catch.Exception());
6896 CHECK_EQ("23", *exception_value2);
6897 try_catch.Reset();
6898 }
6899
6900 // Check whether constructor returns with an object or non-object.
6901 { Local<FunctionTemplate> function_template =
6902 FunctionTemplate::New(FakeConstructorCallback);
6903 Local<Function> function = function_template->GetFunction();
6904 Local<Object> instance1 = function;
6905 context->Global()->Set(v8_str("obj4"), instance1);
6906 v8::TryCatch try_catch;
6907 Local<Value> value;
6908 CHECK(!try_catch.HasCaught());
6909
6910 CHECK(instance1->IsObject());
6911 CHECK(instance1->IsFunction());
6912
6913 value = CompileRun("new obj4(28)");
6914 CHECK(!try_catch.HasCaught());
6915 CHECK(value->IsObject());
6916
6917 Local<Value> args1[] = { v8_num(28) };
6918 value = instance1->CallAsConstructor(1, args1);
6919 CHECK(!try_catch.HasCaught());
6920 CHECK(value->IsObject());
6921
6922 Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6923 instance_template->SetCallAsFunctionHandler(FakeConstructorCallback);
6924 Local<Object> instance2 = instance_template->NewInstance();
6925 context->Global()->Set(v8_str("obj5"), instance2);
6926 CHECK(!try_catch.HasCaught());
6927
6928 CHECK(instance2->IsObject());
6929 CHECK(!instance2->IsFunction());
6930
6931 value = CompileRun("new obj5(28)");
6932 CHECK(!try_catch.HasCaught());
6933 CHECK(!value->IsObject());
6934
6935 Local<Value> args2[] = { v8_num(28) };
6936 value = instance2->CallAsConstructor(1, args2);
6937 CHECK(!try_catch.HasCaught());
6938 CHECK(!value->IsObject());
6939 }
6940 }
6941
6942
6749 THREADED_TEST(FunctionDescriptorException) { 6943 THREADED_TEST(FunctionDescriptorException) {
6750 v8::HandleScope handle_scope; 6944 v8::HandleScope handle_scope;
6751 LocalContext context; 6945 LocalContext context;
6752 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); 6946 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
6753 templ->SetClassName(v8_str("Fun")); 6947 templ->SetClassName(v8_str("Fun"));
6754 Local<Function> cons = templ->GetFunction(); 6948 Local<Function> cons = templ->GetFunction();
6755 context->Global()->Set(v8_str("Fun"), cons); 6949 context->Global()->Set(v8_str("Fun"), cons);
6756 Local<Value> value = CompileRun( 6950 Local<Value> value = CompileRun(
6757 "function test() {" 6951 "function test() {"
6758 " try {" 6952 " try {"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
7021 context->Global()->Set(v8_str("obj2"), instance); 7215 context->Global()->Set(v8_str("obj2"), instance);
7022 v8::TryCatch try_catch; 7216 v8::TryCatch try_catch;
7023 Local<Value> value; 7217 Local<Value> value;
7024 CHECK(!try_catch.HasCaught()); 7218 CHECK(!try_catch.HasCaught());
7025 7219
7026 // Call an object without call-as-function handler through the JS 7220 // Call an object without call-as-function handler through the JS
7027 value = CompileRun("obj2(28)"); 7221 value = CompileRun("obj2(28)");
7028 CHECK(value.IsEmpty()); 7222 CHECK(value.IsEmpty());
7029 CHECK(try_catch.HasCaught()); 7223 CHECK(try_catch.HasCaught());
7030 String::AsciiValue exception_value1(try_catch.Exception()); 7224 String::AsciiValue exception_value1(try_catch.Exception());
7031 CHECK_EQ(*exception_value1, 7225 CHECK_EQ("TypeError: Property 'obj2' of object #<Object> is not a function",
7032 "TypeError: Property 'obj2' of object " 7226 *exception_value1);
7033 "#<Object> is not a function");
7034 try_catch.Reset(); 7227 try_catch.Reset();
7035 7228
7036 // Call an object without call-as-function handler through the API 7229 // Call an object without call-as-function handler through the API
7037 value = CompileRun("obj2(28)"); 7230 value = CompileRun("obj2(28)");
7038 v8::Handle<Value> args[] = { v8_num(28) }; 7231 v8::Handle<Value> args[] = { v8_num(28) };
7039 value = instance->CallAsFunction(instance, 1, args); 7232 value = instance->CallAsFunction(instance, 1, args);
7040 CHECK(value.IsEmpty()); 7233 CHECK(value.IsEmpty());
7041 CHECK(try_catch.HasCaught()); 7234 CHECK(try_catch.HasCaught());
7042 String::AsciiValue exception_value2(try_catch.Exception()); 7235 String::AsciiValue exception_value2(try_catch.Exception());
7043 CHECK_EQ(*exception_value2, "TypeError: [object Object] is not a function"); 7236 CHECK_EQ("TypeError: [object Object] is not a function", *exception_value2);
7044 try_catch.Reset(); 7237 try_catch.Reset();
7045 } 7238 }
7046 7239
7047 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); 7240 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
7048 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); 7241 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
7049 instance_template->SetCallAsFunctionHandler(ThrowValue); 7242 instance_template->SetCallAsFunctionHandler(ThrowValue);
7050 Local<v8::Object> instance = t->GetFunction()->NewInstance(); 7243 Local<v8::Object> instance = t->GetFunction()->NewInstance();
7051 context->Global()->Set(v8_str("obj3"), instance); 7244 context->Global()->Set(v8_str("obj3"), instance);
7052 v8::TryCatch try_catch; 7245 v8::TryCatch try_catch;
7053 Local<Value> value; 7246 Local<Value> value;
7054 CHECK(!try_catch.HasCaught()); 7247 CHECK(!try_catch.HasCaught());
7055 7248
7056 // Catch the exception which is thrown by call-as-function handler 7249 // Catch the exception which is thrown by call-as-function handler
7057 value = CompileRun("obj3(22)"); 7250 value = CompileRun("obj3(22)");
7058 CHECK(try_catch.HasCaught()); 7251 CHECK(try_catch.HasCaught());
7059 String::AsciiValue exception_value1(try_catch.Exception()); 7252 String::AsciiValue exception_value1(try_catch.Exception());
7060 CHECK_EQ(*exception_value1, "22"); 7253 CHECK_EQ("22", *exception_value1);
7061 try_catch.Reset(); 7254 try_catch.Reset();
7062 7255
7063 v8::Handle<Value> args[] = { v8_num(23) }; 7256 v8::Handle<Value> args[] = { v8_num(23) };
7064 value = instance->CallAsFunction(instance, 1, args); 7257 value = instance->CallAsFunction(instance, 1, args);
7065 CHECK(try_catch.HasCaught()); 7258 CHECK(try_catch.HasCaught());
7066 String::AsciiValue exception_value2(try_catch.Exception()); 7259 String::AsciiValue exception_value2(try_catch.Exception());
7067 CHECK_EQ(*exception_value2, "23"); 7260 CHECK_EQ("23", *exception_value2);
7068 try_catch.Reset(); 7261 try_catch.Reset();
7069 } 7262 }
7070 } 7263 }
7071 7264
7072 7265
7073 static int CountHandles() { 7266 static int CountHandles() {
7074 return v8::HandleScope::NumberOfHandles(); 7267 return v8::HandleScope::NumberOfHandles();
7075 } 7268 }
7076 7269
7077 7270
(...skipping 7080 matching lines...) Expand 10 before | Expand all | Expand 10 after
14158 14351
14159 // Disallow but setting a global callback that will allow the calls. 14352 // Disallow but setting a global callback that will allow the calls.
14160 context->AllowCodeGenerationFromStrings(false); 14353 context->AllowCodeGenerationFromStrings(false);
14161 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); 14354 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed);
14162 CheckCodeGenerationAllowed(); 14355 CheckCodeGenerationAllowed();
14163 14356
14164 // Set a callback that disallows the code generation. 14357 // Set a callback that disallows the code generation.
14165 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); 14358 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed);
14166 CheckCodeGenerationDisallowed(); 14359 CheckCodeGenerationDisallowed();
14167 } 14360 }
OLDNEW
« src/api.cc ('K') | « src/execution.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698