Skip to content

Commit ef35d19

Browse files
committed
Fix bug in Ast
1 parent cb8bb42 commit ef35d19

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

src/prometheus/ast.lua

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,10 @@ end
371371

372372
function Ast.OrExpression(lhs, rhs, simplify)
373373
if(simplify and rhs.isConstant and lhs.isConstant) then
374-
local val = lhs.value or rhs.value;
375-
return Ast.ConstantNode(val);
374+
local success, val = pcall(function() return lhs.value or rhs.value end);
375+
if success then
376+
return Ast.ConstantNode(val);
377+
end
376378
end
377379

378380
return {
@@ -385,8 +387,10 @@ end
385387

386388
function Ast.AndExpression(lhs, rhs, simplify)
387389
if(simplify and rhs.isConstant and lhs.isConstant) then
388-
local val = lhs.value and rhs.value;
389-
return Ast.ConstantNode(val);
390+
local success, val = pcall(function() return lhs.value and rhs.value end);
391+
if success then
392+
return Ast.ConstantNode(val);
393+
end
390394
end
391395

392396
return {
@@ -399,8 +403,10 @@ end
399403

400404
function Ast.LessThanExpression(lhs, rhs, simplify)
401405
if(simplify and rhs.isConstant and lhs.isConstant) then
402-
local val = lhs.value < rhs.value;
403-
return Ast.ConstantNode(val);
406+
local success, val = pcall(function() return lhs.value < rhs.value end);
407+
if success then
408+
return Ast.ConstantNode(val);
409+
end
404410
end
405411

406412
return {
@@ -413,8 +419,10 @@ end
413419

414420
function Ast.GreaterThanExpression(lhs, rhs, simplify)
415421
if(simplify and rhs.isConstant and lhs.isConstant) then
416-
local val = lhs.value > rhs.value;
417-
return Ast.ConstantNode(val);
422+
local success, val = pcall(function() return lhs.value > rhs.value end);
423+
if success then
424+
return Ast.ConstantNode(val);
425+
end
418426
end
419427

420428
return {
@@ -427,8 +435,10 @@ end
427435

428436
function Ast.LessThanOrEqualsExpression(lhs, rhs, simplify)
429437
if(simplify and rhs.isConstant and lhs.isConstant) then
430-
local val = lhs.value <= rhs.value;
431-
return Ast.ConstantNode(val);
438+
local success, val = pcall(function() return lhs.value <= rhs.value end);
439+
if success then
440+
return Ast.ConstantNode(val);
441+
end
432442
end
433443

434444
return {
@@ -441,8 +451,10 @@ end
441451

442452
function Ast.GreaterThanOrEqualsExpression(lhs, rhs, simplify)
443453
if(simplify and rhs.isConstant and lhs.isConstant) then
444-
local val = lhs.value >= rhs.value;
445-
return Ast.ConstantNode(val);
454+
local success, val = pcall(function() return lhs.value >= rhs.value end);
455+
if success then
456+
return Ast.ConstantNode(val);
457+
end
446458
end
447459

448460
return {
@@ -455,8 +467,10 @@ end
455467

456468
function Ast.NotEqualsExpression(lhs, rhs, simplify)
457469
if(simplify and rhs.isConstant and lhs.isConstant) then
458-
local val = lhs.value ~= rhs.value;
459-
return Ast.ConstantNode(val);
470+
local success, val = pcall(function() return lhs.value ~= rhs.value end);
471+
if success then
472+
return Ast.ConstantNode(val);
473+
end
460474
end
461475

462476
return {
@@ -469,8 +483,10 @@ end
469483

470484
function Ast.EqualsExpression(lhs, rhs, simplify)
471485
if(simplify and rhs.isConstant and lhs.isConstant) then
472-
local val = lhs.value == rhs.value;
473-
return Ast.ConstantNode(val);
486+
local success, val = pcall(function() return lhs.value == rhs.value end);
487+
if success then
488+
return Ast.ConstantNode(val);
489+
end
474490
end
475491

476492
return {
@@ -483,8 +499,10 @@ end
483499

484500
function Ast.StrCatExpression(lhs, rhs, simplify)
485501
if(simplify and rhs.isConstant and lhs.isConstant) then
486-
local val = lhs.value .. rhs.value;
487-
return Ast.ConstantNode(val);
502+
local success, val = pcall(function() return lhs.value .. rhs.value end);
503+
if success then
504+
return Ast.ConstantNode(val);
505+
end
488506
end
489507

490508
return {
@@ -497,8 +515,10 @@ end
497515

498516
function Ast.AddExpression(lhs, rhs, simplify)
499517
if(simplify and rhs.isConstant and lhs.isConstant) then
500-
local val = lhs.value + rhs.value;
501-
return Ast.ConstantNode(val);
518+
local success, val = pcall(function() return lhs.value + rhs.value end);
519+
if success then
520+
return Ast.ConstantNode(val);
521+
end
502522
end
503523

504524
return {
@@ -511,8 +531,10 @@ end
511531

512532
function Ast.SubExpression(lhs, rhs, simplify)
513533
if(simplify and rhs.isConstant and lhs.isConstant) then
514-
local val = lhs.value - rhs.value;
515-
return Ast.ConstantNode(val);
534+
local success, val = pcall(function() return lhs.value - rhs.value end);
535+
if success then
536+
return Ast.ConstantNode(val);
537+
end
516538
end
517539

518540
return {
@@ -525,8 +547,10 @@ end
525547

526548
function Ast.MulExpression(lhs, rhs, simplify)
527549
if(simplify and rhs.isConstant and lhs.isConstant) then
528-
local val = lhs.value * rhs.value;
529-
return Ast.ConstantNode(val);
550+
local success, val = pcall(function() return lhs.value * rhs.value end);
551+
if success then
552+
return Ast.ConstantNode(val);
553+
end
530554
end
531555

532556
return {
@@ -539,8 +563,10 @@ end
539563

540564
function Ast.DivExpression(lhs, rhs, simplify)
541565
if(simplify and rhs.isConstant and lhs.isConstant) then
542-
local val = lhs.value / rhs.value;
543-
return Ast.ConstantNode(val);
566+
local success, val = pcall(function() return lhs.value / rhs.value end);
567+
if success then
568+
return Ast.ConstantNode(val);
569+
end
544570
end
545571

546572
return {
@@ -553,8 +579,10 @@ end
553579

554580
function Ast.ModExpression(lhs, rhs, simplify)
555581
if(simplify and rhs.isConstant and lhs.isConstant) then
556-
local val = lhs.value % rhs.value;
557-
return Ast.ConstantNode(val);
582+
local success, val = pcall(function() return lhs.value % rhs.value end);
583+
if success then
584+
return Ast.ConstantNode(val);
585+
end
558586
end
559587

560588
return {
@@ -567,8 +595,10 @@ end
567595

568596
function Ast.NotExpression(rhs, simplify)
569597
if(simplify and rhs.isConstant) then
570-
local val = not rhs.value;
571-
return Ast.ConstantNode(val);
598+
local success, val = pcall(function() return not rhs.value end);
599+
if success then
600+
return Ast.ConstantNode(val);
601+
end
572602
end
573603

574604
return {
@@ -580,8 +610,10 @@ end
580610

581611
function Ast.NegateExpression(rhs, simplify)
582612
if(simplify and rhs.isConstant) then
583-
local val = -rhs.value;
584-
return Ast.ConstantNode(val);
613+
local success, val = pcall(function() return -rhs.value end);
614+
if success then
615+
return Ast.ConstantNode(val);
616+
end
585617
end
586618

587619
return {
@@ -593,8 +625,10 @@ end
593625

594626
function Ast.LenExpression(rhs, simplify)
595627
if(simplify and rhs.isConstant) then
596-
local val = #(rhs.value);
597-
return Ast.ConstantNode(val);
628+
local success, val = pcall(function() return #rhs.value end);
629+
if success then
630+
return Ast.ConstantNode(val);
631+
end
598632
end
599633

600634
return {
@@ -606,8 +640,10 @@ end
606640

607641
function Ast.PowExpression(lhs, rhs, simplify)
608642
if(simplify and rhs.isConstant and lhs.isConstant) then
609-
local val = lhs.value ^ rhs.value;
610-
return Ast.ConstantNode(val);
643+
local success, val = pcall(function() return lhs.value ^ rhs.value end);
644+
if success then
645+
return Ast.ConstantNode(val);
646+
end
611647
end
612648

613649
return {

0 commit comments

Comments
 (0)