Quellcode durchsuchen

Bring functions example up to speed

Ole vor 9 Jahren
Ursprung
Commit
2d7f5e8bb0
2 geänderte Dateien mit 8 neuen und 13 gelöschten Zeilen
  1. 7 12
      examples/functions.cpp
  2. 1 1
      lib/luwra/functions.hpp

+ 7 - 12
examples/functions.cpp

@@ -18,8 +18,8 @@ int my_function_3(int a, int b) {
 }
 
 int main() {
-	lua_State* state = luaL_newstate();
-	luaL_openlibs(state);
+	luwra::StateWrapper state;
+	state.loadStandardLibrary();
 
 	// Register 'my_function_1'
 	auto wrapped_1 = LUWRA_WRAP(my_function_1);
@@ -34,8 +34,7 @@ int main() {
 	luwra::setGlobal(state, "my_function_3", wrapped_3);
 
 	// Load Lua code
-	luaL_loadstring(
-		state,
+	int ret = state.runString(
 		// Invoke 'my_function_1'
 		"my_function_1(1337, 'Hello')\n"
 
@@ -49,14 +48,10 @@ int main() {
 	);
 
 	// Invoke the attached script
-	if (lua_pcall(state, 0, LUA_MULTRET, 0) != 0) {
-		const char* error_msg = lua_tostring(state, -1);
-		std::cerr << "An error occured: " << error_msg << std::endl;
-
-		lua_close(state);
+	if (ret != LUA_OK) {
+		std::cerr << "An error occured: " << lua_tostring(state, -1) << std::endl;
 		return 1;
-	} else {
-		lua_close(state);
-		return 0;
 	}
+
+	return 0;
 }

+ 1 - 1
lib/luwra/functions.hpp

@@ -99,7 +99,7 @@ struct Value<NativeFunction<R>> {
 	static inline
 	NativeFunction<R> read(State* state, int index) {
 		luaL_checktype(state, index, LUA_TFUNCTION);
-		return NativeFunction<R>(state, index);
+		return {state, index};
 	}
 };