Scripting: Add Table iteration

This commit is contained in:
Vicki Pfau 2022-05-15 02:08:16 -07:00
parent c14fb54a74
commit ca073379fb
2 changed files with 60 additions and 0 deletions

View File

@ -650,6 +650,11 @@ bool mScriptTableInsert(struct mScriptValue* table, struct mScriptValue* key, st
bool mScriptTableRemove(struct mScriptValue* table, struct mScriptValue* key); bool mScriptTableRemove(struct mScriptValue* table, struct mScriptValue* key);
struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScriptValue* key); struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScriptValue* key);
bool mScriptTableClear(struct mScriptValue* table); bool mScriptTableClear(struct mScriptValue* table);
bool mScriptTableIteratorStart(struct mScriptValue* table, struct TableIterator*);
bool mScriptTableIteratorNext(struct mScriptValue* table, struct TableIterator*);
struct mScriptValue* mScriptTableIteratorGetKey(struct mScriptValue* table, struct TableIterator*);
struct mScriptValue* mScriptTableIteratorGetValue(struct mScriptValue* table, struct TableIterator*);
bool mScriptTableIteratorLookup(struct mScriptValue* table, struct TableIterator*, struct mScriptValue* key);
void mScriptFrameInit(struct mScriptFrame* frame); void mScriptFrameInit(struct mScriptFrame* frame);
void mScriptFrameDeinit(struct mScriptFrame* frame); void mScriptFrameDeinit(struct mScriptFrame* frame);

View File

@ -787,6 +787,61 @@ bool mScriptTableClear(struct mScriptValue* table) {
return true; return true;
} }
bool mScriptTableIteratorStart(struct mScriptValue* table, struct TableIterator* iter) {
if (table->type == mSCRIPT_TYPE_MS_WRAPPER) {
table = mScriptValueUnwrap(table);
}
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
return false;
}
struct Table* t = table->value.opaque;
return HashTableIteratorStart(t, iter);
}
bool mScriptTableIteratorNext(struct mScriptValue* table, struct TableIterator* iter) {
if (table->type == mSCRIPT_TYPE_MS_WRAPPER) {
table = mScriptValueUnwrap(table);
}
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
return false;
}
struct Table* t = table->value.opaque;
return HashTableIteratorNext(t, iter);
}
struct mScriptValue* mScriptTableIteratorGetKey(struct mScriptValue* table, struct TableIterator* iter) {
if (table->type == mSCRIPT_TYPE_MS_WRAPPER) {
table = mScriptValueUnwrap(table);
}
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
return NULL;
}
struct Table* t = table->value.opaque;
return HashTableIteratorGetCustomKey(t, iter);
}
struct mScriptValue* mScriptTableIteratorGetValue(struct mScriptValue* table, struct TableIterator* iter) {
if (table->type == mSCRIPT_TYPE_MS_WRAPPER) {
table = mScriptValueUnwrap(table);
}
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
return NULL;
}
struct Table* t = table->value.opaque;
return HashTableIteratorGetValue(t, iter);
}
bool mScriptTableIteratorLookup(struct mScriptValue* table, struct TableIterator* iter, struct mScriptValue* key) {
if (table->type == mSCRIPT_TYPE_MS_WRAPPER) {
table = mScriptValueUnwrap(table);
}
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
return false;
}
struct Table* t = table->value.opaque;
return HashTableIteratorLookupCustom(t, iter, key);
}
void mScriptFrameInit(struct mScriptFrame* frame) { void mScriptFrameInit(struct mScriptFrame* frame) {
mScriptListInit(&frame->arguments, 4); mScriptListInit(&frame->arguments, 4);
mScriptListInit(&frame->returnValues, 1); mScriptListInit(&frame->returnValues, 1);