@@ -112,14 +112,14 @@ class ElunaObject
112
112
const char * type_name;
113
113
};
114
114
115
- template <typename T>
115
+ template <typename T = void >
116
116
struct ElunaRegister
117
117
{
118
118
const char * name;
119
119
int (*mfunc)(lua_State*, T*);
120
120
};
121
121
122
- template <typename T>
122
+ template <typename T = void >
123
123
class ElunaTemplate
124
124
{
125
125
public:
@@ -170,7 +170,7 @@ class ElunaTemplate
170
170
lua_setfield (E->L , metatable, " __add" );
171
171
172
172
// make new indexes saved to methods
173
- lua_pushcfunction (E->L , Substract );
173
+ lua_pushcfunction (E->L , Subtract );
174
174
lua_setfield (E->L , metatable, " __sub" );
175
175
176
176
// make new indexes saved to methods
@@ -347,9 +347,8 @@ class ElunaTemplate
347
347
{
348
348
// Get object pointer (and check type, no error)
349
349
ElunaObject* obj = Eluna::CHECKOBJ<ElunaObject>(L, 1 , false );
350
- if (obj && manageMemory)
351
- delete static_cast <T*>(obj->GetObj ());
352
- delete obj;
350
+ obj->~ElunaObject ();
351
+
353
352
return 0 ;
354
353
}
355
354
@@ -363,7 +362,7 @@ class ElunaTemplate
363
362
static int ArithmeticError (lua_State* L) { return luaL_error (L, " attempt to perform arithmetic on a %s value" , tname); }
364
363
static int CompareError (lua_State* L) { return luaL_error (L, " attempt to compare %s" , tname); }
365
364
static int Add (lua_State* L) { return ArithmeticError (L); }
366
- static int Substract (lua_State* L) { return ArithmeticError (L); }
365
+ static int Subtract (lua_State* L) { return ArithmeticError (L); }
367
366
static int Multiply (lua_State* L) { return ArithmeticError (L); }
368
367
static int Divide (lua_State* L) { return ArithmeticError (L); }
369
368
static int Mod (lua_State* L) { return ArithmeticError (L); }
@@ -375,6 +374,9 @@ class ElunaTemplate
375
374
static int Less (lua_State* L) { return CompareError (L); }
376
375
static int LessOrEqual (lua_State* L) { return CompareError (L); }
377
376
static int Call (lua_State* L) { return luaL_error (L, " attempt to call a %s value" , tname); }
377
+
378
+ static int MethodWrongState (lua_State* L) { luaL_error (L, " attempt to call method '%s' that does not exist for state: %d" , lua_tostring (L, lua_upvalueindex (1 )), lua_tointeger (L, lua_upvalueindex (2 ))); return 0 ; }
379
+ static int MethodUnimpl (lua_State* L) { luaL_error (L, " attempt to call method '%s' that is not implemented for this emulator" , lua_tostring (L, lua_upvalueindex (1 ))); return 0 ; }
378
380
};
379
381
380
382
template <typename T>
@@ -386,4 +388,38 @@ ElunaObject::ElunaObject(T * obj, bool manageMemory) : callstackid(1), _invalida
386
388
template <typename T> const char * ElunaTemplate<T>::tname = NULL ;
387
389
template <typename T> bool ElunaTemplate<T>::manageMemory = false ;
388
390
391
+ template <typename T>
392
+ class ElunaTemplateHelper
393
+ {
394
+ public:
395
+ static int PerformOp (lua_State* L, std::function<T(T, T)> op)
396
+ {
397
+ T val1 = Eluna::CHECKVAL<T>(L, 1 );
398
+ T val2 = Eluna::CHECKVAL<T>(L, 2 );
399
+ Eluna::Push (L, op (val1, val2));
400
+ return 1 ;
401
+ }
402
+ static int PerformOp (lua_State* L, std::function<T(T)> op)
403
+ {
404
+ T val = Eluna::CHECKVAL<T>(L, 1 );
405
+ Eluna::Push (L, op (val));
406
+ return 1 ;
407
+ }
408
+ static int ToString (lua_State* L)
409
+ {
410
+ T val = Eluna::CHECKVAL<T>(L, 1 );
411
+ std::ostringstream ss;
412
+ ss << val;
413
+ Eluna::Push (L, ss.str ());
414
+ return 1 ;
415
+ }
416
+ static int Pow (lua_State* L)
417
+ {
418
+ T val1 = Eluna::CHECKVAL<T>(L, 1 );
419
+ T val2 = Eluna::CHECKVAL<T>(L, 2 );
420
+ Eluna::Push (L, static_cast <T>(powl (static_cast <long double >(val1), static_cast <long double >(val2))));
421
+ return 1 ;
422
+ }
423
+ };
424
+
389
425
#endif
0 commit comments