tryEval: Allocate true and false once

This commit is contained in:
Robert Hensing 2024-03-20 23:06:23 +01:00
parent d71e74838a
commit a865049c4f
3 changed files with 23 additions and 4 deletions

View file

@ -437,6 +437,8 @@ EvalState::EvalState(
vEmptyList.mkList(buildList(0)); vEmptyList.mkList(buildList(0));
vNull.mkNull(); vNull.mkNull();
vTrue.mkBool(true);
vFalse.mkBool(false);
vStringRegular.mkString("regular"); vStringRegular.mkString("regular");
vStringDirectory.mkString("directory"); vStringDirectory.mkString("directory");
vStringSymlink.mkString("symlink"); vStringSymlink.mkString("symlink");

View file

@ -187,10 +187,26 @@ public:
Value vEmptyList; Value vEmptyList;
/** /**
* Null constant. * `null` constant.
*
* This is _not_ a singleton. Pointer equality is _not_ sufficient.
*/ */
Value vNull; Value vNull;
/**
* `true` constant.
*
* This is _not_ a singleton. Pointer equality is _not_ sufficient.
*/
Value vTrue;
/**
* `true` constant.
*
* This is _not_ a singleton. Pointer equality is _not_ sufficient.
*/
Value vFalse;
/** `"regular"` */ /** `"regular"` */
Value vStringRegular; Value vStringRegular;
/** `"directory"` */ /** `"directory"` */

View file

@ -896,10 +896,11 @@ static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Va
try { try {
state.forceValue(*args[0], pos); state.forceValue(*args[0], pos);
attrs.insert(state.sValue, args[0]); attrs.insert(state.sValue, args[0]);
attrs.alloc("success").mkBool(true); attrs.insert(state.symbols.create("success"), &state.vTrue);
} catch (AssertionError & e) { } catch (AssertionError & e) {
attrs.alloc(state.sValue).mkBool(false); // `value = false;` is unfortunate but removing it is a breaking change.
attrs.alloc("success").mkBool(false); attrs.insert(state.sValue, &state.vFalse);
attrs.insert(state.symbols.create("success"), &state.vFalse);
} }
// restore the debugRepl pointer if we saved it earlier. // restore the debugRepl pointer if we saved it earlier.