You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ra...@apache.org on 2012/01/27 02:37:14 UTC

[7/7] git commit: COUCHDB-111 handle multiple files in couchjs

COUCHDB-111 handle multiple files in couchjs

Rather than concatenating all the input files and compiling them into a
script with the name '<multiple files>', compile each one and execute
it with the same globals.

This change is intended to aide in COUCHDB-111 by preserving file name
and line number information when executing scripts.


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/5f55e9f1
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/5f55e9f1
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/5f55e9f1

Branch: refs/heads/master
Commit: 5f55e9f18397b671cefcf1ce8eef1b787f36c398
Parents: 7309340
Author: Randall Leeds <ra...@apache.org>
Authored: Wed Nov 30 21:28:35 2011 -0800
Committer: Randall Leeds <ra...@apache.org>
Committed: Thu Jan 26 16:14:34 2012 -0800

----------------------------------------------------------------------
 src/couchdb/priv/couch_js/sm170.c |   50 ++++++++++++++++-------------
 src/couchdb/priv/couch_js/sm180.c |   50 ++++++++++++++++-------------
 src/couchdb/priv/couch_js/sm185.c |   54 +++++++++++++++++++-------------
 src/couchdb/priv/couch_js/util.c  |   17 +---------
 src/couchdb/priv/couch_js/util.h  |    7 ++--
 test/javascript/run.tpl           |    6 ++--
 6 files changed, 94 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/src/couchdb/priv/couch_js/sm170.c
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/sm170.c b/src/couchdb/priv/couch_js/sm170.c
index c900b85..796c1d6 100644
--- a/src/couchdb/priv/couch_js/sm170.c
+++ b/src/couchdb/priv/couch_js/sm170.c
@@ -295,6 +295,7 @@ main(int argc, const char* argv[])
     size_t slen;
     jsval sroot;
     jsval result;
+    int i;
 
     couch_args* args = couch_parse_args(argc, argv);
     
@@ -342,32 +343,35 @@ main(int argc, const char* argv[])
         }
     } 
 
-    // Convert script source to jschars.
-    scriptsrc = dec_string(cx, args->script, strlen(args->script));
-    if(!scriptsrc)
-        return 1;
+    for (i = 0 ; args->scripts[i] ; i++) {
+        // Convert script source to jschars.
+        scriptsrc = couch_readfile(cx, args->scripts[i]);
+        if(!scriptsrc)
+            return 1;
 
-    schars = JS_GetStringChars(scriptsrc);
-    slen = JS_GetStringLength(scriptsrc);
-    
-    // Root it so GC doesn't collect it.
-    sroot = STRING_TO_JSVAL(scriptsrc); 
-    if(JS_AddRoot(cx, &sroot) != JS_TRUE) {
-        fprintf(stderr, "Internal root error.\n");
-        return 1;
-    }
+        schars = JS_GetStringChars(scriptsrc);
+        slen = JS_GetStringLength(scriptsrc);
 
-    // Compile and run
-    script = JS_CompileUCScript(cx, global, schars, slen, args->script_name, 1);
-    if(!script) {
-        fprintf(stderr, "Failed to compile script.\n");
-        return 1;
+        // Root it so GC doesn't collect it.
+        sroot = STRING_TO_JSVAL(scriptsrc);
+        if(JS_AddRoot(cx, &sroot) != JS_TRUE) {
+            fprintf(stderr, "Internal root error.\n");
+            return 1;
+        }
+
+        // Compile and run
+        script = JS_CompileUCScript(cx, global, schars, slen,
+                                    args->scripts[i], 1);
+        if(!script) {
+            fprintf(stderr, "Failed to compile script.\n");
+            return 1;
+        }
+
+        JS_ExecuteScript(cx, global, script, &result);
+
+        // Warning message if we don't remove it.
+        JS_RemoveRoot(cx, &sroot);
     }
-    
-    JS_ExecuteScript(cx, global, script, &result);
-    
-    // Warning message if we don't remove it.
-    JS_RemoveRoot(cx, &sroot);
 
     FINISH_REQUEST(cx);
     JS_DestroyContext(cx);

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/src/couchdb/priv/couch_js/sm180.c
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/sm180.c b/src/couchdb/priv/couch_js/sm180.c
index dee16a7..4d1bbf9 100644
--- a/src/couchdb/priv/couch_js/sm180.c
+++ b/src/couchdb/priv/couch_js/sm180.c
@@ -304,6 +304,7 @@ main(int argc, const char* argv[])
     size_t slen;
     jsval sroot;
     jsval result;
+    int i;
 
     couch_args* args = couch_parse_args(argc, argv);
     
@@ -351,32 +352,35 @@ main(int argc, const char* argv[])
         }
     } 
 
