Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
libhxcpp 6.0.0 (unreleased)
------------------------------------------------------------

* Fixed cppia's adoptInstances moving instances that code then used with the wrong layout: a class whose methods changed order or number moved, so old code still running called its moved instances by the old vtable slots, reaching another method or crashing, and a class moved while a field could hold an instance of a class that stayed, which new code read past its end. Such classes keep their instances now, and the report says why: methods must be the same, in the same slots, and so must the script interfaces a class implements, fields compare their types by name, and a class stays with the classes its fields can hold (libhxcpp #464)
* Fixed malformed cppia files crashing the host instead of failing to load: a MAIN in a module with no types, an unknown script enum constructor or one called with the wrong number of arguments, two classes with one type id, a string constant or file id past the string table, which grew the table to it, an enum parameter index past the value, break or continue outside a loop under the JIT, and a super constructor call to a class other than the super class. Each fails to load, or is read safely (libhxcpp #467)
* Fixed cppia's interpreter and JIT giving different results for array builtins, both of them now matching C++: under the JIT, a[i] += x and the other compound assignments on an Array<Dynamic> stored x alone, a[i]++, insert and toString on one did nothing, a Bool stored in an Array<Any> became an Int, bitwise compound assignments on an Array<Null<Int>> did nothing, an Int array read out of range as a String gave "null", and ++d[i] on a Dynamic as a statement decremented. Interpreted, a typed indexOf or lastIndexOf used as a Float gave 0, and join, toString and the iterators used as a statement did not run (libhxcpp #468)
* Fixed a Haxe 4 haxe.atomic.AtomicInt kept in a field reading freed memory after a collection: Haxe 4's AtomicInt on C++ is a cpp.Pointer into a one-element array nothing else holds, and the collector ignored cpp.Pointer fields, so it freed the array (HaxeFoundation/hxcpp#1225; Haxe 5 changed AtomicInt instead, in HaxeFoundation/haxe#12236). A cpp.Pointer field, or a pointer boxed in a Dynamic, now keeps what it points into in the GC heap, as a pointer on the stack does, and pins it under HXCPP_GC_MOVING. Of 10,000 atomics in fields, 9,999 read freed memory after a collection and none do now
* Fixed HXCPP_GC_INCREMENTAL failing to compile ("Incremental GC requires regenerated C++") with libhxcpp installed from a release, 6.0.0-rc.2 among them: the release workflow rewrote extraParams.hxml with the version and dropped its other line, the macro that turns on the generated write barriers the incremental collector needs. Only the version lines are rewritten now
* Fixed a change of link flags or libraries alone leaving the program as it was: the build tool linked again only when an object or library file had changed, so -D HXCPP_LINK_STACK_SIZE, HXCPP_LINK_NO_MEMORY_GROWTH, HXCPP_LINK_NO_EMBIND or HXCPP_LINK_NODERAWFS on an Emscripten build, which change nothing that is compiled, or a flag or library added to a target in Build.xml, did nothing until the objects changed. The flags, libraries and library paths are now part of what a link is checked against, so each program links once more after updating
Expand Down
4 changes: 2 additions & 2 deletions docs/CppiaModuleRelease.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ untyped previous.release();

- `name`, the class's name.
- `moved`, how many of its instances moved.
- `reason`, null when they could move, otherwise why not, naming the first field that differs.
- `reason`, null when they could move, otherwise why not, naming the first field or method that differs, or the field that can hold an instance that stays.

The rule is strict, as an instance stays where it is, at its size. Both classes must extend the same host class, through super classes of the same names, implement the same interfaces, and have the same fields, dynamic functions included, with the same names, storage types and offsets. Methods can differ in any way, so editing method bodies, the usual case, is covered. A class that adds, removes, retypes or reorders a field keeps its instances, which keep the old module loaded until they go.
The rule is strict, as an instance stays where it is, at its size, and old code still running calls it by the slots its calls linked with. Both classes must extend the same host class, through super classes of the same names, implement the same interfaces, and have the same fields, dynamic functions included, with the same names, types and offsets. They must have the same methods, their super classes' included, in the same order, so that each keeps its slot, and so must the script interfaces they implement. Method bodies can differ in any way, so editing them, the usual case, is covered. A class that adds, removes, retypes or reorders a field or a method keeps its instances, which keep the old module loaded until they go. So does a class with a field that can hold an instance of a class that stays, since the new code would read that instance with the new layout.

What does not move:

Expand Down
26 changes: 22 additions & 4 deletions src/hx/cppia/ArrayBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ struct ArraySetter : public ArrayBuiltinBase
case aoShr:
case aoUShr:
rightHandType = etInt;
// Fell through to the element's type, so on an Array<Null<Int>>
// these did nothing under the JIT
break;

default:
rightHandType = elemType;
Expand Down Expand Up @@ -693,7 +696,8 @@ struct ArrayBuiltin : public ArrayBuiltinBase
return result;
}

