Skip to content

Commit

Permalink
Add method to register libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
felipedaragon committed Apr 1, 2014
1 parent 4d2f34e commit 384a31b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ PS: A way to make a Lua file to provide itself as JS can be found under `\exampl

```html
<?lua@client?><!-- Serve the VM first-->
<script>
<script type="text/javascript">
function myalert(L) {
var str = C.luaL_checkstring(L, 1);
window.alert(str);
Expand All @@ -146,6 +146,36 @@ myalert('Hello World from Lua!')
?>
```

#### Adding a Library #

```html
<?lua@client?><!-- Serve the VM first-->
<script type="text/javascript">
var MyLib = {
alert: function(L) {
var str = C.luaL_checkstring(L, 1);
window.alert(str);
return 0;
},
upper: function(L) {
var str = C.luaL_checkstring(L, 1);
C.lua_pushstring(L,str.toUpperCase());
return 1;
}
}
var MyLibFuncs = [
["alert", MyLib.alert],
["upper", MyLib.upper]
];
LuaCS.addLibrary("test", MyLibFuncs);
</script>
<?lua@client
msg = test.upper('It works!')
test.alert(msg)
?>
```

#### Script Tag: An Alternative Way #

```html
Expand Down
37 changes: 37 additions & 0 deletions examples/addlibrary.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>
<head>
<title>Lua@Client - Add Library</title>
</head>
<body onload="LuaCS.runScripts()">

<script src="../js/lib/lua5.1.5.min.js"></script>
<script src="../js/latclient.js"></script>
<script src="../js/js-lua.js"></script>

<script>
var MyLib = {
alert: function(L) {
var str = C.luaL_checkstring(L, 1);
window.alert(str);
return 0;
},
upper: function(L) {
var str = C.luaL_checkstring(L, 1);
C.lua_pushstring(L,str.toUpperCase());
return 1;
}
}

var MyLibFuncs = [
["alert", MyLib.alert],
["upper", MyLib.upper]
];
LuaCS.addLibrary("test", MyLibFuncs);
</script>

<script language="lua">
msg = test.upper('It works!')
test.alert(msg)
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions js/latclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
var C = Lua5_1.C;
var L = C.lua_open();

// Adds the missing luaL_register
// C.luaL_register = F("luaL_register", void_t, [lua_State, const_char_ptr_t, luaL_Reg]);
if (!C.luaL_register) {
C.luaL_register = Module.cwrap("luaL_register", null, ["number", "string", "number"]);
}

var LuaCS =
{
settings : {
Expand All @@ -31,6 +37,18 @@ var LuaCS =
C.lua_pushcfunction(L,Lua5_1.Runtime.addFunction(func));
C.lua_setglobal(L, func_name);
},
addLibrary : function(libname,funcs) {
C.luaL_register(L,libname,null);
if (funcs != undefined) {
for (i=0;i<funcs.length;i++) {
var f = funcs[i];
if (f[0] != null) {
C.lua_pushcfunction(L,Lua5_1.Runtime.addFunction(f[1]));
C.lua_setfield(L, -2, f[0]);
}
}
}
},
runLua : function(L,script) {
if (C.luaL_dostring(L, script) !== 0)
{
Expand Down
2 changes: 1 addition & 1 deletion lua/latclient.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--[[
Lua@Client 0.1.5
Lua@Client 0.1.6
Lua Pages Template Preprocessor Extension and Script Provider
Copyright (c) 2014 Felipe Daragon
Expand Down

0 comments on commit 384a31b

Please sign in to comment.