Fix a cppia Bool constant stored as a number in an Any array under the JIT - #1371
Open
MeguminBOT wants to merge 1 commit into
Open
Fix a cppia Bool constant stored as a number in an Any array under the JIT#1371MeguminBOT wants to merge 1 commit into
MeguminBOT wants to merge 1 commit into
Conversation
DataVal never overrode isBoolInt, so ArrayDef::genCode stored a constant true in an Array<Any> through varraySetInt and it read back as 1. The interpreter was never affected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Same script, different answers depending on whether the JIT is on. A bool constant in an
Array<Any>comes back out as a number:It really is an
Intsitting in the array, so type checks againstBoolfail and it serialises as anumber. It's still truthy, so
if (a[0])keeps working, which is why this can sit there unnoticeduntil something trips over it.
Why
ArrayDef::genCodeasks each item whether it's a bool to pick the store:Constants live in
DataVal<T>, andgetTypesaysetIntfor a bool, soisBoolIntis the onlything that can tell one apart from a number.
DataValnever overrode it, so it answered false andthe element went in through
varraySetInt.Everything else carrying a bool already does this.
CppiaBoolExprreturns true outright,MemReferencegets it fromExprTypeIsBool, and the call and cast expressions read their signature.DataValwas just missed.The interpreter fills the same array through
runObject, which boxes aDynamicfrom the realbool, so it was never wrong.The fix
One override in
src/hx/cppia/Cppia.cpp:This only affects array literals.
var d:Dynamic = truealready boxed correctly, since that pathnever asks
isBoolInt.Test
test/cppiacovers it.ClientBoolConstinClient.hxreturns the arrays andcases/TestCommon.hxchecks what comes back.The
-jitmatters, andRunTests.hxalready runs the suite both ways. It passes in both.Without the fix,
testBoolConstInAnyArrayfails under-jitand passes interpreted.One thing worth knowing if you touch these tests:
They hand the array back to the host on purpose.
Write it the obvious way, reading the array back in the same function, and the analyzer replaces it
with locals. No array gets built and the test quietly checks nothing. Took me a couple of runs to
notice.
testBoolComparisonInAnyArrayis there as a control, since a comparison in the same slotalready answers
isBoolIntand passes either way.Reproducing by hand
Built with
haxe -m Script --cppia script.cppiaand loaded from a host built with-D scriptable --dce no, callingcpp.cppia.Host.enableJit(true)beforecpp.cppia.Module.fromData(bytes).boot(), then readingStd.string(arr[0]).1truetrueWheter there's a better fix for this I'm not sure, if there is, please point it out and I'll fix it up.