Author: [email protected]
Date: Thu Apr 16 09:34:24 2009
New Revision: 1727

Modified:
    branches/bleeding_edge/src/func-name-inferrer.cc
    branches/bleeding_edge/src/func-name-inferrer.h
    branches/bleeding_edge/src/rewriter.cc
    branches/bleeding_edge/test/cctest/test-func-name-inference.cc

Log:
Allow multiple function literals to be assigned to the same var / property.

In such a case all functions get the same name. I think it's a good  
performance / usability tradeoff. In case a developer wants more clarity,  
it's up to him to give names to functions.

Review URL: http://codereview.chromium.org/67168

Modified: branches/bleeding_edge/src/func-name-inferrer.cc
==============================================================================
--- branches/bleeding_edge/src/func-name-inferrer.cc    (original)
+++ branches/bleeding_edge/src/func-name-inferrer.cc    Thu Apr 16 09:34:24  
2009
@@ -63,11 +63,12 @@
  }


-void FuncNameInferrer::MaybeInferFunctionName() {
-  if (func_to_infer_ != NULL) {
-    func_to_infer_->set_inferred_name(MakeNameFromStack());
-    func_to_infer_ = NULL;
+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);
    }
+  funcs_to_infer_.Rewind(0);
  }



Modified: branches/bleeding_edge/src/func-name-inferrer.h
==============================================================================
--- branches/bleeding_edge/src/func-name-inferrer.h     (original)
+++ branches/bleeding_edge/src/func-name-inferrer.h     Thu Apr 16 09:34:24 2009
@@ -45,7 +45,7 @@
    FuncNameInferrer() :
        entries_stack_(10),
        names_stack_(5),
-      func_to_infer_(NULL),
+      funcs_to_infer_(4),
        dot_(Factory::NewStringFromAscii(CStrVector("."))) {
    }

@@ -57,39 +57,34 @@
      entries_stack_.Add(names_stack_.length());
    }

-  void Leave() {
-    ASSERT(IsOpen());
-    names_stack_.Rewind(entries_stack_.RemoveLast());
-  }
-
    void PushName(Handle<String> name) {
      if (IsOpen()) {
        names_stack_.Add(name);
      }
    }

-  void SetFuncToInfer(FunctionLiteral* func_to_infer) {
+  void AddFunction(FunctionLiteral* func_to_infer) {
      if (IsOpen()) {
-      // If we encounter another function literal after already having
-      // encountered one, the second one replaces the first.
-      func_to_infer_ = func_to_infer;
+      funcs_to_infer_.Add(func_to_infer);
      }
    }

    void InferAndLeave() {
      ASSERT(IsOpen());
-    MaybeInferFunctionName();
-    Leave();
+    if (!funcs_to_infer_.is_empty()) {
+      InferFunctionsNames();
+    }
+    names_stack_.Rewind(entries_stack_.RemoveLast());
    }

   private:
    Handle<String> MakeNameFromStack();
    Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev);
-  void MaybeInferFunctionName();
+  void InferFunctionsNames();

    List<int> entries_stack_;
    List<Handle<String> > names_stack_;
-  FunctionLiteral* func_to_infer_;
+  List<FunctionLiteral*> funcs_to_infer_;
    Handle<String> dot_;

    DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);

Modified: branches/bleeding_edge/src/rewriter.cc
==============================================================================
--- branches/bleeding_edge/src/rewriter.cc      (original)
+++ branches/bleeding_edge/src/rewriter.cc      Thu Apr 16 09:34:24 2009
@@ -194,7 +194,7 @@

    if (node->name()->length() == 0) {
      // Anonymous function.
-    func_name_inferrer_.SetFuncToInfer(node);
+    func_name_inferrer_.AddFunction(node);
    }
  }

@@ -282,10 +282,7 @@
      case Token::ASSIGN:
        // No type can be infered from the general assignment.

-      if (node->value()->AsFunctionLiteral() != NULL ||
-          node->value()->AsObjectLiteral() != NULL) {
-        scoped_fni.Enter();
-      }
+      scoped_fni.Enter();
        break;
      case Token::ASSIGN_BIT_OR:
      case Token::ASSIGN_BIT_XOR:

Modified: branches/bleeding_edge/test/cctest/test-func-name-inference.cc
==============================================================================
--- branches/bleeding_edge/test/cctest/test-func-name-inference.cc       
(original)
+++ branches/bleeding_edge/test/cctest/test-func-name-inference.cc      Thu Apr 
 
16 09:34:24 2009
@@ -221,3 +221,30 @@
    CheckFunctionName(script, "return 2", "");
    CheckFunctionName(script, "return 3", "");
  }
+
+
+TEST(MultipleFuncsConditional) {
+  InitializeVM();
+  v8::HandleScope scope;
+
+  v8::Handle<v8::Script> script = Compile(
+      "fun1 = 0 ?\n"
+      "    function() { return 1; } :\n"
+      "    function() { return 2; }");
+  CheckFunctionName(script, "return 1", "fun1");
+  CheckFunctionName(script, "return 2", "fun1");
+}
+
+
+TEST(MultipleFuncsInLiteral) {
+  InitializeVM();
+  v8::HandleScope scope;
+
+  v8::Handle<v8::Script> script = Compile(
+      "function MyClass() {}\n"
+      "MyClass.prototype = {\n"
+      "  method1: 0 ? function() { return 1; } :\n"
+      "               function() { return 2; } }");
+  CheckFunctionName(script, "return 1", "MyClass.method1");
+  CheckFunctionName(script, "return 2", "MyClass.method1");
+}

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to