- added a 'new' intrinsic to create new objects from inside scripts.

This is not tested yet and likely to not working correctly yet. Will be fixed once I get far enough to use it later.
This commit is contained in:
Christoph Oelckers 2017-02-03 22:56:03 +01:00
parent d5962b290c
commit 8578a5a12e
6 changed files with 106 additions and 0 deletions

View file

@ -346,6 +346,7 @@ xx(SinH)
xx(TanH) xx(TanH)
xx(ATan2) xx(ATan2)
xx(VectorAngle) xx(VectorAngle)
xx(New)
xx(Alpha) xx(Alpha)
xx(Angle) xx(Angle)
xx(Args) xx(Args)

View file

@ -4918,6 +4918,70 @@ ExpEmit FxATan2::Emit(VMFunctionBuilder *build)
return out; return out;
} }
//==========================================================================
//
//
//
//==========================================================================
FxNew::FxNew(FxExpression *v)
: FxExpression(EFX_New, v->ScriptPosition)
{
val = new FxClassTypeCast(NewClassPointer(RUNTIME_CLASS(DObject)), v, false);
ValueType = NewPointer(RUNTIME_CLASS(DObject));
}
//==========================================================================
//
//
//
//==========================================================================
FxNew::~FxNew()
{
SAFE_DELETE(val);
}
//==========================================================================
//
//
//
//==========================================================================
FxExpression *FxNew::Resolve(FCompileContext &ctx)
{
CHECKRESOLVED();
SAFE_RESOLVE(val, ctx);
if (!val->ValueType->IsKindOf(RUNTIME_CLASS(PClassPointer)))
{
ScriptPosition.Message(MSG_ERROR, "Class type expected");
delete this;
return nullptr;
}
if (val->isConstant())
{
auto cls = static_cast<PClass *>(static_cast<FxConstant*>(val)->GetValue().GetPointer());
ValueType = NewPointer(cls);
}
return this;
}
//==========================================================================
//
//
//
//==========================================================================
ExpEmit FxNew::Emit(VMFunctionBuilder *build)
{
assert(ValueType == val->ValueType);
ExpEmit from = val->Emit(build);
from.Free(build);
ExpEmit to(build, REGT_POINTER);
build->Emit(from.Konst ? OP_NEW_K : OP_NEW, to.RegNum, from.RegNum);
return to;
}
//========================================================================== //==========================================================================
// //
// The atan2 opcode only takes registers as parameters, so any constants // The atan2 opcode only takes registers as parameters, so any constants
@ -7428,6 +7492,15 @@ FxExpression *FxFunctionCall::Resolve(FCompileContext& ctx)
} }
break; break;
case NAME_New:
if (CheckArgSize(MethodName, ArgList, 1, 1, ScriptPosition))
{
func = new FxNew(ArgList[0]);
ArgList[0] = nullptr;
}
break;
default: default:
ScriptPosition.Message(MSG_ERROR, "Call to unknown function '%s'", MethodName.GetChars()); ScriptPosition.Message(MSG_ERROR, "Call to unknown function '%s'", MethodName.GetChars());
break; break;

View file

@ -242,6 +242,7 @@ enum EFxType
EFX_Conditional, EFX_Conditional,
EFX_Abs, EFX_Abs,
EFX_ATan2, EFX_ATan2,
EFX_New,
EFX_MinMax, EFX_MinMax,
EFX_Random, EFX_Random,
EFX_RandomPick, EFX_RandomPick,
@ -1176,6 +1177,26 @@ public:
private: private:
ExpEmit ToReg(VMFunctionBuilder *build, FxExpression *val); ExpEmit ToReg(VMFunctionBuilder *build, FxExpression *val);
}; };
//==========================================================================
//
//
//
//==========================================================================
class FxNew : public FxExpression
{
FxExpression *val;
public:
FxNew(FxExpression *v);
~FxNew();
FxExpression *Resolve(FCompileContext&);
ExpEmit Emit(VMFunctionBuilder *build);
};
//========================================================================== //==========================================================================
// //
// //

View file

@ -72,6 +72,7 @@
#define RFRF MODE_AF | MODE_BF | MODE_CUNUSED #define RFRF MODE_AF | MODE_BF | MODE_CUNUSED
#define RSRS MODE_AS | MODE_BS | MODE_CUNUSED #define RSRS MODE_AS | MODE_BS | MODE_CUNUSED
#define RPRP MODE_AP | MODE_BP | MODE_CUNUSED #define RPRP MODE_AP | MODE_BP | MODE_CUNUSED
#define RPKP MODE_AP | MODE_BKP | MODE_CUNUSED
#define RXRXI8 MODE_AX | MODE_BX | MODE_CIMMZ #define RXRXI8 MODE_AX | MODE_BX | MODE_CIMMZ
#define RPRPRP MODE_AP | MODE_BP | MODE_CP #define RPRPRP MODE_AP | MODE_BP | MODE_CP
#define RPRPKP MODE_AP | MODE_BP | MODE_CKP #define RPRPKP MODE_AP | MODE_BP | MODE_CKP

View file

@ -759,6 +759,14 @@ begin:
assert(0); assert(0);
NEXTOP; NEXTOP;
OP(NEW_K):
OP(NEW):
{
PClass *cls = (PClass*)(op == OP_NEW ? reg.a[C] : konsta[C].v);
reg.a[B] = cls->CreateNew();
NEXTOP;
}
OP(TRY): OP(TRY):
assert(try_depth < MAX_TRY_DEPTH); assert(try_depth < MAX_TRY_DEPTH);
if (try_depth >= MAX_TRY_DEPTH) if (try_depth >= MAX_TRY_DEPTH)

View file

@ -104,6 +104,8 @@ xx(TAIL_K, tail, KPI8, TAIL, 1, REGT_POINTER),
xx(RESULT, result, __BCP, NOP, 0, 0), // Result should go in register encoded in BC (in caller, after CALL) xx(RESULT, result, __BCP, NOP, 0, 0), // Result should go in register encoded in BC (in caller, after CALL)
xx(RET, ret, I8BCP, NOP, 0, 0), // Copy value from register encoded in BC to return value A, possibly returning xx(RET, ret, I8BCP, NOP, 0, 0), // Copy value from register encoded in BC to return value A, possibly returning
xx(RETI, reti, I8I16, NOP, 0, 0), // Copy immediate from BC to return value A, possibly returning xx(RETI, reti, I8I16, NOP, 0, 0), // Copy immediate from BC to return value A, possibly returning
xx(NEW, new, RPRP, NOP, 0, 0),
xx(NEW_K, new, RPKP, NOP, 0, 0),
xx(TRY, try, I24, NOP, 0, 0), // When an exception is thrown, start searching for a handler at pc + ABC xx(TRY, try, I24, NOP, 0, 0), // When an exception is thrown, start searching for a handler at pc + ABC
xx(UNTRY, untry, I8, NOP, 0, 0), // Pop A entries off the exception stack xx(UNTRY, untry, I8, NOP, 0, 0), // Pop A entries off the exception stack
xx(THROW, throw, THROW, NOP, 0, 0), // A == 0: Throw exception object pB xx(THROW, throw, THROW, NOP, 0, 0), // A == 0: Throw exception object pB