call-optimization.cc 4.9 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/ic/call-optimization.h"
6
#include "src/objects-inl.h"
7 8 9 10

namespace v8 {
namespace internal {

11
CallOptimization::CallOptimization(Isolate* isolate, Handle<Object> function) {
12 13 14 15 16
  constant_function_ = Handle<JSFunction>::null();
  is_simple_api_call_ = false;
  expected_receiver_type_ = Handle<FunctionTemplateInfo>::null();
  api_call_info_ = Handle<CallHandlerInfo>::null();
  if (function->IsJSFunction()) {
17
    Initialize(isolate, Handle<JSFunction>::cast(function));
18
  } else if (function->IsFunctionTemplateInfo()) {
19
    Initialize(isolate, Handle<FunctionTemplateInfo>::cast(function));
20
  }
21 22
}

23
Context CallOptimization::GetAccessorContext(Map holder_map) const {
24 25 26
  if (is_constant_call()) {
    return constant_function_->context()->native_context();
  }
27
  JSFunction constructor = JSFunction::cast(holder_map->GetConstructor());
28 29 30
  return constructor->context()->native_context();
}

31
bool CallOptimization::IsCrossContextLazyAccessorPair(Context native_context,
32
                                                      Map holder_map) const {
33 34 35 36 37
  DCHECK(native_context->IsNativeContext());
  if (is_constant_call()) return false;
  return native_context != GetAccessorContext(holder_map);
}

38
Handle<JSObject> CallOptimization::LookupHolderOfExpectedType(
39
    Handle<Map> object_map, HolderLookup* holder_lookup) const {
40 41 42 43 44 45 46 47 48 49
  DCHECK(is_simple_api_call());
  if (!object_map->IsJSObjectMap()) {
    *holder_lookup = kHolderNotFound;
    return Handle<JSObject>::null();
  }
  if (expected_receiver_type_.is_null() ||
      expected_receiver_type_->IsTemplateFor(*object_map)) {
    *holder_lookup = kHolderIsReceiver;
    return Handle<JSObject>::null();
  }
50
  if (object_map->has_hidden_prototype()) {
51
    JSObject raw_prototype = JSObject::cast(object_map->prototype());
52 53
    Handle<JSObject> prototype(raw_prototype, raw_prototype->GetIsolate());
    object_map = handle(prototype->map(), prototype->GetIsolate());
54 55 56 57 58 59 60 61 62 63 64 65 66
    if (expected_receiver_type_->IsTemplateFor(*object_map)) {
      *holder_lookup = kHolderFound;
      return prototype;
    }
  }
  *holder_lookup = kHolderNotFound;
  return Handle<JSObject>::null();
}


bool CallOptimization::IsCompatibleReceiver(Handle<Object> receiver,
                                            Handle<JSObject> holder) const {
  DCHECK(is_simple_api_call());
67
  if (!receiver->IsHeapObject()) return false;
68
  Handle<Map> map(HeapObject::cast(*receiver)->map(), holder->GetIsolate());
69
  return IsCompatibleReceiverMap(map, holder);
70 71 72
}


73 74
bool CallOptimization::IsCompatibleReceiverMap(Handle<Map> map,
                                               Handle<JSObject> holder) const {
75 76 77 78 79 80 81 82 83 84 85
  HolderLookup holder_lookup;
  Handle<JSObject> api_holder = LookupHolderOfExpectedType(map, &holder_lookup);
  switch (holder_lookup) {
    case kHolderNotFound:
      return false;
    case kHolderIsReceiver:
      return true;
    case kHolderFound:
      if (api_holder.is_identical_to(holder)) return true;
      // Check if holder is in prototype chain of api_holder.
      {
86
        JSObject object = *api_holder;
87
        while (true) {
88
          Object prototype = object->map()->prototype();
89 90 91 92 93 94 95 96 97 98
          if (!prototype->IsJSObject()) return false;
          if (prototype == *holder) return true;
          object = JSObject::cast(prototype);
        }
      }
      break;
  }
  UNREACHABLE();
}

99
void CallOptimization::Initialize(
100
    Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info) {
101
  if (function_template_info->call_code()->IsUndefined(isolate)) return;
102 103
  api_call_info_ = handle(
      CallHandlerInfo::cast(function_template_info->call_code()), isolate);
104

105
  if (!function_template_info->signature()->IsUndefined(isolate)) {
106
    expected_receiver_type_ =
107 108
        handle(FunctionTemplateInfo::cast(function_template_info->signature()),
               isolate);
109 110 111
  }
  is_simple_api_call_ = true;
}
112

113 114
void CallOptimization::Initialize(Isolate* isolate,
                                  Handle<JSFunction> function) {
115 116 117
  if (function.is_null() || !function->is_compiled()) return;

  constant_function_ = function;
118
  AnalyzePossibleApiFunction(isolate, function);
119 120
}

121 122
void CallOptimization::AnalyzePossibleApiFunction(Isolate* isolate,
                                                  Handle<JSFunction> function) {
123
  if (!function->shared()->IsApiFunction()) return;
124 125
  Handle<FunctionTemplateInfo> info(function->shared()->get_api_func_data(),
                                    isolate);
126 127

  // Require a C++ callback.
128 129
  if (info->call_code()->IsUndefined(isolate)) return;
  api_call_info_ = handle(CallHandlerInfo::cast(info->call_code()), isolate);
130

131
  if (!info->signature()->IsUndefined(isolate)) {
dcarney's avatar
dcarney committed
132
    expected_receiver_type_ =
133
        handle(FunctionTemplateInfo::cast(info->signature()), isolate);
134 135 136 137
  }

  is_simple_api_call_ = true;
}
138 139
}  // namespace internal
}  // namespace v8