octo-deno/lib/utils/script.js

109 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-04-22 22:49:49 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
utils.script = {
methodHead: function (list, numOptional) {
if (isNaN(numOptional)) {
numOptional = 0
2017-04-22 22:49:49 +00:00
}
2017-05-02 14:01:32 +00:00
let arity = list.length - random.number(numOptional)
let params = []
for (let i = 0; i < arity; i++) {
params.push(random.pick([list[i]]))
2017-04-22 22:49:49 +00:00
}
return '(' + params.join(', ') + ')'
2017-04-22 22:49:49 +00:00
},
methodCall: function (objectName, methodHash) {
if (!utils.common.getKeysFromHash(methodHash).length || !objectName) {
return ''
2017-04-22 22:49:49 +00:00
}
2017-05-02 14:01:32 +00:00
let methodName = random.key(methodHash)
let methodArgs = methodHash[methodName]
if (typeof (methodArgs) === 'function') { // Todo: Hmmmm..
return methodArgs()
2017-04-22 22:49:49 +00:00
}
return objectName + '.' + methodName + utils.script.methodHead(methodArgs)
2017-04-22 22:49:49 +00:00
},
setAttribute: function (objectName, attributeHash) {
if (!utils.common.getKeysFromHash(attributeHash).length || !objectName) {
return ''
2017-04-22 22:49:49 +00:00
}
2017-05-02 14:01:32 +00:00
let attributeName = random.key(attributeHash)
let attributeValue = random.pick(attributeHash[attributeName])
let operator = ' = '
2017-04-22 22:49:49 +00:00
/*
if (typeof(attributeValue) == "number" && Random.chance(8)) {
operator = " " + Make.randomAssignmentOperator() + " ";
}
if (typeof(attributeValue) == "string") {
attributeValue = "'" + attributeValue + "'";
}
*/
return objectName + '.' + attributeName + operator + attributeValue + ';'
2017-04-22 22:49:49 +00:00
},
makeConstraint: function (keys, values) {
2017-05-02 14:01:32 +00:00
let o = {}
let n = random.range(0, keys.length)
2017-04-22 22:49:49 +00:00
while (n--) {
o[random.pick(keys)] = random.pick(values)
2017-04-22 22:49:49 +00:00
}
return o
2017-04-22 22:49:49 +00:00
},
makeRandomOptions: function (baseObject) {
2017-05-02 14:01:32 +00:00
let o = {}
let unique = random.some(Object.keys(baseObject))
for (let i = 0; i < unique.length; i++) {
o[unique[i]] = random.pick(baseObject[unique[i]])
2017-04-22 22:49:49 +00:00
}
return JSON.stringify(o)
2017-04-22 22:49:49 +00:00
},
safely: function (s) {
if (window.debug) {
return 'try { ' + s + ' } catch(e) { logger.JSError(e); }'
2017-04-22 22:49:49 +00:00
}
return 'try { ' + s + ' } catch(e) { }'
2017-04-22 22:49:49 +00:00
},
makeLoop: function (s, max) {
2017-05-02 14:01:32 +00:00
return 'for (let i = 0; i < ' + (max || make.number.rangeNumber()) + '; i++) {' + s + '}'
2017-04-22 22:49:49 +00:00
},
makeArray: function (type, arrayLength, cb) {
2017-04-25 15:22:15 +00:00
if (type === null || type === undefined) {
type = random.index(['Uint8', 'Float32'])
2017-04-22 22:49:49 +00:00
}
switch (random.number(8)) {
case 0:
2017-05-02 14:01:32 +00:00
let src = 'function() { let buffer = new ' + type + 'Array(' + arrayLength + ');'
src += utils.script.makeLoop('buffer[i] = ' + cb() + ';', arrayLength)
src += 'return buffer;}()'
return src
2017-04-22 22:49:49 +00:00
case 1:
return 'new ' + type + 'Array([' + make.arrays.filledArray(cb, arrayLength) + '])'
2017-04-22 22:49:49 +00:00
default:
return 'new ' + type + 'Array(' + arrayLength + ')'
2017-04-22 22:49:49 +00:00
}
},
randListIndex: function (objName) {
return random.number() + ' % ' + o.pick(objName) + '.length'
2017-04-22 22:49:49 +00:00
},
addElementToBody: function (name) {
return '(document.body || document.documentElement).appendChild' + utils.script.methodHead([name])
2017-04-22 22:49:49 +00:00
},
forceGC: function () {
2017-04-26 14:14:19 +00:00
if (utils.platform.isMozilla) {
2017-04-22 22:49:49 +00:00
}
2017-04-26 14:14:19 +00:00
if (utils.platform.isChrome) {
if (window.GCController) {
return GCController.collect() // eslint-disable-line no-undef
}
2017-04-22 22:49:49 +00:00
}
2017-04-26 14:14:19 +00:00
if (utils.platform.isSafari) {
2017-04-22 22:49:49 +00:00
}
2017-04-26 14:14:19 +00:00
if (utils.platform.isIE) {
2017-04-22 22:49:49 +00:00
}
},
getRandomElement: function () {
return 'document.getElementsByTagName(\'*\')[' + random.number(document.getElementsByTagName('*').length) + ']'
2017-04-22 22:49:49 +00:00
}
}