[Pkg-mozext-commits] [adblock-plus] 14/464: Some basic AST work.
David Prévot
taffit at moszumanska.debian.org
Tue Jul 22 20:43:57 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 b09c9553a5ea0ac5fcad37f48b543065e5e9c498
Author: Joshua Cranmer <Pidgeot18 at gmail.com>
Date: Mon Feb 2 12:36:25 2009 -0500
Some basic AST work.
This is causing segfaults in some places, I'm not entirely sure why, though.
---
jshydra.cpp | 12 ++++++++----
scripts/cleanast.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/ast.js | 11 +++++++++++
3 files changed, 73 insertions(+), 4 deletions(-)
diff --git a/jshydra.cpp b/jshydra.cpp
index 972d54b..aa3930c 100644
--- a/jshydra.cpp
+++ b/jshydra.cpp
@@ -74,7 +74,7 @@ TokenValue tokens[] = {
LIST, /*TOK_VAR*/
BINARY, /*TOK_WITH*/
UNARY, /*TOK_RETURN*/
- ERROR, /*TOK_NEW*/
+ LIST, /*TOK_NEW*/
ERROR, /*TOK_DELETE*/
UNARY, /*TOK_DEFSHARP*/
NULLARY, /*TOK_USESHARP (use pn_num)*/
@@ -106,10 +106,10 @@ TokenValue tokens[] = {
LIST, /*TOK_ARRAYCOMP*/
UNARY, /*TOK_ARRAYPUSH*/
NAME, /*TOK_LEXICALSCOPE*/
- ERROR, /*TOK_LET*/
+ LIST, /*TOK_LET*/
ERROR, /*TOK_SEQ*/
- ERROR, /*TOK_FORHEAD*/
- //TOK_RESERVED,
+ TERNARY, /*TOK_FORHEAD*/
+ LIST, /*TOK_RESERVED [I don't understand this...] */
//TOK_LIMIT
ERROR
};
@@ -125,6 +125,10 @@ JSObject *makeNode(JSParseNode *node) {
switch (tokens[node->pn_type]) {
case FUNCTION: {
setIntProperty(object, "flags", node->pn_flags);
+ JSFunction *func = (JSFunction *) node->pn_funpob->object;
+ if (func->atom)
+ jshydra_defineProperty(cx, object, "name", ATOM_KEY(func->atom));
+
JSObject *array = JS_NewArrayObject(cx, 0, NULL);
setArrayElement(array, 0, makeNode(node->pn_body));
setObjectProperty(object, "kids", array);
diff --git a/scripts/cleanast.js b/scripts/cleanast.js
new file mode 100644
index 0000000..44996d9
--- /dev/null
+++ b/scripts/cleanast.js
@@ -0,0 +1,54 @@
+/**
+ * Takes the node rooted at the AST and decomposes it into readable sections.
+ */
+function clean_ast(ast) {
+ // TOK_LC
+ assert(ast.type == 25);
+ let info = {
+ variables: [],
+ functions: [],
+ constants: [],
+ code: []
+ };
+ for each (let statement in ast.kids) {
+ if (statement.op == JSOP_DEFVAR) {
+ info.variables = info.variables.concat(make_variables(statement));
+ } else if (statement.op == JSOP_DEFCONST) {
+ info.constants = info.constants.concat(make_variables(statement));
+ } else if (statement.type == 34) { // TOK_FUNCTION
+ info.functions.push(make_function(statement));
+ } else {
+ info.code.push(statement);
+ }
+ }
+ return info;
+}
+
+function make_variables(var_root) {
+ assert(var_root.op == JSOP_DEFVAR || var_root.op == JSOP_DEFCONST);
+ let variables = [];
+ 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);
+ }
+ return variables;
+}
+
+function make_function(func_root) {
+ assert(func_root.type == 34); // TOK_FUNCTION
+ let stmts = func_root.kids[0];
+ assert(stmts.type == 25); // TOK_LC
+ return { name: func_root.name, body: stmts, loc: get_location(func_root)};
+}
+
+function assert(cmd) {
+ if (!cmd) {
+ _print("ACK! I fail!");
+ }
+}
+
+function get_location(ast_node) {
+ return { line: ast_node.line, column: ast_node.column };
+}
diff --git a/tests/ast.js b/tests/ast.js
new file mode 100644
index 0000000..12d1c88
--- /dev/null
+++ b/tests/ast.js
@@ -0,0 +1,11 @@
+// This is a simple test to test global magic
+
+include("../scripts/cleanast.js");
+
+var glob = this;
+const LS = "foobar";
+
+function process_js(ast) {
+ let toplevel = clean_ast(ast);
+ _print(uneval(toplevel));
+}
--
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