[Pkg-mozext-commits] [adblock-plus] 123/464: True AST (i.e., Reflect.parse) support

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:09 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 f0a2d73990cd667104efd9b77d6ed35c7fb8ec40
Author: Joshua Cranmer <Pidgeot18 at gmail.com>
Date:   Tue Jan 11 00:14:13 2011 -0500

    True AST (i.e., Reflect.parse) support
---
 Makefile           |  2 +-
 jshydra.cpp        | 52 +++++++++++++++++++++++++++++++++-------------------
 jshydra_bridge.cpp |  3 +++
 jshydra_funcs.cpp  |  3 ++-
 4 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/Makefile b/Makefile
index b852f61..1a55d31 100644
--- a/Makefile
+++ b/Makefile
@@ -52,6 +52,6 @@ full-check:: jshydra$(BIN_SUFFIX)
 	set -e; \
 	for f in $$(find jstest -name '*.js'); do \
 		echo $$f; \
-		./jshydra$(BIN_SUFFIX) scripts/decompile.js "$(MOZ_SRCDIR)/js/src/tests$${f#jstest}" >$$f; \
+		./jshydra$(BIN_SUFFIX) scripts/astDecompile.js --trueast "$(MOZ_SRCDIR)/js/src/tests$${f#jstest}" >$$f; \
 	done
 	#python jstest/jstests.py --tinderbox fake_js.sh
diff --git a/jshydra.cpp b/jshydra.cpp
index 7562e56..3a4e026 100644
--- a/jshydra.cpp
+++ b/jshydra.cpp
@@ -13,6 +13,8 @@
 
 using namespace js;
 
+bool useReflect = false;
+
 void setIntProperty(JSObject *obj, const char *name, int value) {
     jshydra_defineProperty(cx, obj, name, INT_TO_JSVAL(value));
 }
@@ -161,27 +163,36 @@ JSObject *makeNode(JSParseNode *node) {
 bool parseFile(FILE *file, char *filename, char *argstr) {
     js::Compiler compiler(cx, NULL, NULL);
     // Read in the file into a buffer
-        struct stat st;
-        int ok = fstat(fileno(file), &st);
-        if (ok != 0)
-            return false;
-        jschar *buf = (jschar*)malloc(st.st_size * sizeof(jschar));
-        int i = 0, c;
-        while ((c = getc(file)) != EOF)
-            buf[i++] = (jschar)(unsigned char)c;
-        if (!compiler.init(buf, st.st_size, filename, 1))
-            return false;
-    JSParseNode *root = compiler.parser.parse(globalObj);
-	free(buf);
-    JSObject *ast = makeNode(root);
-    jshydra_rootObject(cx, ast);
+    struct stat st;
+    int ok = fstat(fileno(file), &st);
+    if (ok != 0)
+        return false;
+    jschar *buf = (jschar*)malloc(st.st_size * sizeof(jschar));
+    int i = 0, c;
+    while ((c = getc(file)) != EOF)
+        buf[i++] = (jschar)(unsigned char)c;
+    if (!compiler.init(buf, st.st_size, filename, 1))
+        return false;
+    jsval rval, argv[3];
+    if (useReflect) {
+        JS_GetProperty(cx, globalObj, "Reflect", &rval);
+        JSObject *reflect = JSVAL_TO_OBJECT(rval);
+        JSString *str = JS_NewUCStringCopyN(cx, buf, st.st_size);
+        argv[0] = STRING_TO_JSVAL(str);
+        JS_CallFunctionName(cx, reflect, "parse", 1, argv, &rval);
+        argv[0] = rval;
+    } else {
+        JSParseNode *root = compiler.parser.parse(globalObj);
+        JSObject *ast = makeNode(root);
+        argv[0] = OBJECT_TO_JSVAL(ast);
+        jshydra_rootObject(cx, ast);
+    }
+    free(buf);
     jsval func = jshydra_getToplevelFunction(cx, "process_js");
     if (JS_TypeOfValue(cx, func) != JSTYPE_FUNCTION) {
         fprintf(stderr, "No function process_js!\n");
         return false;
     }
-    jsval rval, argv[3];
-    argv[0] = OBJECT_TO_JSVAL(ast);
     JSString *newfname = JS_NewStringCopyZ(cx, filename);
     argv[1] = STRING_TO_JSVAL(newfname);
     JSString *jsArgStr = JS_NewStringCopyZ(cx, argstr);
@@ -200,7 +211,7 @@ int main(int argc, char **argv) {
     argv++;
     char *argstr = NULL;
 
-  bool failure = false;
+    bool failure = false;
     do {
         argc--;
         argv++;
@@ -209,6 +220,9 @@ int main(int argc, char **argv) {
             argv++;
             argstr = argv[0];
             continue;
+        } else if(!strcmp(argv[0], "--trueast")) {
+            useReflect = true;
+            continue;
         }
         FILE *input = fopen(argv[0], "r");
         if (!input) {
@@ -216,8 +230,8 @@ int main(int argc, char **argv) {
             continue;
         }
         failure |= !parseFile(input, argv[0], argstr);
-    if (failure)
-      fprintf(stderr, "Failure happened on input %s\n", argv[0]);
+        if (failure)
+            fprintf(stderr, "Failure happened on input %s\n", argv[0]);
     } while (argc > 1);
 
     return !!failure;
diff --git a/jshydra_bridge.cpp b/jshydra_bridge.cpp
index f379b80..cc7fdbe 100644
--- a/jshydra_bridge.cpp
+++ b/jshydra_bridge.cpp
@@ -49,6 +49,8 @@ JSClass js_node_class = {
   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 };
 
+extern JSObject *js_InitReflectClass(JSContext *cx, JSObject *obj);
+
 void jshydra_init(const char *file) {
   static JSFunctionSpec shell_functions[] = {
     JS_FN("_print",          Print,          0,     0),
@@ -72,6 +74,7 @@ void jshydra_init(const char *file) {
   globalObj = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
   JS_EnterCrossCompartmentCall(cx, globalObj);
   JS_InitStandardClasses (cx, globalObj);
+  js_InitReflectClass(cx, globalObj);
   /* register error handler */
   JS_SetErrorReporter (cx, ErrorReporter);
   xassert (JS_DefineFunctions (cx, globalObj, shell_functions));
diff --git a/jshydra_funcs.cpp b/jshydra_funcs.cpp
index 29fec40..a96a103 100644
--- a/jshydra_funcs.cpp
+++ b/jshydra_funcs.cpp
@@ -429,6 +429,7 @@ ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
     JS_GetProperty(cx, JSVAL_TO_OBJECT (exn), "stack", &stack);
     if (JS_TypeOfValue (cx, stack) == JSTYPE_STRING) {
       char *str = JS_EncodeString(cx, JSVAL_TO_STRING (stack));
+	  char *save_str = str;
       int counter = 0;
       do {
         char *eol = strchr (str, '\n');
@@ -447,7 +448,7 @@ ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
           break;
         }
       } while (*str);
-      JS_free(cx, str);
+      JS_free(cx, save_str);
     }
   }
   fflush(stderr);

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