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() --- .gitignore | 3 ++- couchjs/c_src/SConscript | 51 ++++++++++++++++++++++++++++-------------------- 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 ++++++------ 7 files changed, 70 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index beafc50b..56d81888 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,5 @@ apps/couch/test/temp* # dev rel/dev* -rel/tmpdata \ No newline at end of file +rel/tmpdata +/.project diff --git a/couchjs/c_src/SConscript b/couchjs/c_src/SConscript index ea78c587..8529ad4d 100644 --- a/couchjs/c_src/SConscript +++ b/couchjs/c_src/SConscript @@ -11,8 +11,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. -import commands import os +import commands import platform def require_lib(name): @@ -23,13 +23,19 @@ def require_lib(name): def runcmd(cmd): return commands.getstatusoutput(cmd) +# support --static for static builds +AddOption('--static', dest='static', action="store_true", default=False) +# support --nocurl to build without curl +AddOption('--nocurl', dest='usecurl', action="store_false", default=True) + env = Environment(CC="c++", CCFLAGS='-g -O2 -DXP_UNIX', CPPPATH=os.getenv("CPPPATH")) -if os.uname()[0] == 'Linux': - platlibpath = "/usr/lib/%s-linux-gnu" % platform.machine() - if os.path.exists(platlibpath): - env.Append(LINKFLAGS="-L%s" % platlibpath) +# possibly attempt a static build of couchjs +# warning! requires a libmozjs with --disable-threads! +if GetOption('static'): + env.Append(LINKFLAGS=['-static', '-static-libgcc', + '-static-libstdc++']) if os.uname()[0] == 'SunOS': env['CC'] = '/usr/sfw/bin/gcc' @@ -43,7 +49,7 @@ if os.uname()[0] == 'FreeBSD': env['LIB_COMPAT'] = 'compat' if os.path.exists('/usr/bin/pkg-config'): - for pkg in ["mozilla-js-185", "mozilla-js"]: + for pkg in ["mozilla-js-185", "mozilla-js", "mozjs185"]: (s1, output) = runcmd("/usr/bin/pkg-config %s --cflags" % pkg) if s1 == 0: env.Append(CCFLAGS=" " + output) @@ -56,10 +62,6 @@ if os.path.exists('/usr/bin/pkg-config'): if not env.GetOption('clean'): conf = Configure(env, config_h='config.h') - require_lib('m') - require_lib('pthread') - require_lib('nspr4') - ## check for SpiderMonkey development header if conf.CheckHeader('mozjs/jsapi.h'): jsapi = 'mozjs/jsapi.h' @@ -74,9 +76,9 @@ if not env.GetOption('clean'): ## check for SpiderMonkey library as libjs or libmozjs if not conf.CheckLibWithHeader('mozjs185-1.0', jsapi, 'c', autoadd=1): - if not conf.CheckLibWithHeader('mozjs', jsapi, 'c', autoadd=1): - if not conf.CheckLibWithHeader('js', jsapi, 'c', autoadd=1): - if not conf.CheckLibWithHeader('mozjs185', jsapi, 'c', autoadd=1): + if not conf.CheckLibWithHeader('mozjs185', jsapi, 'c', autoadd=1): + if not conf.CheckLibWithHeader('mozjs', jsapi, 'c', autoadd=1): + if not conf.CheckLibWithHeader('js', jsapi, 'c', autoadd=1): print 'Could not find JS library.', \ 'Is Mozilla SpiderMonkey installed?' Exit(1) @@ -101,14 +103,21 @@ if not env.GetOption('clean'): conf.Define("JSSCRIPT_TYPE", "JSObject*") ## Check if curl is available - try: - vsn = runcmd("curl-config --version")[1] - vsn = vsn.split()[-1].strip().split(".") - vsn = tuple(map(int, vsn)) - if vsn > (7, 18, 0): - require_lib('curl') - except: - pass + if GetOption('usecurl'): + try: + vsn = runcmd("curl-config --version")[1] + vsn = vsn.split()[-1].strip().split(".") + vsn = tuple(map(int, vsn)) + if vsn > (7, 18, 0): + require_lib('curl') + except: + pass + + ## Must be specified after js/curl in this order for a static build + if not GetOption('static'): + require_lib('nspr4') + require_lib('pthread') + require_lib('m') ## Define properties for -h / -V 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