aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/lstrlib.js6
-rw-r--r--tests/lstrlib.js31
3 files changed, 38 insertions, 1 deletions
diff --git a/README.md b/README.md
index 51c4b29..dffa1ec 100644
--- a/README.md
+++ b/README.md
@@ -213,6 +213,7 @@
- [x] string.len
- [x] string.lower
- [x] string.rep
+ - [x] string.reverse
- [x] string.upper
- [ ] string.byte
- [ ] string.dump
@@ -223,7 +224,6 @@
- [ ] string.match
- [ ] string.pack
- [ ] string.packsize
- - [ ] string.reverse
- [ ] string.sub
- [ ] string.unpack
- [ ] Package
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 11b266a..2f89777 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -25,6 +25,11 @@ const str_char = function(L) {
return 1;
};
+const str_reverse = function(L) {
+ lapi.lua_pushstring(L, lauxlib.luaL_checkstring(L, 1).split("").reverse().join(""));
+ return 1;
+};
+
const str_lower = function(L) {
lapi.lua_pushstring(L, lauxlib.luaL_checkstring(L, 1).toLowerCase());
return 1;
@@ -49,6 +54,7 @@ const strlib = {
"len": str_len,
"lower": str_lower,
"rep": str_rep,
+ "reverse": str_reverse,
"upper": str_upper
};
diff --git a/tests/lstrlib.js b/tests/lstrlib.js
index 56fa2a3..d4e8493 100644
--- a/tests/lstrlib.js
+++ b/tests/lstrlib.js
@@ -146,4 +146,35 @@ test('string.rep', function (t) {
"hello, hello, hello",
"Correct element(s) on the stack"
);
+});
+
+
+test('string.reverse', function (t) {
+ let luaCode = `
+ return string.reverse("olleh")
+ `, L;
+
+ t.plan(3);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, luaCode);
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "hello",
+ "Correct element(s) on the stack"
+ );
}); \ No newline at end of file