src/platform/python/engine.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "engine.h"
7
8#include <mgba/core/scripting.h>
9#include <mgba-util/string.h>
10#include <mgba-util/vfs.h>
11
12#ifdef USE_DEBUGGERS
13#include <mgba/debugger/debugger.h>
14#endif
15
16#include "lib.h"
17
18static const char* mPythonScriptEngineName(struct mScriptEngine*);
19static bool mPythonScriptEngineInit(struct mScriptEngine*, struct mScriptBridge*);
20static void mPythonScriptEngineDeinit(struct mScriptEngine*);
21static bool mPythonScriptEngineIsScript(struct mScriptEngine*, const char* name, struct VFile* vf);
22static bool mPythonScriptEngineLoadScript(struct mScriptEngine*, const char* name, struct VFile* vf);
23static void mPythonScriptEngineRun(struct mScriptEngine*);
24static bool mPythonScriptEngineLookupSymbol(struct mScriptEngine*, const char* name, int32_t* out);
25
26#ifdef USE_DEBUGGERS
27static void mPythonScriptDebuggerEntered(struct mScriptEngine*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
28#endif
29
30struct mPythonScriptEngine {
31 struct mScriptEngine d;
32 struct mScriptBridge* sb;
33};
34
35struct mPythonScriptEngine* mPythonCreateScriptEngine(void) {
36 struct mPythonScriptEngine* engine = malloc(sizeof(*engine));
37 engine->d.name = mPythonScriptEngineName;
38 engine->d.init = mPythonScriptEngineInit;
39 engine->d.deinit = mPythonScriptEngineDeinit;
40 engine->d.isScript = mPythonScriptEngineIsScript;
41 engine->d.loadScript = mPythonScriptEngineLoadScript;
42 engine->d.run = mPythonScriptEngineRun;
43 engine->d.lookupSymbol = mPythonScriptEngineLookupSymbol;
44#ifdef USE_DEBUGGERS
45 engine->d.debuggerEntered = mPythonScriptDebuggerEntered;
46#endif
47 engine->sb = NULL;
48 return engine;
49}
50
51void mPythonSetup(struct mScriptBridge* sb) {
52 struct mPythonScriptEngine* se = mPythonCreateScriptEngine();
53 mScriptBridgeInstallEngine(sb, &se->d);
54}
55
56const char* mPythonScriptEngineName(struct mScriptEngine* se) {
57 UNUSED(se);
58 return "python";
59}
60
61bool mPythonScriptEngineInit(struct mScriptEngine* se, struct mScriptBridge* sb) {
62 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
63 engine->sb = sb;
64 return true;
65}
66
67void mPythonScriptEngineDeinit(struct mScriptEngine* se) {
68 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
69 free(se);
70}
71
72bool mPythonScriptEngineIsScript(struct mScriptEngine* se, const char* name, struct VFile* vf) {
73 UNUSED(se);
74 UNUSED(vf);
75 return endswith(name, ".py");
76}
77
78bool mPythonScriptEngineLoadScript(struct mScriptEngine* se, const char* name, struct VFile* vf) {
79 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
80 return mPythonLoadScript(name, vf);
81}
82
83void mPythonScriptEngineRun(struct mScriptEngine* se) {
84 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
85
86#ifdef USE_DEBUGGERS
87 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
88 if (debugger) {
89 mPythonSetDebugger(debugger);
90 }
91#endif
92
93 mPythonRunPending();
94}
95
96bool mPythonScriptEngineLookupSymbol(struct mScriptEngine* se, const char* name, int32_t* out) {
97 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
98 return mPythonLookupSymbol(name, out);
99}
100
101#ifdef USE_DEBUGGERS
102void mPythonScriptDebuggerEntered(struct mScriptEngine* se, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
103 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
104
105 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
106 if (!debugger) {
107 return;
108 }
109
110 mPythonSetDebugger(debugger);
111 mPythonDebuggerEntered(reason, info);
112}
113#endif