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 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
87 if (debugger) {
88 mPythonSetDebugger(debugger);
89 }
90
91 mPythonRunPending();
92}
93
94bool mPythonScriptEngineLookupSymbol(struct mScriptEngine* se, const char* name, int32_t* out) {
95 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
96 return mPythonLookupSymbol(name, out);
97}
98
99#ifdef USE_DEBUGGERS
100void mPythonScriptDebuggerEntered(struct mScriptEngine* se, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
101 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
102
103 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
104 if (!debugger) {
105 return;
106 }
107
108 mPythonSetDebugger(debugger);
109 mPythonDebuggerEntered(reason, info);
110}
111#endif