From 3c4555f3cf27cbb62be8e6a817060ffc5333cf08 Mon Sep 17 00:00:00 2001 From: wohali Date: Thu, 15 Nov 2012 13:20:10 -0500 Subject: Update couchjs for static build option, eliminate eval() --- couchjs/js/loop.js | 9 ++++++--- couchjs/js/render.js | 2 +- couchjs/js/state.js | 6 +++++- couchjs/js/util.js | 40 ++++++++++++++++++++-------------------- couchjs/js/views.js | 12 ++++++------ 5 files changed, 38 insertions(+), 31 deletions(-) (limited to 'couchjs/js') diff --git a/couchjs/js/loop.js b/couchjs/js/loop.js index d2a07f61..af4d65df 100644 --- a/couchjs/js/loop.js +++ b/couchjs/js/loop.js @@ -19,7 +19,7 @@ function init_sandbox() { sandbox.emit = Views.emit; sandbox.sum = Views.sum; sandbox.log = log; - sandbox.toJSON = Couch.toJSON; + sandbox.toJSON = JSON.stringify; sandbox.JSON = JSON; sandbox.provides = Mime.provides; sandbox.registerType = Mime.registerType; @@ -27,6 +27,7 @@ function init_sandbox() { sandbox.send = Render.send; sandbox.getRow = Render.getRow; sandbox.isArray = isArray; + sandbox.index = Dreyfus.index; } catch (e) { log(e.toSource()); } @@ -100,11 +101,13 @@ var Loop = function() { "ddoc" : DDoc.ddoc, // "view" : Views.handler, "reset" : State.reset, + "add_att" : State.addAtt, "add_fun" : State.addFun, "add_lib" : State.addLib, "map_doc" : Views.mapDoc, "reduce" : Views.reduce, - "rereduce" : Views.rereduce + "rereduce" : Views.rereduce, + "index_doc": Dreyfus.indexDoc }; function handleError(e) { var type = e[0]; @@ -122,7 +125,7 @@ var Loop = function() { } }; while (line = readline()) { - cmd = eval('('+line+')'); + cmd = JSON.parse(line); State.line_length = line.length; try { cmdkey = cmd.shift(); diff --git a/couchjs/js/render.js b/couchjs/js/render.js index 93ff6332..0dcefb08 100644 --- a/couchjs/js/render.js +++ b/couchjs/js/render.js @@ -174,7 +174,7 @@ var Render = (function() { blowChunks(); } var line = readline(); - var json = eval('('+line+')'); + var json = JSON.parse(line); if (json[0] == "list_end") { lastRow = true; return null; diff --git a/couchjs/js/state.js b/couchjs/js/state.js index e6416382..3c075294 100644 --- a/couchjs/js/state.js +++ b/couchjs/js/state.js @@ -22,7 +22,11 @@ var State = { }, addFun : function(newFun) { // Compile to a function and add it to funs array - State.funs.push(Couch.compileFunction(newFun, {views : {lib : State.lib}})); + State.funs.push(Couch.compileFunction(newFun, {views : {lib : State.lib}})); + print("true"); + }, + addAtt: function(name, len, md5) { + // Decline attachments. print("true"); }, addLib : function(lib) { diff --git a/couchjs/js/util.js b/couchjs/js/util.js index 0b812fe1..2e67ca19 100644 --- a/couchjs/js/util.js +++ b/couchjs/js/util.js @@ -70,25 +70,25 @@ var Couch = { ddoc._module_cache = {}; } var require = function(name, module) { - module = module || {}; - var newModule = resolveModule(name.split('/'), module.parent, ddoc); - if (!ddoc._module_cache.hasOwnProperty(newModule.id)) { - // create empty exports object before executing the module, - // stops circular requires from filling the stack - ddoc._module_cache[newModule.id] = {}; - var s = "function (module, exports, require) { " + newModule.current + " }"; - try { - var func = sandbox ? evalcx(s, sandbox) : eval(s); - func.apply(sandbox, [newModule, newModule.exports, function(name) { - return require(name, newModule); - }]); - } catch(e) { - throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()]; + module = module || {}; + var newModule = resolveModule(name.split('/'), module.parent, ddoc); + if (!ddoc._module_cache.hasOwnProperty(newModule.id)) { + // create empty exports object before executing the module, + // stops circular requires from filling the stack + ddoc._module_cache[newModule.id] = {}; + var s = "function (module, exports, require) { " + newModule.current + " }"; + try { + var func = sandbox ? evalcx(s, sandbox) : eval(s); + func.apply(sandbox, [newModule, newModule.exports, function(name) { + return require(name, newModule); + }]); + } catch(e) { + throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()]; + } + ddoc._module_cache[newModule.id] = newModule.exports; } - ddoc._module_cache[newModule.id] = newModule.exports; + return ddoc._module_cache[newModule.id]; } - return ddoc._module_cache[newModule.id]; - } sandbox.require = require; } var functionObject = evalcx(source, sandbox); @@ -121,10 +121,10 @@ var Couch = { } } -// prints the object as JSON, and rescues and logs any toJSON() related errors +// prints the object as JSON, and rescues and logs any JSON.stringify() related errors function respond(obj) { try { - print(Couch.toJSON(obj)); + print(JSON.stringify(obj)); } catch(e) { log("Error converting object to JSON: " + e.toString()); log("error on obj: "+ obj.toSource()); @@ -136,7 +136,7 @@ function log(message) { if (typeof message == "xml") { message = message.toXMLString(); } else if (typeof message != "string") { - message = Couch.toJSON(message); + message = JSON.stringify(message); } respond(["log", String(message)]); }; diff --git a/couchjs/js/views.js b/couchjs/js/views.js index 2a15ee56..f7357b79 100644 --- a/couchjs/js/views.js +++ b/couchjs/js/views.js @@ -30,7 +30,7 @@ var Views = (function() { reductions[i] = null; } }; - var reduce_line = Couch.toJSON(reductions); + var reduce_line = JSON.stringify(reductions); var reduce_length = reduce_line.length; // TODO make reduce_limit config into a number if (State.query_config && State.query_config.reduce_limit && @@ -108,19 +108,19 @@ var Views = (function() { Couch.recursivelySeal(doc); var buf = []; - for (var i = 0; i < State.funs.length; i++) { + for each (fun in State.funs) { map_results = []; try { - State.funs[i](doc); - buf.push(Couch.toJSON(map_results)); + fun(doc); + buf.push(map_results); } catch (err) { handleViewError(err, doc); // If the error is not fatal, we treat the doc as if it // did not emit anything, by buffering an empty array. - buf.push("[]"); + buf.push([]); } } - print("[" + buf.join(", ") + "]"); + print(JSON.stringify(buf)); } } })(); -- cgit v1.2.3