[vlc-commits] lua: Expose rand functions

Hugo Beauzée-Luyssen git at videolan.org
Fri Jul 13 11:41:20 CEST 2018


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Jul 12 16:14:30 2018 +0200| [b9e2274d6b1b1221604310fd04cae7f8aa5b3915] | committer: Hugo Beauzée-Luyssen

lua: Expose rand functions

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b9e2274d6b1b1221604310fd04cae7f8aa5b3915
---

 modules/lua/Makefile.am |  3 ++-
 modules/lua/extension.c |  1 +
 modules/lua/intf.c      |  1 +
 modules/lua/libs.h      |  1 +
 modules/lua/libs/misc.c |  1 +
 modules/lua/libs/rand.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
 share/lua/README.txt    |  6 +++++
 7 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/modules/lua/Makefile.am b/modules/lua/Makefile.am
index 7cc56dbb85..059c4eca4d 100644
--- a/modules/lua/Makefile.am
+++ b/modules/lua/Makefile.am
@@ -30,7 +30,8 @@ liblua_plugin_la_SOURCES = \
 	lua/libs/volume.c \
 	lua/libs/xml.c \
 	lua/libs/io.c \
-	lua/libs/errno.c
+	lua/libs/errno.c \
+	lua/libs/rand.c
 
 if HAVE_WIN32
 liblua_plugin_la_SOURCES += lua/libs/win.c
diff --git a/modules/lua/extension.c b/modules/lua/extension.c
index 6102deea02..39a72496eb 100644
--- a/modules/lua/extension.c
+++ b/modules/lua/extension.c
@@ -854,6 +854,7 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
         luaopen_xml( L );
         luaopen_vlcio( L );
         luaopen_errno( L );
+        luaopen_rand( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
         luaopen_win( L );
 #endif
diff --git a/modules/lua/intf.c b/modules/lua/intf.c
index 864f5d7c55..782b2c7708 100644
--- a/modules/lua/intf.c
+++ b/modules/lua/intf.c
@@ -279,6 +279,7 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
     luaopen_equalizer( L );
     luaopen_vlcio( L );
     luaopen_errno( L );
+    luaopen_rand( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
     luaopen_win( L );
 #endif
diff --git a/modules/lua/libs.h b/modules/lua/libs.h
index 45669a76b6..3a61684c32 100644
--- a/modules/lua/libs.h
+++ b/modules/lua/libs.h
@@ -47,6 +47,7 @@ void luaopen_xml( lua_State *L );
 void luaopen_equalizer( lua_State *L );
 void luaopen_vlcio( lua_State *L );
 void luaopen_errno( lua_State *L );
+void luaopen_rand( lua_State *L );
 #ifdef _WIN32
 void luaopen_win( lua_State *L );
 #endif
diff --git a/modules/lua/libs/misc.c b/modules/lua/libs/misc.c
index 45661ccbac..3d367c1370 100644
--- a/modules/lua/libs/misc.c
+++ b/modules/lua/libs/misc.c
@@ -43,6 +43,7 @@
 #include <vlc_interface.h>
 #include <vlc_actions.h>
 #include <vlc_interrupt.h>
+#include <vlc_rand.h>
 
 #include "../vlc.h"
 #include "../libs.h"
diff --git a/modules/lua/libs/rand.c b/modules/lua/libs/rand.c
new file mode 100644
index 0000000000..4f7c764393
--- /dev/null
+++ b/modules/lua/libs/rand.c
@@ -0,0 +1,62 @@
+/*****************************************************************************
+ * rand.c: random number/bytes generation functions
+ *****************************************************************************
+ * Copyright (C) 2007-2018 the VideoLAN team
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_rand.h>
+
+#include "../vlc.h"
+#include "../libs.h"
+
+static int vlclua_rand_number( lua_State *L )
+{
+    long rand = vlc_lrand48();
+    lua_pushnumber( L, rand );
+    return 1;
+}
+
+static int vlclua_rand_bytes( lua_State *L )
+{
+    lua_Integer i_size = luaL_checkinteger( L, 1 );
+    char* p_buff = malloc( i_size * sizeof( *p_buff ) );
+    if ( unlikely( p_buff == NULL ) )
+        return vlclua_error( L );
+    vlc_rand_bytes( p_buff, i_size );
+    lua_pushlstring( L, p_buff, i_size );
+    free( p_buff );
+    return 1;
+}
+
+static const luaL_Reg vlclua_rand_reg[] = {
+    { "number", vlclua_rand_number },
+    { "bytes", vlclua_rand_bytes },
+
+    { NULL, NULL }
+};
+
+void luaopen_rand( lua_State *L )
+{
+    lua_newtable( L );
+    luaL_register( L, NULL, vlclua_rand_reg );
+    lua_setfield( L, -2, "rand" );
+}
diff --git a/share/lua/README.txt b/share/lua/README.txt
index 6d6ee64cfc..9ddc8ecacd 100644
--- a/share/lua/README.txt
+++ b/share/lua/README.txt
@@ -482,3 +482,9 @@ reader:node_empty(): queries whether the previous invocation of reader:read()
   1 if the node is empty, and 0 if it is not.
 
 The simplexml module can also be used to parse XML documents easily.
+
+Random number & bytes
+---------------------
+vlc.rand.number(): Returns a random number between 0 and 2^31 - 1
+vlc.rand.bytes(size): Returns <size> random bytes
+



More information about the vlc-commits mailing list