-    // Convert script source to jschars.
-    scriptsrc = dec_string(cx, args->script, strlen(args->script));
-    if(!scriptsrc)
-        return 1;
+    for (i = 0 ; args->scripts[i] ; i++) {
+        // Convert script source to jschars.
+        scriptsrc = couch_readfile(cx, args->scripts[i]);
+        if(!scriptsrc)
+            return 1;
 
-    schars = JS_GetStringChars(scriptsrc);
-    slen = JS_GetStringLength(scriptsrc);
-    
-    // Root it so GC doesn't collect it.
-    sroot = STRING_TO_JSVAL(scriptsrc); 
-    if(JS_AddRoot(cx, &sroot) != JS_TRUE) {
-        fprintf(stderr, "Internal root error.\n");
-        return 1;
-    }
+        schars = JS_GetStringChars(scriptsrc);
+        slen = JS_GetStringLength(scriptsrc);
 
-    // Compile and run
-    script = JS_CompileUCScript(cx, global, schars, slen, args->script_name, 1);
-    if(!script) {
-        fprintf(stderr, "Failed to compile script.\n");
-        return 1;
-    }
-    
-    JS_ExecuteScript(cx, global, script, &result);
+        // Root it so GC doesn't collect it.
+        sroot = STRING_TO_JSVAL(scriptsrc);
+        if(JS_AddRoot(cx, &sroot) != JS_TRUE) {
+            fprintf(stderr, "Internal root error.\n");
+            return 1;
+        }
+
+        // Compile and run
+        script = JS_CompileUCScript(cx, global, schars, slen,
+                                    args->scripts[i], 1);
+        if(!script) {
+            fprintf(stderr, "Failed to compile script.\n");
+            return 1;
+        }
+
+        JS_ExecuteScript(cx, global, script, &result);
 
-    // Warning message if we don't remove it.
-    JS_RemoveRoot(cx, &sroot);
+        // Warning message if we don't remove it.
+        JS_RemoveRoot(cx, &sroot);
+    }
 
     FINISH_REQUEST(cx);
     JS_DestroyContext(cx);

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/src/couchdb/priv/couch_js/sm185.c
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/sm185.c b/src/couchdb/priv/couch_js/sm185.c
index 6a4522e..8c4e536 100644
--- a/src/couchdb/priv/couch_js/sm185.c
+++ b/src/couchdb/priv/couch_js/sm185.c
@@ -316,6 +316,7 @@ main(int argc, const char* argv[])
     size_t slen;
     jsval sroot;
     jsval result;
+    int i;
 
     couch_args* args = couch_parse_args(argc, argv);
 
@@ -365,31 +366,40 @@ main(int argc, const char* argv[])
         }
     } 
 
