From da8136892be9991c07d1da7966dc44881f349cb0 Mon Sep 17 00:00:00 2001 From: deuce <> Date: Sat, 16 Mar 2013 08:50:24 +0000 Subject: [PATCH] Add a flatten() command which takes a string as an argument. This is to deal with the fact that when you concatenate JS strings, the JS engine simply notes that they are concatenated, but keeps all the original strings around until something flattens them. This code: for(;;) { str += ascii(random(256)); log(str.length); } Fails when str reaches about 256KiB then crashes with out of memory but this code: for(;;) { str += ascii(random(256)); log(str.length); } Reaches a size of 4MB without stopping. Any time you actually paw through the string, it gets flattened, but when you do a bunch of concatenation, manually flattening can be a Good Thing. --- src/sbbs3/js_global.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/sbbs3/js_global.c b/src/sbbs3/js_global.c index 5a160733aa..792e0a3021 100644 --- a/src/sbbs3/js_global.c +++ b/src/sbbs3/js_global.c @@ -3695,6 +3695,19 @@ static JSBool js_getsize(JSContext *cx, uintN argc, jsval *arglist) return(JS_TRUE); } +static JSBool js_flatten(JSContext *cx, uintN argc, jsval *arglist) +{ + jsval *argv=JS_ARGV(cx, arglist); + + if(!JSVAL_IS_STRING(argv[0])) { + JS_ReportError(cx, "get_size() error! Parameter is not a string."); + return(JS_FALSE); + } + JS_FlattenString(cx, JSVAL_TO_STRING(argv[0])); + JS_SET_RVAL(cx, arglist, JSVAL_VOID); + return(JS_TRUE); +} + static JSBool js_flags_str(JSContext *cx, uintN argc, jsval *arglist) @@ -4045,7 +4058,11 @@ static jsSyncMethodSpec js_global_functions[] = { }, {"get_size", js_getsize, 1, JSTYPE_NUMBER, JSDOCSTR("[number]") ,JSDOCSTR("Gets the size in bytes the object uses in memory (forces GC) ") - ,314 + ,315 + }, + {"flatten", js_flatten, 1, JSTYPE_NUMBER, JSDOCSTR("[null]") + ,JSDOCSTR("Flattens a string ") + ,315 }, {0} }; -- GitLab