You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.0 KiB
103 lines
3.0 KiB
10 years ago
|
|
||
|
/**
|
||
10 years ago
|
* This plugin exposes 'evaluateExpression' method which should be used
|
||
|
* to evaluate natspec description
|
||
10 years ago
|
*/
|
||
|
|
||
10 years ago
|
/// Object which should be used by NatspecExpressionEvaluator
|
||
|
/// abi - abi of the contract that will be used
|
||
|
/// method - name of the method that is called
|
||
|
/// params - input params of the method that will be called
|
||
|
var globals = {
|
||
|
abi: [],
|
||
|
method: "",
|
||
|
params: []
|
||
|
};
|
||
|
|
||
|
/// Helper method
|
||
10 years ago
|
/// Should be called to copy values from object to global context
|
||
10 years ago
|
var copyToContext = function (obj, context) {
|
||
10 years ago
|
var keys = Object.keys(obj);
|
||
|
keys.forEach(function (key) {
|
||
10 years ago
|
context[key] = obj[key];
|
||
10 years ago
|
});
|
||
|
}
|
||
|
|
||
10 years ago
|
/// Helper method
|
||
|
/// Should be called to get method with given name from the abi
|
||
|
/// @param contract's abi
|
||
|
/// @param name of the method that we are looking for
|
||
|
var getMethodWithName = function(abi, name) {
|
||
|
for (var i = 0; i < abi.length; i++) {
|
||
|
if (abi[i].name === name) {
|
||
|
return abi[i];
|
||
|
}
|
||
|
}
|
||
|
//console.warn('could not find method with name: ' + name);
|
||
|
return undefined;
|
||
|
};
|
||
|
|
||
10 years ago
|
/// Function called to get all contract's storage values
|
||
10 years ago
|
/// @returns hashmap with contract properties which are used
|
||
10 years ago
|
/// TODO: check if this function will be used
|
||
10 years ago
|
var getContractProperties = function (address, abi) {
|
||
10 years ago
|
return {};
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
/// Function called to get all contract's methods
|
||
|
/// @returns hashmap with used contract's methods
|
||
10 years ago
|
/// TODO: check if this function will be used
|
||
10 years ago
|
var getContractMethods = function (address, abi) {
|
||
10 years ago
|
//return web3.eth.contract(address, abi); // commented out web3 usage
|
||
|
return {};
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
/// Function called to get all contract method input variables
|
||
|
/// @returns hashmap with all contract's method input variables
|
||
10 years ago
|
var getMethodInputParams = function (method, params) {
|
||
10 years ago
|
return method.inputs.reduce(function (acc, current, index) {
|
||
|
acc[current.name] = params[index];
|
||
|
return acc;
|
||
|
}, {});
|
||
|
};
|
||
|
|
||
10 years ago
|
/// Should be called to evaluate single expression
|
||
|
/// Is internally using javascript's 'eval' method
|
||
|
/// Should be checked if it is safe
|
||
|
var evaluateExpression = function (expression) {
|
||
|
|
||
10 years ago
|
var self = this;
|
||
10 years ago
|
|
||
|
//var storage = getContractProperties(address, abi);
|
||
|
//var methods = getContractMethods(address, abi);
|
||
|
|
||
|
var method = getMethodWithName(globals.abi, globals.method);
|
||
|
if (method) {
|
||
|
var input = getMethodInputParams(method, globals.params);
|
||
|
copyToContext(input, self);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// TODO: test if it is safe
|
||
10 years ago
|
var evaluatedExpression = "";
|
||
|
|
||
|
// match everything in `` quotes
|
||
|
var pattern = /\`(?:\\.|[^`\\])*\`/gim
|
||
|
var match;
|
||
|
var lastIndex = 0;
|
||
|
while ((match = pattern.exec(expression)) !== null) {
|
||
|
var startIndex = pattern.lastIndex - match[0].length;
|
||
|
|
||
|
var toEval = match[0].slice(1, match[0].length - 1);
|
||
|
|
||
|
evaluatedExpression += expression.slice(lastIndex, startIndex);
|
||
|
evaluatedExpression += eval(toEval).toString();
|
||
|
|
||
|
lastIndex = pattern.lastIndex;
|
||
|
}
|
||
|
|
||
|
evaluatedExpression += expression.slice(lastIndex);
|
||
|
|
||
|
return evaluatedExpression;
|
||
10 years ago
|
};
|
||
|
|