1
1
#include " Wren++.h"
2
- #include < cstdlib> // for malloc
3
- #include < cstring> // for strcmp, memcpy
2
+ #include < cstdlib> // for malloc
3
+ #include < cstring> // for strcmp, memcpy
4
4
#include < cassert>
5
5
#include < iostream>
6
6
7
7
namespace
8
8
{
9
-
10
9
struct BoundState
11
10
{
12
- std::unordered_map< std::size_t , WrenForeignMethodFn > methods{};
13
- std::unordered_map< std::size_t , WrenForeignClassMethods > classes{};
11
+ std::unordered_map<std::size_t , WrenForeignMethodFn> methods{};
12
+ std::unordered_map<std::size_t , WrenForeignClassMethods> classes{};
14
13
};
15
14
16
- WrenForeignMethodFn foreignMethodProvider (WrenVM* vm,
15
+ WrenForeignMethodFn foreignMethodProvider (
16
+ WrenVM* vm,
17
17
const char * module,
18
18
const char * className,
19
19
bool isStatic,
20
20
const char * signature)
21
21
{
22
22
auto * boundState = (BoundState*)wrenGetUserData (vm);
23
- auto it = boundState->methods .find (wrenpp::detail::hashMethodSignature (module, className, isStatic, signature));
23
+ auto it = boundState->methods .find (
24
+ wrenpp::detail::hashMethodSignature (module, className, isStatic, signature));
24
25
if (it == boundState->methods .end ())
25
26
{
26
27
return NULL ;
@@ -35,7 +36,7 @@ WrenForeignClassMethods foreignClassProvider(WrenVM* vm, const char* m, const ch
35
36
auto it = boundState->classes .find (wrenpp::detail::hashClassSignature (m, c));
36
37
if (it == boundState->classes .end ())
37
38
{
38
- return WrenForeignClassMethods{ nullptr , nullptr };
39
+ return WrenForeignClassMethods{nullptr , nullptr };
39
40
}
40
41
41
42
return it->second ;
@@ -45,22 +46,16 @@ inline const char* errorTypeToString(WrenErrorType type)
45
46
{
46
47
switch (type)
47
48
{
48
- case WREN_ERROR_COMPILE: return " WREN_ERROR_COMPILE" ;
49
- case WREN_ERROR_RUNTIME: return " WREN_ERROR_RUNTIME" ;
50
- case WREN_ERROR_STACK_TRACE: return " WREN_ERROR_STACK_TRACE" ;
51
- default : assert (false ); return " " ;
49
+ case WREN_ERROR_COMPILE: return " WREN_ERROR_COMPILE" ;
50
+ case WREN_ERROR_RUNTIME: return " WREN_ERROR_RUNTIME" ;
51
+ case WREN_ERROR_STACK_TRACE: return " WREN_ERROR_STACK_TRACE" ;
52
+ default : assert (false ); return " " ;
52
53
}
53
54
}
54
55
55
- char * loadModuleFnWrapper (WrenVM* vm, const char * mod)
56
- {
57
- return wrenpp::VM::loadModuleFn (mod);
58
- }
56
+ char * loadModuleFnWrapper (WrenVM* vm, const char * mod) { return wrenpp::VM::loadModuleFn (mod); }
59
57
60
- void writeFnWrapper (WrenVM* vm, const char * text)
61
- {
62
- wrenpp::VM::writeFn (text);
63
- }
58
+ void writeFnWrapper (WrenVM* vm, const char * text) { wrenpp::VM::writeFn (text); }
64
59
65
60
void errorFnWrapper (WrenVM*, WrenErrorType type, const char * module, int line, const char * message)
66
61
{
@@ -71,64 +66,51 @@ void* reallocateFnWrapper(void* memory, std::size_t newSize)
71
66
{
72
67
return wrenpp::VM::reallocateFn (memory, newSize);
73
68
}
74
-
75
- }
69
+ } // namespace
76
70
77
71
namespace wrenpp
78
72
{
79
-
80
73
namespace detail
81
74
{
82
- void registerFunction (WrenVM* vm, const std::string& mod, const std::string& cName, bool isStatic, std::string sig, WrenForeignMethodFn function)
75
+ void registerFunction (
76
+ WrenVM* vm,
77
+ const std::string& mod,
78
+ const std::string& cName,
79
+ bool isStatic,
80
+ std::string sig,
81
+ WrenForeignMethodFn function)
83
82
{
84
83
BoundState* boundState = (BoundState*)wrenGetUserData (vm);
85
- std::size_t hash = detail::hashMethodSignature (mod.c_str (), cName.c_str (), isStatic, sig.c_str ());
84
+ std::size_t hash =
85
+ detail::hashMethodSignature (mod.c_str (), cName.c_str (), isStatic, sig.c_str ());
86
86
boundState->methods .insert (std::make_pair (hash, function));
87
87
}
88
88
89
- void registerClass (WrenVM* vm, const std::string& mod, std::string cName, WrenForeignClassMethods methods)
89
+ void registerClass (
90
+ WrenVM* vm,
91
+ const std::string& mod,
92
+ std::string cName,
93
+ WrenForeignClassMethods methods)
90
94
{
91
95
BoundState* boundState = (BoundState*)wrenGetUserData (vm);
92
96
std::size_t hash = detail::hashClassSignature (mod.c_str (), cName.c_str ());
93
97
boundState->classes .insert (std::make_pair (hash, methods));
94
98
}
95
-
96
- }
99
+ } // namespace detail
97
100
98
101
Value null = Value();
99
102
100
- Value::Value (bool val)
101
- : type_{ WREN_TYPE_BOOL }, string_{ nullptr }
102
- {
103
- set (val);
104
- }
103
+ Value::Value (bool val) : type_{WREN_TYPE_BOOL}, string_{nullptr } { set (val); }
105
104
106
- Value::Value (float val)
107
- : type_{ WREN_TYPE_NUM }, string_{ nullptr }
108
- {
109
- set (val);
110
- }
105
+ Value::Value (float val) : type_{WREN_TYPE_NUM}, string_{nullptr } { set (val); }
111
106
112
- Value::Value (double val)
113
- : type_{ WREN_TYPE_NUM }, string_{ nullptr }
114
- {
115
- set (val);
116
- }
107
+ Value::Value (double val) : type_{WREN_TYPE_NUM}, string_{nullptr } { set (val); }
117
108
118
- Value::Value (int val)
119
- : type_{ WREN_TYPE_NUM }, string_{ nullptr }
120
- {
121
- set (val);
122
- }
109
+ Value::Value (int val) : type_{WREN_TYPE_NUM}, string_{nullptr } { set (val); }
123
110
124
- Value::Value (unsigned int val)
125
- : type_{ WREN_TYPE_NUM }, string_{ nullptr }
126
- {
127
- set (val);
128
- }
111
+ Value::Value (unsigned int val) : type_{WREN_TYPE_NUM}, string_{nullptr } { set (val); }
129
112
130
- Value::Value (const char * str)
131
- : type_{ WREN_TYPE_STRING }, string_{ nullptr }
113
+ Value::Value (const char * str) : type_{WREN_TYPE_STRING}, string_{nullptr }
132
114
{
133
115
string_ = (char *)VM::reallocateFn (nullptr , std::strlen (str) + 1 );
134
116
std::strcpy (string_, str);
@@ -143,15 +125,11 @@ Value::~Value()
143
125
}
144
126
145
127
Method::Method (VM* vm, WrenHandle* variable, WrenHandle* method)
146
- : vm_(vm),
147
- method_ (method),
148
- variable_(variable)
149
- {}
150
-
151
- Method::Method (Method&& other)
152
- : vm_(other.vm_),
153
- method_(other.method_),
154
- variable_(other.variable_)
128
+ : vm_(vm), method_(method), variable_(variable)
129
+ {
130
+ }
131
+
132
+ Method::Method (Method&& other) : vm_(other.vm_), method_(other.method_), variable_(other.variable_)
155
133
{
156
134
other.vm_ = nullptr ;
157
135
other.method_ = nullptr ;
@@ -179,28 +157,22 @@ Method& Method::operator=(Method&& rhs)
179
157
rhs.method_ = nullptr ;
180
158
rhs.variable_ = nullptr ;
181
159
}
182
-
160
+
183
161
return *this ;
184
162
}
185
163
186
- ClassContext ModuleContext::beginClass (std::string c)
187
- {
188
- return ClassContext (c, *this );
189
- }
164
+ ClassContext ModuleContext::beginClass (std::string c) { return ClassContext (c, *this ); }
190
165
191
166
void ModuleContext::endModule () {}
192
167
193
- ModuleContext& ClassContext::endClass ()
194
- {
195
- return module_;
196
- }
168
+ ModuleContext& ClassContext::endClass () { return module_; }
197
169
198
- ClassContext::ClassContext (std::string c, ModuleContext& mod)
199
- : module_(mod),
200
- class_ (c)
201
- {}
170
+ ClassContext::ClassContext (std::string c, ModuleContext& mod) : module_(mod), class_(c) {}
202
171
203
- ClassContext& ClassContext::bindCFunction (bool isStatic, std::string signature, WrenForeignMethodFn function)
172
+ ClassContext& ClassContext::bindCFunction (
173
+ bool isStatic,
174
+ std::string signature,
175
+ WrenForeignMethodFn function)
204
176
{
205
177
detail::registerFunction (module_.vm_ , module_.name_ , class_, isStatic, signature, function);
206
178
return *this ;
@@ -211,8 +183,7 @@ ClassContext& ClassContext::bindCFunction(bool isStatic, std::string signature,
211
183
* Uses malloc, because our reallocateFn is set to default:
212
184
* it uses malloc, realloc and free.
213
185
* */
214
- LoadModuleFn VM::loadModuleFn = [](const char * mod) -> char *
215
- {
186
+ LoadModuleFn VM::loadModuleFn = [](const char * mod) -> char * {
216
187
std::string path (mod);
217
188
path += " .wren" ;
218
189
std::string source;
@@ -230,17 +201,15 @@ LoadModuleFn VM::loadModuleFn = [](const char* mod) -> char*
230
201
return buffer;
231
202
};
232
203
233
- WriteFn VM::writeFn = [](const char * text) -> void
234
- {
235
- std::cout << text;
236
- };
204
+ WriteFn VM::writeFn = [](const char * text) -> void { std::cout << text; };
237
205
238
- ErrorFn VM::errorFn = [](WrenErrorType type, const char * module_name, int line, const char * message) -> void
239
- {
206
+ ErrorFn VM::errorFn =
207
+ [](WrenErrorType type, const char * module_name, int line, const char * message) -> void {
240
208
const char * typeStr = errorTypeToString (type);
241
209
if (module_name)
242
210
{
243
- std::cout << typeStr << " in " << module_name << " :" << line << " > " << message << std::endl;
211
+ std::cout << typeStr << " in " << module_name << " :" << line << " > " << message
212
+ << std::endl;
244
213
}
245
214
else
246
215
{
@@ -258,8 +227,7 @@ int VM::heapGrowthPercent = 50;
258
227
259
228
std::size_t VM::chunkSize = 0x500000u ;
260
229
261
- VM::VM ()
262
- : vm_{ nullptr }
230
+ VM::VM () : vm_{nullptr }
263
231
{
264
232
WrenConfiguration configuration{};
265
233
wrenInitConfiguration (&configuration);
@@ -277,11 +245,7 @@ VM::VM()
277
245
vm_ = wrenNewVM (&configuration);
278
246
}
279
247
280
- VM::VM (VM&& other)
281
- : vm_{ other.vm_ }
282
- {
283
- other.vm_ = nullptr ;
284
- }
248
+ VM::VM (VM&& other) : vm_{other.vm_ } { other.vm_ = nullptr ; }
285
249
286
250
VM& VM::operator =(VM&& rhs)
287
251
{
@@ -337,16 +301,9 @@ Result VM::executeString(const std::string& code)
337
301
return Result::Success;
338
302
}
339
303
340
- void VM::collectGarbage ()
341
- {
342
- wrenCollectGarbage (vm_);
343
- }
304
+ void VM::collectGarbage () { wrenCollectGarbage (vm_); }
344
305
345
- Method VM::method (
346
- const std::string& mod,
347
- const std::string& var,
348
- const std::string& sig
349
- )
306
+ Method VM::method (const std::string& mod, const std::string& var, const std::string& sig)
350
307
{
351
308
wrenEnsureSlots (vm_, 1 );
352
309
wrenGetVariable (vm_, mod.c_str (), var.c_str (), 0 );
@@ -355,9 +312,5 @@ Method VM::method(
355
312
return Method (this , variable, handle);
356
313
}
357
314
358
- ModuleContext VM::beginModule (std::string name)
359
- {
360
- return ModuleContext (vm_, name);
361
- }
362
-
363
- }
315
+ ModuleContext VM::beginModule (std::string name) { return ModuleContext (vm_, name); }
316
+ } // namespace wrenpp
0 commit comments