From 3a08b41f8c2a97eee0904b6359e559ed76dfc9fd Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Tue, 20 May 2008 20:15:05 +0000 Subject: Some javascript code cleanup. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@658409 13f79535-47bb-0310-9956-ffa450edef68 --- share/server/main.js | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'share/server') diff --git a/share/server/main.js b/share/server/main.js index 59acb02f..43660c7b 100644 --- a/share/server/main.js +++ b/share/server/main.js @@ -17,17 +17,16 @@ var map_results = []; // holds temporary emitted values during doc map var sandbox = null; emit = function(key, value) { - map_results.push([key, value]); - } - + map_results.push([key, value]); +} + sum = function(values) { - var values_sum=0; - for(var i in values) { - values_sum += values[i]; - } - return values_sum; + var rv = 0; + for (var i in values) { + rv += values[i]; } - + return rv; +} try { // if possible, use evalcx (not always available) @@ -53,8 +52,8 @@ while (cmd = eval(readline())) { case "add_fun": // The second arg is a string that will compile to a function. // and then we add it to funs array - funs.push(safe_compile_function(cmd[1])); - print("true"); + funs.push(compileFunction(cmd[1])); + print("true"); break; case "map_doc": // The second arg is a document. We compute all the map functions against @@ -97,10 +96,10 @@ while (cmd = eval(readline())) { } print(toJSON(buf)); break; - + case "combine": case "reduce": - { + { var keys = null; var values = null; var reduceFuns = cmd[1]; @@ -117,11 +116,11 @@ while (cmd = eval(readline())) { values = cmd[2]; is_combine = true; } - - for(var i in reduceFuns) { - reduceFuns[i] = safe_compile_function(reduceFuns[i]); + + for (var i in reduceFuns) { + reduceFuns[i] = compileFunction(reduceFuns[i]); } - + var reductions = new Array(funs.length); for (var i = 0; i < reduceFuns.length; i++) { try { @@ -138,30 +137,30 @@ while (cmd = eval(readline())) { print("[true," + toJSON(reductions) + "]"); } break; - + default: print(toJSON({error: "query_server_error", reason: "unknown command '" + cmd[0] + "'"})); quit(); } - } catch(exception) { + } catch (exception) { print(toJSON(exception)); } } -function safe_compile_function(Src) { +function compileFunction(source) { try { - var functionObject = sandbox ? evalcx(Src, sandbox) : eval(Src); + var functionObject = sandbox ? evalcx(source, sandbox) : eval(source); } catch (err) { throw {error: "compilation_error", - reason: err.toString() + " (" + Src + ")"}; + reason: err.toString() + " (" + source + ")"}; } if (typeof(functionObject) == "function") { return functionObject; } else { throw {error: "compilation_error", - reason: "expression does not eval to a function. (" + Src + ")"}; + reason: "expression does not eval to a function. (" + source + ")"}; } } -- cgit v1.2.3