-    // Convert script source to jschars.
-    scriptsrc = dec_string(cx, args->script, strlen(args->script));
-    if(!scriptsrc)
-        return 1;
+    for(i = 0 ; args->scripts[i] ; i++) {
+        // Convert script source to jschars.
+        scriptsrc = couch_readfile(cx, args->scripts[i]);
+        if(!scriptsrc)
+            return 1;
 
-    schars = JS_GetStringCharsAndLength(cx, scriptsrc, &slen);
-    
-    // Root it so GC doesn't collect it.
-    sroot = STRING_TO_JSVAL(scriptsrc); 
-    if(JS_AddValueRoot(cx, &sroot) != JS_TRUE) {
-        fprintf(stderr, "Internal root error.\n");
-        return 1;
-    }
+        schars = JS_GetStringCharsAndLength(cx, scriptsrc, &slen);
 
-    // Compile and run
-    script = JS_CompileUCScript(cx, global, schars, slen, args->script_name, 1);
-    if(!script) {
-        fprintf(stderr, "Failed to compile script.\n");
-        return 1;
-    }
-    
-    JS_ExecuteScript(cx, global, script, &result);
+        // Root it so GC doesn't collect it.
+        sroot = STRING_TO_JSVAL(scriptsrc);
+        if(JS_AddValueRoot(cx, &sroot) != JS_TRUE) {
+            fprintf(stderr, "Internal root error.\n");
+            return 1;
+        }
+
+        // Compile and run
+        script = JS_CompileUCScript(cx, global, schars, slen,
+                                    args->scripts[i], 1);
+        if(!script) {
+            fprintf(stderr, "Failed to compile script.\n");
+            return 1;
+        }
 
-    // Warning message if we don't remove it.
-    JS_RemoveValueRoot(cx, &sroot);
+        if(JS_ExecuteScript(cx, global, script, &result) != JS_TRUE) {
+            fprintf(stderr, "Failed to execute script.\n");
+            return 1;
+        }
+
+        // Warning message if we don't remove it.
+        JS_RemoveValueRoot(cx, &sroot);
+
+        // Give the GC a chance to run.
+        JS_MaybeGC(cx);
+    }
 
     JS_LeaveCrossCompartmentCall(call);
     FINISH_REQUEST(cx);

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/src/couchdb/priv/couch_js/util.c
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c
index d95151c..0b1e92a 100644
--- a/src/couchdb/priv/couch_js/util.c
+++ b/src/couchdb/priv/couch_js/util.c
@@ -103,24 +103,11 @@ couch_parse_args(int argc, const char* argv[])
         i++;
     }
 
-    while(i < argc) {
-        slurp_file(argv[i], &args->script);
-        if(args->script_name == NULL) {
-            if(strcmp(argv[i], "-") == 0) {
-                args->script_name = "<stdin>";
-            } else {
-                args->script_name = argv[i];
-            }
-        } else {
-            args->script_name = "<multiple_files>";
-        }
-        i++;
-    }
-
-    if(args->script_name == NULL || args->script == NULL) {
+    if(i >= argc) {
         DISPLAY_USAGE;
         exit(3);
     }
+    args->scripts = argv + i;
 
     return args;
 }

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/src/couchdb/priv/couch_js/util.h
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/util.h b/src/couchdb/priv/couch_js/util.h
index d58f276..1319e62 100644
--- a/src/couchdb/priv/couch_js/util.h
+++ b/src/couchdb/priv/couch_js/util.h
@@ -16,10 +16,9 @@
 #include <jsapi.h>
 
 typedef struct {
-    int         use_http;
-    int         stack_size;
-    const char* script_name;
-    char*       script;
+    int          use_http;
+    int          stack_size;
+    const char** scripts;
 } couch_args;
 
 couch_args* couch_parse_args(int argc, const char* argv[]);

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5f55e9f1/test/javascript/run.tpl
----------------------------------------------------------------------
diff --git a/test/javascript/run.tpl b/test/javascript/run.tpl
index cae5a09..35ff531 100644
--- a/test/javascript/run.tpl
+++ b/test/javascript/run.tpl
@@ -52,7 +52,8 @@ if [ -z $COUCHDB_NO_START ]; then
 	sleep 1 # give it a sec
 fi
 
-cat $SCRIPT_DIR/json2.js \
+$COUCHJS -H \
+	$SCRIPT_DIR/json2.js \
 	$SCRIPT_DIR/sha1.js \
 	$SCRIPT_DIR/oauth.js \
 	$SCRIPT_DIR/couch.js \
@@ -60,8 +61,7 @@ cat $SCRIPT_DIR/json2.js \
 	$SCRIPT_DIR/couch_tests.js \
 	$TEST_SRC \
 	$JS_TEST_DIR/couch_http.js \
-	$JS_TEST_DIR/cli_runner.js \
-    | $COUCHJS -H -
+	$JS_TEST_DIR/cli_runner.js
 
 if [ -z $COUCHDB_NO_START ]; then
 	# stop CouchDB