Scripting: Add lambdas that bind an object method call

This commit is contained in:
Vicki Pfau 2023-04-24 03:41:33 -07:00
parent b8261a0c66
commit 0b79184bf3
2 changed files with 35 additions and 0 deletions

View File

@ -345,6 +345,7 @@ bool mScriptObjectGetConst(const struct mScriptValue* obj, const char* member, s
bool mScriptObjectSet(struct mScriptValue* obj, const char* member, struct mScriptValue*); bool mScriptObjectSet(struct mScriptValue* obj, const char* member, struct mScriptValue*);
bool mScriptObjectCast(const struct mScriptValue* input, const struct mScriptType* type, struct mScriptValue* output) ; bool mScriptObjectCast(const struct mScriptValue* input, const struct mScriptType* type, struct mScriptValue* output) ;
void mScriptObjectFree(struct mScriptValue* obj); void mScriptObjectFree(struct mScriptValue* obj);
struct mScriptValue* mScriptObjectBindLambda(struct mScriptValue* obj, const char* member, struct mScriptList* args);
bool mScriptPopS32(struct mScriptList* list, int32_t* out); bool mScriptPopS32(struct mScriptList* list, int32_t* out);
bool mScriptPopU32(struct mScriptList* list, uint32_t* out); bool mScriptPopU32(struct mScriptList* list, uint32_t* out);

View File

@ -1682,6 +1682,40 @@ void mScriptObjectFree(struct mScriptValue* value) {
} }
} }
struct mScriptValue* mScriptObjectBindLambda(struct mScriptValue* obj, const char* member, struct mScriptList* args) {
if (obj->type->base != mSCRIPT_TYPE_OBJECT) {
return false;
}
struct mScriptTypeClass* cls = obj->type->details.cls;
if (!cls) {
return false;
}
mScriptClassInit(cls);
struct mScriptList arguments;
struct mScriptValue fn;
if (!mScriptObjectGetConst(obj, member, &fn)) {
return NULL;
}
mScriptListInit(&arguments, 0);
mScriptValueWrap(obj, mScriptListAppend(&arguments));
if (args) {
size_t i;
for (i = 0; i < mScriptListSize(args); ++i) {
memcpy(mScriptListAppend(&arguments), mScriptListGetConstPointer(args, i), sizeof(struct mScriptValue));
}
}
struct mScriptValue* value = mScriptValueAlloc(fn.type);
struct mScriptValue* lambda = mScriptLambdaCreate0(value, &arguments);
mScriptValueDeref(value);
mScriptListDeinit(&arguments);
return lambda;
}
bool mScriptPopS32(struct mScriptList* list, int32_t* out) { bool mScriptPopS32(struct mScriptList* list, int32_t* out) {
mSCRIPT_POP(list, S32, val); mSCRIPT_POP(list, S32, val);
*out = val; *out = val;