MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/custom_reader.cpp:53–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51}
52
53int main() {
54 std::cout << "=== custom reader ===" << std::endl;
55
56 // make a file to use for the custom reader
57 {
58 std::ofstream bjork("bjork.lua", std::ios::binary);
59 bjork << "print('hello!')\n";
60 }
61 struct on_scope_exit {
62 ~on_scope_exit() {
63 // remove file when done
64 std::remove("bjork.lua");
65 }
66 } remove_on_exit;
67
68
69 sol::state lua;
70 lua.open_libraries(sol::lib::base);
71
72 FILE* bjork_fp;
73#ifdef _MSC_VER
74 if (fopen_s(&bjork_fp, "bjork.lua", "r") != 0) {
75 std::cerr << "failed to open bjork.lua -- exiting"
76 << std::endl;
77 return -1;
78 }
79#else
80 bjork_fp = fopen("bjork.lua", "r");
81#endif
82 if (bjork_fp == nullptr) {
83 std::cerr << "failed to open bjork.lua -- exiting"
84 << std::endl;
85 return -1;
86 }
87 custom_reader reader(bjork_fp);
88
89 // load the code using our custom reader, then run it
90 auto result = lua.safe_script(custom_reader_function,
91 &reader,
92 sol::script_pass_on_error);
93 // make sure we ran loaded and ran the code successfully
94 SOL_ASSERT(result.valid());
95
96 // note there are lua.load( ... ) variants that take a
97 // custom reader than JUST run the code, too!
98
99 std::cout << std::endl;
100 return 0;
101}

Callers

nothing calls this directly

Calls 3

open_librariesMethod · 0.80
safe_scriptMethod · 0.80
validMethod · 0.45

Tested by

no test coverage detected