if (FUNC==afPush)
// indexOf and lastIndexOf used as a Float returned 0 without running
if (FUNC==afPush || FUNC==afIndexOf || FUNC==afLastIndexOf)
return runInt(ctx);
return 0.0;
}
Expand Down Expand Up @@ -980,9 +984,14 @@ struct ArrayBuiltin : public ArrayBuiltinBase
runInt(ctx);

if (FUNC==afConcat || FUNC==afCopy || FUNC==afReverse || FUNC==afSplice || FUNC==afSlice ||
FUNC==afMap || FUNC==afFilter)
FUNC==afMap || FUNC==afFilter || FUNC==afIterator || FUNC==afKeyValueIterator)
runObject(ctx);

// Statements too: they run the receiver and each element's toString,
// which were skipped, as the JIT and the other targets do not
if (FUNC==afJoin || FUNC==afToString)
runString(ctx);

if (FUNC==afReverse)
{
Array_obj<ELEM> *thisVal = (Array_obj<ELEM>*)thisExpr->runObject(ctx);
Expand Down Expand Up @@ -1550,8 +1559,17 @@ struct ArrayBuiltin : public ArrayBuiltinBase
// Check length..
JumpId lengthOk = compiler->compare( cmpI_LESS, sJitTemp0.as(jtInt),
sJitTemp1.star(jtInt, ArrayBase::lengthOffset()) );
// Out of bounds - return null / zero
compiler->returnNull(inDest, destType);
// Out of bounds - the element type's zero, as the interpreter
// and the other targets read it: an Int array's read as a
// String was "null", not "0"
ExprType outElemType = (ExprType)ExprTypeOf<ELEM>::value;
if ((outElemType==etInt || outElemType==etFloat) && (destType==etString || destType==etObject))
{
compiler->move(sJitTemp2.as(jtInt), (int)0);
compiler->convert(sJitTemp2.as(jtInt), etInt, inDest, destType, isBoolElem());
}
else
compiler->returnNull(inDest, destType);
writtenNull = compiler->jump();


Expand Down
174 changes: 121 additions & 53 deletions src/hx/cppia/ArrayVirtual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,41 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase
}
const char *getName() HXCPP_OVERRIDE { return gArrayFuncNames[FUNC]; }

// a[i] op= val and ++ and --, for the interpreter and the JIT alike
static Dynamic applyAssign(Dynamic orig, Dynamic val)
{
switch(OP)
{
case aoAdd: return orig + val;
case aoMult: return orig * val;
case aoDiv: return orig / val;
case aoSub: return orig - val;
case aoAnd: return orig->__ToInt() & val->__ToInt();
case aoOr: return orig->__ToInt() | val->__ToInt();
case aoXOr: return orig->__ToInt() ^ val->__ToInt();
case aoShl: return orig->__ToInt() << val->__ToInt();
case aoShr: return orig->__ToInt() >> val->__ToInt();
case aoUShr: return hx::UShr(orig,val);
case aoMod: return orig % val;
default: return val;
}
}

static Dynamic applyCrement(ArrayAnyImpl *thisVal, int i)
{
if (OP==coPreInc)
return thisVal->__SetItem(i, thisVal->__GetItem(i) + 1);
if (OP==coPreDec)
return thisVal->__SetItem(i, thisVal->__GetItem(i) - 1);

Dynamic result = thisVal->__GetItem(i);
if (OP==coPostDec)
thisVal->__SetItem(i, thisVal->__GetItem(i) - 1);
if (OP==coPostInc)
thisVal->__SetItem(i, thisVal->__GetItem(i) + 1);
return result;
}

int runInt(CppiaCtx *ctx) HXCPP_OVERRIDE
{
if (FUNC==afPush)
Expand Down Expand Up @@ -306,62 +341,14 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase

Dynamic val = args[1]->runObject(ctx);
BCR_CHECK;
switch(OP)
{
case aoSet: break;

case aoAdd:
val = orig + val;
break;
case aoMult:
val = orig * val;
break;
case aoDiv:
val = orig / val;
break;
case aoSub:
val = orig - val;
break;
case aoAnd:
val = orig->__ToInt() & val->__ToInt();
break;
case aoOr:
val = orig->__ToInt() | val->__ToInt();
break;
case aoXOr:
val = orig->__ToInt() ^ val->__ToInt();
break;
case aoShl:
val = orig->__ToInt() << val->__ToInt();
break;
case aoShr:
val = orig->__ToInt() >> val->__ToInt();
break;
case aoUShr:
val = hx::UShr(orig,val);
break;
case aoMod:
val = orig % val;
break;
default: ;
}
val = applyAssign(orig, val);
return thisVal->__SetItem(i,val).mPtr;
}
if (FUNC==af__crement)
{
int i = args[0]->runInt(ctx);
BCR_CHECK;
if (OP==coPreInc)
return thisVal->__SetItem(i, thisVal->__GetItem(i) + 1).mPtr;
if (OP==coPreDec)
return thisVal->__SetItem(i, thisVal->__GetItem(i) - 1).mPtr;

Dynamic result = thisVal->__GetItem(i);
if (OP==coPostDec)
thisVal->__SetItem(i, thisVal->__GetItem(i) - 1);
if (OP==coPostInc)
thisVal->__SetItem(i, thisVal->__GetItem(i) + 1);
return result.mPtr;
return applyCrement(thisVal, i).mPtr;
}

