Cogs.Core
ScriptingEngine.h
1#pragma once
2
3#include "Base.h"
4#include "Utilities/Parsing.h"
5
6#include "Foundation/Collections/Pool.h"
7#include "Foundation/ComponentModel/Entity.h"
8#include "Foundation/StringView.h"
9
10#include <functional>
11#include <memory>
12#include <vector>
13
14namespace Cogs
15{
16 namespace Core
17 {
18 class Context;
19
20 enum class ScriptFlags
21 {
22 None = 0,
23 Unknown = 1 << 0,
24 JavaScript = 1 << 1,
25 Lua = 1 << 2,
26 SourceText = 1 << 3,
27 SourcePath = 1 << 4,
28 };
29
30 ENABLE_ENUM_FLAGS(ScriptFlags);
31
32 enum class ScriptValueType
33 {
34 Undefined = 0,
35 Object = 1,
36 String = 2,
37 Proxy = 3,
38 };
39
40 struct ScriptObject;
41
42 struct ScriptArg
43 {
44 std::string stringValue;
45 };
46
47 using ScriptArgs = std::vector<ScriptArg>;
48
49 using ScriptFunctionType = std::function<ScriptObject *(ScriptObject *, const ScriptArgs & args)>;
50 using ScriptProxyGet = std::function<ScriptObject *(ScriptObject *, const StringView & key)>;
51 using ScriptProxySet = std::function<bool(ScriptObject *, const StringView & key, const ScriptArg & arg)>;
52
54 {
55 ScriptFunctionType func;
56 ScriptValueType retValue;
57 std::vector<ScriptValueType> argTypes;
58 };
59
61 {
62 ScriptFunctionType getter;
63 ScriptFunctionType setter;
64 ScriptValueType type;
65 };
66
67 class ScriptContext;
68
70 {
71 std::vector<ScriptFunction> functions;
72 ScriptValueType type;
73 ScriptProxyGet proxyGet;
74 ScriptProxySet proxySet;
75 std::weak_ptr<ScriptContext> context;
76 void * heapPtr;
77 int heapIndex;
78 int generation;
79 };
80
82 {
83 public:
84 virtual ~ScriptContext() {}
85 virtual void update() {}
86
87 virtual bool addScript(const StringView & /*source*/, ScriptFlags /*flags*/ = ScriptFlags::None) { return false; }
88 virtual Cogs::StringView eval(const StringView & /*str*/) { return {}; }
89
90 virtual ScriptObject * createObject(ScriptValueType /*type*/ = ScriptValueType::Object, void * /*value*/ = nullptr) { return nullptr; }
91 virtual ScriptObject * createObjectProxy(ScriptProxyGet /*get*/, ScriptProxySet /*set*/) { return nullptr; }
92
93 virtual void setProperty(ScriptObject * /*target*/, const StringView & /*name*/, ScriptObject * /*value*/) {}
94 virtual void setProperty(ScriptObject * /*target*/, const StringView & /*name*/, const StringView & /*value*/) {}
95 virtual void setProperty(ScriptObject * /*target*/, const StringView & /*name*/, double /*value*/) {}
96
97 virtual void addFunction(ScriptObject *, const StringView & /*name*/, ScriptValueType /*returnValue*/, std::vector<ScriptValueType> /*args*/, ScriptFunctionType /*func*/) {}
98 virtual void addProperty(ScriptObject *, const StringView & /*name*/, ScriptValueType /*type*/, ScriptFunctionType /*getter*/, ScriptFunctionType /*setter*/) {}
99
100 virtual void callFunction(ScriptObject *, const StringView & /*name*/, ScriptObject * /*arg*/) {}
101
102 std::string source;
103 std::string content;
104
105 std::string evalResult;
106 std::string evalStack;
107
109 std::weak_ptr<ScriptContext> self;
110 };
111
112 using ScriptContextHandle = std::shared_ptr<ScriptContext>;
113
115 {
116 public:
117 virtual ~ScriptingEngine() {}
118
119 virtual void initialize() = 0;
120 virtual void cleanup() = 0;
121
122 virtual Cogs::StringView eval(const StringView & /*str*/) { return {}; }
123 virtual void completions(const TokenStream & /*tokens*/, TokenStream & /*completions*/) {}
124
125 virtual ScriptContextHandle createContext(Cogs::ComponentModel::Entity * /*entity*/, const StringView & /*source*/, ScriptFlags /*flags*/) = 0;
126
128 {
129 std::string name;
130
131 std::string buffer = std::string(16384, '\0');
132 std::vector<std::string> history;
133 std::vector<std::string> commandHistory;
134 int historyIndex = 0;
135 bool movedUp = false;
136 bool movedDown = false;
137
138 TokenStream currentCompletions;
139 int completionIndex = 0;
140 size_t completionOffset = 0;
141 std::string original;
142 std::string suggested;
143
144 bool scrollToBottom = true;
145 } editorContext;
146
147 virtual bool canLoad(ScriptFlags flags) const = 0;
148
149 protected:
150 ScriptingEngine(Context * context) : context(context) {}
151
152 Context * context;
153 };
154 }
155}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Pool used to store elements of ElementType.
Definition: Pool.h:17