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

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

Issue 6119009: Wrap external pointers more carefully. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressing Lasse's comments Created 9 years, 11 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
« 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 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 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 796 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 807
808 Local<Value> result = v8_compile("(new obj()).toString()")->Run(); 808 Local<Value> result = v8_compile("(new obj()).toString()")->Run();
809 CHECK_EQ(v8_str("[object funky]"), result); 809 CHECK_EQ(v8_str("[object funky]"), result);
810 810
811 result = v8_compile("(new obj()).m")->Run(); 811 result = v8_compile("(new obj()).m")->Run();
812 CHECK_EQ(239, result->Int32Value()); 812 CHECK_EQ(239, result->Int32Value());
813 } 813 }
814 } 814 }
815 815
816 816
817 static void* expected_ptr;
818 static v8::Handle<v8::Value> callback(const v8::Arguments& args) {
819 void* ptr = v8::External::Unwrap(args.Data());
820 CHECK_EQ(expected_ptr, ptr);
821 return v8::Boolean::New(true);
822 }
823
824
825 static void TestExternalPointerWrapping() {
826 v8::HandleScope scope;
827 LocalContext env;
828
829 v8::Handle<v8::Value> data = v8::External::Wrap(expected_ptr);
830
831 v8::Handle<v8::Object> obj = v8::Object::New();
832 obj->Set(v8_str("func"),
833 v8::FunctionTemplate::New(callback, data)->GetFunction());
834 env->Global()->Set(v8_str("obj"), obj);
835
836 CHECK(CompileRun(
837 "function foo() {\n"
838 " for (var i = 0; i < 13; i++) obj.func();\n"
839 "}\n"
840 "foo(), true")->BooleanValue());
841 }
842
843
844 THREADED_TEST(ExternalWrap) {
845 // Check heap allocated object.
846 int* ptr = new int;
847 expected_ptr = ptr;
848 TestExternalPointerWrapping();
849 delete ptr;
850
851 // Check stack allocated object.
852 int foo;
853 expected_ptr = &foo;
854 TestExternalPointerWrapping();
855
856 // Check not aligned addresses.
857 const int n = 100;
858 char* s = new char[n];
859 for (int i = 0; i < n; i++) {
860 expected_ptr = s + i;
861 TestExternalPointerWrapping();
862 }
863
864 delete[] s;
865
866 // Check several invalid addresses.
867 expected_ptr = reinterpret_cast<void*>(1);
868 TestExternalPointerWrapping();
869
870 expected_ptr = reinterpret_cast<void*>(0xdeadbeef);
871 TestExternalPointerWrapping();
872
873 expected_ptr = reinterpret_cast<void*>(0xdeadbeef + 1);
874 TestExternalPointerWrapping();
875
876 #if defined(V8_HOST_ARCH_X64)
877 expected_ptr = reinterpret_cast<void*>(0xdeadbeefdeadbeef);
878 TestExternalPointerWrapping();
879
880 expected_ptr = reinterpret_cast<void*>(0xdeadbeefdeadbeef + 1);
881 TestExternalPointerWrapping();
882 #endif
883 }
884
885
817 THREADED_TEST(FindInstanceInPrototypeChain) { 886 THREADED_TEST(FindInstanceInPrototypeChain) {
818 v8::HandleScope scope; 887 v8::HandleScope scope;
819 LocalContext env; 888 LocalContext env;
820 889
821 Local<v8::FunctionTemplate> base = v8::FunctionTemplate::New(); 890 Local<v8::FunctionTemplate> base = v8::FunctionTemplate::New();
822 Local<v8::FunctionTemplate> derived = v8::FunctionTemplate::New(); 891 Local<v8::FunctionTemplate> derived = v8::FunctionTemplate::New();
823 Local<v8::FunctionTemplate> other = v8::FunctionTemplate::New(); 892 Local<v8::FunctionTemplate> other = v8::FunctionTemplate::New();
824 derived->Inherit(base); 893 derived->Inherit(base);
825 894
826 Local<v8::Function> base_function = base->GetFunction(); 895 Local<v8::Function> base_function = base->GetFunction();
(...skipping 11246 matching lines...) Expand 10 before | Expand all | Expand 10 after
12073 v8::Context::Scope context_scope(context.local()); 12142 v8::Context::Scope context_scope(context.local());
12074 12143
12075 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); 12144 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New();
12076 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); 12145 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator);
12077 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); 12146 context->Global()->Set(v8_str("o"), tmpl->NewInstance());
12078 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( 12147 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun(
12079 "var result = []; for (var k in o) result.push(k); result")); 12148 "var result = []; for (var k in o) result.push(k); result"));
12080 CHECK_EQ(1, result->Length()); 12149 CHECK_EQ(1, result->Length());
12081 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); 12150 CHECK_EQ(v8_str("universalAnswer"), result->Get(0));
12082 } 12151 }
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