2011-05-03 08:23:58 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "ast.h"
|
|
|
|
#include "func-name-inferrer.h"
|
2011-05-03 08:23:58 +00:00
|
|
|
#include "list-inl.h"
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
FuncNameInferrer::FuncNameInferrer(Isolate* isolate, Zone* zone)
|
2011-06-22 20:23:48 +00:00
|
|
|
: isolate_(isolate),
|
2012-06-11 12:42:31 +00:00
|
|
|
entries_stack_(10, zone),
|
|
|
|
names_stack_(5, zone),
|
|
|
|
funcs_to_infer_(4, zone),
|
|
|
|
zone_(zone) {
|
2011-06-22 20:23:48 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
void FuncNameInferrer::PushEnclosingName(Handle<String> name) {
|
|
|
|
// Enclosing name is a name of a constructor function. To check
|
|
|
|
// that it is really a constructor, we check that it is not empty
|
|
|
|
// and starts with a capital letter.
|
2011-03-18 20:35:07 +00:00
|
|
|
if (name->length() > 0 && Runtime::IsUpperCaseChar(
|
2011-06-22 20:23:48 +00:00
|
|
|
isolate()->runtime_state(), name->Get(0))) {
|
2012-06-11 12:42:31 +00:00
|
|
|
names_stack_.Add(Name(name, kEnclosingConstructorName), zone());
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-23 13:26:03 +00:00
|
|
|
void FuncNameInferrer::PushLiteralName(Handle<String> name) {
|
2014-04-11 07:27:25 +00:00
|
|
|
if (IsOpen() &&
|
|
|
|
!String::Equals(isolate()->factory()->prototype_string(), name)) {
|
2012-06-11 12:42:31 +00:00
|
|
|
names_stack_.Add(Name(name, kLiteralName), zone());
|
2010-08-23 13:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FuncNameInferrer::PushVariableName(Handle<String> name) {
|
2014-04-11 07:27:25 +00:00
|
|
|
if (IsOpen() &&
|
|
|
|
!String::Equals(isolate()->factory()->dot_result_string(), name)) {
|
2012-06-11 12:42:31 +00:00
|
|
|
names_stack_.Add(Name(name, kVariableName), zone());
|
2010-08-23 13:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
Handle<String> FuncNameInferrer::MakeNameFromStack() {
|
2011-06-22 20:23:48 +00:00
|
|
|
return MakeNameFromStackHelper(0, isolate()->factory()->empty_string());
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> FuncNameInferrer::MakeNameFromStackHelper(int pos,
|
|
|
|
Handle<String> prev) {
|
2011-06-22 20:23:48 +00:00
|
|
|
if (pos >= names_stack_.length()) return prev;
|
|
|
|
if (pos < names_stack_.length() - 1 &&
|
|
|
|
names_stack_.at(pos).type == kVariableName &&
|
|
|
|
names_stack_.at(pos + 1).type == kVariableName) {
|
|
|
|
// Skip consecutive variable declarations.
|
|
|
|
return MakeNameFromStackHelper(pos + 1, prev);
|
2009-04-14 00:51:59 +00:00
|
|
|
} else {
|
2011-06-22 20:23:48 +00:00
|
|
|
if (prev->length() > 0) {
|
2014-03-20 12:27:36 +00:00
|
|
|
Handle<String> name = names_stack_.at(pos).name;
|
|
|
|
if (prev->length() + name->length() + 1 > String::kMaxLength) return prev;
|
2011-06-22 20:23:48 +00:00
|
|
|
Factory* factory = isolate()->factory();
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> curr =
|
|
|
|
factory->NewConsString(factory->dot_string(), name).ToHandleChecked();
|
|
|
|
curr = factory->NewConsString(prev, curr).ToHandleChecked();
|
2014-03-20 12:27:36 +00:00
|
|
|
return MakeNameFromStackHelper(pos + 1, curr);
|
2011-06-22 20:23:48 +00:00
|
|
|
} else {
|
|
|
|
return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name);
|
|
|
|
}
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 16:34:24 +00:00
|
|
|
void FuncNameInferrer::InferFunctionsNames() {
|
|
|
|
Handle<String> func_name = MakeNameFromStack();
|
|
|
|
for (int i = 0; i < funcs_to_infer_.length(); ++i) {
|
|
|
|
funcs_to_infer_[i]->set_inferred_name(func_name);
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
2009-04-16 16:34:24 +00:00
|
|
|
funcs_to_infer_.Rewind(0);
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|