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

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 exception handling and some new test cases 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 if (args[0]->IsInt32()) {
Mads Ager (chromium) 2011/05/05 09:03:33 Add a test where the input is a string, undefined,
6753 Local<Object> This;
6754
6755 if (args.IsConstructCall()) {
6756 Local<Object> Holder = args.Holder();
6757 This = Object::New();
6758 Local<Value> proto = Holder->GetPrototype();
6759 if (proto->IsObject()) {
6760 This->SetPrototype(proto);
6761 }
6762 } else {
6763 This = args.This();
6764 }
6765
6766 This->Set(v8_str("a"), v8_num(args[0]->Int32Value()));
6767 return This;
6768 }
6769
6770 return args[0];
6771 }
6772
6773
6774 THREADED_TEST(ConstructorForObject) {
6775 v8::HandleScope handle_scope;
6776 LocalContext context;
6777
6778 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6779 instance_template->SetCallAsFunctionHandler(ConstructorCallback);
6780 Local<Object> instance = instance_template->NewInstance();
6781 context->Global()->Set(v8_str("obj"), instance);
6782 v8::TryCatch try_catch;
6783 Local<Value> value;
6784 CHECK(!try_catch.HasCaught());
6785
6786 value = CompileRun("(function() { var o = new obj(28); return o.a; })()");
6787 CHECK(!try_catch.HasCaught());
6788 CHECK_EQ(28, value->Int32Value());
6789
6790 Local<Value> args[] = { v8_num(29) };
6791 Local<Object> object = instance->CallAsConstructor(1, args);
6792 Local<Value> value2 = object->Get(v8_str("a"));
6793 CHECK(!try_catch.HasCaught());
6794 CHECK_EQ(29, value2->Int32Value());
6795 }
6796
6797 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6798 Local<Object> instance = instance_template->NewInstance();
6799 context->Global()->Set(v8_str("obj2"), instance);
6800 v8::TryCatch try_catch;
6801 Local<Value> value;
6802 CHECK(!try_catch.HasCaught());
6803
6804 value = CompileRun("new obj2(28)");
6805 CHECK(try_catch.HasCaught());
6806 String::AsciiValue exception_value1(try_catch.Exception());
6807 CHECK_EQ("TypeError: object is not a function", *exception_value1);
6808 try_catch.Reset();
6809
6810 Local<Value> args[] = { v8_num(29) };
6811 Local<Object> object = instance->CallAsConstructor(1, args);
6812 CHECK(try_catch.HasCaught());
6813 String::AsciiValue exception_value2(try_catch.Exception());
6814 CHECK_EQ("TypeError: #<Object> is not a function", *exception_value2);
6815 try_catch.Reset();
6816 }
6817
6818 { Local<ObjectTemplate> instance_template = ObjectTemplate::New();
6819 instance_template->SetCallAsFunctionHandler(ThrowValue);
6820 Local<Object> instance = instance_template->NewInstance();
6821 context->Global()->Set(v8_str("obj3"), instance);
6822 v8::TryCatch try_catch;
6823 Local<Value> value;
6824 CHECK(!try_catch.HasCaught());
6825
6826 value = CompileRun("new obj3(22)");
6827 CHECK(try_catch.HasCaught());
6828 String::AsciiValue exception_value1(try_catch.Exception());
6829 CHECK_EQ(*exception_value1, "22");
6830 try_catch.Reset();
6831
6832 Local<Value> args[] = { v8_num(23) };
6833 Local<Object> object = instance->CallAsConstructor(1, args);
6834 CHECK(try_catch.HasCaught());
6835 String::AsciiValue exception_value2(try_catch.Exception());
6836 CHECK_EQ(*exception_value2, "23");
6837 try_catch.Reset();
6838 }
6839 }
6840
6841
6749 THREADED_TEST(FunctionDescriptorException) { 6842 THREADED_TEST(FunctionDescriptorException) {
6750 v8::HandleScope handle_scope; 6843 v8::HandleScope handle_scope;
6751 LocalContext context; 6844 LocalContext context;
6752 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); 6845 Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
6753 templ->SetClassName(v8_str("Fun")); 6846 templ->SetClassName(v8_str("Fun"));
6754 Local<Function> cons = templ->GetFunction(); 6847 Local<Function> cons = templ->GetFunction();
6755 context->Global()->Set(v8_str("Fun"), cons); 6848 context->Global()->Set(v8_str("Fun"), cons);
6756 Local<Value> value = CompileRun( 6849 Local<Value> value = CompileRun(
6757 "function test() {" 6850 "function test() {"
6758 " try {" 6851 " try {"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
7021 context->Global()->Set(v8_str("obj2"), instance); 7114 context->Global()->Set(v8_str("obj2"), instance);
7022 v8::TryCatch try_catch; 7115 v8::TryCatch try_catch;
7023 Local<Value> value; 7116 Local<Value> value;
7024 CHECK(!try_catch.HasCaught()); 7117 CHECK(!try_catch.HasCaught());
7025 7118
7026 // Call an object without call-as-function handler through the JS 7119 // Call an object without call-as-function handler through the JS
7027 value = CompileRun("obj2(28)"); 7120 value = CompileRun("obj2(28)");
7028 CHECK(value.IsEmpty()); 7121 CHECK(value.IsEmpty());
7029 CHECK(try_catch.HasCaught()); 7122 CHECK(try_catch.HasCaught());
7030 String::AsciiValue exception_value1(try_catch.Exception()); 7123 String::AsciiValue exception_value1(try_catch.Exception());
7031 CHECK_EQ(*exception_value1, 7124 CHECK_EQ("TypeError: Property 'obj2' of object #<Object> is not a function",
7032 "TypeError: Property 'obj2' of object " 7125 *exception_value1);
7033 "#<Object> is not a function");
7034 try_catch.Reset(); 7126 try_catch.Reset();
7035 7127
7036 // Call an object without call-as-function handler through the API 7128 // Call an object without call-as-function handler through the API
7037 value = CompileRun("obj2(28)"); 7129 value = CompileRun("obj2(28)");
7038 v8::Handle<Value> args[] = { v8_num(28) }; 7130 v8::Handle<Value> args[] = { v8_num(28) };
7039 value = instance->CallAsFunction(instance, 1, args); 7131 value = instance->CallAsFunction(instance, 1, args);
7040 CHECK(value.IsEmpty()); 7132 CHECK(value.IsEmpty());
7041 CHECK(try_catch.HasCaught()); 7133 CHECK(try_catch.HasCaught());
7042 String::AsciiValue exception_value2(try_catch.Exception()); 7134 String::AsciiValue exception_value2(try_catch.Exception());
7043 CHECK_EQ(*exception_value2, "TypeError: [object Object] is not a function"); 7135 CHECK_EQ("TypeError: [object Object] is not a function", *exception_value2);
7044 try_catch.Reset(); 7136 try_catch.Reset();
7045 } 7137 }
7046 7138
7047 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); 7139 { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
7048 Local<ObjectTemplate> instance_template = t->InstanceTemplate(); 7140 Local<ObjectTemplate> instance_template = t->InstanceTemplate();
7049 instance_template->SetCallAsFunctionHandler(ThrowValue); 7141 instance_template->SetCallAsFunctionHandler(ThrowValue);
7050 Local<v8::Object> instance = t->GetFunction()->NewInstance(); 7142 Local<v8::Object> instance = t->GetFunction()->NewInstance();
7051 context->Global()->Set(v8_str("obj3"), instance); 7143 context->Global()->Set(v8_str("obj3"), instance);
7052 v8::TryCatch try_catch; 7144 v8::TryCatch try_catch;
7053 Local<Value> value; 7145 Local<Value> value;
(...skipping 7104 matching lines...) Expand 10 before | Expand all | Expand 10 after
14158 14250
14159 // Disallow but setting a global callback that will allow the calls. 14251 // Disallow but setting a global callback that will allow the calls.
14160 context->AllowCodeGenerationFromStrings(false); 14252 context->AllowCodeGenerationFromStrings(false);
14161 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); 14253 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed);
14162 CheckCodeGenerationAllowed(); 14254 CheckCodeGenerationAllowed();
14163 14255
14164 // Set a callback that disallows the code generation. 14256 // Set a callback that disallows the code generation.
14165 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); 14257 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed);
14166 CheckCodeGenerationDisallowed(); 14258 CheckCodeGenerationDisallowed();
14167 } 14259 }
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