lua_environment.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include "core/error/error.h"
  7. #include "core/json/json_object.inl"
  8. #include "core/json/sjson.h"
  9. #include "core/memory/temp_allocator.inl"
  10. #include "core/strings/dynamic_string.inl"
  11. #include "core/strings/string_id.inl"
  12. #include "core/strings/string_stream.inl"
  13. #include "device/device.h"
  14. #include "device/log.h"
  15. #include "lua/lua_environment.h"
  16. #include "lua/lua_stack.inl"
  17. #include "resource/lua_resource.h"
  18. #include "resource/resource_manager.h"
  19. #include <stdarg.h>
  20. LOG_SYSTEM(LUA, "lua")
  21. namespace crown
  22. {
  23. extern void load_api(LuaEnvironment &env);
  24. static int luaB_print(lua_State *L)
  25. {
  26. TempAllocator2048 ta;
  27. StringStream ss(ta);
  28. int n = lua_gettop(L); /* number of arguments */
  29. lua_getglobal(L, "tostring");
  30. for (int i = 1; i <= n; ++i) {
  31. const char *s;
  32. lua_pushvalue(L, -1); /* function to be called */
  33. lua_pushvalue(L, i); /* value to print */
  34. lua_call(L, 1, 1);
  35. s = lua_tostring(L, -1); /* get result */
  36. if (s == NULL)
  37. return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
  38. if (i > 1)
  39. ss << "\t";
  40. ss << s;
  41. lua_pop(L, 1); /* pop result */
  42. }
  43. logi(LUA, string_stream::c_str(ss));
  44. return 0;
  45. }
  46. static int msghandler(lua_State *L)
  47. {
  48. const char *msg = lua_tostring(L, 1);
  49. if (msg == NULL) { /* is error object not a string? */
  50. if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
  51. lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
  52. return 1; /* that is the message */
  53. else
  54. msg = lua_pushfstring(L, "(error object is a %s value)",
  55. luaL_typename(L, 1));
  56. }
  57. #if CROWN_USE_LUAJIT
  58. luaL_traceback(L, L, msg, 1); /* append a standard traceback */
  59. #else
  60. lua_pushstring(L, "No traceback");
  61. #endif
  62. return 1; /* return the traceback */
  63. }
  64. /*
  65. ** Prints (calling the Lua 'print' function) any values on the stack
  66. */
  67. static void l_print(lua_State *L)
  68. {
  69. int n = lua_gettop(L);
  70. if (n > 0) { /* any result to be printed? */
  71. luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
  72. lua_getglobal(L, "print");
  73. lua_insert(L, 1);
  74. if (lua_pcall(L, n, 0, 0) != LUA_OK)
  75. loge(LUA, lua_pushfstring(L, "error calling 'print' (%s)", lua_tostring(L, -1)));
  76. }
  77. }
  78. /*
  79. ** Try to compile line on the stack as 'return <line>;'; on return, stack
  80. ** has either compiled chunk or original line (if compilation failed).
  81. */
  82. static int addreturn(lua_State *L)
  83. {
  84. const char *line = lua_tostring(L, -1); /* original line */
  85. const char *retline = lua_pushfstring(L, "return %s;", line);
  86. int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
  87. if (status == LUA_OK)
  88. lua_remove(L, -2); /* remove modified line */
  89. else
  90. lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
  91. return status;
  92. }
  93. /*
  94. ** Read a line and try to load (compile) it first as an expression (by
  95. ** adding "return " in front of it) and second as a statement. Return
  96. ** the final status of load/call with the resulting function (if any)
  97. ** in the top of the stack.
  98. */
  99. static int loadline(lua_State *L)
  100. {
  101. int status;
  102. if ((status = addreturn(L)) != LUA_OK) { /* 'return ...' did not work? */
  103. // status = multiline(L); /* try as command, maybe with continuation lines */
  104. size_t len;
  105. const char *line = lua_tolstring(L, 1, &len); /* get what it has */
  106. status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
  107. }
  108. lua_remove(L, 1); /* remove line from the stack */
  109. lua_assert(lua_gettop(L) == 1);
  110. return status;
  111. }
  112. /*
  113. ** Check whether 'status' is not OK and, if so, prints the error
  114. ** message on the top of the stack. It assumes that the error object
  115. ** is a string, as it was either generated by Lua or by 'msghandler'.
  116. */
  117. int report(lua_State *L, int status)
  118. {
  119. if (status != LUA_OK) {
  120. const char *msg = lua_tostring(L, -1);
  121. loge(LUA, msg);
  122. lua_pop(L, 1); /* remove message */
  123. }
  124. return status;
  125. }
  126. static int loader(lua_State *L)
  127. {
  128. LuaStack stack(L);
  129. int status;
  130. const LuaResource *lr = (LuaResource *)device()->_resource_manager->get(RESOURCE_TYPE_SCRIPT, stack.get_resource_name(1));
  131. status = luaL_loadbuffer(L, lua_resource::program(lr), lr->size, "");
  132. if (status != LUA_OK) {
  133. report(L, status);
  134. device()->pause();
  135. }
  136. return 1;
  137. }
  138. #if CROWN_DEBUG
  139. static int require_internal(lua_State *L)
  140. {
  141. const char *module_name = lua_tostring(L, 1);
  142. bool already_loaded = false;
  143. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  144. lua_getfield(L, -1, module_name);
  145. already_loaded = lua_toboolean(L, -1);
  146. lua_pop(L, 2);
  147. lua_getglobal(L, "original_require");
  148. lua_pushvalue(L, -2);
  149. lua_remove(L, -3);
  150. lua_call(L, 1, 1);
  151. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  152. lua_getfield(L, 2, module_name);
  153. if (lua_toboolean(L, -1) && !already_loaded) {
  154. lua_pop(L, 2);
  155. lua_getglobal(L, "package");
  156. lua_getfield(L, -1, "load_order");
  157. lua_pushstring(L, module_name);
  158. lua_rawseti(L, -2, int(lua_objlen(L, -2) + 1));
  159. lua_pop(L, 2); // Pop "package.load_order" and "package"
  160. }
  161. return 1;
  162. }
  163. #endif // if CROWN_DEBUG
  164. LuaEnvironment::LuaEnvironment()
  165. : L(NULL)
  166. , _num_vec3(0)
  167. , _num_quat(0)
  168. , _num_mat4(0)
  169. #if CROWN_DEBUG
  170. , _vec3_marker(0)
  171. , _quat_marker(0)
  172. , _mat4_marker(0)
  173. , _random(0)
  174. #endif
  175. {
  176. #if CROWN_DEBUG
  177. // Initialize temporaries markers with random values.
  178. _random._seed = (s32)guid::new_guid().data1;
  179. reset_temporaries();
  180. #endif
  181. L = luaL_newstate();
  182. CE_ASSERT(L, "Unable to create lua state");
  183. }
  184. LuaEnvironment::~LuaEnvironment()
  185. {
  186. lua_close(L);
  187. }
  188. void LuaEnvironment::load_libs()
  189. {
  190. lua_gc(L, LUA_GCSTOP, 0);
  191. // Open default libraries
  192. lua_pushcfunction(L, luaopen_base);
  193. lua_pushstring(L, "");
  194. lua_call(L, 1, 0);
  195. lua_pushcfunction(L, luaopen_package);
  196. lua_pushstring(L, LUA_LOADLIBNAME);
  197. lua_call(L, 1, 0);
  198. lua_pushcfunction(L, luaopen_table);
  199. lua_pushstring(L, LUA_TABLIBNAME);
  200. lua_call(L, 1, 0);
  201. lua_pushcfunction(L, luaopen_string);
  202. lua_pushstring(L, LUA_STRLIBNAME);
  203. lua_call(L, 1, 0);
  204. lua_pushcfunction(L, luaopen_math);
  205. lua_pushstring(L, LUA_MATHLIBNAME);
  206. lua_call(L, 1, 0);
  207. lua_pushcfunction(L, luaopen_debug);
  208. lua_pushstring(L, LUA_DBLIBNAME);
  209. lua_call(L, 1, 0);
  210. lua_pushcfunction(L, luaopen_os);
  211. lua_pushstring(L, LUA_OSLIBNAME);
  212. lua_call(L, 1, 0);
  213. #if CROWN_USE_LUAJIT
  214. lua_pushcfunction(L, luaopen_bit);
  215. lua_pushstring(L, LUA_BITLIBNAME);
  216. lua_call(L, 1, 0);
  217. lua_pushcfunction(L, luaopen_jit);
  218. lua_pushstring(L, LUA_JITLIBNAME);
  219. lua_call(L, 1, 0);
  220. #endif
  221. // Override print to redirect output to logging system
  222. add_module_function("_G", "print", luaB_print);
  223. // Register crown libraries
  224. load_api(*this);
  225. // Register custom loader
  226. lua_getglobal(L, "package");
  227. lua_getfield(L, -1, "loaders");
  228. lua_pushcfunction(L, loader);
  229. lua_rawseti(L, -2, 1); // package.loaders[1] = loader
  230. lua_pushnil(L);
  231. lua_rawseti(L, -2, 2); // package.loaders[2] = nil
  232. lua_pushnil(L);
  233. lua_rawseti(L, -2, 3); // package.loaders[3] = nil
  234. lua_pushnil(L);
  235. lua_rawseti(L, -2, 4); // package.loaders[4] = nil
  236. lua_pop(L, 1); // pop "package.loaders"
  237. #if CROWN_DEBUG
  238. // Create new empty table to store package load order
  239. lua_newtable(L);
  240. lua_setfield(L, -2, "load_order"); // package.load_order = {}
  241. // Override require() to keep track of libraries' load order
  242. lua_getglobal(L, "require");
  243. lua_setglobal(L, "original_require");
  244. lua_pushcclosure(L, require_internal, 0); // pass original require() to new require as upvalue
  245. lua_setglobal(L, "require");
  246. #endif
  247. lua_pop(L, 1); // pop "package"
  248. // Create metatable for lightuserdata
  249. lua_pushlightuserdata(L, 0);
  250. lua_getfield(L, LUA_REGISTRYINDEX, "Lightuserdata_mt");
  251. lua_setmetatable(L, -2);
  252. lua_pop(L, 1);
  253. // Ensure stack is clean
  254. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  255. lua_gc(L, LUA_GCRESTART, 0);
  256. }
  257. void LuaEnvironment::do_file(const char *name)
  258. {
  259. int status;
  260. lua_getglobal(L, "dofile");
  261. lua_pushstring(L, name);
  262. status = this->call(1, 0);
  263. if (status != LUA_OK) {
  264. report(L, status);
  265. device()->pause();
  266. }
  267. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  268. }
  269. LuaStack LuaEnvironment::require(const char *name, int nres)
  270. {
  271. LuaStack stack(L);
  272. int status;
  273. lua_getglobal(L, "require");
  274. lua_pushstring(L, name);
  275. status = this->call(1, nres);
  276. if (status != LUA_OK) {
  277. report(L, status);
  278. device()->pause();
  279. }
  280. return stack;
  281. }
  282. LuaStack LuaEnvironment::execute(const LuaResource *lr, int nres)
  283. {
  284. LuaStack stack(L);
  285. int status;
  286. status = luaL_loadbuffer(L, lua_resource::program(lr), lr->size, "<unknown>");
  287. if (status == LUA_OK)
  288. status = this->call(0, nres);
  289. if (status != LUA_OK) {
  290. report(L, status);
  291. device()->pause();
  292. }
  293. return stack;
  294. }
  295. LuaStack LuaEnvironment::execute_string(const char *s)
  296. {
  297. LuaStack stack(L);
  298. int status;
  299. status = luaL_loadstring(L, s);
  300. if (status == LUA_OK)
  301. status = this->call(0, 0);
  302. if (status != LUA_OK) {
  303. report(L, status);
  304. device()->pause();
  305. }
  306. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  307. return stack;
  308. }
  309. void LuaEnvironment::add_module_function(const char *module, const char *name, const lua_CFunction func)
  310. {
  311. luaL_Reg entry[2];
  312. entry[0].name = name;
  313. entry[0].func = func;
  314. entry[1].name = NULL;
  315. entry[1].func = NULL;
  316. luaL_register(L, module, entry);
  317. lua_pop(L, 1);
  318. }
  319. void LuaEnvironment::add_module_function(const char *module, const char *name, const char *func)
  320. {
  321. // Create module if it does not exist
  322. luaL_Reg entry;
  323. entry.name = NULL;
  324. entry.func = NULL;
  325. luaL_register(L, module, &entry);
  326. lua_pop(L, 1);
  327. lua_getglobal(L, module);
  328. lua_getglobal(L, func);
  329. lua_setfield(L, -2, name);
  330. lua_setglobal(L, module);
  331. }
  332. void LuaEnvironment::add_module_metafunction(const char *module, const char *name, const lua_CFunction func)
  333. {
  334. // Create module if it does not exist
  335. luaL_Reg entry[2];
  336. entry[0].name = NULL;
  337. entry[0].func = NULL;
  338. luaL_register(L, module, entry);
  339. lua_pop(L, 1);
  340. luaL_newmetatable(L, module);
  341. if (func) {
  342. entry[0].name = name;
  343. entry[0].func = func;
  344. entry[1].name = NULL;
  345. entry[1].func = NULL;
  346. luaL_register(L, NULL, entry);
  347. } else {
  348. lua_pushstring(L, name);
  349. lua_pushvalue(L, -2);
  350. lua_settable(L, -3);
  351. }
  352. lua_getglobal(L, module);
  353. lua_pushvalue(L, -2);
  354. lua_setmetatable(L, -2);
  355. lua_pop(L, -1);
  356. }
  357. void LuaEnvironment::set_module_number(const char *module, const char *name, f64 value)
  358. {
  359. // Create module if it does not exist
  360. luaL_Reg entry;
  361. entry.name = NULL;
  362. entry.func = NULL;
  363. luaL_register(L, module, &entry);
  364. lua_pop(L, 1);
  365. lua_getglobal(L, module);
  366. lua_pushnumber(L, value);
  367. lua_setfield(L, -2, name);
  368. lua_pop(L, 1);
  369. }
  370. int LuaEnvironment::call(int narg, int nres)
  371. {
  372. int status;
  373. int base = lua_gettop(L) - narg; /* function index */
  374. lua_pushcfunction(L, msghandler); /* push message handler */
  375. lua_insert(L, base); /* put it under function and args */
  376. status = lua_pcall(L, narg, nres, base);
  377. lua_remove(L, base); /* remove message handler from the stack */
  378. return status;
  379. }
  380. void LuaEnvironment::call_global(const char *func, ArgType::Enum *arg_types, Arg *args, int narg, int nres)
  381. {
  382. int status;
  383. LuaStack stack(L);
  384. CE_ENSURE(NULL != func);
  385. lua_getglobal(L, func);
  386. stack.push_args(arg_types, args, narg);
  387. status = call(narg, nres);
  388. if (status != LUA_OK) {
  389. report(L, status);
  390. device()->pause();
  391. }
  392. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  393. }
  394. LuaStack LuaEnvironment::get_global(const char *global)
  395. {
  396. LuaStack stack(L);
  397. lua_getglobal(L, global);
  398. return stack;
  399. }
  400. Vector3 *LuaEnvironment::next_vector3(const Vector3 &v)
  401. {
  402. CE_ASSERT(_num_vec3 < LUA_MAX_VECTOR3, "Maximum number of Vector3 reached");
  403. Vector3 *vec3 = &(_vec3[_num_vec3++] = v);
  404. uintptr_t ptr = (uintptr_t)(void *)vec3;
  405. #if CROWN_DEBUG
  406. ptr |= _vec3_marker;
  407. #endif
  408. return (Vector3 *)ptr;
  409. }
  410. Quaternion *LuaEnvironment::next_quaternion(const Quaternion &q)
  411. {
  412. CE_ASSERT(_num_quat < LUA_MAX_QUATERNION, "Maximum number of Quaternion reached");
  413. Quaternion *quat = &(_quat[_num_quat++] = q);
  414. uintptr_t ptr = (uintptr_t)(void *)quat;
  415. #if CROWN_DEBUG
  416. ptr |= _quat_marker;
  417. #endif
  418. return (Quaternion *)ptr;
  419. }
  420. Matrix4x4 *LuaEnvironment::next_matrix4x4(const Matrix4x4 &m)
  421. {
  422. CE_ASSERT(_num_mat4 < LUA_MAX_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  423. Matrix4x4 *mat4 = &(_mat4[_num_mat4++] = m);
  424. uintptr_t ptr = (uintptr_t)(void *)mat4;
  425. #if CROWN_DEBUG
  426. ptr |= _mat4_marker;
  427. #endif
  428. return (Matrix4x4 *)ptr;
  429. }
  430. bool LuaEnvironment::is_vector3(const void *ptr)
  431. {
  432. return ptr >= &_vec3[0]
  433. && ptr <= &_vec3[LUA_MAX_VECTOR3 - 1];
  434. }
  435. bool LuaEnvironment::is_quaternion(const void *ptr)
  436. {
  437. return ptr >= &_quat[0]
  438. && ptr <= &_quat[LUA_MAX_QUATERNION - 1];
  439. }
  440. bool LuaEnvironment::is_matrix4x4(const void *ptr)
  441. {
  442. return ptr >= &_mat4[0]
  443. && ptr <= &_mat4[LUA_MAX_MATRIX4X4 - 1];
  444. }
  445. #if CROWN_DEBUG
  446. Vector3 *LuaEnvironment::check_valid(const Vector3 *ptr)
  447. {
  448. LUA_ASSERT(((uintptr_t)ptr & LUA_VECTOR3_MARKER_MASK) >> LUA_VECTOR3_MARKER_SHIFT == _vec3_marker
  449. , LuaStack(L)
  450. , "Stale Vector3 ptr: %p, expected marker: %p"
  451. , ptr
  452. , _vec3_marker
  453. );
  454. return (Vector3 *)(uintptr_t(ptr) & ~LUA_VECTOR3_MARKER_MASK);
  455. }
  456. Quaternion *LuaEnvironment::check_valid(const Quaternion *ptr)
  457. {
  458. LUA_ASSERT(((uintptr_t)ptr & LUA_QUATERNION_MARKER_MASK) >> LUA_QUATERNION_MARKER_SHIFT == _quat_marker
  459. , LuaStack(L)
  460. , "Stale Quaternion ptr: %p, expected marker: %p"
  461. , ptr
  462. , _quat_marker
  463. );
  464. return (Quaternion *)(uintptr_t(ptr) & ~LUA_QUATERNION_MARKER_MASK);
  465. }
  466. Matrix4x4 *LuaEnvironment::check_valid(const Matrix4x4 *ptr)
  467. {
  468. LUA_ASSERT(((uintptr_t)ptr & LUA_MATRIX4X4_MARKER_MASK) >> LUA_MATRIX4X4_MARKER_SHIFT == _mat4_marker
  469. , LuaStack(L)
  470. , "Stale Matrix4x4 ptr: %p, expected marker: %p"
  471. , ptr
  472. , _mat4_marker
  473. );
  474. return (Matrix4x4 *)(uintptr_t(ptr) & ~LUA_MATRIX4X4_MARKER_MASK);
  475. }
  476. #endif // if CROWN_DEBUG
  477. void LuaEnvironment::reload()
  478. {
  479. lua_getglobal(L, "package");
  480. lua_getfield(L, -1, "load_order");
  481. for (size_t i = 1, n = lua_objlen(L, -1); i < n + 1; ++i) {
  482. lua_rawgeti(L, -1, (int)i);
  483. LuaStack stack(L);
  484. StringId64 name = stack.get_resource_name(-1);
  485. this->execute((const LuaResource *)device()->_resource_manager->get(RESOURCE_TYPE_SCRIPT, name), 0);
  486. lua_pop(L, 1);
  487. }
  488. lua_pop(L, 2);
  489. }
  490. void LuaEnvironment::temp_count(u32 &num_vec3, u32 &num_quat, u32 &num_mat4)
  491. {
  492. num_vec3 = _num_vec3;
  493. num_quat = _num_quat;
  494. num_mat4 = _num_mat4;
  495. }
  496. void LuaEnvironment::set_temp_count(u32 num_vec3, u32 num_quat, u32 num_mat4)
  497. {
  498. _num_vec3 = num_vec3;
  499. _num_quat = num_quat;
  500. _num_mat4 = num_mat4;
  501. }
  502. void LuaEnvironment::reset_temporaries()
  503. {
  504. _num_vec3 = 0;
  505. _num_quat = 0;
  506. _num_mat4 = 0;
  507. #if CROWN_DEBUG
  508. _vec3_marker = (uintptr_t)_random.integer(1 + LUA_VECTOR3_MARKER_MASK);
  509. _quat_marker = (uintptr_t)_random.integer(1 + LUA_QUATERNION_MARKER_MASK);
  510. _mat4_marker = (uintptr_t)_random.integer(1 + LUA_MATRIX4X4_MARKER_MASK);
  511. #endif
  512. }
  513. static void console_command_script(ConsoleServer &cs, u32 client_id, const char *json, void *user_data)
  514. {
  515. CE_UNUSED_2(cs, client_id);
  516. TempAllocator1024 ta;
  517. JsonObject obj(ta);
  518. DynamicString script(default_allocator());
  519. sjson::parse(obj, json);
  520. sjson::parse_verbatim(script, obj["script"]);
  521. ((LuaEnvironment *)user_data)->execute_string(script.c_str());
  522. }
  523. static void do_REPL(LuaEnvironment *env, const char *lua)
  524. {
  525. lua_State *L = env->L;
  526. int status;
  527. lua_settop(L, 0);
  528. lua_pushstring(L, lua);
  529. if ((status = loadline(L)) != -1) { // This is never -1
  530. if (status == LUA_OK) {
  531. status = env->call(0, LUA_MULTRET);
  532. }
  533. if (status == LUA_OK)
  534. l_print(L);
  535. else
  536. report(L, status);
  537. }
  538. lua_settop(L, 0); /* clear stack */
  539. return;
  540. }
  541. static void console_command_REPL(ConsoleServer &cs, u32 client_id, const char *json, void *user_data)
  542. {
  543. CE_UNUSED_2(cs, client_id);
  544. TempAllocator1024 ta;
  545. JsonObject obj(ta);
  546. DynamicString script(default_allocator());
  547. sjson::parse(obj, json);
  548. sjson::parse_verbatim(script, obj["repl"]);
  549. do_REPL((LuaEnvironment *)user_data, script.c_str());
  550. }
  551. void LuaEnvironment::register_console_commands(ConsoleServer &cs)
  552. {
  553. cs.register_message_type("script", console_command_script, this);
  554. cs.register_message_type("repl", console_command_REPL, this);
  555. }
  556. } // namespace crown