Skip to content

Commit

Permalink
Merge pull request #52 from 0xPolygonHermez/fix/update-json-defines-info
Browse files Browse the repository at this point in the history
fix command line defined constants on metadata
  • Loading branch information
krlosMata authored Jan 31, 2023
2 parents 125f311 + 0c854c0 commit 9e086a8
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,24 @@ module.exports = async function compile(fileName, ctx, config) {
function defineConstant(ctx, name, ctype, value) {
const l = ctx.currentLine;

if (typeof ctx.constants[name] !== 'undefined') {
throw error(l, `Redefinition of constant ${name} previously defined on `+
`${ctx.constants[name].fileName}:${ctx.constants[name].line}`);
}

if (ctx.config && ctx.config.defines && typeof ctx.config.defines[name] !== 'undefined') {
console.log(`NOTICE: Ignore constant definition ${name} on ${l.fileName}:${l.line} because it was defined by command line`);
ctx.constants[name] = {
value: ctx.config.defines[name].value,
type: ctx.config.defines[name].type,
originalValue: value,
originalType: ctype,
defines: true,
line: l.line,
fileName: l.fileName};
return;
}

if (typeof ctx.constants[name] !== 'undefined') {
throw error(l, `Redefinition of constant ${name} previously defined on `+
`${ctx.constants[name].fileName}:${ctx.constants[name].line}`);
}
if (ctype == 'CONSTL') {
if (value > maxConstl || value < minConstl) {
throw error(l, `Constant ${name} out of range, value ${value} must be in range [${minConstl},${maxConstl}]`);
Expand All @@ -299,6 +308,13 @@ function defineConstant(ctx, name, ctype, value) {

function getConstant(ctx, name, throwIfNotExists = true) {
if (ctx.config && ctx.config.defines && typeof ctx.config.defines[name] !== 'undefined') {
if (typeof ctx.constants[name] == 'undefined') {
ctx.constants[name] = {
value: ctx.config.defines[name].value,
type: ctx.config.defines[name].type,
defines: true
};
}
return [ctx.config.defines[name].value, ctx.config.defines[name].type];
}

Expand Down

0 comments on commit 9e086a8

Please sign in to comment.