octo-deno/utils/script.js

100 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-04-13 13:20:04 +00:00
utils.script = {
methodHead: function (list, numOptional) {
if (isNaN(numOptional)) {
numOptional = 0;
}
2017-04-22 19:02:38 +00:00
var arity = list.length - random.number(numOptional);
2017-04-13 13:20:04 +00:00
var params = [];
for (var i = 0; i < arity; i++) {
2017-04-22 19:02:38 +00:00
params.push(random.pick([list[i]]));
2017-04-13 13:20:04 +00:00
}
return "(" + params.join(", ") + ")";
},
methodCall: function (objectName, methodHash) {
2017-04-22 19:02:38 +00:00
if(!utils.common.getKeysFromHash(methodHash).length || !objectName) {
2017-04-13 13:20:04 +00:00
return "";
}
2017-04-22 19:02:38 +00:00
var methodName = random.key(methodHash);
2017-04-13 13:20:04 +00:00
var methodArgs = methodHash[methodName];
if (typeof(methodArgs) == "function") { // Todo: Hmmmm..
return methodArgs();
}
2017-04-22 19:02:38 +00:00
return objectName + "." + methodName + utils.script..methodHead(methodArgs);
2017-04-13 13:20:04 +00:00
},
setAttribute: function (objectName, attributeHash) {
2017-04-22 19:02:38 +00:00
if(!utils.common.getKeysFromHash(attributeHash).length || !objectName) {
2017-04-13 13:20:04 +00:00
return "";
}
2017-04-22 19:02:38 +00:00
var attributeName = random.key(attributeHash);
var attributeValue = random.pick(attributeHash[attributeName]);
2017-04-13 13:20:04 +00:00
var operator = " = ";
/*
if (typeof(attributeValue) == "number" && Random.chance(8)) {
operator = " " + Make.randomAssignmentOperator() + " ";
}
if (typeof(attributeValue) == "string") {
attributeValue = "'" + attributeValue + "'";
}
*/
return objectName + "." + attributeName + operator + attributeValue + ";";
},
makeConstraint: function (keys, values) {
var o = {};
2017-04-22 19:02:38 +00:00
var n = random.range(0, keys.length);
2017-04-13 13:20:04 +00:00
while (n--) {
2017-04-22 19:02:38 +00:00
o[random.pick(keys)] = random.pick(values);
2017-04-13 13:20:04 +00:00
}
return o;
},
makeRandomOptions: function (base_o) {
2017-04-22 19:02:38 +00:00
var o = {}, unique = random.some(Object.keys(base_o));
2017-04-13 13:20:04 +00:00
for (var i = 0; i < unique.length; i++) {
2017-04-22 19:02:38 +00:00
o[unique[i]] = random.pick(base_o[unique[i]]);
2017-04-13 13:20:04 +00:00
}
return JSON.stringify(o);
},
safely: function (s) {
if (window.debug) {
return "try { " + s + " } catch(e) { Logger.JSError(e); }";
}
return "try { " + s + " } catch(e) { }";
},
makeLoop: function (s, max) {
2017-04-22 19:02:38 +00:00
return "for (var i = 0; i < " + (max || make.numbers.rangeNumber()) + "; i++) {" + s + "}";
2017-04-13 13:20:04 +00:00
},
makeArray: function (type, arrayLength, cb) {
if (type == null || type === undefined) {
2017-04-22 19:02:38 +00:00
type = random.index(["Uint8", "Float32"]);
2017-04-13 13:20:04 +00:00
}
2017-04-22 19:02:38 +00:00
switch (random.number(8)) {
2017-04-13 13:20:04 +00:00
case 0:
var src = "function() { var buffer = new " + type + "Array(" + arrayLength + ");";
2017-04-22 19:02:38 +00:00
src += utils.script.makeLoop("buffer[i] = " + cb() + ";", arrayLength);
2017-04-13 13:20:04 +00:00
src += "return buffer;}()";
return src;
case 1:
2017-04-22 19:02:38 +00:00
return "new " + type + "Array([" + make.arrays.filledArray(cb, arrayLength) + "])";
2017-04-13 13:20:04 +00:00
default:
return "new " + type + "Array(" + arrayLength + ")";
}
},
randListIndex: function (objName) {
2017-04-22 19:02:38 +00:00
return random.number() + ' % ' + o.pick(objName) + '.length';
2017-04-13 13:20:04 +00:00
},
addElementToBody: function (name) {
2017-04-22 19:02:38 +00:00
return "(document.body || document.documentElement).appendChild" + utils.script..methodHead([name]);
2017-04-13 13:20:04 +00:00
},
forceGC: function () {
2017-04-22 19:02:38 +00:00
if (platform.isMozilla) {}
if (platform.isChrome) {
2017-04-13 13:20:04 +00:00
if (window.GCController)
return GCController.collect();
}
2017-04-22 19:02:38 +00:00
if (platform.isSafari) {}
if (platform.isIE) {}
2017-04-13 13:20:04 +00:00
},
getRandomElement: function() {
2017-04-22 19:02:38 +00:00
return "document.getElementsByTagName('*')[" + random.number(document.getElementsByTagName("*").length) + "]";
2017-04-13 13:20:04 +00:00
}
};