Scripting: Migrate some stuff from docgen into libmgba

This commit is contained in:
Vicki Pfau 2023-01-08 16:00:21 -08:00
parent 697e80a5a1
commit a154690694
3 changed files with 80 additions and 61 deletions

View File

@ -0,0 +1,20 @@
/* Copyright (c) 2013-2023 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef M_SCRIPT_TYPES_INTERNAL_H
#define M_SCRIPT_TYPES_INTERNAL_H
#include <mgba-util/common.h>
CXX_GUARD_START
struct Table;
void mScriptContextGetInputTypes(struct Table*);
void mScriptTypeAdd(struct Table*, const struct mScriptType* type);
CXX_GUARD_END
#endif

View File

@ -6,6 +6,7 @@
#include <mgba/core/core.h>
#include <mgba/core/scripting.h>
#include <mgba/core/version.h>
#include <mgba/internal/script/types.h>
#include <mgba/script/context.h>
#include <mgba-util/string.h>
@ -36,62 +37,6 @@ void explainValue(struct mScriptValue* value, const char* name, int level);
void explainValueScoped(struct mScriptValue* value, const char* name, const char* scope, int level);
void explainType(struct mScriptType* type, int level);
void addTypesFromTuple(const struct mScriptTypeTuple*);
void addTypesFromTable(struct Table*);
void addType(const struct mScriptType* type) {
if (HashTableLookup(&types, type->name) || type->isConst) {
return;
}
HashTableInsert(&types, type->name, (struct mScriptType*) type);
switch (type->base) {
case mSCRIPT_TYPE_FUNCTION:
addTypesFromTuple(&type->details.function.parameters);
addTypesFromTuple(&type->details.function.returnType);
break;
case mSCRIPT_TYPE_OBJECT:
mScriptClassInit(type->details.cls);
if (type->details.cls->parent) {
addType(type->details.cls->parent);
}
addTypesFromTable(&type->details.cls->instanceMembers);
break;
case mSCRIPT_TYPE_OPAQUE:
case mSCRIPT_TYPE_WRAPPER:
if (type->details.type) {
addType(type->details.type);
}
case mSCRIPT_TYPE_VOID:
case mSCRIPT_TYPE_SINT:
case mSCRIPT_TYPE_UINT:
case mSCRIPT_TYPE_FLOAT:
case mSCRIPT_TYPE_STRING:
case mSCRIPT_TYPE_LIST:
case mSCRIPT_TYPE_TABLE:
case mSCRIPT_TYPE_WEAKREF:
// No subtypes
break;
}
}
void addTypesFromTuple(const struct mScriptTypeTuple* tuple) {
size_t i;
for (i = 0; i < tuple->count; ++i) {
addType(tuple->entries[i]);
}
}
void addTypesFromTable(struct Table* table) {
struct TableIterator iter;
if (!HashTableIteratorStart(table, &iter)) {
return;
}
do {
struct mScriptClassMember* member = HashTableIteratorGetValue(table, &iter);
addType(member->type);
} while(HashTableIteratorNext(table, &iter));
}
void printchomp(const char* string, int level) {
char indent[(level + 1) * 2 + 1];
memset(indent, ' ', sizeof(indent) - 1);
@ -265,7 +210,7 @@ void explainObject(struct mScriptValue* value, int level) {
continue;
}
fprintf(out, "%s%s:\n", indent, details->info.member.name);
addType(details->info.member.type);
mScriptTypeAdd(&types, details->info.member.type);
if (mScriptObjectGet(value, details->info.member.name, &member)) {
struct mScriptValue* unwrappedMember;
if (member.type->base == mSCRIPT_TYPE_WRAPPER) {
@ -288,7 +233,7 @@ void explainValueScoped(struct mScriptValue* value, const char* name, const char
memset(indent, ' ', sizeof(indent) - 1);
indent[sizeof(indent) - 1] = '\0';
value = mScriptContextAccessWeakref(&context, value);
addType(value->type);
mScriptTypeAdd(&types, value->type);
fprintf(out, "%stype: %s\n", indent, value->type->name);
const char* docstring = NULL;
@ -421,7 +366,7 @@ void explainCore(struct mCore* core) {
mScriptContextAttachCore(&context, core);
struct mScriptValue* emu = mScriptContextGetGlobal(&context, "emu");
addType(emu->type);
mScriptTypeAdd(&types, emu->type);
if (mScriptObjectGet(emu, "memory", &wrapper)) {
struct mScriptValue* memory = mScriptValueUnwrap(&wrapper);
@ -434,7 +379,7 @@ void explainCore(struct mCore* core) {
fprintf(out, " %s:\n", name->value.string->buffer);
value = mScriptContextAccessWeakref(&context, value);
addType(value->type);
mScriptTypeAdd(&types, value->type);
struct mScriptFrame frame;
uint32_t baseVal;
@ -486,7 +431,7 @@ void initTypes(void) {
size_t i;
for (i = 0; baseTypes[i]; ++i) {
addType(baseTypes[i]);
mScriptTypeAdd(&types, baseTypes[i]);
}
}

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba/script/types.h>
#include <mgba/internal/script/types.h>
#include <mgba/script/context.h>
#include <mgba/script/macros.h>
#include <mgba-util/hash.h>
@ -1524,3 +1525,56 @@ bool mScriptCoerceFrame(const struct mScriptTypeTuple* types, struct mScriptList
}
return true;
}
static void addTypesFromTuple(struct Table* types, const struct mScriptTypeTuple* tuple) {
size_t i;
for (i = 0; i < tuple->count; ++i) {
mScriptTypeAdd(types, tuple->entries[i]);
}
}
static void addTypesFromTable(struct Table* types, struct Table* table) {
struct TableIterator iter;
if (!HashTableIteratorStart(table, &iter)) {
return;
}
do {
struct mScriptClassMember* member = HashTableIteratorGetValue(table, &iter);
mScriptTypeAdd(types, member->type);
} while(HashTableIteratorNext(table, &iter));
}
void mScriptTypeAdd(struct Table* types, const struct mScriptType* type) {
if (HashTableLookup(types, type->name) || type->isConst) {
return;
}
HashTableInsert(types, type->name, (struct mScriptType*) type);
switch (type->base) {
case mSCRIPT_TYPE_FUNCTION:
addTypesFromTuple(types, &type->details.function.parameters);
addTypesFromTuple(types, &type->details.function.returnType);
break;
case mSCRIPT_TYPE_OBJECT:
mScriptClassInit(type->details.cls);
if (type->details.cls->parent) {
mScriptTypeAdd(types, type->details.cls->parent);
}
addTypesFromTable(types, &type->details.cls->instanceMembers);
break;
case mSCRIPT_TYPE_OPAQUE:
case mSCRIPT_TYPE_WRAPPER:
if (type->details.type) {
mScriptTypeAdd(types, type->details.type);
}
case mSCRIPT_TYPE_VOID:
case mSCRIPT_TYPE_SINT:
case mSCRIPT_TYPE_UINT:
case mSCRIPT_TYPE_FLOAT:
case mSCRIPT_TYPE_STRING:
case mSCRIPT_TYPE_LIST:
case mSCRIPT_TYPE_TABLE:
case mSCRIPT_TYPE_WEAKREF:
// No subtypes
break;
}
}