[Pkg-mozext-commits] [adblock-plus] 22/464: Added license block and dxr stuff, in case a random cosmic ray destroys my hard drive in a few minutes.

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:43:58 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 20f73b89b5983e421b4cb4f48b2a4bc720b95357
Author: Joshua Cranmer <Pidgeot18 at gmail.com>
Date:   Wed Apr 22 14:40:28 2009 -0400

    Added license block and dxr stuff, in case a random cosmic ray destroys my hard drive in a few minutes.
---
 README              |  31 ++++++++++++--
 jshydra.cpp         |   9 +++-
 scripts/cleanast.js | 120 +++++++++++++++++++++++++++++++++++++++++++++++++---
 tests/ast.js        |   2 +
 tests/dxr.js        |  63 +++++++++++++++++++++++++++
 5 files changed, 213 insertions(+), 12 deletions(-)

diff --git a/README b/README
index 7a20499..c02536d 100644
--- a/README
+++ b/README
@@ -1,7 +1,30 @@
 JSHydra static analysis tool
+© 2009 Joshua Cranmer
 
-jshydra_builtins.c and jshydra_funcs.c are copied from the dehydra sources with
-minor changes.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-The code requires a recent spidermonkey build, as it uses part of the internal
-API. Modify the /src/ paths in Makefile as appropriate to build.
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+To build the code, set the assignment to MOZ_SRCDIR and MOZ_OBJDIR to point to
+the source and build directories for a mozilla-central checkout. The source code
+for SpiderMonkey is required, since this uses internal APIs. A revision newer
+than 2cf0bbe3772a is required, since that revision changed the parse APIs.
+
+The code is not known to work with all JS features; the only code that
+probably does not work is E4X code. If you find a bug in the program, feel free
+to contact me on irc.mozilla.org (nick: jcranmer) or email me at
+<Pidgeot18 at gmail.com>.
diff --git a/jshydra.cpp b/jshydra.cpp
index 6b75757..973fda7 100644
--- a/jshydra.cpp
+++ b/jshydra.cpp
@@ -60,7 +60,7 @@ TokenValue tokens[] = {
     NAME, /*TOK_NAME*/
     DOUBLELITERAL, /*TOK_NUMBER*/
     NAME, /*TOK_STRING*/
-    NAME, /*TOK_REGEXP*/
+    OBJLITERAL, /*TOK_REGEXP*/
     NULLARY, /*TOK_PRIMARY*/
     FUNCTION, /*TOK_FUNCTION*/
     TERNARY, /*TOK_IF*/
@@ -203,7 +203,12 @@ JSObject *makeNode(JSParseNode *node) {
 		break;
 	}
 	//case APAIR:
-	//case OBJLITERAL:
+	case OBJLITERAL: {
+    setObjectProperty(object, "value", node->pn_objbox->object);
+		JSObject *array = JS_NewArrayObject(cx, 0, NULL);
+		setArrayElement(array, 0, makeNode(node->pn_expr));
+		setObjectProperty(object, "kids", array);
+  }
 	case DOUBLELITERAL: {
 		jsval dval;
 		if (!JS_NewDoubleValue(cx, node->pn_dval, &dval)) {
diff --git a/scripts/cleanast.js b/scripts/cleanast.js
index db53750..76571ac 100644
--- a/scripts/cleanast.js
+++ b/scripts/cleanast.js
@@ -16,17 +16,26 @@ function clean_ast(ast) {
 	assert(ast.type == 25);
 	let info = {
 		variables: [],
-		functions: [],
 		constants: [],
+    objects: [],
+    classes: [],
+		functions: [],
 		code: []
 	};
 	for each (let statement in ast.kids) {
-		if (statement.op == JSOP_DEFVAR) {
-			info.variables = info.variables.concat(make_variables(statement));
+    if (statement.op == JSOP_DEFVAR) {
+      let ret = make_variables(statement);
+			info.variables = info.variables.concat(ret.vars);
+      info.objects = info.objects.concat(ret.objs);
 		} else if (statement.op == JSOP_DEFCONST) {
-			info.constants = info.constants.concat(make_variables(statement));
+      let ret = make_variables(statement);
+			info.constants = info.constants.concat(ret.vars);
+      info.objects = info.objects.concat(ret.objs);
 		} else if (statement.type == 34) { // TOK_FUNCTION
 			info.functions.push(make_function(statement));
+    } else if (prototype_assign(statement)) {
+      let obj = make_class(statement);
+      merge_class(info, obj);
 		} else {
 			info.code.push(statement);
 		}
@@ -34,16 +43,115 @@ function clean_ast(ast) {
 	return info;
 }
 
+function prototype_assign(statement) {
+  if (statement.type != 2 || !statement.kids[0]) // TOK_SEMI
+    return false;
+  statement = statement.kids[0];
+  if (statement.type != 4 || !statement.kids[0]) // TOK_ASSIGN
+    return false;
+
+  statement = statement.kids[0];
+  // Statement is either prototype or a property of prototype
+  if (statement.op != JSOP_SETPROP)
+    return false;
+  if (statement.atom == "prototype")
+    return true;
+  if (statement.kids[0] && statement.kids[0] == "prototype")
+    return true;
+
+  // Or not!
+  return false;
+}
+
+function make_class(class_root) {
+  let clazz = {};
+  
+  class_root = class_root.kids[0];
+  let lhs = class_root.kids[0], rhs = class_root.kids[1];
+  if (lhs.atom == "prototype") {
+    clazz.init = rhs;
+    clazz = make_object(clazz);
+  } else {
+    clazz.functions = {};
+    clazz.functions[lhs.atom] = make_function(rhs);
+    lhs = lhs.kids[0];
+  }
+  clazz.name = lhs.kids[0].atom;
+  return clazz;
+}
+
+function merge_class(info_list, obj) {
+  let name = obj.name;
+  for (let i = 0; i < info_list.functions.length; i++) {
+    if (info_list.functions[i].name == name) {
+      obj.constructor = info_list.functions[i];
+      // XXX: remove from info_list
+      break;
+    }
+  }
+  if (obj.constructor)
+    obj.loc = obj.constructor.loc;
+  let oldObj = null;
+  for (let i = 0; i < info_list.classes.length; i++) {
+    if (info_list.classes[i].name == name) {
+      oldObj = info_list.classes[i];
+      break;
+    }
+  }
+  if (oldObj) {
+    for (let prop in obj) {
+      if (!(prop in oldObj)) {
+        oldObj[prop] = obj[prop];
+      } else if (typeof obj[prop] == "object") {
+        for (let item in obj[prop])
+          oldObj[prop][item] = obj[prop][item];
+      }
+    }
+  } else {
+    info_list.classes = info_list.classes.concat(obj);
+  }
+}
 function make_variables(var_root) {
 	assert(var_root.op == JSOP_DEFVAR || var_root.op == JSOP_DEFCONST);
 	let variables = [];
+  let objects = [];
 	for each (let name in var_root.kids) {
 		let v = { name: name.atom };
 		v.init = (name.kids.length > 0 ? name.kids[0] : null);
 		v.loc = get_location(var_root);
-		variables.push(v);
+    if (v.init && v.init.op == JSOP_NEWINIT)
+      objects.push(make_object(v));
+    else
+  		variables.push(v);
 	}
-	return variables;
+	return { vars: variables, objs: objects };
+}
+
+function make_object(stub) {
+  stub.variables = {};
+  stub.functions = {};
+  stub.getters = {};
+  stub.setters = {};
+  let ast = stub.init;
+  delete stub['init'];
+  for each (let init in ast.kids) {
+    assert(init.type == 6); // TOK_COLON
+    if (init.kids[0].type == 29) { // TOK_NAME
+      let name = init.kids[0].atom;
+      let value = init.kids[1];
+      if (init.op == JSOP_GETTER)
+        stub.getters[name] = make_function(value);
+      else if (init.op == JSOP_SETTER)
+        stub.setters[name] = make_function(value);
+      else if (value.type == 34) // TOK_FUNCTION
+        stub.functions[name] = make_function(value);
+      else
+        stub.variables[name] = { loc: get_location(value), init: value };
+    } else {
+      dump_ast(init);
+    }
+  }
+  return stub;
 }
 
 function make_function(func_root) {
diff --git a/tests/ast.js b/tests/ast.js
index a531434..8367aef 100644
--- a/tests/ast.js
+++ b/tests/ast.js
@@ -1,11 +1,13 @@
 // This is a simple test to test global magic
 
 include("../scripts/cleanast.js");
+include("../scripts/dumpast.js");
 
 var glob = this;
 const LS = "foobar";
 
 function process_js(ast) {
+  dump_ast(ast);
 	let toplevel = clean_ast(ast);
 	_print("Global variables:");
 	for each (let v in toplevel.variables) {
diff --git a/tests/dxr.js b/tests/dxr.js
new file mode 100644
index 0000000..8251924
--- /dev/null
+++ b/tests/dxr.js
@@ -0,0 +1,63 @@
+// This is a simple test to test global magic
+
+include("../scripts/cleanast.js");
+include("../scripts/dumpast.js");
+
+function process_js(ast, f) {
+  function loc(l) {
+    return f + ":" + l.line + ":" + l.column;
+  }
+  let toplevel = clean_ast(ast);
+  _print("Global variables:");
+  for each (let v in toplevel.variables) {
+    _print("\t" + v.name + " at " + f + ":" + v.loc.line + ":" + v.loc.column);
+  }
+  _print("Global constants:");
+  for each (let v in toplevel.constants) {
+    _print("\t" + v.name + " at " + f + ":" + v.loc.line + ":" + v.loc.column);
+  }
+  _print("Global objects:");
+  for each (let v in toplevel.objects) {
+    _print("\t" + v.name + " at " + f + ":" + v.loc.line + ":" + v.loc.column);
+    _print("\tFunctions:");
+    for (let name in v.functions) {
+      _print("\t\t" + name + " at " + loc(v.functions[name].loc));
+    }
+    _print("\tVariables:");
+    for (let name in v.variables) {
+      _print("\t\t" + name + " at " + loc(v.variables[name].loc));
+    }
+    _print("\tGetters:");
+    for (let name in v.getters) {
+      _print("\t\t" + name + " at " + loc(v.getters[name].loc));
+    }
+    _print("\tSetters:");
+    for (let name in v.setters) {
+      _print("\t\t" + name + " at " + loc(v.setters[name].loc));
+    }
+  }
+  _print("Global classes:");
+  for each (let v in toplevel.classes) {
+    _print("\t" + v.name + " at " + f + ":" + v.loc.line + ":" + v.loc.column);
+    _print("\tFunctions:");
+    for (let name in v.functions) {
+      _print("\t\t" + name + " at " + loc(v.functions[name].loc));
+    }
+    _print("\tVariables:");
+    for (let name in v.variables) {
+      _print("\t\t" + name + " at " + loc(v.variables[name].loc));
+    }
+    _print("\tGetters:");
+    for (let name in v.getters) {
+      _print("\t\t" + name + " at " + loc(v.getters[name].loc));
+    }
+    _print("\tSetters:");
+    for (let name in v.setters) {
+      _print("\t\t" + name + " at " + loc(v.setters[name].loc));
+    }
+  }
+  _print("Global functions:");
+  for each (let v in toplevel.functions) {
+    _print("\t" + v.name + " at " + f + ":" + v.loc.line + ":" + v.loc.column);
+  }
+}

-- 
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