Selaa lähdekoodia

Use StateWrapper in examples

Ole 9 vuotta sitten
vanhempi
commit
2e0ecde073
3 muutettua tiedostoa jossa 6 lisäystä ja 11 poistoa
  1. 2 3
      examples/stack.cpp
  2. 2 3
      examples/types.cpp
  3. 2 5
      examples/usertypes.cpp

+ 2 - 3
examples/stack.cpp

@@ -8,8 +8,8 @@ double sum3(int a, int b, double c) {
 }
 
 int main() {
-	lua_State* state = luaL_newstate();
-	luaL_openlibs(state);
+	luwra::StateWrapper state;
+	state.loadStandardLibrary();
 
 	// Build stack
 	luwra::push(state, 13);
@@ -26,6 +26,5 @@ int main() {
 	          << luwra::apply(state, sum3)
 	          << std::endl;
 
-	lua_close(state);
 	return 0;
 }

+ 2 - 3
examples/types.cpp

@@ -41,8 +41,8 @@ void read_chars(char a, char b) {
 }
 
 int main() {
-	lua_State* state = luaL_newstate();
-	luaL_openlibs(state);
+	luwra::StateWrapper state;
+	state.loadStandardLibrary();
 
 	// Build stack
 	luwra::push(state, 'H');
@@ -56,6 +56,5 @@ int main() {
 	luwra::push(state, 'Y', 'o');
 	luwra::apply(state, read_chars);
 
-	lua_close(state);
 	return 0;
 }

+ 2 - 5
examples/usertypes.cpp

@@ -28,8 +28,8 @@ struct Point {
 LUWRA_DEF_REGISTRY_NAME(Point, "Point")
 
 int main() {
-	lua_State* state = luaL_newstate();
-	luaL_openlibs(state);
+	luwra::StateWrapper state;
+	state.loadStandardLibrary();
 
 	// Register our user type.
 	// This function also registers a garbage-collector hook and a string representation function.
@@ -79,11 +79,8 @@ int main() {
 	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);
 		return 1;
 	} else {
-		lua_close(state);
 		return 0;
 	}
 }