code comments

This commit is contained in:
Marek Kotewicz 2015-03-07 20:06:26 +01:00
parent b8505d1761
commit 60ae274f34
3 changed files with 64 additions and 24 deletions

42
dist/ethereum.js vendored
View File

@ -27,29 +27,43 @@ var types = require('./types');
var c = require('./config');
var f = require('./formatters');
/**
* Displays error about incorrect type
* @param {String} type
*/
var displayTypeError = function (type) {
console.error('parser does not support type: ' + type);
};
/// This method should be called if we want to check if givent type is an array type
/// @returns true if it is, otherwise false
var arrayType = function (type) {
/** This method should be called if we want to check if givent type is an array type
* @param {String} type name
* @returns {Boolean} true if it is, otherwise false
*/
var isArrayType = function (type) {
return type.slice(-2) === '[]';
};
/**
* This method should be called to return dynamic type length in hex
* @param {String} type
* @param {String|Array} dynamic type
* @return {String} length of dynamic type in hex or empty string if type is not dynamic
*/
var dynamicTypeBytes = function (type, value) {
// TODO: decide what to do with array of strings
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
return f.formatInputInt(value.length);
return "";
};
var inputTypes = types.inputTypes();
/// Formats input params to bytes
/// @param abi contract method inputs
/// @param array of params that will be formatted to bytes
/// @returns bytes representation of input params
/**
* Formats input params to bytes
* @param {Array} abi inputs of method
* @param {Array} params that will be formatted to bytes
* @returns bytes representation of input params
*/
var formatInput = function (inputs, params) {
var bytes = "";
var toAppendConstant = "";
@ -72,7 +86,7 @@ var formatInput = function (inputs, params) {
var formatter = inputTypes[j - 1].format;
if (arrayType(inputs[i].type))
if (isArrayType(inputs[i].type))
toAppendArrayContent += params[i].reduce(function (acc, curr) {
return acc + formatter(curr);
}, "");
@ -87,8 +101,14 @@ var formatInput = function (inputs, params) {
return bytes;
};
/**
* This method should be called to predict the length of dynamic type
*
* @param {String} type
* @returns {Number} length of dynamic type, 0 or multiplication of ETH_PADDING (32)
*/
var dynamicBytesLength = function (type) {
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
return c.ETH_PADDING * 2;
return 0;
};
@ -124,7 +144,7 @@ var formatOutput = function (outs, output) {
}
var formatter = outputTypes[j - 1].format;
if (arrayType(outs[i].type)) {
if (isArrayType(outs[i].type)) {
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
dynamicPart = dynamicPart.slice(padding);
var array = [];

File diff suppressed because one or more lines are too long

View File

@ -26,29 +26,43 @@ var types = require('./types');
var c = require('./config');
var f = require('./formatters');
/**
* Displays error about incorrect type
* @param {String} type
*/
var displayTypeError = function (type) {
console.error('parser does not support type: ' + type);
};
/// This method should be called if we want to check if givent type is an array type
/// @returns true if it is, otherwise false
var arrayType = function (type) {
/** This method should be called if we want to check if givent type is an array type
* @param {String} type name
* @returns {Boolean} true if it is, otherwise false
*/
var isArrayType = function (type) {
return type.slice(-2) === '[]';
};
/**
* This method should be called to return dynamic type length in hex
* @param {String} type
* @param {String|Array} dynamic type
* @return {String} length of dynamic type in hex or empty string if type is not dynamic
*/
var dynamicTypeBytes = function (type, value) {
// TODO: decide what to do with array of strings
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
return f.formatInputInt(value.length);
return "";
};
var inputTypes = types.inputTypes();
/// Formats input params to bytes
/// @param abi contract method inputs
/// @param array of params that will be formatted to bytes
/// @returns bytes representation of input params
/**
* Formats input params to bytes
* @param {Array} abi inputs of method
* @param {Array} params that will be formatted to bytes
* @returns bytes representation of input params
*/
var formatInput = function (inputs, params) {
var bytes = "";
var toAppendConstant = "";
@ -71,7 +85,7 @@ var formatInput = function (inputs, params) {
var formatter = inputTypes[j - 1].format;
if (arrayType(inputs[i].type))
if (isArrayType(inputs[i].type))
toAppendArrayContent += params[i].reduce(function (acc, curr) {
return acc + formatter(curr);
}, "");
@ -86,8 +100,14 @@ var formatInput = function (inputs, params) {
return bytes;
};
/**
* This method should be called to predict the length of dynamic type
*
* @param {String} type
* @returns {Number} length of dynamic type, 0 or multiplication of ETH_PADDING (32)
*/
var dynamicBytesLength = function (type) {
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
return c.ETH_PADDING * 2;
return 0;
};
@ -123,7 +143,7 @@ var formatOutput = function (outs, output) {
}
var formatter = outputTypes[j - 1].format;
if (arrayType(outs[i].type)) {
if (isArrayType(outs[i].type)) {
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
dynamicPart = dynamicPart.slice(padding);
var array = [];