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*);
24
25#ifdef USE_DEBUGGERS
26static void mPythonScriptDebuggerEntered(struct mScriptEngine*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
27#endif
28
29struct mPythonScriptEngine {
30 struct mScriptEngine d;
31 struct mScriptBridge* sb;
32};
33
34struct mPythonScriptEngine* mPythonCreateScriptEngine(void) {
35 struct mPythonScriptEngine* engine = malloc(sizeof(*engine));
36 engine->d.name = mPythonScriptEngineName;
37 engine->d.init = mPythonScriptEngineInit;
38 engine->d.deinit = mPythonScriptEngineDeinit;
39 engine->d.isScript = mPythonScriptEngineIsScript;
40 engine->d.loadScript = mPythonScriptEngineLoadScript;
41 engine->d.run = mPythonScriptEngineRun;
42#ifdef USE_DEBUGGERS
43 engine->d.debuggerEntered = mPythonScriptDebuggerEntered;
44#endif
45 engine->sb = NULL;
46 return engine;
47}
48
49void mPythonSetup(struct mScriptBridge* sb) {
50 struct mPythonScriptEngine* se = mPythonCreateScriptEngine();
51 mScriptBridgeInstallEngine(sb, &se->d);
52}
53
54const char* mPythonScriptEngineName(struct mScriptEngine* se) {
55 UNUSED(se);
56 return "python";
57}
58
59bool mPythonScriptEngineInit(struct mScriptEngine* se, struct mScriptBridge* sb) {
60 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
61 engine->sb = sb;
62 return true;
63}
64
65void mPythonScriptEngineDeinit(struct mScriptEngine* se) {
66 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
67 free(se);
68}
69
70bool mPythonScriptEngineIsScript(struct mScriptEngine* se, const char* name, struct VFile* vf) {
71 UNUSED(se);
72 UNUSED(vf);
73 return endswith(name, ".py");
74}
75
76bool mPythonScriptEngineLoadScript(struct mScriptEngine* se, const char* name, struct VFile* vf) {
77 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
78 return mPythonLoadScript(name, vf);
79}
80
81void mPythonScriptEngineRun(struct mScriptEngine* se) {
82 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
83
84 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
85 if (debugger) {
86 mPythonSetDebugger(debugger);
87 }
88
89 mPythonRunPending();
90}
91
92#ifdef USE_DEBUGGERS
93void mPythonScriptDebuggerEntered(struct mScriptEngine* se, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
94 struct mPythonScriptEngine* engine = (struct mPythonScriptEngine*) se;
95
96 struct mDebugger* debugger = mScriptBridgeGetDebugger(engine->sb);
97 if (!debugger) {
98 return;
99 }
100
101 mPythonDebuggerEntered(reason, info);
102}
103#endif