summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-17 09:45:49 +0100
committerBenoit Giannangeli <giann@users.noreply.github.com>2017-03-17 10:36:57 +0100
commit18631188532881934261e72321d9839ad42d1b06 (patch)
treea426425e76be16eee6eb4fafec411f147bb4d472
parent0cbdb3527041d016097aa3384af9c5908af2cce6 (diff)
downloadfengari-18631188532881934261e72321d9839ad42d1b06.tar.gz
fengari-18631188532881934261e72321d9839ad42d1b06.tar.bz2
fengari-18631188532881934261e72321d9839ad42d1b06.zip
lua_todataview to use string as binary representation
-rw-r--r--src/lapi.js27
-rw-r--r--src/lstrlib.js5
-rw-r--r--tests/lstrlib.js35
3 files changed, 33 insertions, 34 deletions
diff --git a/src/lapi.js b/src/lapi.js
index a51796b..a884a97 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -240,18 +240,6 @@ const lua_pushstring = function (L, s) {
return s;
};
-// Push string without 8-bit conversion
-const lua_pushrawstring = function(L, s) {
- if (typeof s !== "string")
- L.stack[L.top] = ldo.nil;
- else {
- L.stack[L.top] = new TValue(CT.LUA_TLNGSTR, s.split('').map(e => e.charCodeAt(0)));
- }
- L.top++;
- assert(L.top <= L.ci.top, "stack overflow");
- return s;
-};
-
const lua_pushliteral = lua_pushstring;
const lua_pushcclosure = function(L, fn, n) {
@@ -554,6 +542,19 @@ const lua_tolstring = function(L, idx) {
const lua_tostring = lua_tolstring;
+// Convert a string on the stack to a dataview, because lua_tostring will perform utf-8 to utf-16 conversion
+const lua_todataview = function(L, idx) {
+ let o = index2addr(L, idx);
+
+ if (!o.ttisstring() && !o.ttisnumber())
+ return null;
+
+ let dv = new DataView(new ArrayBuffer(o.value.length));
+ o.value.forEach((e, i) => dv.setUint8(i, e, true));
+
+ return dv;
+};
+
const lua_rawlen = function(L, idx) {
let o = index2addr(L, idx);
switch (o.ttype()) {
@@ -893,7 +894,6 @@ module.exports.lua_pushliteral = lua_pushliteral;
module.exports.lua_pushlstring = lua_pushlstring;
module.exports.lua_pushnil = lua_pushnil;
module.exports.lua_pushnumber = lua_pushnumber;
-module.exports.lua_pushrawstring = lua_pushrawstring;
module.exports.lua_pushstring = lua_pushstring;
module.exports.lua_pushthread = lua_pushthread;
module.exports.lua_pushtvalue = lua_pushtvalue;
@@ -916,6 +916,7 @@ module.exports.lua_setupvalue = lua_setupvalue;
module.exports.lua_status = lua_status;
module.exports.lua_stringtonumber = lua_stringtonumber;
module.exports.lua_toboolean = lua_toboolean;
+module.exports.lua_todataview = lua_todataview;
module.exports.lua_tointeger = lua_tointeger;
module.exports.lua_tointegerx = lua_tointegerx;
module.exports.lua_tolstring = lua_tolstring;
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 83e82b0..02feff6 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -3,9 +3,10 @@
const assert = require('assert');
const sprintf = require('sprintf');
-const lua = require('./lua.js');
const lapi = require('./lapi.js');
const lauxlib = require('./lauxlib.js');
+const lobject = require('./lobject.js');
+const lua = require('./lua.js');
const luaconf = require('./luaconf.js');
const CT = lua.constant_types;
@@ -61,7 +62,7 @@ const str_dump = function(L) {
lapi.lua_settop(L, 1);
if (lapi.lua_dump(L, writer, b, strip) !== 0)
return lauxlib.luaL_error(L, "unable to dump given function");
- lapi.lua_pushrawstring(L, b.map(e => String.fromCharCode(e)).join(''));
+ L.stack[L.top++] = new lobject.TValue(CT.LUA_TLNGSTR, b); // We don't want lua > js > lua string conversion here
return 1;
};
diff --git a/tests/lstrlib.js b/tests/lstrlib.js
index aec8e2a..a7693a1 100644
--- a/tests/lstrlib.js
+++ b/tests/lstrlib.js
@@ -418,17 +418,15 @@ test('string.sub', function (t) {
test('string.dump', function (t) {
- let luaCodeToDump = `
- local todump = function(p1, p2, p3)
- local s = "hello"
- local i = 12
- local f = 12.5
- local b = true
-
- return p1 + p2 + p3
- end`,
- luaCode = `
- ${luaCodeToDump}
+ let luaCode = `
+ local todump = function()
+ local s = "hello"
+ local i = 12
+ local f = 12.5
+ local b = true
+
+ return s .. i .. f
+ end
return string.dump(todump)
`, L, bytes = [];
@@ -436,11 +434,6 @@ test('string.dump', function (t) {
t.plan(3);
t.doesNotThrow(function () {
-
- let bc = toByteCode(luaCodeToDump).dataView;
- for (let i = 0; i < bc.byteLength; i++)
- bytes.push(bc.getUint8(i, true));
-
L = lauxlib.luaL_newstate();
linit.luaL_openlibs(L);
@@ -453,11 +446,15 @@ test('string.dump', function (t) {
lapi.lua_call(L, 0, -1);
+ let dv = lapi.lua_todataview(L, -1);
+
+ lapi.lua_load(L, null, dv, "test", "binary");
+
}, "Lua program ran without error");
- t.deepEqual(
- L.stack[L.top -1].value,
- bytes,
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "hello1212.5",
"Correct element(s) on the stack"
);
}); \ No newline at end of file