From c507157ab8d5266eb5272bec92781a8d0759555b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 15 Feb 2022 17:44:07 -0800 Subject: [PATCH] Scripting: Start bringing up execution contexts --- include/mgba/script/context.h | 56 +++++++++++++++++++++++++++++++++++ include/mgba/script/types.h | 9 +++--- src/script/CMakeLists.txt | 1 + src/script/context.c | 22 ++++++++++++++ src/script/test/types.c | 1 + src/script/types.c | 7 ----- 6 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 include/mgba/script/context.h create mode 100644 src/script/context.c diff --git a/include/mgba/script/context.h b/include/mgba/script/context.h new file mode 100644 index 000000000..9c2310a41 --- /dev/null +++ b/include/mgba/script/context.h @@ -0,0 +1,56 @@ +/* Copyright (c) 2013-2022 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_CONTEXT_H +#define M_SCRIPT_CONTEXT_H + +#include + +CXX_GUARD_START + +#include +#include +#include + +struct mScriptFrame; +struct mScriptFunction; +struct mScriptEngineContext; + +struct mScriptContext { + struct mScriptValue rootScope; + struct Table engines; +}; + +struct mScriptEngine2 { + const char* name; + + void (*init)(struct mScriptEngine2*); + void (*deinit)(struct mScriptEngine2*); + + struct mScriptEngineContext* (*create)(struct mScriptEngine2*, struct mScriptContext*); +}; + +struct mScriptEngineContext { + struct mScriptContext* context; + void (*destroy)(struct mScriptEngineContext*); + + bool (*setGlobal)(struct mScriptEngineContext*, const char* name, struct mScriptValue*); + struct mScriptValue* (*getGlobal)(struct mScriptEngineContext*, const char* name); + + bool (*load)(struct mScriptEngineContext*, struct VFile*, const char** error); + bool (*run)(struct mScriptEngineContext*); +}; + +void mScriptContextInit(struct mScriptContext*); +void mScriptContextDeinit(struct mScriptContext*); + +void mScriptContextRegisterEngine(struct mScriptContext*, struct mScriptEngine2*); + +void mScriptContextAddGlobal(struct mScriptContext*, const char* key, struct mScriptValue* value); +void mScriptContextRemoveGlobal(struct mScriptContext*, const char* key); + +bool mScriptInvoke(const struct mScriptFunction* fn, struct mScriptFrame* frame); + +#endif diff --git a/include/mgba/script/types.h b/include/mgba/script/types.h index 1e9ee73c2..0af2c11db 100644 --- a/include/mgba/script/types.h +++ b/include/mgba/script/types.h @@ -131,7 +131,8 @@ CXX_GUARD_START } #define mSCRIPT_BIND_FUNCTION(NAME, RETURN, FUNCTION, NPARAMS, ...) \ - static bool _binding_ ## NAME(struct mScriptFrame* frame) { \ + static bool _binding_ ## NAME(struct mScriptFrame* frame, void* ctx) { \ + UNUSED(ctx); \ mSCRIPT_POP_ ## NPARAMS(&frame->arguments, __VA_ARGS__); \ if (mScriptListSize(&frame->arguments)) { \ return false; \ @@ -154,7 +155,8 @@ CXX_GUARD_START }; #define mSCRIPT_BIND_VOID_FUNCTION(NAME, FUNCTION, NPARAMS, ...) \ - static bool _binding_ ## NAME(struct mScriptFrame* frame) { \ + static bool _binding_ ## NAME(struct mScriptFrame* frame, void* ctx) { \ + UNUSED(ctx); \ mSCRIPT_POP_ ## NPARAMS(&frame->arguments, __VA_ARGS__); \ if (mScriptListSize(&frame->arguments)) { \ return false; \ @@ -276,7 +278,7 @@ struct mScriptFrame { struct mScriptFunction { struct mScriptTypeFunction signature; - bool (*call)(struct mScriptFrame*); + bool (*call)(struct mScriptFrame*, void* context); void* context; }; @@ -303,6 +305,5 @@ bool mScriptPopPointer(struct mScriptList* list, void** out); bool mScriptCast(const struct mScriptType* type, const struct mScriptValue* input, struct mScriptValue* output); bool mScriptCoerceFrame(const struct mScriptTypeTuple* types, struct mScriptList* frame); -bool mScriptInvoke(const struct mScriptFunction* fn, struct mScriptFrame* frame); #endif diff --git a/src/script/CMakeLists.txt b/src/script/CMakeLists.txt index 5d1d1ae6f..c7b39d104 100644 --- a/src/script/CMakeLists.txt +++ b/src/script/CMakeLists.txt @@ -1,5 +1,6 @@ include(ExportDirectory) set(SOURCE_FILES + context.c types.c) set(TEST_FILES diff --git a/src/script/context.c b/src/script/context.c new file mode 100644 index 000000000..f8ae22e19 --- /dev/null +++ b/src/script/context.c @@ -0,0 +1,22 @@ +/* Copyright (c) 2013-2022 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/. */ +#include + +void mScriptContextInit(struct mScriptContext* context) { + // TODO: rootScope + HashTableInit(&context->engines, 0, NULL); +} + +void mScriptContextDeinit(struct mScriptContext* context) { + HashTableDeinit(&context->engines); +} + +bool mScriptInvoke(const struct mScriptFunction* fn, struct mScriptFrame* frame) { + if (!mScriptCoerceFrame(&fn->signature.parameters, &frame->arguments)) { + return false; + } + return fn->call(frame, fn->context); +} diff --git a/src/script/test/types.c b/src/script/test/types.c index fc0e85a33..caeb8cd05 100644 --- a/src/script/test/types.c +++ b/src/script/test/types.c @@ -5,6 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "util/test/suite.h" +#include #include struct Test { diff --git a/src/script/types.c b/src/script/types.c index c750b1810..ff7cc1e24 100644 --- a/src/script/types.c +++ b/src/script/types.c @@ -723,10 +723,3 @@ bool mScriptCoerceFrame(const struct mScriptTypeTuple* types, struct mScriptList } return true; } - -bool mScriptInvoke(const struct mScriptFunction* fn, struct mScriptFrame* frame) { - if (!mScriptCoerceFrame(&fn->signature.parameters, &frame->arguments)) { - return false; - } - return fn->call(frame); -}