Reviewers: jochen,

Message:
tfr


https://codereview.chromium.org/336863007/diff/1/include/v8.h
File include/v8.h (right):

https://codereview.chromium.org/336863007/diff/1/include/v8.h#newcode4138
include/v8.h:4138: kUseCounterFeatureCount
On 2014/06/30 13:23:21, jochen wrote:
can you add a comment similar to the one in
Source/core/frame/UseCounter.h that
new features need to be added before this one?

Done.

Description:
Parser: add usage counters for "use asm".

[email protected]
BUG=

Please review this at https://codereview.chromium.org/336863007/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+61, -3 lines):
  M include/v8.h
  M src/ast-value-factory.h
  M src/parser.h
  M src/parser.cc
  M test/cctest/test-parsing.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index cbbc0ff450490135cb93d476e53fefdc13e8c4da..0f58bce3736a7b0fa5dc6b6b571cde4e5b8a6de6 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -4134,7 +4134,8 @@ class V8_EXPORT Isolate {
    * list.
    */
   enum UseCounterFeature {
-    kUseAsm = 0
+    kUseAsm = 0,
+    kUseCounterFeatureCount  // This enum value must be last.
   };

   typedef void (*UseCounterCallback)(Isolate* isolate,
Index: src/ast-value-factory.h
diff --git a/src/ast-value-factory.h b/src/ast-value-factory.h
index e70271f96ed3f76c5e5dbb7f241846ddff66cb76..4711bb4a22efd88aa4bbe5de39b03d415cb69833 100644
--- a/src/ast-value-factory.h
+++ b/src/ast-value-factory.h
@@ -260,6 +260,7 @@ class AstValue : public ZoneObject {
   F(proto, "__proto__") \
   F(prototype, "prototype") \
   F(this, "this") \
+  F(use_asm, "use asm") \
   F(use_strict, "use strict") \
   F(value, "value")

Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index a2900b51ae3821b8218395df5f4701bc542ab802..cecfbc2af0c8d846edef5ce05cc386b2ef8792a5 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -783,6 +783,10 @@ Parser::Parser(CompilationInfo* info)
   set_allow_generators(FLAG_harmony_generators);
   set_allow_for_of(FLAG_harmony_iteration);
   set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
+  for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
+       ++feature) {
+    use_counts_[feature] = 0;
+  }
 }


@@ -1087,11 +1091,13 @@ void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
       if ((e_stat = stat->AsExpressionStatement()) != NULL &&
           (literal = e_stat->expression()->AsLiteral()) != NULL &&
           literal->raw_value()->IsString()) {
-        // Check "use strict" directive (ES5 14.1).
+ // Check "use strict" directive (ES5 14.1) and "use asm" directive. Only
+        // one can be present.
         if (strict_mode() == SLOPPY &&
             literal->raw_value()->AsString() ==
                 ast_value_factory_->use_strict_string() &&
-            token_loc.end_pos - token_loc.beg_pos == 12) {
+            token_loc.end_pos - token_loc.beg_pos ==
+                ast_value_factory_->use_strict_string()->length() + 2) {
// TODO(mstarzinger): Global strict eval calls, need their own scope // as specified in ES5 10.4.2(3). The correct fix would be to always // add this scope in DoParseProgram(), but that requires adaptations @@ -1108,6 +1114,13 @@ void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
           scope_->SetStrictMode(STRICT);
           // "use strict" is the only directive for now.
           directive_prologue = false;
+        } else if (literal->raw_value()->AsString() ==
+                       ast_value_factory_->use_asm_string() &&
+                   token_loc.end_pos - token_loc.beg_pos ==
+ ast_value_factory_->use_asm_string()->length() + 2) { + // Store the usage count; The actual use counter on the isolate is
+          // incremented after parsing is done.
+          ++use_counts_[v8::Isolate::kUseAsm];
         }
       } else {
         // End of the directive prologue.
@@ -3899,6 +3912,16 @@ void Parser::ThrowPendingError() {
 }


+void Parser::InternalizeUseCounts() {
+  for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
+       ++feature) {
+    for (int i = 0; i < use_counts_[feature]; ++i) {
+      isolate()->CountUsage(v8::Isolate::UseCounterFeature(feature));
+    }
+  }
+}
+
+
// ----------------------------------------------------------------------------
 // Regular expressions

@@ -4840,6 +4863,9 @@ bool Parser::Parse() {
     info()->SetAstValueFactory(ast_value_factory_);
   }
   ast_value_factory_ = NULL;
+
+  InternalizeUseCounts();
+
   return (result != NULL);
 }

Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index c05b2d9cb330be6b82b6a193fe7404ff17bc6423..346f75fa2b12d12255e66c766456a79c16a58b97 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -798,6 +798,8 @@ class Parser : public ParserBase<ParserTraits> {

   void ThrowPendingError();

+  void InternalizeUseCounts();
+
   Isolate* isolate_;

   Handle<Script> script_;
@@ -818,6 +820,8 @@ class Parser : public ParserBase<ParserTraits> {
   const AstRawString* pending_error_arg_;
   const char* pending_error_char_arg_;
   bool pending_error_is_reference_error_;
+
+  int use_counts_[v8::Isolate::kUseCounterFeatureCount];
 };


Index: test/cctest/test-parsing.cc
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc
index b36621742ae2824c01b72a538145b6263d184c8c..32c515cc89899c17068cf28257610caa7d5a9b7a 100644
--- a/test/cctest/test-parsing.cc
+++ b/test/cctest/test-parsing.cc
@@ -2745,3 +2745,29 @@ TEST(InnerAssignment) {
     }
   }
 }
+
+namespace {
+
+int* global_use_counts = NULL;
+
+void MockUseCounterCallback(v8::Isolate* isolate,
+                            v8::Isolate::UseCounterFeature feature) {
+  ++global_use_counts[feature];
+}
+
+}
+
+
+TEST(UseAsmUseCount) {
+  i::Isolate* isolate = CcTest::i_isolate();
+  i::HandleScope scope(isolate);
+  LocalContext env;
+  int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
+  global_use_counts = use_counts;
+  CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
+  CompileRun("\"use asm\";\n"
+             "var foo = 1;\n"
+             "\"use asm\";\n"  // Only the first one counts.
+             "function bar() { \"use asm\"; var baz = 1; }");
+  CHECK_EQ(2, use_counts[v8::Isolate::kUseAsm]);
+}


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to