OLD | NEW |
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 21822 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21833 context->Global()->Set(v8_str("P"), templ->NewInstance()); | 21833 context->Global()->Set(v8_str("P"), templ->NewInstance()); |
21834 CompileRun( | 21834 CompileRun( |
21835 "function C1() {" | 21835 "function C1() {" |
21836 " this.x = 23;" | 21836 " this.x = 23;" |
21837 "};" | 21837 "};" |
21838 "C1.prototype = P;" | 21838 "C1.prototype = P;" |
21839 "for (var i = 0; i < 4; i++ ) {" | 21839 "for (var i = 0; i < 4; i++ ) {" |
21840 " new C1();" | 21840 " new C1();" |
21841 "}"); | 21841 "}"); |
21842 } | 21842 } |
| 21843 |
| 21844 |
| 21845 class ApiCallOptimizationChecker { |
| 21846 private: |
| 21847 static Local<Object> data; |
| 21848 static Local<Object> receiver; |
| 21849 static Local<Object> holder; |
| 21850 static Local<Object> callee; |
| 21851 static int count; |
| 21852 |
| 21853 static void OptimizationCallback( |
| 21854 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 21855 CHECK(callee == info.Callee()); |
| 21856 CHECK(data == info.Data()); |
| 21857 CHECK(receiver == info.This()); |
| 21858 CHECK(holder == info.Holder()); |
| 21859 count++; |
| 21860 } |
| 21861 |
| 21862 public: |
| 21863 template<bool use_signature, bool global> |
| 21864 void Run() { |
| 21865 v8::Isolate* isolate = CcTest::isolate(); |
| 21866 v8::HandleScope scope(isolate); |
| 21867 // Build a template for signature checks. |
| 21868 Local<v8::ObjectTemplate> signature_template; |
| 21869 Local<v8::Signature> signature; |
| 21870 { |
| 21871 Local<v8::FunctionTemplate> parent_template = |
| 21872 FunctionTemplate::New(isolate); |
| 21873 parent_template->SetHiddenPrototype(true); |
| 21874 Local<v8::FunctionTemplate> function_template |
| 21875 = FunctionTemplate::New(isolate); |
| 21876 function_template->Inherit(parent_template); |
| 21877 if (use_signature) { |
| 21878 signature = v8::Signature::New(isolate, parent_template); |
| 21879 } |
| 21880 signature_template = function_template->InstanceTemplate(); |
| 21881 } |
| 21882 // Global object must pass checks. |
| 21883 Local<v8::Context> context = |
| 21884 v8::Context::New(isolate, NULL, signature_template); |
| 21885 v8::Context::Scope context_scope(context); |
| 21886 // Install regular object that can pass signature checks. |
| 21887 Local<Object> function_receiver = signature_template->NewInstance(); |
| 21888 context->Global()->Set(v8_str("function_receiver"), function_receiver); |
| 21889 // Get the holder objects. |
| 21890 Local<Object> inner_global = |
| 21891 Local<Object>::Cast(context->Global()->GetPrototype()); |
| 21892 Local<Object> function_holder = |
| 21893 Local<Object>::Cast(function_receiver->GetPrototype()); |
| 21894 // Install function on hidden prototype object. |
| 21895 data = Object::New(isolate); |
| 21896 Local<FunctionTemplate> function_template = FunctionTemplate::New( |
| 21897 isolate, OptimizationCallback, data, signature); |
| 21898 Local<Function> function = function_template->GetFunction(); |
| 21899 Local<Object>::Cast( |
| 21900 inner_global->GetPrototype())->Set(v8_str("global_f"), function); |
| 21901 function_holder->Set(v8_str("f"), function); |
| 21902 // Initialize expected values. |
| 21903 callee = function; |
| 21904 count = 0; |
| 21905 if (global) { |
| 21906 receiver = context->Global(); |
| 21907 holder = inner_global; |
| 21908 } else { |
| 21909 holder = function_receiver; |
| 21910 // If not using a signature, add something else to the prototype chain |
| 21911 // to test the case that holder != receiver |
| 21912 if (!use_signature) { |
| 21913 receiver = Local<Object>::Cast(CompileRun( |
| 21914 "var receiver_subclass = {};\n" |
| 21915 "receiver_subclass.__proto__ = function_receiver;\n" |
| 21916 "receiver_subclass")); |
| 21917 } else { |
| 21918 receiver = Local<Object>::Cast(CompileRun( |
| 21919 "var receiver_subclass = function_receiver;\n" |
| 21920 "receiver_subclass")); |
| 21921 } |
| 21922 } |
| 21923 // With no signature, the holder is not set. |
| 21924 if (!use_signature) holder = receiver; |
| 21925 // build wrap_function |
| 21926 int key = (use_signature ? 1 : 0) + 2 * (global ? 1 : 0); |
| 21927 i::ScopedVector<char> wrap_function(100); |
| 21928 if (global) { |
| 21929 i::OS::SNPrintF( |
| 21930 wrap_function, |
| 21931 "function wrap_%d() { var f = global_f; return f(); }\n", |
| 21932 key); |
| 21933 } else { |
| 21934 i::OS::SNPrintF( |
| 21935 wrap_function, |
| 21936 "function wrap_%d() { return receiver_subclass.f(); }\n", |
| 21937 key); |
| 21938 } |
| 21939 // build source string |
| 21940 i::ScopedVector<char> source(500); |
| 21941 i::OS::SNPrintF( |
| 21942 source, |
| 21943 "%s\n" // wrap_function |
| 21944 "function wrap2() { wrap_%d(); }\n" |
| 21945 "wrap2();\n" |
| 21946 "wrap2();\n" |
| 21947 "%%OptimizeFunctionOnNextCall(wrap_%d);\n" |
| 21948 "wrap2();\n", |
| 21949 wrap_function.start(), key, key); |
| 21950 v8::TryCatch try_catch; |
| 21951 CompileRun(source.start()); |
| 21952 ASSERT(!try_catch.HasCaught()); |
| 21953 CHECK_EQ(3, count); |
| 21954 } |
| 21955 }; |
| 21956 |
| 21957 |
| 21958 Local<Object> ApiCallOptimizationChecker::data; |
| 21959 Local<Object> ApiCallOptimizationChecker::receiver; |
| 21960 Local<Object> ApiCallOptimizationChecker::holder; |
| 21961 Local<Object> ApiCallOptimizationChecker::callee; |
| 21962 int ApiCallOptimizationChecker::count = 0; |
| 21963 |
| 21964 |
| 21965 TEST(TestFunctionCallOptimization) { |
| 21966 i::FLAG_allow_natives_syntax = true; |
| 21967 ApiCallOptimizationChecker checker; |
| 21968 checker.Run<true, true>(); |
| 21969 checker.Run<false, true>(); |
| 21970 checker.Run<true, false>(); |
| 21971 checker.Run<false, false>(); |
| 21972 } |
OLD | NEW |