if (FUNC==afPop)
Expand Down Expand Up @@ -666,6 +653,49 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase
}


// Compound assignment, ++ and --, insert and toString, which the JIT did
// not compile: a[i] op= val stored val alone, and the others did nothing
// (libhxcpp #468). They work as the interpreter does
static hx::Object * SLJIT_CALL runAssign(ArrayAnyImpl *inArray, int inIndex, hx::Object *inValue)
{
TRY_NATIVE
Dynamic orig = inArray->__GetItem(inIndex);
if (!orig.mPtr)
NullReference("Object", false);
return inArray->__SetItem(inIndex, applyAssign(orig, Dynamic(inValue))).mPtr;
CATCH_NATIVE
return 0;
}

static hx::Object * SLJIT_CALL runCrement(ArrayAnyImpl *inArray, int inIndex)
{
TRY_NATIVE
return applyCrement(inArray, inIndex).mPtr;
CATCH_NATIVE
return 0;
}

static void SLJIT_CALL runInsert(ArrayAnyImpl *inArray, hx::Object *inPos, hx::Object *inValue)
{
TRY_NATIVE
inArray->CALL(insert)(Dynamic(inPos), Dynamic(inValue));
CATCH_NATIVE
}

static void SLJIT_CALL runToString(ArrayAnyImpl *inArray, String *outValue)
{
TRY_NATIVE
JitStore(outValue, inArray->toString());
CATCH_NATIVE
}

// A Bool goes in boxed, as a Bool: the Int paths stored it as an Int
static ExprType anyStoreType(CppiaExpr *inValue)
{
ExprType type = inValue->getType();
return type==etInt && inValue->isBoolInt() ? etObject : type;
}

void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType) HXCPP_OVERRIDE
{
JitTemp thisVal(compiler,etObject);
Expand Down Expand Up @@ -826,7 +856,16 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase
JitTemp index(compiler,etInt);
args[0]->genCode(compiler, index, etInt);

switch(args[1]->getType())
if (OP!=aoSet)
{
args[1]->genCode(compiler, sJitArg2.as(jtPointer), etObject);
compiler->callNative( (void *)runAssign, thisVal, index, sJitArg2.as(jtPointer));
compiler->checkException();
compiler->convertReturnReg(etObject, inDest, destType);
break;
}

switch(anyStoreType(args[1]))
{
case etInt:
args[1]->genCode(compiler, sJitArg2.as(jtInt), etInt);
Expand Down Expand Up @@ -863,7 +902,7 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase

case afPush:
{
switch(args[0]->getType())
switch(anyStoreType(args[0]))
{
case etInt:
args[0]->genCode(compiler, sJitArg1.as(jtInt), etInt);
Expand Down Expand Up @@ -896,7 +935,7 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase

case afUnshift:
{
switch(args[0]->getType())
switch(anyStoreType(args[0]))
{
case etInt:
args[0]->genCode(compiler, sJitArg1.as(jtInt), etInt);
Expand Down Expand Up @@ -928,6 +967,35 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase



case af__crement:
{
JitTemp index(compiler,etInt);
args[0]->genCode(compiler, index, etInt);
compiler->callNative( (void *)runCrement, thisVal, index);
compiler->checkException();
compiler->convertReturnReg(etObject, inDest, destType);
}
break;

case afInsert:
{
JitTemp pos(compiler,jtPointer);
args[0]->genCode(compiler, pos, etObject);
args[1]->genCode(compiler, sJitArg2.as(jtPointer), etObject);
compiler->callNative( (void *)runInsert, thisVal, pos, sJitArg2.as(jtPointer));
compiler->checkException();
}
break;

case afToString:
{
JitTemp result(compiler,jtString);
compiler->callNative( (void *)runToString, thisVal, result);
compiler->checkException();
compiler->convert(result, etString, inDest, destType);
}
break;

default:
compiler->traceStrings("Unknown ArrayBuiltinAny:", getName() );
}
Expand Down
Loading
Loading