| 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 21855 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 21866 | 21866 |
| 21867 static void OptimizationCallback( | 21867 static void OptimizationCallback( |
| 21868 const v8::FunctionCallbackInfo<v8::Value>& info) { | 21868 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 21869 CHECK(callee == info.Callee()); | 21869 CHECK(callee == info.Callee()); |
| 21870 CHECK(data == info.Data()); | 21870 CHECK(data == info.Data()); |
| 21871 CHECK(receiver == info.This()); | 21871 CHECK(receiver == info.This()); |
| 21872 CHECK(holder == info.Holder()); | 21872 CHECK(holder == info.Holder()); |
| 21873 count++; | 21873 count++; |
| 21874 } | 21874 } |
| 21875 | 21875 |
| 21876 // TODO(dcarney): move this to v8.h |
| 21877 static void SetAccessorProperty(Local<Object> object, |
| 21878 Local<String> name, |
| 21879 Local<Function> getter, |
| 21880 Local<Function> setter = Local<Function>()) { |
| 21881 i::Isolate* isolate = CcTest::i_isolate(); |
| 21882 v8::AccessControl settings = v8::DEFAULT; |
| 21883 v8::PropertyAttribute attribute = v8::None; |
| 21884 i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter); |
| 21885 i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true); |
| 21886 if (setter_i.is_null()) setter_i = isolate->factory()->null_value(); |
| 21887 i::JSObject::DefineAccessor(v8::Utils::OpenHandle(*object), |
| 21888 v8::Utils::OpenHandle(*name), |
| 21889 getter_i, |
| 21890 setter_i, |
| 21891 static_cast<PropertyAttributes>(attribute), |
| 21892 settings); |
| 21893 } |
| 21894 |
| 21876 public: | 21895 public: |
| 21877 void Run(bool use_signature, bool global) { | 21896 void Run(bool use_signature, bool global) { |
| 21878 v8::Isolate* isolate = CcTest::isolate(); | 21897 v8::Isolate* isolate = CcTest::isolate(); |
| 21879 v8::HandleScope scope(isolate); | 21898 v8::HandleScope scope(isolate); |
| 21880 // Build a template for signature checks. | 21899 // Build a template for signature checks. |
| 21881 Local<v8::ObjectTemplate> signature_template; | 21900 Local<v8::ObjectTemplate> signature_template; |
| 21882 Local<v8::Signature> signature; | 21901 Local<v8::Signature> signature; |
| 21883 { | 21902 { |
| 21884 Local<v8::FunctionTemplate> parent_template = | 21903 Local<v8::FunctionTemplate> parent_template = |
| 21885 FunctionTemplate::New(isolate); | 21904 FunctionTemplate::New(isolate); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 21902 // Get the holder objects. | 21921 // Get the holder objects. |
| 21903 Local<Object> inner_global = | 21922 Local<Object> inner_global = |
| 21904 Local<Object>::Cast(context->Global()->GetPrototype()); | 21923 Local<Object>::Cast(context->Global()->GetPrototype()); |
| 21905 Local<Object> function_holder = | 21924 Local<Object> function_holder = |
| 21906 Local<Object>::Cast(function_receiver->GetPrototype()); | 21925 Local<Object>::Cast(function_receiver->GetPrototype()); |
| 21907 // Install function on hidden prototype object. | 21926 // Install function on hidden prototype object. |
| 21908 data = Object::New(isolate); | 21927 data = Object::New(isolate); |
| 21909 Local<FunctionTemplate> function_template = FunctionTemplate::New( | 21928 Local<FunctionTemplate> function_template = FunctionTemplate::New( |
| 21910 isolate, OptimizationCallback, data, signature); | 21929 isolate, OptimizationCallback, data, signature); |
| 21911 Local<Function> function = function_template->GetFunction(); | 21930 Local<Function> function = function_template->GetFunction(); |
| 21912 Local<Object>::Cast( | 21931 Local<Object> global_holder = Local<Object>::Cast( |
| 21913 inner_global->GetPrototype())->Set(v8_str("global_f"), function); | 21932 inner_global->GetPrototype()); |
| 21933 global_holder->Set(v8_str("g_f"), function); |
| 21934 SetAccessorProperty(global_holder, v8_str("g_p1"), function); |
| 21914 function_holder->Set(v8_str("f"), function); | 21935 function_holder->Set(v8_str("f"), function); |
| 21936 SetAccessorProperty(function_holder, v8_str("p1"), function); |
| 21915 // Initialize expected values. | 21937 // Initialize expected values. |
| 21916 callee = function; | 21938 callee = function; |
| 21917 count = 0; | 21939 count = 0; |
| 21918 if (global) { | 21940 if (global) { |
| 21919 receiver = context->Global(); | 21941 receiver = context->Global(); |
| 21920 holder = inner_global; | 21942 holder = inner_global; |
| 21921 } else { | 21943 } else { |
| 21922 holder = function_receiver; | 21944 holder = function_receiver; |
| 21923 // If not using a signature, add something else to the prototype chain | 21945 // If not using a signature, add something else to the prototype chain |
| 21924 // to test the case that holder != receiver | 21946 // to test the case that holder != receiver |
| 21925 if (!use_signature) { | 21947 if (!use_signature) { |
| 21926 receiver = Local<Object>::Cast(CompileRun( | 21948 receiver = Local<Object>::Cast(CompileRun( |
| 21927 "var receiver_subclass = {};\n" | 21949 "var receiver_subclass = {};\n" |
| 21928 "receiver_subclass.__proto__ = function_receiver;\n" | 21950 "receiver_subclass.__proto__ = function_receiver;\n" |
| 21929 "receiver_subclass")); | 21951 "receiver_subclass")); |
| 21930 } else { | 21952 } else { |
| 21931 receiver = Local<Object>::Cast(CompileRun( | 21953 receiver = Local<Object>::Cast(CompileRun( |
| 21932 "var receiver_subclass = function_receiver;\n" | 21954 "var receiver_subclass = function_receiver;\n" |
| 21933 "receiver_subclass")); | 21955 "receiver_subclass")); |
| 21934 } | 21956 } |
| 21935 } | 21957 } |
| 21936 // With no signature, the holder is not set. | 21958 // With no signature, the holder is not set. |
| 21937 if (!use_signature) holder = receiver; | 21959 if (!use_signature) holder = receiver; |
| 21938 // build wrap_function | 21960 // build wrap_function |
| 21939 int key = (use_signature ? 1 : 0) + 2 * (global ? 1 : 0); | 21961 int key = (use_signature ? 1 : 0) + 2 * (global ? 1 : 0); |
| 21940 i::ScopedVector<char> wrap_function(100); | 21962 i::ScopedVector<char> wrap_function(200); |
| 21941 if (global) { | 21963 if (global) { |
| 21942 i::OS::SNPrintF( | 21964 i::OS::SNPrintF( |
| 21943 wrap_function, | 21965 wrap_function, |
| 21944 "function wrap_%d() { var f = global_f; return f(); }\n", | 21966 "function wrap_f_%d() { var f = g_f; return f(); }\n" |
| 21945 key); | 21967 "function wrap_p1_%d() { return this.g_p1; }\n", |
| 21968 key, key); |
| 21946 } else { | 21969 } else { |
| 21947 i::OS::SNPrintF( | 21970 i::OS::SNPrintF( |
| 21948 wrap_function, | 21971 wrap_function, |
| 21949 "function wrap_%d() { return receiver_subclass.f(); }\n", | 21972 "function wrap_f_%d() { return receiver_subclass.f(); }\n" |
| 21950 key); | 21973 "function wrap_p1_%d() { return receiver_subclass.p1; }\n", |
| 21974 key, key); |
| 21951 } | 21975 } |
| 21952 // build source string | 21976 // build source string |
| 21953 i::ScopedVector<char> source(500); | 21977 i::ScopedVector<char> source(500); |
| 21954 i::OS::SNPrintF( | 21978 i::OS::SNPrintF( |
| 21955 source, | 21979 source, |
| 21956 "%s\n" // wrap_function | 21980 "%s\n" // wrap functions |
| 21957 "function wrap2() { wrap_%d(); }\n" | 21981 "function wrap_f() { wrap_f_%d(); }\n" |
| 21958 "wrap2();\n" | 21982 "function wrap_p1() { wrap_p1_%d(); }\n" |
| 21959 "wrap2();\n" | 21983 "wrap_f();\n" |
| 21960 "%%OptimizeFunctionOnNextCall(wrap_%d);\n" | 21984 "wrap_f();\n" |
| 21961 "wrap2();\n", | 21985 "%%OptimizeFunctionOnNextCall(wrap_f_%d);\n" |
| 21962 wrap_function.start(), key, key); | 21986 "wrap_f();\n" |
| 21963 v8::TryCatch try_catch; | 21987 "wrap_p1();\n" |
| 21988 "wrap_p1();\n" |
| 21989 "%%OptimizeFunctionOnNextCall(wrap_p1_%d);\n" |
| 21990 "wrap_p1();\n", |
| 21991 wrap_function.start(), key, key, key, key); |
| 21992 // v8::TryCatch try_catch; |
| 21993 // fprintf(stderr, "%s", source.start()); |
| 21964 CompileRun(source.start()); | 21994 CompileRun(source.start()); |
| 21965 ASSERT(!try_catch.HasCaught()); | 21995 // ASSERT(!try_catch.HasCaught()); |
| 21966 CHECK_EQ(3, count); | 21996 CHECK_EQ(6, count); |
| 21967 } | 21997 } |
| 21968 }; | 21998 }; |
| 21969 | 21999 |
| 21970 | 22000 |
| 21971 Local<Object> ApiCallOptimizationChecker::data; | 22001 Local<Object> ApiCallOptimizationChecker::data; |
| 21972 Local<Object> ApiCallOptimizationChecker::receiver; | 22002 Local<Object> ApiCallOptimizationChecker::receiver; |
| 21973 Local<Object> ApiCallOptimizationChecker::holder; | 22003 Local<Object> ApiCallOptimizationChecker::holder; |
| 21974 Local<Object> ApiCallOptimizationChecker::callee; | 22004 Local<Object> ApiCallOptimizationChecker::callee; |
| 21975 int ApiCallOptimizationChecker::count = 0; | 22005 int ApiCallOptimizationChecker::count = 0; |
| 21976 | 22006 |
| 21977 | 22007 |
| 21978 TEST(TestFunctionCallOptimization) { | 22008 TEST(TestFunctionCallOptimization) { |
| 21979 i::FLAG_allow_natives_syntax = true; | 22009 i::FLAG_allow_natives_syntax = true; |
| 21980 ApiCallOptimizationChecker checker; | 22010 ApiCallOptimizationChecker checker; |
| 21981 checker.Run(true, true); | 22011 checker.Run(true, true); |
| 21982 checker.Run(false, true); | 22012 checker.Run(false, true); |
| 21983 checker.Run(true, false); | 22013 checker.Run(true, false); |
| 21984 checker.Run(false, false); | 22014 checker.Run(false, false); |
| 21985 } | 22015 } |
| OLD | NEW |