<commit_before>#![allow(non_snake_case)]
use core::prelude::*;
use core::{mem, ptr, raw};
use core::str::raw::c_str_to_static_slice;
use collections::str::StrAllocating;
use collections::string::String;
use libc::{c_char, c_void, size_t};

use lua::lib::raw::*;
use lua::aux::raw::*;
use lua::raw::*;
use luajit::*;
use luajit_constants::*;

use glcommon::GLResult;
use log::{logi, loge};

use lua_callbacks::LuaCallback;

static mut GLDRAW_LUA_CREATE_SANDBOX: *mut c_void = 0 as *mut c_void;
static mut GLDRAW_LUA_STOPFNS: *mut c_void = 0 as *mut c_void;
static mut gldraw_lua_key: i32 = 0;
static LUA_FFI_SCRIPT: &'static str = include_str!("../includes/lua/ffi_loader.lua");
static LUA_RUNNER: &'static str = include_str!("../includes/lua/lua_runner.lua");
static DEFAULT_SCRIPT: &'static str = include_str!("../includes/lua/default_interpolator.lua");

static mut STATIC_LUA: Option<*mut lua_State> = None;

macro_rules! assert_stacksize {
    ($L:expr, $body:expr) => (
        {
            let stacksize = lua_gettop($L);
            let result = $body;
            assert_eq!(stacksize, lua_gettop($L));
            result
        }
    )
}

type ReaderState<'a> = (&'a str, bool);

enum SandboxMode {
    Sandboxed(LuaValue),
    Unsandboxed,
}

enum LuaValue {
    RegistryValue(*mut c_void),
    IndexValue(i32),
}

impl LuaValue {
    unsafe fn push_self(&self, L: *mut lua_State) {
        match self {
            &RegistryValue(key) => {
                lua_pushlightuserdata(L, key);
                lua_gettable(L, LUA_REGISTRYINDEX);
            },
            &IndexValue(idx) => {
                lua_pushvalue(L, idx);
            },
        }
    }
}

#[allow(unused)]
extern "C" fn stringreader(L: *mut lua_State, data: *mut c_void, size: *mut size_t) -> *const c_char {
    unsafe {
        let state: &mut ReaderState = mem::transmute(data);
        let (ref chars, ref mut done) = *state;
        if *done {
            ptr::null()
        } else {
            *done = true;
            *size = chars.len() as u32;
            chars.as_ptr() as *const i8
        }
    }
}

unsafe fn err_to_str(L: *mut lua_State) -> String {
    let mut size: size_t = 0;
    let strptr = lua_tolstring(L, -1, &mut size);
    let luastr: &str = mem::transmute(raw::Slice { data: strptr, len: size as uint });
    let result = luastr.into_string();
    lua_pop(L, -1);
    result
}

unsafe fn runstring(L: *mut lua_State, s: &str, filename: *const i8, env: SandboxMode) -> bool {
    let mut state: ReaderState = (s, false);
    let stateref: *mut c_void = mem::transmute(&mut state);
    if 0 != lua_load(L, stringreader, stateref, filename) {
        false
        //Err(format!("script failed to load: {}", err_to_str(L)))
    } else {
        if let Sandboxed(key) = env {
            key.push_self(L);
            lua_setfenv(L, -2);
        }
        let result = lua_pcall(L, 0, MULTRET, 0);
        if 0 != result {
            false
            //Err(format!("script failed to run: {}", err_to_str(L)));
        } else {
            true
            //Ok(())
        }<commit_msg>change it from MULTRET and it works.  What the f.<commit_after>#![allow(non_snake_case)]
use core::prelude::*;
use core::{mem, ptr, raw};
use core::str::raw::c_str_to_static_slice;
use collections::str::StrAllocating;
use collections::string::String;
use libc::{c_char, c_void, size_t};

use lua::lib::raw::*;
use lua::aux::raw::*;
use lua::raw::*;
use luajit::*;
use luajit_constants::*;

use glcommon::GLResult;
use log::{logi, loge};

use lua_callbacks::LuaCallback;

static mut GLDRAW_LUA_CREATE_SANDBOX: *mut c_void = 0 as *mut c_void;
static mut GLDRAW_LUA_STOPFNS: *mut c_void = 0 as *mut c_void;
static mut gldraw_lua_key: i32 = 0;
static LUA_FFI_SCRIPT: &'static str = include_str!("../includes/lua/ffi_loader.lua");
static LUA_RUNNER: &'static str = include_str!("../includes/lua/lua_runner.lua");
static DEFAULT_SCRIPT: &'static str = include_str!("../includes/lua/default_interpolator.lua");

static mut STATIC_LUA: Option<*mut lua_State> = None;

macro_rules! assert_stacksize {
    ($L:expr, $body:expr) => (
        {
            let stacksize = lua_gettop($L);
            let result = $body;
            assert_eq!(stacksize, lua_gettop($L));
            result
        }
    )
}

type ReaderState<'a> = (&'a str, bool);

enum SandboxMode {
    Sandboxed(LuaValue),
    Unsandboxed,
}

enum LuaValue {
    RegistryValue(*mut c_void),
    IndexValue(i32),
}

impl LuaValue {
    unsafe fn push_self(&self, L: *mut lua_State) {
        match self {
            &RegistryValue(key) => {
                lua_pushlightuserdata(L, key);
                lua_gettable(L, LUA_REGISTRYINDEX);
            },
            &IndexValue(idx) => {
                lua_pushvalue(L, idx);
            },
        }
    }
}

#[allow(unused)]
extern "C" fn stringreader(L: *mut lua_State, data: *mut c_void, size: *mut size_t) -> *const c_char {
    unsafe {
        let state: &mut ReaderState = mem::transmute(data);
        let (ref chars, ref mut done) = *state;
        if *done {
            ptr::null()
        } else {
            *done = true;
            *size = chars.len() as u32;
            chars.as_ptr() as *const i8
        }
    }
}

unsafe fn err_to_str(L: *mut lua_State) -> String {
    let mut size: size_t = 0;
    let strptr = lua_tolstring(L, -1, &mut size);
    let luastr: &str = mem::transmute(raw::Slice { data: strptr, len: size as uint });
    let result = luastr.into_string();
    lua_pop(L, -1);
    result
}

unsafe fn runstring(L: *mut lua_State, s: &str, filename: *const i8, env: SandboxMode) -> bool {
    let mut state: ReaderState = (s, false);
    let stateref: *mut c_void = mem::transmute(&mut state);
    if 0 != lua_load(L, stringreader, stateref, filename) {
        false
        //Err(format!("script failed to load: {}", err_to_str(L)))
    } else {
        if let Sandboxed(key) = env {
            key.push_self(L);
            lua_setfenv(L, -2);
        }
        let result = lua_pcall(L, 0, 0, 0);
        if 0 != result {
            false
            //Err(format!("script failed to run: {}", err_to_str(L)));
        } else {
            true
            //Ok(())
        }
{'fraction_non_alphanumeric': 0.14808866045615163, 'fraction_numerical': 0.006264053967234179, 'mean_word_length': 2.589048991354467, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '336140', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2200, 'n_words': 531}