Scripting: Add 8/16-bit integer types for struct interop

This commit is contained in:
Vicki Pfau 2022-04-27 00:29:53 -07:00
parent 4c1e736915
commit c8848876fa
3 changed files with 82 additions and 14 deletions

View File

@ -17,6 +17,10 @@ CXX_GUARD_START
#define mSCRIPT_VALUE_UNREF -1
#define mSCRIPT_PARAMS_MAX 8
#define mSCRIPT_TYPE_C_S8 int8_t
#define mSCRIPT_TYPE_C_U8 uint8_t
#define mSCRIPT_TYPE_C_S16 int16_t
#define mSCRIPT_TYPE_C_U16 uint16_t
#define mSCRIPT_TYPE_C_S32 int32_t
#define mSCRIPT_TYPE_C_U32 uint32_t
#define mSCRIPT_TYPE_C_F32 float
@ -30,6 +34,10 @@ CXX_GUARD_START
#define mSCRIPT_TYPE_C_WRAPPER struct mScriptValue*
#define mSCRIPT_TYPE_C_S(STRUCT) struct STRUCT*
#define mSCRIPT_TYPE_FIELD_S8 s32
#define mSCRIPT_TYPE_FIELD_U8 s32
#define mSCRIPT_TYPE_FIELD_S16 s32
#define mSCRIPT_TYPE_FIELD_U16 s32
#define mSCRIPT_TYPE_FIELD_S32 s32
#define mSCRIPT_TYPE_FIELD_U32 u32
#define mSCRIPT_TYPE_FIELD_F32 f32
@ -43,6 +51,10 @@ CXX_GUARD_START
#define mSCRIPT_TYPE_FIELD_WRAPPER opaque
#define mSCRIPT_TYPE_FIELD_S(STRUCT) opaque
#define mSCRIPT_TYPE_MS_S8 (&mSTSInt8)
#define mSCRIPT_TYPE_MS_U8 (&mSTUInt8)
#define mSCRIPT_TYPE_MS_S16 (&mSTSInt16)
#define mSCRIPT_TYPE_MS_U16 (&mSTUInt16)
#define mSCRIPT_TYPE_MS_S32 (&mSTSInt32)
#define mSCRIPT_TYPE_MS_U32 (&mSTUInt32)
#define mSCRIPT_TYPE_MS_F32 (&mSTFloat32)
@ -58,6 +70,10 @@ CXX_GUARD_START
#define _mSCRIPT_FIELD_NAME(V) (V)->name
#define mSCRIPT_TYPE_CMP_GENERIC(TYPE0, TYPE1) (TYPE0 == TYPE1)
#define mSCRIPT_TYPE_CMP_U8(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_U8, TYPE)
#define mSCRIPT_TYPE_CMP_S8(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_S8, TYPE)
#define mSCRIPT_TYPE_CMP_U16(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_U16, TYPE)
#define mSCRIPT_TYPE_CMP_S16(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_S16, TYPE)
#define mSCRIPT_TYPE_CMP_U32(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_U32, TYPE)
#define mSCRIPT_TYPE_CMP_S32(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_S32, TYPE)
#define mSCRIPT_TYPE_CMP_F32(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_F32, TYPE)
@ -200,6 +216,10 @@ CXX_GUARD_START
}, \
} \
#define mSCRIPT_MAKE_S8(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_S8, s32, VALUE)
#define mSCRIPT_MAKE_U8(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_U8, u32, VALUE)
#define mSCRIPT_MAKE_S16(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_S16, s32, VALUE)
#define mSCRIPT_MAKE_U16(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_U16, u32, VALUE)
#define mSCRIPT_MAKE_S32(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_S32, s32, VALUE)
#define mSCRIPT_MAKE_U32(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_U32, u32, VALUE)
#define mSCRIPT_MAKE_F32(VALUE) mSCRIPT_MAKE(mSCRIPT_TYPE_MS_F32, f32, VALUE)
@ -226,6 +246,10 @@ enum {
struct Table;
struct mScriptType;
extern const struct mScriptType mSTVoid;
extern const struct mScriptType mSTSInt8;
extern const struct mScriptType mSTUInt8;
extern const struct mScriptType mSTSInt16;
extern const struct mScriptType mSTUInt16;
extern const struct mScriptType mSTSInt32;
extern const struct mScriptType mSTUInt32;
extern const struct mScriptType mSTFloat32;

View File

@ -166,7 +166,7 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
bool ok = true;
switch (value->type->base) {
case mSCRIPT_TYPE_SINT:
if (value->type->size == 4) {
if (value->type->size <= 4) {
lua_pushinteger(luaContext->lua, value->value.s32);
} else if (value->type->size == 8) {
lua_pushinteger(luaContext->lua, value->value.s64);
@ -175,7 +175,7 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
}
break;
case mSCRIPT_TYPE_UINT:
if (value->type->size == 4) {
if (value->type->size <= 4) {
lua_pushinteger(luaContext->lua, value->value.u32);
} else if (value->type->size == 8) {
lua_pushinteger(luaContext->lua, value->value.u64);

View File

@ -45,6 +45,50 @@ const struct mScriptType mSTVoid = {
.cast = NULL,
};
const struct mScriptType mSTSInt8 = {
.base = mSCRIPT_TYPE_SINT,
.size = 1,
.name = "s8",
.alloc = NULL,
.free = NULL,
.hash = _hashScalar,
.equal = _s32Equal,
.cast = _castScalar,
};
const struct mScriptType mSTUInt8 = {
.base = mSCRIPT_TYPE_UINT,
.size = 1,
.name = "u8",
.alloc = NULL,
.free = NULL,
.hash = _hashScalar,
.equal = _u32Equal,
.cast = _castScalar,
};
const struct mScriptType mSTSInt16 = {
.base = mSCRIPT_TYPE_SINT,
.size = 2,
.name = "s16",
.alloc = NULL,
.free = NULL,
.hash = _hashScalar,
.equal = _s32Equal,
.cast = _castScalar,
};
const struct mScriptType mSTUInt16 = {
.base = mSCRIPT_TYPE_UINT,
.size = 2,
.name = "u16",
.alloc = NULL,
.free = NULL,
.hash = _hashScalar,
.equal = _u32Equal,
.cast = _castScalar,
};
const struct mScriptType mSTSInt32 = {
.base = mSCRIPT_TYPE_SINT,
.size = 4,
@ -222,14 +266,14 @@ uint32_t _hashScalar(const struct mScriptValue* val) {
bool _as ## NAME(const struct mScriptValue* input, mSCRIPT_TYPE_C_ ## TYPE * T) { \
switch (input->type->base) { \
case mSCRIPT_TYPE_SINT: \
if (input->type->size == 4) { \
if (input->type->size <= 4) { \
*T = input->value.s32; \
} else if (input->type->size == 8) { \
*T = input->value.s64; \
} \
break; \
case mSCRIPT_TYPE_UINT: \
if (input->type->size == 4) { \
if (input->type->size <= 4) { \
*T = input->value.u32; \
} else if (input->type->size == 8) { \
*T = input->value.u64; \
@ -258,7 +302,7 @@ _mAPPLY(AS(Float64, F64));
bool _castScalar(const struct mScriptValue* input, const struct mScriptType* type, struct mScriptValue* output) {
switch (type->base) {
case mSCRIPT_TYPE_SINT:
if (type->size == 4) {
if (type->size <= 4) {
if (!_asSInt32(input, &output->value.s32)) {
return false;
}
@ -271,7 +315,7 @@ bool _castScalar(const struct mScriptValue* input, const struct mScriptType* typ
}
break;
case mSCRIPT_TYPE_UINT:
if (type->size == 4) {
if (type->size <= 4) {
if (!_asUInt32(input, &output->value.u32)) {
return false;
}
@ -333,7 +377,7 @@ bool _s32Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
int32_t val;
switch (b->type->base) {
case mSCRIPT_TYPE_SINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
val = b->value.s32;
} else if (b->type->size == 8) {
if (b->value.s64 > INT_MAX || b->value.s64 < INT_MIN) {
@ -348,7 +392,7 @@ bool _s32Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
if (a->value.s32 < 0) {
return false;
}
if (b->type->size == 4) {
if (b->type->size <= 4) {
if (b->value.u32 > (uint32_t) INT_MAX) {
return false;
}
@ -374,7 +418,7 @@ bool _u32Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
uint32_t val;
switch (b->type->base) {
case mSCRIPT_TYPE_SINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
if (a->value.u32 > (uint32_t) INT_MAX) {
return false;
}
@ -392,7 +436,7 @@ bool _u32Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
}
break;
case mSCRIPT_TYPE_UINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
val = b->value.u32;
} else if (b->type->size == 8) {
if (b->value.u64 > UINT_MAX) {
@ -433,7 +477,7 @@ bool _s64Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
int64_t val;
switch (b->type->base) {
case mSCRIPT_TYPE_SINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
val = b->value.s32;
} else if (b->type->size == 8) {
val = b->value.s64;
@ -445,7 +489,7 @@ bool _s64Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
if (a->value.s64 < 0) {
return false;
}
if (b->type->size == 4) {
if (b->type->size <= 4) {
val = b->value.u32;
} else if (b->type->size == 8) {
if (b->value.u64 > (uint64_t) INT64_MAX) {
@ -468,7 +512,7 @@ bool _u64Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
uint64_t val;
switch (b->type->base) {
case mSCRIPT_TYPE_SINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
if (a->value.u64 > (uint64_t) INT_MAX) {
return false;
}
@ -489,7 +533,7 @@ bool _u64Equal(const struct mScriptValue* a, const struct mScriptValue* b) {
}
break;
case mSCRIPT_TYPE_UINT:
if (b->type->size == 4) {
if (b->type->size <= 4) {
val = b->value.u32;
} else if (b->type->size == 8) {
val = b->value.u64;