Scripting: Start bringing up execution contexts

This commit is contained in:
Vicki Pfau 2022-02-15 17:44:07 -08:00
parent 2a81e5a1ba
commit c507157ab8
6 changed files with 85 additions and 11 deletions

View File

@ -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 <mgba-util/common.h>
CXX_GUARD_START
#include <mgba/script/types.h>
#include <mgba-util/table.h>
#include <mgba-util/vfs.h>
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

View File

@ -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

View File

@ -1,5 +1,6 @@
include(ExportDirectory)
set(SOURCE_FILES
context.c
types.c)
set(TEST_FILES

22
src/script/context.c Normal file
View File

@ -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 <mgba/script/context.h>
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);
}

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "util/test/suite.h"
#include <mgba/script/context.h>
#include <mgba/script/types.h>
struct Test {

View File

@ -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);
}