Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update couchjs for static build option, eliminate eval()

  • Loading branch information...
commit 3c4555f3cf27cbb62be8e6a817060ffc5333cf08 1 parent 7af6efe
wohali authored
View
3  .gitignore
@@ -28,4 +28,5 @@ apps/couch/test/temp*
# dev
rel/dev*
-rel/tmpdata
+rel/tmpdata
+/.project
View
51 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
View
9 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();
View
2  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;
View
6 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) {
View
40 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)]);
};
View
12 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));
}
}
})();
Please sign in to comment.
Something went wrong with that request. Please try again.