Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function readDefinition(definition: AstDefinition): Uint8Array {
debug("definition %o", definition);
switch(CodecUtils.Definition.typeClass(definition))
{
case "rational":
let numericalValue: BN = CodecUtils.Definition.rationalValue(definition);
return CodecUtils.Conversion.toBytes(numericalValue, CodecUtils.EVM.WORD_SIZE);
//you may be wondering, why do we not just use definition.value here,
//like we do below? answer: because if this isn't a literal, that may not
//exist
case "stringliteral":
return CodecUtils.Conversion.toBytes(definition.hexValue);
default:
//unfortunately, other types of constants are just too complicated to
//handle right now. sorry.
debug("unsupported constant definition type");
throw new DecodingError(
{
kind: "UnsupportedConstantError",
definition
}
);
}
export function readStack(stack: Uint8Array[], from: number, to: number): Uint8Array {
if(from < 0 || to >= stack.length) {
throw new DecodingError(
{
kind: "ReadErrorStack",
from,
to
}
);
}
//unforunately, Uint8Arrays don't support concat; if they did the rest of
//this would be one line. Or similarly if they worked with lodash's flatten,
//but they don't support that either. But neither of those are the case, so
//we'll have to concatenate a bit more manually.
let words = stack.slice(from, to + 1);
let result = new Uint8Array(words.length * CodecUtils.EVM.WORD_SIZE);
//shouldn't we total up the lengths? yeah, but each one should have a
//length of 32, so unless somehting's gone wrong we can just multiply
for(let index = 0; index < words.length; index++) {
result.set(words[index], index * CodecUtils.EVM.WORD_SIZE);
}
return result;
}
kind: "ReadErrorStack",
from,
to
}
);
}
//unforunately, Uint8Arrays don't support concat; if they did the rest of
//this would be one line. Or similarly if they worked with lodash's flatten,
//but they don't support that either. But neither of those are the case, so
//we'll have to concatenate a bit more manually.
let words = stack.slice(from, to + 1);
let result = new Uint8Array(words.length * CodecUtils.EVM.WORD_SIZE);
//shouldn't we total up the lengths? yeah, but each one should have a
//length of 32, so unless somehting's gone wrong we can just multiply
for(let index = 0; index < words.length; index++) {
result.set(words[index], index * CodecUtils.EVM.WORD_SIZE);
}
return result;
}
export function read(memory: Uint8Array, offset: number) {
return readBytes(memory, offset, CodecUtils.EVM.WORD_SIZE);
}