Skip to content

Commit

Permalink
21139: Improves consistency of nulls handling for various opcodes (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
howsohazard authored Aug 5, 2024
1 parent 4551a1a commit 29f0291
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Amalgam/evaluablenode/EvaluableNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class EvaluableNode
static inline size_t GetDeepSize(EvaluableNode *n)
{
if(n == nullptr)
return 0;
return 1;

if(!n->GetNeedCycleCheck())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Amalgam/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ EvaluableNodeReference Interpreter::InterpretNodeIntoUniqueStringIDValueEvaluabl

double Interpreter::InterpretNodeIntoNumberValue(EvaluableNode *n)
{
if(n == nullptr)
if(EvaluableNode::IsNull(n))
return std::numeric_limits<double>::quiet_NaN();

auto type = n->GetType();
Expand Down Expand Up @@ -659,7 +659,7 @@ EvaluableNodeReference Interpreter::InterpretNodeIntoUniqueNumberValueEvaluableN
bool Interpreter::InterpretNodeIntoBoolValue(EvaluableNode *n, bool value_if_null)
{
//shortcut if the node has what is being asked
if(n == nullptr)
if(EvaluableNode::IsNull(n))
return value_if_null;

auto result = InterpretNodeForImmediateUse(n, true);
Expand Down
22 changes: 13 additions & 9 deletions src/Amalgam/interpreter/InterpreterOpcodesBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_CONCLUDE_and_RETURN(Evalua
auto &ocn = en->GetOrderedChildNodes();

//if no parameter, then return itself for performance
if(ocn.size() == 0 || ocn[0] == nullptr)
if(ocn.size() == 0)
return EvaluableNodeReference(en, false);

//if idempotent, can just return a copy without any metadata
Expand All @@ -458,7 +458,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_CALL(EvaluableNode *en, bo
return EvaluableNodeReference::Null();

auto function = InterpretNodeForImmediateUse(ocn[0]);
if(function == nullptr)
if(EvaluableNode::IsNull(function))
return EvaluableNodeReference::Null();

auto node_stack = CreateInterpreterNodeStackStateSaver(function);
Expand Down Expand Up @@ -497,7 +497,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_CALL_SANDBOXED(EvaluableNo
return EvaluableNodeReference::Null();

auto function = InterpretNodeForImmediateUse(ocn[0]);
if(function == nullptr)
if(EvaluableNode::IsNull(function))
return EvaluableNodeReference::Null();

auto node_stack = CreateInterpreterNodeStackStateSaver(function);
Expand Down Expand Up @@ -1066,7 +1066,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_RETRIEVE(EvaluableNode *en
#endif

//get the value(s)
if(to_lookup == nullptr || IsEvaluableNodeTypeImmediate(to_lookup->GetType()))
if(EvaluableNode::IsNull(to_lookup) || IsEvaluableNodeTypeImmediate(to_lookup->GetType()))
{
StringInternPool::StringID symbol_name_sid = EvaluableNode::ToStringIDIfExists(to_lookup);
EvaluableNode* symbol_value = GetCallStackSymbol(symbol_name_sid);
Expand Down Expand Up @@ -1415,7 +1415,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ARGS(EvaluableNode *en, bo
EvaluableNodeReference GenerateRandomValueBasedOnRandParam(EvaluableNodeReference param, Interpreter *interpreter,
RandomStream &random_stream, bool &can_free_param, bool immediate_result)
{
if(param == nullptr)
if(EvaluableNode::IsNull(param))
return interpreter->AllocReturn(random_stream.RandFull(), immediate_result);

auto &ocn = param->GetOrderedChildNodes();
Expand Down Expand Up @@ -1548,7 +1548,9 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_RAND(EvaluableNode *en, bo
}

if(can_free_param)
{
evaluableNodeManager->FreeNodeTreeIfPossible(param);
}
else
{
//if used the parameters, a parameter might be used more than once
Expand Down Expand Up @@ -1714,14 +1716,14 @@ size_t GetRandomWeightedValueIndex(std::vector<EvaluableNode *> &probability_nod
// if any part of param is preserved in the return value, then can_free_param will be set to false, otherwise it will be left alone
EvaluableNodeReference GenerateWeightedRandomValueBasedOnRandParam(EvaluableNodeReference param, EvaluableNodeManager *enm, RandomStream &random_stream, bool &can_free_param)
{
if(param == nullptr)
if(EvaluableNode::IsNull(param))
return EvaluableNodeReference::Null();

auto &ocn = param->GetOrderedChildNodes();
//need to have a value and probability list
if(ocn.size() >= 2)
{
if(ocn[0] == nullptr || ocn[1] == nullptr)
if(EvaluableNode::IsNull(ocn[0]) || EvaluableNode::IsNull(ocn[1]))
return EvaluableNodeReference::Null();

can_free_param = false;
Expand Down Expand Up @@ -1798,7 +1800,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_WEIGHTED_RAND(EvaluableNod
{
EvaluableNodeReference retval(evaluableNodeManager->AllocNode(ENT_LIST), true);

if(param_ocn.size() < 2 || param_ocn[0] == nullptr || param_ocn[1] == nullptr)
if(param_ocn.size() < 2 || EvaluableNode::IsNull(param_ocn[0]) || EvaluableNode::IsNull(param_ocn[1]))
return retval;

//clamp to the maximum number that can possibly be generated
Expand Down Expand Up @@ -1864,7 +1866,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_WEIGHTED_RAND(EvaluableNod
//if generating many values with weighted probabilites, use fast method
if(param_ocn.size() > 0 && (number_to_generate > 10 || (number_to_generate > 3 && param_ocn.size() > 200)))
{
if(param_ocn.size() < 2 || param_ocn[0] == nullptr || param_ocn[1] == nullptr)
if(param_ocn.size() < 2 || EvaluableNode::IsNull(param_ocn[0]) || EvaluableNode::IsNull(param_ocn[1]))
{
evaluableNodeManager->FreeNodeIfPossible(param);
return retval;
Expand Down Expand Up @@ -1918,7 +1920,9 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_WEIGHTED_RAND(EvaluableNod
}

if(can_free_param)
{
evaluableNodeManager->FreeNodeTreeIfPossible(param);
}
else
{
//if used the parameters, a parameter might be used more than once
Expand Down
4 changes: 2 additions & 2 deletions src/Amalgam/interpreter/InterpreterOpcodesDataTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,8 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ZIP_LABELS(EvaluableNode *
auto source = InterpretNode(ocn[1]);

//if no label list, or no source or source is immediate, then just return the source
if(label_list == nullptr || !label_list->IsOrderedArray()
|| source == nullptr || !source->IsOrderedArray())
if(EvaluableNode::IsNull(label_list) || !label_list->IsOrderedArray()
|| EvaluableNode::IsNull(source) || !source->IsOrderedArray())
return source;

node_stack.PopEvaluableNode();
Expand Down
6 changes: 3 additions & 3 deletions src/Amalgam/interpreter/InterpreterOpcodesTransformations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_REWRITE(EvaluableNode *en,
return EvaluableNodeReference::Null();

auto function = InterpretNodeForImmediateUse(ocn[0]);
if(function == nullptr)
if(EvaluableNode::IsNull(function))
return EvaluableNodeReference::Null();
auto node_stack = CreateInterpreterNodeStackStateSaver(function);

Expand Down Expand Up @@ -1645,7 +1645,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ZIP(EvaluableNode *en, boo
}

//if no function, then just put value into the appropriate slot for the index
if(function == nullptr)
if(EvaluableNode::IsNull(function))
{
result->SetMappedChildNodeWithReferenceHandoff(index_sid, value, true);
}
Expand Down Expand Up @@ -1674,7 +1674,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ZIP(EvaluableNode *en, boo
//the index list has been converted to strings, so therefore can be freed
evaluableNodeManager->FreeNodeTreeIfPossible(index_list);

if(function != nullptr)
if(!EvaluableNode::IsNull(function))
{
//the values have likely been copied, so only the top node can be freed as long as it doesn't point back to itself
if(!value_list.GetNeedCycleCheck())
Expand Down

0 comments on commit 29f0291

Please sign in to comment.