<commit_before>"use strict";

const lua     = require('./lua.js');
const lauxlib = require('./lauxlib.js');

const luaB_print = function(L) {
    let n = lua.lua_gettop(L); /* number of arguments */
    let str = [];

    lua.lua_getglobal(L, lua.to_luastring("tostring", true));
    for (let i = 1; i <= n; i++) {
        lua.lua_pushvalue(L, -1);  /* function to be called */
        lua.lua_pushvalue(L, i);  /* value to print */
        lua.lua_call(L, 1, 1);
        let s = lua.lua_tolstring(L, -1);
        if (s === null)
            return lauxlib.luaL_error(L, lua.to_luastring("'tostring' must return a string to 'print'", true));
        if (i > 1) s = ["\t".charCodeAt(0)].concat(s);
        str = str.concat(s);
        lua.lua_pop(L, 1);
    }

    // Don't use console.log if Node
    if (process.stdout) process.stdout.write(lua.to_jsstring(str) + "\n");
    else console.log(lua.to_jsstring(str));
    return 0;
};

const luaB_tostring = function(L) {
    lauxlib.luaL_checkany(L, 1);
    lauxlib.luaL_tolstring(L, 1);

    return 1;
};

const luaB_getmetatable = function(L) {
    lauxlib.luaL_checkany(L, 1);
    if (!lua.lua_getmetatable(L, 1)) {
        lua.lua_pushnil(L);
        return 1;  /* no metatable */
    }
    lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true));
    return 1;  /* returns either __metatable field (if present) or metatable */
};

const luaB_setmetatable = function(L) {
    let t = lua.lua_type(L, 2);
    lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE);
    lauxlib.luaL_argcheck(L, t === lua.LUA_TNIL || t === lua.LUA_TTABLE, 2, lua.to_luastring("nil or table expected", true));
    if (lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true)) !== lua.LUA_TNIL)<commit_msg>src/lbaselib.js: Make print() correctly handle 8bit chars under node<commit_after>"use strict";

const lua     = require('./lua.js');
const lauxlib = require('./lauxlib.js');

const luaB_print = function(L) {
    let n = lua.lua_gettop(L); /* number of arguments */
    let str = [];

    lua.lua_getglobal(L, lua.to_luastring("tostring", true));
    for (let i = 1; i <= n; i++) {
        lua.lua_pushvalue(L, -1);  /* function to be called */
        lua.lua_pushvalue(L, i);  /* value to print */
        lua.lua_call(L, 1, 1);
        let s = lua.lua_tolstring(L, -1);
        if (s === null)
            return lauxlib.luaL_error(L, lua.to_luastring("'tostring' must return a string to 'print'", true));
        if (i > 1) s = ["\t".charCodeAt(0)].concat(s);
        str = str.concat(s);
        lua.lua_pop(L, 1);
    }

    // Don't use console.log if Node
    if (process.stdout) {
        str.push("\n".charCodeAt(0));
        process.stdout.write(Buffer.from(str));
    } else console.log(lua.to_jsstring(str));
    return 0;
};

const luaB_tostring = function(L) {
    lauxlib.luaL_checkany(L, 1);
    lauxlib.luaL_tolstring(L, 1);

    return 1;
};

const luaB_getmetatable = function(L) {
    lauxlib.luaL_checkany(L, 1);
    if (!lua.lua_getmetatable(L, 1)) {
        lua.lua_pushnil(L);
        return 1;  /* no metatable */
    }
    lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true));
    return 1;  /* returns either __metatable field (if present) or metatable */
};

const luaB_setmetatable = function(L) {
    let t = lua.lua_type(L, 2);
    lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE);
    lauxlib.luaL_argcheck(L, t === lua.LUA_TNIL || t === lua.LUA_TTABLE, 2, lua.to_luastring("nil or table expected", true));
    if (lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true)) !== lua.LUA_TNIL)
{'fraction_non_alphanumeric': 0.16555740432612312, 'fraction_numerical': 0.012201885745978924, 'mean_word_length': 2.9724669603524227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7171872', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1370, 'n_words': 297}