2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-08-10 23:17:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
struct custom_reader {
|
|
|
|
FILE* f;
|
|
|
|
// We use 2 here to demonstrate
|
|
|
|
// multiple calls to the function to
|
|
|
|
// parse whole file.
|
|
|
|
// Please don't use 2.
|
|
|
|
// PLEASE DO NOT USE A BUFFER
|
|
|
|
// OF SIZE 2!
|
|
|
|
char buffer[2];
|
|
|
|
std::size_t current_size;
|
|
|
|
std::size_t read_count;
|
|
|
|
|
|
|
|
custom_reader(FILE* f_) : f(f_), buffer(), current_size(0), read_count(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read() {
|
|
|
|
std::cout << "custom read: read #" << ++read_count << std::endl;
|
2021-03-06 14:03:23 +08:00
|
|
|
current_size = fread(buffer, 1, 2, f);
|
2018-08-10 23:17:31 +08:00
|
|
|
return current_size > 0 && ferror(f) == 0;
|
|
|
|
}
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
~custom_reader() {
|
|
|
|
std::fclose(f);
|
2018-08-10 23:17:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// function must match signature found in type lua_Reader:
|
|
|
|
// const char* ( lua_State*, void*, size_t* )
|
|
|
|
const char* custom_reader_function(lua_State*, void* pointer_to_my_object, size_t* data_size) {
|
|
|
|
custom_reader& cr = *(static_cast<custom_reader*>(pointer_to_my_object));
|
2021-03-06 14:03:23 +08:00
|
|
|
if (cr.read()) {
|
2018-08-10 23:17:31 +08:00
|
|
|
*data_size = cr.current_size;
|
|
|
|
return cr.buffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*data_size = 0;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main() {
|
2018-08-10 23:17:31 +08:00
|
|
|
std::cout << "=== custom reader ===" << std::endl;
|
|
|
|
|
|
|
|
// make a file to use for the custom reader
|
|
|
|
{
|
|
|
|
std::ofstream bjork("bjork.lua", std::ios::binary);
|
|
|
|
bjork << "print('hello!')\n";
|
|
|
|
}
|
|
|
|
struct on_scope_exit {
|
|
|
|
~on_scope_exit() {
|
|
|
|
// remove file when done
|
|
|
|
std::remove("bjork.lua");
|
|
|
|
}
|
|
|
|
} remove_on_exit;
|
|
|
|
|
|
|
|
|
|
|
|
sol::state lua;
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.open_libraries(sol::lib::base);
|
2018-08-10 23:17:31 +08:00
|
|
|
|
|
|
|
FILE* bjork_fp;
|
|
|
|
#ifdef _MSC_VER
|
2021-03-06 14:03:23 +08:00
|
|
|
if (fopen_s(&bjork_fp, "bjork.lua", "r") != 0) {
|
2018-08-10 23:17:31 +08:00
|
|
|
std::cerr << "failed to open bjork.lua -- exiting" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2021-03-06 14:03:23 +08:00
|
|
|
bjork_fp = fopen("bjork.lua", "r");
|
2018-08-10 23:17:31 +08:00
|
|
|
#endif
|
|
|
|
if (bjork_fp == nullptr) {
|
|
|
|
std::cerr << "failed to open bjork.lua -- exiting" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
custom_reader reader(bjork_fp);
|
|
|
|
|
|
|
|
// load the code using our custom reader, then run it
|
2021-03-06 14:03:23 +08:00
|
|
|
auto result = lua.safe_script(custom_reader_function, &reader, sol::script_pass_on_error);
|
2018-08-10 23:17:31 +08:00
|
|
|
// make sure we ran loaded and ran the code successfully
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(result.valid());
|
2018-08-10 23:17:31 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// note there are lua.load( ... ) variants that take a
|
2018-08-10 23:17:31 +08:00
|
|
|
// custom reader than JUST run the code, too!
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|