[Pkg-mozext-commits] [adblock-plus] 284/464: Rewrite generators into functions returning an array

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:25 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 393a29661fb79729bc44df46cf705be977fb9bdf
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Fri Aug 24 16:08:16 2012 +0200

    Rewrite generators into functions returning an array
---
 autotest/abprewrite_source.js               | 15 +++++++
 autotest/test_abprewrite.js.expected        | 19 +++++++++
 autotest/test_abprewrite_module.js.expected | 19 +++++++++
 scripts/abprewrite.js                       | 61 +++++++++++++++++++++++++++++
 4 files changed, 114 insertions(+)

diff --git a/autotest/abprewrite_source.js b/autotest/abprewrite_source.js
index 3a4531f..56f5e16 100644
--- a/autotest/abprewrite_source.js
+++ b/autotest/abprewrite_source.js
@@ -38,3 +38,18 @@ let a = function() 1;
 let b = {
   get foo() 1
 };
+
+function genFunc()
+{
+  for (var i = 0; i < 10; i++)
+  {
+    yield i;
+  }
+}
+var a = function()
+{
+  for (var i = 0; i < 10; i++)
+  {
+    yield i;
+  }
+};
diff --git a/autotest/test_abprewrite.js.expected b/autotest/test_abprewrite.js.expected
index 03b3d98..5188531 100644
--- a/autotest/test_abprewrite.js.expected
+++ b/autotest/test_abprewrite.js.expected
@@ -66,3 +66,22 @@ var b =
     return 1;
   }
 };
+
+function genFunc()
+{
+  var _generatorResult10 = [];
+  for (var i = 0; i < 10; i++)
+  {
+    _generatorResult10.push(i);
+  }
+  return _generatorResult10;
+}
+var a = function()
+{
+  var _generatorResult10 = [];
+  for (var i = 0; i < 10; i++)
+  {
+    _generatorResult10.push(i);
+  }
+  return _generatorResult10;
+};
diff --git a/autotest/test_abprewrite_module.js.expected b/autotest/test_abprewrite_module.js.expected
index dad60a7..5bf48cb 100644
--- a/autotest/test_abprewrite_module.js.expected
+++ b/autotest/test_abprewrite_module.js.expected
@@ -69,5 +69,24 @@ require.scopes["abprewrite_source"] = (function()
       return 1;
     }
   };
+
+  function genFunc()
+  {
+    var _generatorResult10 = [];
+    for (var i = 0; i < 10; i++)
+    {
+      _generatorResult10.push(i);
+    }
+    return _generatorResult10;
+  }
+  var a = function()
+  {
+    var _generatorResult10 = [];
+    for (var i = 0; i < 10; i++)
+    {
+      _generatorResult10.push(i);
+    }
+    return _generatorResult10;
+  };
   return exports;
 })();
diff --git a/scripts/abprewrite.js b/scripts/abprewrite.js
index ef65ea1..fc0c851 100644
--- a/scripts/abprewrite.js
+++ b/scripts/abprewrite.js
@@ -361,9 +361,70 @@ function modifyFunctionExpression(ast)
       ]
     };
   }
+
+  if (ast.generator)
+  {
+    // Convert generators:
+    // function()
+    // {
+    //   ...
+    //   yield "foo";
+    //   ...
+    // }
+    //
+    // Change into:
+    // function()
+    // {
+    //   var _generatorResult44 = [];
+    //   ...
+    //   _generatorResult44.push("foo");
+    //   ...
+    //   return _generatorResult44;
+    // }
+    //
+    // Note: yield statements are converted in modifyYieldExpression().
+    if (!("generatorVar" in options))
+      options.generatorVar = Identifier("_generatorResult" + options.varIndex++);
+
+    ast.generator = false;
+    ast.body.body.unshift(VariableDeclaration(options.generatorVar, {
+      type: "ArrayExpression",
+      elements: null
+    }));
+    ast.body.body.push({
+      type: "ReturnStatement",
+      argument: options.generatorVar
+    });
+  }
+
   return ast;
 }
 
+function modifyFunctionDeclaration(ast)
+{
+  return modifyFunctionExpression(ast);
+}
+
+function modifyYieldExpression(ast)
+{
+  // Convert generators into functions returning arrays:
+  // yield "foo";
+  //
+  // Change into:
+  // _generatorResult44.push("foo");
+
+  if (ast.argument)
+  {
+    return {
+      type: "CallExpression",
+      callee: Member(options.generatorVar, "push"),
+      arguments: [ast.argument]
+    };
+  }
+  else
+    return null;
+}
+
 process_js = function(ast, filename, args)
 {
   for each (let arg in args.split(/\s+/))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list