/examples/as-lua-lib/mathx.rs

https://gitlab.com/cjcole/rust-lua53 · Rust · 87 lines · 40 code · 10 blank · 37 comment · 0 complexity · 116eb58c8278a0c05f9bf2518cdee833 MD5 · raw file

  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2014 J.C. Moyer
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #![crate_name="mathx"]
  23. #![crate_type="dylib"]
  24. extern crate libc;
  25. extern crate lua;
  26. // import low level lua_State
  27. use lua::ffi::lua_State;
  28. // import high level lua_State wrapper and a type we'll need for library
  29. // registration
  30. use lua::{State, Function};
  31. // import c_int since we need this to interface with Lua
  32. use libc::c_int;
  33. // simple binding to Rust's sin function
  34. #[allow(non_snake_case)]
  35. unsafe extern "C" fn sin(L: *mut lua_State) -> c_int {
  36. let mut state = State::from_ptr(L);
  37. // convert the value on top of the stack to a number
  38. let num = state.to_number(-1);
  39. // push the sine of that number onto the stack
  40. state.push_number(num.sin());
  41. // return one value to Lua
  42. 1
  43. }
  44. // simple binding to Rust's cos function
  45. #[allow(non_snake_case)]
  46. unsafe extern "C" fn cos(L: *mut lua_State) -> c_int {
  47. let mut state = State::from_ptr(L);
  48. let num = state.to_number(-1);
  49. state.push_number(num.cos());
  50. 1
  51. }
  52. // simple binding to Rust's tan function
  53. #[allow(non_snake_case)]
  54. unsafe extern "C" fn tan(L: *mut lua_State) -> c_int {
  55. let mut state = State::from_ptr(L);
  56. let num = state.to_number(-1);
  57. state.push_number(num.tan());
  58. 1
  59. }
  60. // mapping of function name to function pointer
  61. const MATHX_LIB: [(&'static str, Function); 3] = [
  62. ("sin", Some(sin)),
  63. ("cos", Some(cos)),
  64. ("tan", Some(tan)),
  65. ];
  66. // the format of this function name is defined by the Lua manual; the Lua
  67. // interpreter will call into this when you require() this library
  68. #[allow(non_snake_case)]
  69. #[no_mangle]
  70. pub extern "C" fn luaopen_mathx(L: *mut lua_State) -> c_int {
  71. // construct a state wrapper object from the pointer we were given
  72. let mut state = State::from_ptr(L);
  73. // create a new table and set fields for each function defined in MATHX_LIB
  74. state.new_lib(&MATHX_LIB);
  75. // return it on the stack
  76. 1
  77. }