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