ソースを参照

Inline Lua code into example units

Ole Krüger 10 年 前
コミット
a82eda3ad5
共有4 個のファイルを変更した37 個の追加27 個の削除を含む
  1. 16 1
      examples/functions.cpp
  2. 0 10
      examples/functions.lua
  3. 21 1
      examples/usertypes.cpp
  4. 0 15
      examples/usertypes.lua

+ 16 - 1
examples/functions.cpp

@@ -37,8 +37,23 @@ int main() {
 	luwra::push(state, wrapped_3);
 	lua_setglobal(state, "my_function_3");
 
+	// Load Lua code
+	luaL_loadstring(
+		state,
+		// Invoke 'my_function_1'
+		"my_function_1(1337, 'Hello')\n"
+
+		// Invoke 'my_function_2'
+		"local result2 = my_function_2()\n"
+		"print('my_function_2() = ' .. result2)\n"
+
+		// Invoke 'my_function_3'
+		"local result3 = my_function_3(13, 37)\n"
+		"print('my_function_3(13, 38) = ' .. result3)\n"
+	);
+
 	// Invoke the attached script
-	if (luaL_loadfile(state, "functions.lua") != 0 || lua_pcall(state, 0, LUA_MULTRET, 0) != 0) {
+	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;
 

+ 0 - 10
examples/functions.lua

@@ -1,10 +0,0 @@
--- Invoke 'my_function_1'
-my_function_1(1337, "Hello")
-
--- Invoke 'my_function_2'
-local result2 = my_function_2()
-print("my_function_2() = " .. result2)
-
--- Invoke 'my_function_3'
-local result3 = my_function_3(13, 37)
-print("my_function_3(13, 38) = " .. result3)

+ 21 - 1
examples/usertypes.cpp

@@ -53,8 +53,28 @@ int main() {
 	luwra::push(state, luwra::wrap_constructor<Point, double, double>);
 	lua_setglobal(state, "Point");
 
+	// Load Lua code
+	luaL_loadstring(
+		state,
+		// Instantiate type
+		"local p = Point(13, 37)\n"
+		"print('p =', p)\n"
+
+		// Invoke 'scale' method
+		"p:scale(2)\n"
+		"print('p =', p)\n"
+
+		// Access 'x' and 'y' property
+		"print('p.x =', p:x())\n"
+		"print('p.y =', p:y())\n"
+
+		// Modify 'x' property
+		"p:x(10)\n"
+		"print('p.x =', p:x())\n"
+	);
+
 	// Invoke the attached script
-	if (luaL_loadfile(state, "usertypes.lua") != 0 || lua_pcall(state, 0, LUA_MULTRET, 0) != 0) {
+	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;
 

+ 0 - 15
examples/usertypes.lua

@@ -1,15 +0,0 @@
--- Instantiate type
-local p = Point(13, 37)
-print("p =", p)
-
--- Invoke 'scale' method
-p:scale(2)
-print("p =", p)
-
--- Access 'x' and 'y' property
-print("p.x =", p:x())
-print("p.y =", p:y())
-
--- Modify 'x' property
-p:x(10)
-print("p.x =", p:x())