├── KernelLuaVm ├── crt │ ├── libmd │ │ ├── _fltused.c │ │ ├── ldexp.c │ │ ├── lgamma.c │ │ ├── log10.c │ │ ├── signbit.c │ │ ├── tanh.c │ │ ├── fpclassify.c │ │ ├── fabs.c │ │ ├── trunc.c │ │ ├── nearbyint.c │ │ ├── frexp.c │ │ ├── __expo2.c │ │ ├── rint.c │ │ ├── acosh.c │ │ ├── modf.c │ │ ├── atanh.c │ │ ├── scalbn.c │ │ ├── ceil.c │ │ ├── floor.c │ │ ├── asinh.c │ │ ├── cosh.c │ │ ├── sinh.c │ │ ├── fmod.c │ │ ├── copysign.c │ │ ├── tan.c │ │ ├── cos.c │ │ ├── sin.c │ │ ├── __sin.c │ │ ├── __cos.c │ │ ├── acos.c │ │ ├── atan2.c │ │ ├── asin.c │ │ ├── log1p.c │ │ ├── __tan.c │ │ ├── atan.c │ │ ├── log.c │ │ ├── exp.c │ │ ├── __rem_pio2.c │ │ ├── sqrt.c │ │ └── tgamma.c │ ├── misc.c │ ├── stdint.h │ ├── memory.cpp │ ├── string.c │ ├── io.c │ └── crt.h ├── lua │ ├── api.hpp │ ├── state.hpp │ ├── native_function.hpp │ ├── native_function.cpp │ └── state.cpp ├── driver_io.hpp ├── logger.hpp ├── KernelLuaVm.inf ├── runtime.lua └── KernelLuaVm.vcxproj.filters ├── LuaLib ├── lua.hpp ├── lapi.h ├── lundump.h ├── lprefix.h ├── lualib.h ├── LuaLib.sln ├── ldebug.h ├── lstring.h ├── lzio.c ├── lzio.h ├── lfunc.h ├── linit.c ├── ltm.h ├── lctype.h ├── ldo.h ├── ltable.h ├── lctype.c ├── LuaLib.vcxproj.filters ├── llex.h ├── lmem.h ├── lmem.c ├── lcode.h ├── lopcodes.c ├── lvm.h ├── lfunc.c ├── lcorolib.c ├── lparser.h ├── lgc.h ├── ltm.c ├── ldump.c ├── lbitlib.c └── lundump.c ├── ActualConsole ├── ActualConsole.vcxproj.filters ├── main.cpp └── ActualConsole.vcxproj ├── .gitignore ├── scripts └── EscalateToken.lua ├── README.md └── NtLua.sln /KernelLuaVm/crt/libmd/_fltused.c: -------------------------------------------------------------------------------- 1 | extern "C" char _fltused = 1; -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/ldexp.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double ldexp(double x, int n) 4 | { 5 | return scalbn(x, n); 6 | } 7 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/lgamma.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double __lgamma_r(double, int*); 4 | 5 | double lgamma(double x) { 6 | int sign; 7 | return __lgamma_r(x, &sign); 8 | } 9 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/log10.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | static const double _M_LN10 = 2.302585092994046; 4 | 5 | double log10(double x) { 6 | return log(x) / (double)_M_LN10; 7 | } 8 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/misc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "crt.h" 3 | 4 | void abort() 5 | { 6 | __debugbreak(); 7 | } 8 | 9 | char* getenv() 10 | { 11 | return ( char* ) "virtual://"; 12 | } -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/signbit.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | int signbit(double x) 4 | { 5 | union { 6 | double d; 7 | uint64_t i; 8 | } y = { x }; 9 | return y.i>>63; 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /LuaLib/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /ActualConsole/ActualConsole.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/tanh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double tanh(double x) { 4 | int sign = 0; 5 | if (x < 0) { 6 | sign = 1; 7 | x = -x; 8 | } 9 | x = expm1(-2 * x); 10 | x = x / (x + 2); 11 | return sign ? x : -x; 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.db 2 | *.ipch 3 | *.opendb 4 | *.user 5 | *.log 6 | *.exe 7 | *.tlog 8 | *.obj 9 | *.exp 10 | *.pdb 11 | *.lib 12 | *.suo 13 | *.ilk 14 | *.db-shm 15 | *.db-wal 16 | .vs/ 17 | x64/Debug/ 18 | x64/Release/ 19 | 20 | LuaLib/x64/Release/ 21 | 22 | LuaLib/x64/Debug/ 23 | 24 | *.recipe -------------------------------------------------------------------------------- /KernelLuaVm/crt/stdint.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | typedef unsigned __int8 uint8_t; 3 | typedef unsigned __int16 uint16_t; 4 | typedef unsigned __int32 uint32_t; 5 | typedef unsigned __int64 uint64_t; 6 | typedef __int8 int8_t; 7 | typedef __int16 int16_t; 8 | typedef __int32 int32_t; 9 | typedef __int64 int64_t; -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/fpclassify.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | int fpclassify(double x) 4 | { 5 | union {double f; uint64_t i;} u = {x}; 6 | int e = u.i>>52 & 0x7ff; 7 | if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO; 8 | if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE; 9 | return FP_NORMAL; 10 | } 11 | -------------------------------------------------------------------------------- /KernelLuaVm/lua/api.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../crt/crt.h" 3 | #include "native_function.hpp" 4 | 5 | namespace lua 6 | { 7 | bool attach_process( PEPROCESS process ); 8 | bool attach_pid( uint64_t pid ); 9 | bool detach(); 10 | 11 | // Exposes the NtLua API to the Lua state. 12 | // 13 | void expose_api( lua_State* L ); 14 | }; 15 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/fabs.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | typedef union 4 | { 5 | double d; 6 | struct 7 | { 8 | uint64_t m : 52; 9 | uint64_t e : 11; 10 | uint64_t s : 1; 11 | }; 12 | } double_s_t; 13 | 14 | double fabs( double x ) 15 | { 16 | double_s_t dx = { x }; 17 | dx.s = 0; 18 | return dx.d; 19 | } -------------------------------------------------------------------------------- /KernelLuaVm/driver_io.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Assuming the platform specific header is included already. 4 | #define NTLUA_RUN CTL_CODE( 0x13, 0x37, METHOD_BUFFERED, FILE_ANY_ACCESS ) 5 | #define NTLUA_RESET CTL_CODE( 0x13, 0x38, METHOD_BUFFERED, FILE_ANY_ACCESS ) 6 | 7 | // Shared structures. 8 | // 9 | struct ntlua_result 10 | { 11 | char* errors; 12 | char* outputs; 13 | }; -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/trunc.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double trunc(double x) 4 | { 5 | union {double f; uint64_t i;} u = {x}; 6 | int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12; 7 | uint64_t m; 8 | 9 | if (e >= 52 + 12) 10 | return x; 11 | if (e < 12) 12 | e = 1; 13 | m = -1ULL >> e; 14 | if ((u.i & m) == 0) 15 | return x; 16 | FORCE_EVAL(x + 0x1p120f); 17 | u.i &= ~m; 18 | return u.f; 19 | } 20 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/nearbyint.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* nearbyint is the same as rint, but it must not raise the inexact exception */ 4 | 5 | double nearbyint(double x) 6 | { 7 | #ifdef FE_INEXACT 8 | #pragma STDC FENV_ACCESS ON 9 | int e; 10 | 11 | e = fetestexcept(FE_INEXACT); 12 | #endif 13 | x = rint(x); 14 | #ifdef FE_INEXACT 15 | if (!e) 16 | feclearexcept(FE_INEXACT); 17 | #endif 18 | return x; 19 | } 20 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/frexp.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double frexp(double x, int *e) 4 | { 5 | union { double d; uint64_t i; } y = { x }; 6 | int ee = y.i>>52 & 0x7ff; 7 | 8 | if (!ee) { 9 | if (x) { 10 | x = frexp(x*0x1p64, e); 11 | *e -= 64; 12 | } else *e = 0; 13 | return x; 14 | } else if (ee == 0x7ff) { 15 | return x; 16 | } 17 | 18 | *e = ee - 0x3fe; 19 | y.i &= 0x800fffffffffffffull; 20 | y.i |= 0x3fe0000000000000ull; 21 | return y.d; 22 | } 23 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/__expo2.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* k is such that k*ln2 has minimal relative error and x - kln2 > log(DBL_MIN) */ 4 | static const int k = 2043; 5 | static const double kln2 = 0x1.62066151add8bp+10; 6 | 7 | /* exp(x)/2 for x >= log(DBL_MAX), slightly better than 0.5*exp(x/2)*exp(x/2) */ 8 | double __expo2(double x) 9 | { 10 | double scale; 11 | 12 | /* note that k is odd and scale*scale overflows */ 13 | INSERT_WORDS(scale, (uint32_t)(0x3ff + k/2) << 20, 0); 14 | /* exp(x - k ln2) * 2**(k-1) */ 15 | return exp(x - kln2) * scale * scale; 16 | } 17 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/rint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "..\libmd.h" 3 | 4 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 5 | #define EPS DBL_EPSILON 6 | #elif FLT_EVAL_METHOD==2 7 | #define EPS LDBL_EPSILON 8 | #endif 9 | static const double_t toint = 1/EPS; 10 | 11 | double rint(double x) 12 | { 13 | union {double f; uint64_t i;} u = {x}; 14 | int e = u.i>>52 & 0x7ff; 15 | int s = u.i>>63; 16 | double_t y; 17 | 18 | if (e >= 0x3ff+52) 19 | return x; 20 | if (s) 21 | y = x - toint + toint; 22 | else 23 | y = x + toint - toint; 24 | if (y == 0) 25 | return s ? -0.0 : 0; 26 | return y; 27 | } 28 | -------------------------------------------------------------------------------- /LuaLib/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /scripts/EscalateToken.lua: -------------------------------------------------------------------------------- 1 | function FindProcess(pid) 2 | local Tmp = tmp(0x8) 3 | Tmp:set(0) 4 | nt.PsLookupProcessByProcessId(pid, Tmp:ref()) 5 | return Tmp:get() 6 | end 7 | 8 | function EscalateToken(Target, Source) 9 | Source = Source or read8(nt.PsInitialSystemProcess:address()) 10 | local Token = nt.PsReferencePrimaryToken(Source) 11 | local TokenOffset = 0 12 | 13 | for i=0,0x500,0x8 do 14 | if (read8(Source+i)|0xF) == (Token|0xF) then 15 | TokenOffset = i 16 | break 17 | end 18 | end 19 | nt.PsDereferencePrimaryToken( Token ) 20 | 21 | write8( Target + TokenOffset, Token | 0xF ) 22 | end 23 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/acosh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | #if FLT_EVAL_METHOD==2 4 | #undef sqrt 5 | #define sqrt sqrtl 6 | #endif 7 | 8 | /* acosh(x) = log(x + sqrt(x*x-1)) */ 9 | double acosh(double x) 10 | { 11 | union {double f; uint64_t i;} u = { x }; 12 | unsigned e = u.i >> 52 & 0x7ff; 13 | 14 | /* x < 1 domain error is handled in the called functions */ 15 | 16 | if (e < 0x3ff + 1) 17 | /* |x| < 2, up to 2ulp error in [1,1.125] */ 18 | return log1p(x-1 + sqrt((x-1)*(x-1)+2*(x-1))); 19 | if (e < 0x3ff + 26) 20 | /* |x| < 0x1p26 */ 21 | return log(2*x - 1/(x+sqrt(x*x-1))); 22 | /* |x| >= 0x1p26 or nan */ 23 | return log(x) + 0.693147180559945309417232121458176568; 24 | } 25 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/modf.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double modf(double x, double *iptr) 4 | { 5 | union {double f; uint64_t i;} u = {x}; 6 | uint64_t mask; 7 | int e = (int)(u.i>>52 & 0x7ff) - 0x3ff; 8 | 9 | /* no fractional part */ 10 | if (e >= 52) { 11 | *iptr = x; 12 | if (e == 0x400 && u.i<<12 != 0) /* nan */ 13 | return x; 14 | u.i &= 1ULL<<63; 15 | return u.f; 16 | } 17 | 18 | /* no integral part*/ 19 | if (e < 0) { 20 | u.i &= 1ULL<<63; 21 | *iptr = u.f; 22 | return x; 23 | } 24 | 25 | mask = -1ULL>>12>>e; 26 | if ((u.i & mask) == 0) { 27 | *iptr = x; 28 | u.i &= 1ULL<<63; 29 | return u.f; 30 | } 31 | u.i &= ~mask; 32 | *iptr = u.f; 33 | return x - u.f; 34 | } 35 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/atanh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */ 4 | double atanh(double x) 5 | { 6 | union {double f; uint64_t i;} u = { x }; 7 | unsigned e = u.i >> 52 & 0x7ff; 8 | unsigned s = u.i >> 63; 9 | double_t y; 10 | 11 | /* |x| */ 12 | u.i &= (uint64_t)-1/2; 13 | y = u.f; 14 | 15 | if (e < 0x3ff - 1) { 16 | if (e < 0x3ff - 32) { 17 | /* handle underflow */ 18 | if (e == 0) 19 | FORCE_EVAL((float)y); 20 | } else { 21 | /* |x| < 0.5, up to 1.7ulp error */ 22 | y = 0.5*log1p(2*y + 2*y*y/(1-y)); 23 | } 24 | } else { 25 | /* avoid overflow */ 26 | y = 0.5*log1p(2*(y/(1-y))); 27 | } 28 | return s ? -y : y; 29 | } 30 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/scalbn.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double scalbn(double x, int n) 4 | { 5 | union {double f; uint64_t i;} u; 6 | double_t y = x; 7 | 8 | if (n > 1023) { 9 | y *= 0x1p1023; 10 | n -= 1023; 11 | if (n > 1023) { 12 | y *= 0x1p1023; 13 | n -= 1023; 14 | if (n > 1023) 15 | n = 1023; 16 | } 17 | } else if (n < -1022) { 18 | /* make sure final n < -53 to avoid double 19 | rounding in the subnormal range */ 20 | y *= 0x1p-1022 * 0x1p53; 21 | n += 1022 - 53; 22 | if (n < -1022) { 23 | y *= 0x1p-1022 * 0x1p53; 24 | n += 1022 - 53; 25 | if (n < -1022) 26 | n = -1022; 27 | } 28 | } 29 | u.i = (uint64_t)(0x3ff+n)<<52; 30 | x = y * u.f; 31 | return x; 32 | } 33 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/ceil.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 4 | #define EPS DBL_EPSILON 5 | #elif FLT_EVAL_METHOD==2 6 | #define EPS LDBL_EPSILON 7 | #endif 8 | static const double_t toint = 1/EPS; 9 | 10 | double ceil(double x) 11 | { 12 | union {double f; uint64_t i;} u = {x}; 13 | int e = u.i >> 52 & 0x7ff; 14 | double_t y; 15 | 16 | if (e >= 0x3ff+52 || x == 0) 17 | return x; 18 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 19 | if (u.i >> 63) 20 | y = x - toint + toint - x; 21 | else 22 | y = x + toint - toint - x; 23 | /* special case because of non-nearest rounding modes */ 24 | if (e <= 0x3ff-1) { 25 | FORCE_EVAL(y); 26 | return u.i >> 63 ? -0.0 : 1; 27 | } 28 | if (y < 0) 29 | return x + y + 1; 30 | return x + y; 31 | } 32 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/floor.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 4 | #define EPS DBL_EPSILON 5 | #elif FLT_EVAL_METHOD==2 6 | #define EPS LDBL_EPSILON 7 | #endif 8 | static const double_t toint = 1/EPS; 9 | 10 | double floor(double x) 11 | { 12 | union {double f; uint64_t i;} u = {x}; 13 | int e = u.i >> 52 & 0x7ff; 14 | double_t y; 15 | 16 | if (e >= 0x3ff+52 || x == 0) 17 | return x; 18 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 19 | if (u.i >> 63) 20 | y = x - toint + toint - x; 21 | else 22 | y = x + toint - toint - x; 23 | /* special case because of non-nearest rounding modes */ 24 | if (e <= 0x3ff-1) { 25 | FORCE_EVAL(y); 26 | return u.i >> 63 ? -1 : 0; 27 | } 28 | if (y > 0) 29 | return x + y - 1; 30 | return x + y; 31 | } 32 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/asinh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */ 4 | double asinh(double x) 5 | { 6 | union {double f; uint64_t i;} u = { x }; 7 | unsigned e = u.i >> 52 & 0x7ff; 8 | unsigned s = u.i >> 63; 9 | 10 | /* |x| */ 11 | u.i &= (uint64_t)-1/2; 12 | x = u.f; 13 | 14 | if (e >= 0x3ff + 26) { 15 | /* |x| >= 0x1p26 or inf or nan */ 16 | x = log(x) + 0.693147180559945309417232121458176568; 17 | } else if (e >= 0x3ff + 1) { 18 | /* |x| >= 2 */ 19 | x = log(2*x + 1/(sqrt(x*x+1)+x)); 20 | } else if (e >= 0x3ff - 26) { 21 | /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */ 22 | x = log1p(x + x*x/(sqrt(x*x+1)+1)); 23 | } else { 24 | /* |x| < 0x1p-26, raise inexact if x != 0 */ 25 | FORCE_EVAL(x + 0x1p120f); 26 | } 27 | return s ? -x : x; 28 | } 29 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/memory.cpp: -------------------------------------------------------------------------------- 1 | #include "crt.h" 2 | 3 | __declspec( restrict ) void* malloc( size_t n ) 4 | { 5 | return ExAllocatePool( NonPagedPool, n ); 6 | } 7 | 8 | void free( void* p ) 9 | { 10 | if ( p ) ExFreePool( p ); 11 | } 12 | 13 | void* __stdcall operator new( size_t n ) 14 | { 15 | return malloc( n ); 16 | } 17 | 18 | void* operator new[ ] ( size_t n ) 19 | { 20 | return malloc( n ); 21 | } 22 | 23 | void __stdcall operator delete[ ] ( void* p ) 24 | { 25 | return free( p ); 26 | } 27 | 28 | void __stdcall operator delete( void* p, size_t n ) 29 | { 30 | return free( p ); 31 | } 32 | 33 | void __stdcall operator delete[ ] ( void* p, size_t n ) 34 | { 35 | return free( p ); 36 | } 37 | 38 | void __stdcall operator delete( void* p ) 39 | { 40 | return free( p ); 41 | } 42 | 43 | void* operator new( size_t, void* where ) 44 | { 45 | return where; 46 | } -------------------------------------------------------------------------------- /LuaLib/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 30 | void* data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/cosh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* cosh(x) = (exp(x) + 1/exp(x))/2 4 | * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) 5 | * = 1 + x*x/2 + o(x^4) 6 | */ 7 | double cosh(double x) 8 | { 9 | union {double f; uint64_t i;} u = { x }; 10 | uint32_t w; 11 | double t; 12 | 13 | /* |x| */ 14 | u.i &= (uint64_t)-1/2; 15 | x = u.f; 16 | w = u.i >> 32; 17 | 18 | /* |x| < log(2) */ 19 | if (w < 0x3fe62e42) { 20 | if (w < 0x3ff00000 - (26<<20)) { 21 | /* raise inexact if x!=0 */ 22 | FORCE_EVAL(x + 0x1p120f); 23 | return 1; 24 | } 25 | t = expm1(x); 26 | return 1 + t*t/(2*(1+t)); 27 | } 28 | 29 | /* |x| < log(DBL_MAX) */ 30 | if (w < 0x40862e42) { 31 | t = exp(x); 32 | /* note: if x>log(0x1p26) then the 1/t is not needed */ 33 | return 0.5*(t + 1/t); 34 | } 35 | 36 | /* |x| > log(DBL_MAX) or nan */ 37 | /* note: the result is stored to handle overflow */ 38 | t = __expo2(x); 39 | return t; 40 | } 41 | -------------------------------------------------------------------------------- /KernelLuaVm/lua/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../crt/crt.h" 3 | #include 4 | #include "../logger.hpp" 5 | 6 | extern "C" 7 | { 8 | #include 9 | #include 10 | #include 11 | }; 12 | 13 | namespace lua 14 | { 15 | struct lua_context 16 | { 17 | // Jump buffer taken on Lua panic. 18 | // 19 | jmp_buf panic_jump = {}; 20 | }; 21 | 22 | // Initializes a Lua state. 23 | // 24 | lua_State* init(); 25 | 26 | // Destroys a Lua state. 27 | // 28 | void destroy( lua_State* L ); 29 | 30 | // Gets current context from a Lua state. 31 | // 32 | lua_context* get_context( lua_State* L ); 33 | 34 | // Executes code in given Lua state. 35 | // 36 | void execute( lua_State* L, const char* code, bool user_input = false ); 37 | }; 38 | 39 | // Some helpers we need in Lua style. 40 | // 41 | uint64_t lua_asintrinsic( lua_State* L, int i ); 42 | void* lua_adressof( lua_State* L, int i ); -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/sinh.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | /* sinh(x) = (exp(x) - 1/exp(x))/2 4 | * = (exp(x)-1 + (exp(x)-1)/exp(x))/2 5 | * = x + x^3/6 + o(x^5) 6 | */ 7 | double sinh(double x) 8 | { 9 | union {double f; uint64_t i;} u = { x }; 10 | uint32_t w; 11 | double t, h, absx; 12 | 13 | h = 0.5; 14 | if (u.i >> 63) 15 | h = -h; 16 | /* |x| */ 17 | u.i &= (uint64_t)-1/2; 18 | absx = u.f; 19 | w = u.i >> 32; 20 | 21 | /* |x| < log(DBL_MAX) */ 22 | if (w < 0x40862e42) { 23 | t = expm1(absx); 24 | if (w < 0x3ff00000) { 25 | if (w < 0x3ff00000 - (26<<20)) 26 | /* note: inexact and underflow are raised by expm1 */ 27 | /* note: this branch avoids spurious underflow */ 28 | return x; 29 | return h*(2*t - t*t/(t+1)); 30 | } 31 | /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */ 32 | return h*(t + t/(t+1)); 33 | } 34 | 35 | /* |x| > log(DBL_MAX) or nan */ 36 | /* note: the result is stored to handle overflow */ 37 | t = 2*h*__expo2(absx); 38 | return t; 39 | } 40 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/string.c: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "crt.h" 3 | 4 | int isalpha( int c ) 5 | { 6 | return ( 'a' <= c && c <= 'z' ) || 7 | ( 'A' <= c && c <= 'Z' ); 8 | } 9 | 10 | int isdigit( int c ) 11 | { 12 | return ( '0' <= c && c <= '9' ); 13 | } 14 | 15 | int isalnum( int c ) 16 | { 17 | return isalpha( c ) || isdigit( c ); 18 | } 19 | 20 | int iscntrl( int c ) 21 | { 22 | return c <= 0x1F || c == 0x7F; 23 | } 24 | 25 | int isgraph( int c ) 26 | { 27 | return 0x21 <= c && c <= 0x7E; 28 | } 29 | 30 | int ispunct( int c ) 31 | { 32 | for ( auto o : "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" ) 33 | if ( o == c ) return 1; 34 | return 0; 35 | } 36 | 37 | double strtod( char* str, char** endptr ) 38 | { 39 | double v = 0; 40 | if ( sscanf_s( str, "%lf", &v ) && endptr ) 41 | *endptr = str + strlen( str ); 42 | return v; 43 | } 44 | 45 | char* strpbrk( const char* s1, const char* s2 ) 46 | { 47 | while ( *s1 ) 48 | if ( strchr( s2, *s1++ ) ) 49 | return ( char* )--s1; 50 | return 0; 51 | } 52 | 53 | // haha locale go brrr 54 | // 55 | int strcoll( const char* a, const char* b ) 56 | { 57 | return strcmp( a, b ); 58 | } -------------------------------------------------------------------------------- /LuaLib/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | 12 | 13 | /* 14 | ** Standard I/O stuff 15 | */ 16 | #define _NO_CRT_STDIO_INLINE 17 | #include 18 | #define snprintf _snprintf 19 | 20 | /* 21 | ** Allows POSIX/XSI stuff 22 | */ 23 | #if !defined(LUA_USE_C89) /* { */ 24 | 25 | #if !defined(_XOPEN_SOURCE) 26 | #define _XOPEN_SOURCE 600 27 | #elif _XOPEN_SOURCE == 0 28 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 29 | #endif 30 | 31 | /* 32 | ** Allows manipulation of large files in gcc and some other compilers 33 | */ 34 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 35 | #define _LARGEFILE_SOURCE 1 36 | #define _FILE_OFFSET_BITS 64 37 | #endif 38 | 39 | #endif /* } */ 40 | 41 | 42 | /* 43 | ** Windows stuff 44 | */ 45 | #if defined(_WIN32) /* { */ 46 | 47 | #if !defined(_CRT_SECURE_NO_WARNINGS) 48 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 49 | #endif 50 | 51 | #endif /* } */ 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /LuaLib/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_STRLIBNAME "string" 27 | LUAMOD_API int (luaopen_string) (lua_State *L); 28 | 29 | #define LUA_UTF8LIBNAME "utf8" 30 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_LOADLIBNAME "package" 39 | LUAMOD_API int (luaopen_package) (lua_State *L); 40 | 41 | 42 | /* open all previous libraries */ 43 | LUALIB_API void (luaL_openlibs) (lua_State *L); 44 | 45 | 46 | 47 | #if !defined(lua_assert) 48 | #define lua_assert(x) ((void)0) 49 | #endif 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/io.c: -------------------------------------------------------------------------------- 1 | #include "../logger.hpp" 2 | #include "crt.h" 3 | 4 | FILE* __stdcall __acrt_iob_func( unsigned i ) 5 | { 6 | if ( i == 1 ) return FILE_STDOUT; 7 | if ( i == 2 ) return FILE_STDERR; 8 | return 0; 9 | } 10 | 11 | FILE* freopen( const char* filename, const char* mode, FILE* stream ) 12 | { 13 | return 0; 14 | } 15 | 16 | size_t fwrite( const void* ptr, size_t size, size_t count, FILE* stream ) 17 | { 18 | if ( stream == FILE_STDOUT ) 19 | { 20 | logger::logs.append_raw( ( const char* ) ptr, size * count ); 21 | return count; 22 | } 23 | else if ( stream == FILE_STDERR ) 24 | { 25 | logger::errors.append_raw( ( const char* ) ptr, size * count ); 26 | return count; 27 | } 28 | return 0; 29 | } 30 | 31 | size_t fread( void* ptr, size_t size, size_t count, FILE* stream ) 32 | { 33 | return 0; 34 | } 35 | 36 | int getc( FILE* stream ) 37 | { 38 | return -1; 39 | } 40 | 41 | FILE* fopen( const char* filename, const char* mode ) 42 | { 43 | return 0; 44 | } 45 | 46 | int fflush( FILE* stream ) 47 | { 48 | return 0; 49 | } 50 | 51 | int ferror( FILE* stream ) 52 | { 53 | return 1; 54 | } 55 | 56 | int feof( FILE* stream ) 57 | { 58 | return 1; 59 | } 60 | 61 | int fclose( FILE* stream ) 62 | { 63 | return 1; 64 | } -------------------------------------------------------------------------------- /LuaLib/LuaLib.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29709.97 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LuaLib", "LuaLib.vcxproj", "{71FC9FC1-F46F-41D4-913E-07298D6E8220}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Debug|x64.ActiveCfg = Debug|x64 17 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Debug|x64.Build.0 = Debug|x64 18 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Debug|x86.ActiveCfg = Debug|Win32 19 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Debug|x86.Build.0 = Debug|Win32 20 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x64.ActiveCfg = Release|x64 21 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x64.Build.0 = Release|x64 22 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x86.ActiveCfg = Release|Win32 23 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {0E812DA3-D166-4D3C-9218-E8BB24A7A288} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /KernelLuaVm/logger.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | extern "C" { 5 | __declspec( dllimport ) int sprintf_s( char* buffer, size_t sizeOfBuffer, const char* format, ... ); 6 | }; 7 | 8 | // Basic logger implementation. 9 | // 10 | namespace logger 11 | { 12 | struct string_buffer 13 | { 14 | static constexpr size_t buffer_length = 1024 * 1024 * 16; 15 | 16 | char raw[ buffer_length ]; 17 | size_t iterator = 0; 18 | 19 | template 20 | int append( const char* format, T... args ) 21 | { 22 | int result = sprintf_s( &raw[ iterator ], buffer_length - iterator, format, args... ); 23 | if ( result > 0 ) 24 | iterator += result; 25 | return result; 26 | } 27 | 28 | int append_raw( const char* data, size_t len ) 29 | { 30 | if ( len > ( buffer_length - iterator ) ) 31 | len = buffer_length - iterator; 32 | memcpy( &raw[ iterator ], data, len ); 33 | iterator += len; 34 | return len; 35 | } 36 | 37 | void reset() 38 | { 39 | iterator = 0; 40 | } 41 | }; 42 | 43 | inline string_buffer logs = {}; 44 | inline string_buffer errors = {}; 45 | 46 | template inline int error( const char* format, T... args ) { return errors.append( format, args... ); } 47 | template inline int log( const char* format, T... args ) { return logs.append( format, args... ); } 48 | }; -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/fmod.c: -------------------------------------------------------------------------------- 1 | #include "..\libmd.h" 2 | 3 | double fmod(double x, double y) 4 | { 5 | union {double f; uint64_t i;} ux = {x}, uy = {y}; 6 | int ex = ux.i>>52 & 0x7ff; 7 | int ey = uy.i>>52 & 0x7ff; 8 | int sx = ux.i>>63; 9 | uint64_t i; 10 | 11 | /* in the followings uxi should be ux.i, but then gcc wrongly adds */ 12 | /* float load/store to inner loops ruining performance and code size */ 13 | uint64_t uxi = ux.i; 14 | 15 | if (uy.i<<1 == 0 || isnan(y) || ex == 0x7ff) 16 | return (x*y)/(x*y); 17 | if (uxi<<1 <= uy.i<<1) { 18 | if (uxi<<1 == uy.i<<1) 19 | return 0*x; 20 | return x; 21 | } 22 | 23 | /* normalize x and y */ 24 | if (!ex) { 25 | for (i = uxi<<12; i>>63 == 0; ex--, i <<= 1); 26 | uxi <<= -ex + 1; 27 | } else { 28 | uxi &= -1ULL >> 12; 29 | uxi |= 1ULL << 52; 30 | } 31 | if (!ey) { 32 | for (i = uy.i<<12; i>>63 == 0; ey--, i <<= 1); 33 | uy.i <<= -ey + 1; 34 | } else { 35 | uy.i &= -1ULL >> 12; 36 | uy.i |= 1ULL << 52; 37 | } 38 | 39 | /* x mod y */ 40 | for (; ex > ey; ex--) { 41 | i = uxi - uy.i; 42 | if (i >> 63 == 0) { 43 | if (i == 0) 44 | return 0*x; 45 | uxi = i; 46 | } 47 | uxi <<= 1; 48 | } 49 | i = uxi - uy.i; 50 | if (i >> 63 == 0) { 51 | if (i == 0) 52 | return 0*x; 53 | uxi = i; 54 | } 55 | for (; uxi>>52 == 0; uxi <<= 1, ex--); 56 | 57 | /* scale result */ 58 | if (ex > 0) { 59 | uxi -= 1ULL << 52; 60 | uxi |= (uint64_t)ex << 52; 61 | } else { 62 | uxi >>= -ex + 1; 63 | } 64 | uxi |= (uint64_t)sx << 63; 65 | ux.i = uxi; 66 | return ux.f; 67 | } 68 | -------------------------------------------------------------------------------- /LuaLib/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 24 | const TValue *p2); 25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 26 | const TValue *p2, 27 | const char *msg); 28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 29 | const TValue *p2); 30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 31 | const TValue *p2); 32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 34 | TString *src, int line); 35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 36 | LUAI_FUNC void luaG_traceexec (lua_State *L); 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /LuaLib/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | 17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 18 | #define sizeudata(u) sizeludata((u)->len) 19 | 20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21 | (sizeof(s)/sizeof(char))-1)) 22 | 23 | 24 | /* 25 | ** test whether a string is a reserved word 26 | */ 27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 28 | 29 | 30 | /* 31 | ** equality for short strings, which are always internalized 32 | */ 33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 34 | 35 | 36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_clearcache (global_State *g); 41 | LUAI_FUNC void luaS_init (lua_State *L); 42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /LuaLib/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /KernelLuaVm/lua/native_function.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "state.hpp" 3 | 4 | // Converts a C function into a Lua function assuming basic types. 5 | // 6 | struct native_function 7 | { 8 | static constexpr const char* export_name = "native_function"; 9 | const void* address = nullptr; 10 | 11 | // Allocator. 12 | // 13 | static native_function* push( lua_State* L ); 14 | 15 | // Constructor. 16 | // 17 | static int create( lua_State* L ); 18 | 19 | // Getter. 20 | // 21 | static native_function* check( lua_State* L, int index ); 22 | 23 | // Member functions. 24 | // 25 | static int get_address( lua_State* L ); 26 | static int invoke( lua_State* L ); 27 | static int to_string( lua_State* L ); 28 | 29 | // Registrar. 30 | // 31 | static void declare( lua_State* L ) 32 | { 33 | static const luaL_Reg method_table[] = { 34 | { "new", create }, 35 | { "address", get_address }, 36 | { nullptr, nullptr } 37 | }; 38 | 39 | static const luaL_Reg metatable[] = { 40 | { "__tostring", to_string }, 41 | { "__call", invoke }, 42 | { nullptr, nullptr } 43 | }; 44 | 45 | luaL_openlib( L, export_name, method_table, 0 ); 46 | luaL_newmetatable( L, export_name ); 47 | 48 | luaL_openlib( L, 0, metatable, 0 ); 49 | lua_pushliteral( L, "__index" ); 50 | lua_pushvalue( L, -3 ); 51 | lua_rawset( L, -3 ); 52 | lua_pushliteral( L, "__metatable" ); 53 | lua_pushvalue( L, -3 ); 54 | lua_rawset( L, -3 ); 55 | lua_pop( L, 2 ); 56 | } 57 | }; -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/copysign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "..\libmd.h" 28 | 29 | typedef union { 30 | double d; 31 | struct { 32 | uint64_t m : 52; 33 | uint64_t e : 11; 34 | uint64_t s : 1; 35 | }; 36 | } double_s_t; 37 | 38 | double copysign(double x, double y) { 39 | double_s_t dx = { x }; 40 | double_s_t dy = { y }; 41 | 42 | // copy sign bit; 43 | dx.s = dy.s; 44 | 45 | return dx.d; 46 | } -------------------------------------------------------------------------------- /LuaLib/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NtLua 2 | Do you have a burning desire to run coroutines in kernel? 3 | 4 | Do you hate the fact that Microsoft does not consider Admin-Kernel boundary real when they do not feel like paying security researchers and yet refuse to give you Ring-0 access? 5 | 6 | Did your parents explicitly tell you to stay away from kernel? 7 | 8 | Do you get a smile on your face when you imagine device driver developers seeing this and crying over how stupid it is for hours (although half of the hardware manufacturers ship drivers with arbitrary physical memory read/write somehow)? 9 | 10 | Well you've come to the right place to run a scripting language at `DPC_LEVEL`! 11 | 12 | # How to use 13 | 1) Build everything using the solution file. 14 | 2) Create the NtLua service via `sc create NtLua binpath= type= kernel` 15 | 3) Start the NtLua service `sc start NtLua` 16 | 4) Run the console and enjoy! 17 | 18 | # Horrible samples for horrible people 19 | ![](https://i.can.ac/vq7g1.png) 20 | ![](https://i.can.ac/OKncG.png) 21 | ![](https://i.can.ac/PmiNI.png) 22 | ![](https://i.can.ac/K6Da1.png) 23 | ![](https://i.can.ac/lvDuN.png) 24 | ![](https://i.can.ac/lD1bF.png) 25 | 26 | ## Supports: 27 | - Structured exception handling 28 | - Garbage collected temporaries 29 | - UNICODE_STRING/ANSI_STRING via `unicode_string(str) and ansi_string(str)` 30 | - Automatic importing of entire kernel images 31 | 32 | ## Under development: 33 | - File importing via UM console 34 | - Symbol parsing for internal functions and structure declarations 35 | - Lua to C callback wrapping 36 | - Multi-thread support 37 | - HIGH_LEVEL IRQL support 38 | - Other fun stuff you are considering to contribute. 39 | 40 | ---------- 41 | ## If you have any useful scripts, feel free to send a PR to include it in the Repo under /scripts! 42 | -------------------------------------------------------------------------------- /LuaLib/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | /* 33 | ** Upvalues for Lua closures 34 | */ 35 | struct UpVal { 36 | TValue *v; /* points to stack or to its own value */ 37 | lu_mem refcount; /* reference counter */ 38 | union { 39 | struct { /* (when open) */ 40 | UpVal *next; /* linked list */ 41 | int touched; /* mark to avoid cycles with dead threads */ 42 | } open; 43 | TValue value; /* the value (when closed) */ 44 | } u; 45 | }; 46 | 47 | #define upisopen(up) ((up)->v != &(up)->u.value) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 58 | int pc); 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /LuaLib/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | // --------- PATCH ----------// 43 | static const luaL_Reg loadedlibs[] = { 44 | {"_G", luaopen_base}, 45 | {LUA_LOADLIBNAME, luaopen_package}, 46 | {LUA_COLIBNAME, luaopen_coroutine}, 47 | {LUA_TABLIBNAME, luaopen_table}, 48 | {LUA_STRLIBNAME, luaopen_string}, 49 | {LUA_MATHLIBNAME, luaopen_math}, 50 | {LUA_UTF8LIBNAME, luaopen_utf8}, 51 | #if defined(LUA_COMPAT_BITLIB) 52 | {LUA_BITLIBNAME, luaopen_bit32}, 53 | #endif 54 | {NULL, NULL} 55 | }; 56 | 57 | 58 | LUALIB_API void luaL_openlibs (lua_State *L) { 59 | const luaL_Reg *lib; 60 | /* "require" functions from 'loadedlibs' and set results to global table */ 61 | for (lib = loadedlibs; lib->func; lib++) { 62 | luaL_requiref(L, lib->name, lib->func, 1); 63 | lua_pop(L, 1); /* remove lib */ 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /LuaLib/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | 55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 56 | 57 | 58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 59 | 60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 62 | TMS event); 63 | LUAI_FUNC void luaT_init (lua_State *L); 64 | 65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 66 | const TValue *p2, TValue *p3, int hasres); 67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 68 | StkId res, TMS event); 69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 70 | StkId res, TMS event); 71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 72 | const TValue *p2, TMS event); 73 | 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /LuaLib/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/tan.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_tan.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* tan(x) 13 | * Return tangent function of x. 14 | * 15 | * kernel function: 16 | * __tan ... tangent function on [-pi/4,pi/4] 17 | * __rem_pio2 ... argument reduction routine 18 | * 19 | * Method. 20 | * Let S,C and T denote the sin, cos and tan respectively on 21 | * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 22 | * in [-pi/4 , +pi/4], and let n = k mod 4. 23 | * We have 24 | * 25 | * n sin(x) cos(x) tan(x) 26 | * ---------------------------------------------------------- 27 | * 0 S C T 28 | * 1 C -S -1/T 29 | * 2 -S -C T 30 | * 3 -C S -1/T 31 | * ---------------------------------------------------------- 32 | * 33 | * Special cases: 34 | * Let trig be any of sin, cos, or tan. 35 | * trig(+-INF) is NaN, with signals; 36 | * trig(NaN) is that NaN; 37 | * 38 | * Accuracy: 39 | * TRIG(x) returns trig(x) nearly rounded 40 | */ 41 | 42 | #include "..\libmd.h" 43 | 44 | double tan(double x) 45 | { 46 | double y[2]; 47 | uint32_t ix; 48 | unsigned n; 49 | 50 | GET_HIGH_WORD(ix, x); 51 | ix &= 0x7fffffff; 52 | 53 | /* |x| ~< pi/4 */ 54 | if (ix <= 0x3fe921fb) { 55 | if (ix < 0x3e400000) { /* |x| < 2**-27 */ 56 | /* raise inexact if x!=0 and underflow if subnormal */ 57 | FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f); 58 | return x; 59 | } 60 | return __tan(x, 0.0, 0); 61 | } 62 | 63 | /* tan(Inf or NaN) is NaN */ 64 | if (ix >= 0x7ff00000) 65 | return x - x; 66 | 67 | /* argument reduction */ 68 | n = __rem_pio2(x, y); 69 | return __tan(y[0], y[1], n&1); 70 | } 71 | -------------------------------------------------------------------------------- /LuaLib/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } 26 | 27 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 29 | 30 | 31 | 32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 34 | 35 | 36 | /* type of protected functions, to be ran by 'runprotected' */ 37 | typedef void (*Pfunc) (lua_State *L, void *ud); 38 | 39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 40 | const char *mode); 41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 46 | ptrdiff_t oldtop, ptrdiff_t ef); 47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, 48 | int nres); 49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 52 | LUAI_FUNC void luaD_inctop (lua_State *L); 53 | 54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /KernelLuaVm/lua/native_function.cpp: -------------------------------------------------------------------------------- 1 | #include "native_function.hpp" 2 | 3 | // Allocator. 4 | // 5 | native_function* native_function::push( lua_State* L ) 6 | { 7 | native_function* fn = ( native_function* ) lua_newuserdata( L, sizeof( native_function ) ); 8 | luaL_getmetatable( L, export_name ); 9 | lua_setmetatable( L, -2 ); 10 | return fn; 11 | } 12 | 13 | // Constructor. 14 | // 15 | int native_function::create( lua_State* L ) 16 | { 17 | push( L )->address = ( void* ) luaL_checkunsigned( L, 1 ); 18 | return 1; 19 | } 20 | 21 | // Getter. 22 | // 23 | native_function* native_function::check( lua_State* L, int index ) 24 | { 25 | native_function* p; 26 | luaL_checktype( L, index, LUA_TUSERDATA ); 27 | p = ( native_function* ) luaL_checkudata( L, index, export_name ); 28 | if ( !p ) 29 | luaL_error( L, "Type mismatch, expected [%s]\n", export_name ); 30 | return p; 31 | } 32 | 33 | // Member functions. 34 | // 35 | int native_function::get_address( lua_State* L ) 36 | { 37 | lua_pushunsigned( L, ( uint64_t ) ( ( native_function* ) check( L, 1 ) )->address ); 38 | return 1; 39 | } 40 | int native_function::invoke( lua_State* L ) 41 | { 42 | native_function* fn = ( native_function* ) check( L, 1 ); 43 | 44 | // Get number of arguments. 45 | // 46 | int n = lua_gettop( L ) - 1; 47 | if ( n >= 32 ) 48 | luaL_error( L, "Too many arguments provided %d vs maximum of 16\n", n ); 49 | 50 | // Recursively create the call frame and call out. 51 | // 52 | auto rec = [ & ] ( auto&& self, const void* fn, size_t i, Tx... args ) -> uint64_t 53 | { 54 | if constexpr ( sizeof...( Tx ) <= 32 ) 55 | { 56 | if ( i == n ) 57 | return ( ( uint64_t( __stdcall* )( Tx... ) ) fn )( args... ); 58 | else 59 | return self( self, fn, i + 1, args..., lua_asintrinsic( L, i + 2 ) ); 60 | } 61 | __assume( 0 ); 62 | }; 63 | uint64_t result = rec( rec, fn->address, 0 ); 64 | 65 | // Push the result and return. 66 | // 67 | lua_pushunsigned( L, result ); 68 | return 1; 69 | } 70 | int native_function::to_string( lua_State* L ) 71 | { 72 | lua_pushfstring( L, "native_function (0x%p)", check( L, 1 )->address ); 73 | return 1; 74 | } -------------------------------------------------------------------------------- /LuaLib/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | /* 22 | ** writable version of 'gkey'; allows updates to individual fields, 23 | ** but not to the whole (which has incompatible type) 24 | */ 25 | #define wgkey(n) (&(n)->i_key.nk) 26 | 27 | #define invalidateTMcache(t) ((t)->flags = 0) 28 | 29 | 30 | /* true when 't' is using 'dummynode' as its hash part */ 31 | #define isdummy(t) ((t)->lastfree == NULL) 32 | 33 | 34 | /* allocated size for hash nodes */ 35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 36 | 37 | 38 | /* returns the key, given the value of a table entry */ 39 | #define keyfromval(v) \ 40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 41 | 42 | 43 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 44 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 45 | TValue *value); 46 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 47 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 48 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 49 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 50 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 51 | LUAI_FUNC Table *luaH_new (lua_State *L); 52 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 53 | unsigned int nhsize); 54 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 55 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 56 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 57 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/crt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #define _INC_STDIO 3 | #include 4 | #include 5 | #include 6 | #include "stdint.h" 7 | #include "libmd.h" 8 | 9 | #define FILE_STDOUT ((FILE*)0x13370001) 10 | #define FILE_STDERR ((FILE*)0x13370002) 11 | 12 | // CRT initializers. 13 | // 14 | #pragma section(".CRT", read) 15 | using fn_crt_initializer = void(*)(); 16 | __declspec( allocate( ".CRT" ) ) static const fn_crt_initializer crt_tracker = nullptr; 17 | 18 | namespace crt 19 | { 20 | static void initialize() 21 | { 22 | const fn_crt_initializer* entry = &crt_tracker; 23 | while ( *++entry ) 24 | ( *entry )( ); 25 | } 26 | }; 27 | 28 | // C++ memory decleration. 29 | // 30 | __declspec( restrict ) void* malloc( size_t n ); 31 | void free( void* p ); 32 | void* operator new( size_t, void* where ); 33 | void* operator new( size_t Size ); 34 | void* operator new[ ] ( size_t Size ); 35 | void operator delete( void* Adr ); 36 | void operator delete( void* Adr, size_t Size ); 37 | void operator delete[ ] ( void* Adr ); 38 | void operator delete[ ] ( void* Adr, size_t Size ); 39 | 40 | extern "C" 41 | { 42 | // String utils. 43 | // 44 | int isalpha( int c ); 45 | int isdigit( int c ); 46 | int isalnum( int c ); 47 | int iscntrl( int c ); 48 | int isgraph( int c ); 49 | int ispunct( int c ); 50 | __declspec( dllimport ) int sscanf_s( const char* buffer, const char* format, ... ); // @ ntoskrnl.lib 51 | __declspec( dllimport ) int sprintf_s( char* buffer, size_t sizeOfBuffer, const char* format, ... ); // @ ntoskrnl.lib 52 | double strtod( char* str, char** endptr ); 53 | char* strpbrk( const char* s1, const char* s2 ); 54 | int strcoll( const char* a, const char* b ); 55 | 56 | // IO utils. 57 | // 58 | typedef struct _FILE {} FILE; // Maybe it will be implemented one day. 59 | 60 | FILE* __stdcall __acrt_iob_func( unsigned i ); 61 | FILE* freopen( const char* filename, const char* mode, FILE* stream ); 62 | size_t fwrite( const void* ptr, size_t size, size_t count, FILE* stream ); 63 | size_t fread( void* ptr, size_t size, size_t count, FILE* stream ); 64 | int getc( FILE* stream ); 65 | FILE* fopen( const char* filename, const char* mode ); 66 | int fflush( FILE* stream ); 67 | int ferror( FILE* stream ); 68 | int feof( FILE* stream ); 69 | int fclose( FILE* stream ); 70 | 71 | // Misc. functions. 72 | // 73 | void abort(); 74 | char* getenv(); 75 | }; -------------------------------------------------------------------------------- /NtLua.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29709.97 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LuaLib", "LuaLib\LuaLib.vcxproj", "{71FC9FC1-F46F-41D4-913E-07298D6E8220}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ActualConsole", "ActualConsole\ActualConsole.vcxproj", "{82B12053-4DA0-440B-8AF4-55F49658299E}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "KernelLuaVm", "KernelLuaVm\KernelLuaVm.vcxproj", "{7FAC530A-63E4-4C27-86E1-B318205E4C0E}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x64.ActiveCfg = Release|x64 19 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x64.Build.0 = Release|x64 20 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x64.Deploy.0 = Release|x64 21 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x86.ActiveCfg = Release|Win32 22 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x86.Build.0 = Release|Win32 23 | {71FC9FC1-F46F-41D4-913E-07298D6E8220}.Release|x86.Deploy.0 = Release|Win32 24 | {82B12053-4DA0-440B-8AF4-55F49658299E}.Release|x64.ActiveCfg = Release|x64 25 | {82B12053-4DA0-440B-8AF4-55F49658299E}.Release|x64.Build.0 = Release|x64 26 | {82B12053-4DA0-440B-8AF4-55F49658299E}.Release|x86.ActiveCfg = Release|Win32 27 | {82B12053-4DA0-440B-8AF4-55F49658299E}.Release|x86.Build.0 = Release|Win32 28 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x64.ActiveCfg = Release|x64 29 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x64.Build.0 = Release|x64 30 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x64.Deploy.0 = Release|x64 31 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x86.ActiveCfg = Release|Win32 32 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x86.Build.0 = Release|Win32 33 | {7FAC530A-63E4-4C27-86E1-B318205E4C0E}.Release|x86.Deploy.0 = Release|Win32 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(ExtensibilityGlobals) = postSolution 39 | SolutionGuid = {F86C24F0-ACB0-423E-9DBC-C1B65F86BC4A} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/cos.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_cos.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* cos(x) 13 | * Return cosine function of x. 14 | * 15 | * kernel function: 16 | * __sin ... sine function on [-pi/4,pi/4] 17 | * __cos ... cosine function on [-pi/4,pi/4] 18 | * __rem_pio2 ... argument reduction routine 19 | * 20 | * Method. 21 | * Let S,C and T denote the sin, cos and tan respectively on 22 | * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 23 | * in [-pi/4 , +pi/4], and let n = k mod 4. 24 | * We have 25 | * 26 | * n sin(x) cos(x) tan(x) 27 | * ---------------------------------------------------------- 28 | * 0 S C T 29 | * 1 C -S -1/T 30 | * 2 -S -C T 31 | * 3 -C S -1/T 32 | * ---------------------------------------------------------- 33 | * 34 | * Special cases: 35 | * Let trig be any of sin, cos, or tan. 36 | * trig(+-INF) is NaN, with signals; 37 | * trig(NaN) is that NaN; 38 | * 39 | * Accuracy: 40 | * TRIG(x) returns trig(x) nearly rounded 41 | */ 42 | 43 | #include "..\libmd.h" 44 | 45 | double cos(double x) 46 | { 47 | double y[2]; 48 | uint32_t ix; 49 | unsigned n; 50 | 51 | GET_HIGH_WORD(ix, x); 52 | ix &= 0x7fffffff; 53 | 54 | /* |x| ~< pi/4 */ 55 | if (ix <= 0x3fe921fb) { 56 | if (ix < 0x3e46a09e) { /* |x| < 2**-27 * sqrt(2) */ 57 | /* raise inexact if x!=0 */ 58 | FORCE_EVAL(x + 0x1p120f); 59 | return 1.0; 60 | } 61 | return __cos(x, 0); 62 | } 63 | 64 | /* cos(Inf or NaN) is NaN */ 65 | if (ix >= 0x7ff00000) 66 | return x-x; 67 | 68 | /* argument reduction */ 69 | n = __rem_pio2(x, y); 70 | switch (n&3) { 71 | case 0: return __cos(y[0], y[1]); 72 | case 1: return -__sin(y[0], y[1], 1); 73 | case 2: return -__cos(y[0], y[1]); 74 | default: 75 | return __sin(y[0], y[1], 1); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /LuaLib/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/sin.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* sin(x) 13 | * Return sine function of x. 14 | * 15 | * kernel function: 16 | * __sin ... sine function on [-pi/4,pi/4] 17 | * __cos ... cose function on [-pi/4,pi/4] 18 | * __rem_pio2 ... argument reduction routine 19 | * 20 | * Method. 21 | * Let S,C and T denote the sin, cos and tan respectively on 22 | * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 23 | * in [-pi/4 , +pi/4], and let n = k mod 4. 24 | * We have 25 | * 26 | * n sin(x) cos(x) tan(x) 27 | * ---------------------------------------------------------- 28 | * 0 S C T 29 | * 1 C -S -1/T 30 | * 2 -S -C T 31 | * 3 -C S -1/T 32 | * ---------------------------------------------------------- 33 | * 34 | * Special cases: 35 | * Let trig be any of sin, cos, or tan. 36 | * trig(+-INF) is NaN, with signals; 37 | * trig(NaN) is that NaN; 38 | * 39 | * Accuracy: 40 | * TRIG(x) returns trig(x) nearly rounded 41 | */ 42 | 43 | #include "..\libmd.h" 44 | 45 | double sin(double x) 46 | { 47 | double y[2]; 48 | uint32_t ix; 49 | unsigned n; 50 | 51 | /* High word of x. */ 52 | GET_HIGH_WORD(ix, x); 53 | ix &= 0x7fffffff; 54 | 55 | /* |x| ~< pi/4 */ 56 | if (ix <= 0x3fe921fb) { 57 | if (ix < 0x3e500000) { /* |x| < 2**-26 */ 58 | /* raise inexact if x != 0 and underflow if subnormal*/ 59 | FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f); 60 | return x; 61 | } 62 | return __sin(x, 0.0, 0); 63 | } 64 | 65 | /* sin(Inf or NaN) is NaN */ 66 | if (ix >= 0x7ff00000) 67 | return x - x; 68 | 69 | /* argument reduction needed */ 70 | n = __rem_pio2(x, y); 71 | switch (n&3) { 72 | case 0: return __sin(y[0], y[1], 1); 73 | case 1: return __cos(y[0], y[1]); 74 | case 2: return -__sin(y[0], y[1], 1); 75 | default: 76 | return -__cos(y[0], y[1]); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /LuaLib/LuaLib.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/__sin.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/k_sin.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* __sin( x, y, iy) 13 | * kernel sin function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854 14 | * Input x is assumed to be bounded by ~pi/4 in magnitude. 15 | * Input y is the tail of x. 16 | * Input iy indicates whether y is 0. (if iy=0, y assume to be 0). 17 | * 18 | * Algorithm 19 | * 1. Since sin(-x) = -sin(x), we need only to consider positive x. 20 | * 2. Callers must return sin(-0) = -0 without calling here since our 21 | * odd polynomial is not evaluated in a way that preserves -0. 22 | * Callers may do the optimization sin(x) ~ x for tiny x. 23 | * 3. sin(x) is approximated by a polynomial of degree 13 on 24 | * [0,pi/4] 25 | * 3 13 26 | * sin(x) ~ x + S1*x + ... + S6*x 27 | * where 28 | * 29 | * |sin(x) 2 4 6 8 10 12 | -58 30 | * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 31 | * | x | 32 | * 33 | * 4. sin(x+y) = sin(x) + sin'(x')*y 34 | * ~ sin(x) + (1-x*x/2)*y 35 | * For better accuracy, let 36 | * 3 2 2 2 2 37 | * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6)))) 38 | * then 3 2 39 | * sin(x) = x + (S1*x + (x *(r-y/2)+y)) 40 | */ 41 | 42 | #include "..\libmd.h" 43 | 44 | static const double 45 | S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */ 46 | S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */ 47 | S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */ 48 | S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */ 49 | S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */ 50 | S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */ 51 | 52 | double __sin(double x, double y, int iy) 53 | { 54 | double_t z,r,v,w; 55 | 56 | z = x*x; 57 | w = z*z; 58 | r = S2 + z*(S3 + z*S4) + z*w*(S5 + z*S6); 59 | v = z*x; 60 | if (iy == 0) 61 | return x + v*(S1 + z*r); 62 | else 63 | return x - ((z*(0.5*y - v*r) - y) - v*S1); 64 | } 65 | -------------------------------------------------------------------------------- /LuaLib/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | } LexState; 73 | 74 | 75 | LUAI_FUNC void luaX_init (lua_State *L); 76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 77 | TString *source, int firstchar); 78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 79 | LUAI_FUNC void luaX_next (LexState *ls); 80 | LUAI_FUNC int luaX_lookahead (LexState *ls); 81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /LuaLib/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /KernelLuaVm/KernelLuaVm.inf: -------------------------------------------------------------------------------- 1 | ; 2 | ; KernelLuaVm.inf 3 | ; 4 | 5 | [Version] 6 | Signature="$WINDOWS NT$" 7 | Class=Sample ; TODO: edit Class 8 | ClassGuid={78A1C341-4539-11d3-B88D-00C04FAD5171} ; TODO: edit ClassGuid 9 | Provider=%ManufacturerName% 10 | CatalogFile=KernelLuaVm.cat 11 | DriverVer= ; TODO: set DriverVer in stampinf property pages 12 | 13 | [DestinationDirs] 14 | DefaultDestDir = 12 15 | KernelLuaVm_Device_CoInstaller_CopyFiles = 11 16 | 17 | ; ================= Class section ===================== 18 | 19 | [ClassInstall32] 20 | Addreg=SampleClassReg 21 | 22 | [SampleClassReg] 23 | HKR,,,0,%ClassName% 24 | HKR,,Icon,,-5 25 | 26 | [SourceDisksNames] 27 | 1 = %DiskName%,,,"" 28 | 29 | [SourceDisksFiles] 30 | KernelLuaVm.sys = 1,, 31 | WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll=1 ; make sure the number matches with SourceDisksNames 32 | 33 | ;***************************************** 34 | ; Install Section 35 | ;***************************************** 36 | 37 | [Manufacturer] 38 | %ManufacturerName%=Standard,NT$ARCH$ 39 | 40 | [Standard.NT$ARCH$] 41 | %KernelLuaVm.DeviceDesc%=KernelLuaVm_Device, Root\KernelLuaVm ; TODO: edit hw-id 42 | 43 | [KernelLuaVm_Device.NT] 44 | CopyFiles=Drivers_Dir 45 | 46 | [Drivers_Dir] 47 | KernelLuaVm.sys 48 | 49 | ;-------------- Service installation 50 | [KernelLuaVm_Device.NT.Services] 51 | AddService = KernelLuaVm,%SPSVCINST_ASSOCSERVICE%, KernelLuaVm_Service_Inst 52 | 53 | ; -------------- KernelLuaVm driver install sections 54 | [KernelLuaVm_Service_Inst] 55 | DisplayName = %KernelLuaVm.SVCDESC% 56 | ServiceType = 1 ; SERVICE_KERNEL_DRIVER 57 | StartType = 3 ; SERVICE_DEMAND_START 58 | ErrorControl = 1 ; SERVICE_ERROR_NORMAL 59 | ServiceBinary = %12%\KernelLuaVm.sys 60 | 61 | ; 62 | ;--- KernelLuaVm_Device Coinstaller installation ------ 63 | ; 64 | 65 | [KernelLuaVm_Device.NT.CoInstallers] 66 | AddReg=KernelLuaVm_Device_CoInstaller_AddReg 67 | CopyFiles=KernelLuaVm_Device_CoInstaller_CopyFiles 68 | 69 | [KernelLuaVm_Device_CoInstaller_AddReg] 70 | HKR,,CoInstallers32,0x00010000, "WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll,WdfCoInstaller" 71 | 72 | [KernelLuaVm_Device_CoInstaller_CopyFiles] 73 | WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll 74 | 75 | [KernelLuaVm_Device.NT.Wdf] 76 | KmdfService = KernelLuaVm, KernelLuaVm_wdfsect 77 | [KernelLuaVm_wdfsect] 78 | KmdfLibraryVersion = $KMDFVERSION$ 79 | 80 | [Strings] 81 | SPSVCINST_ASSOCSERVICE= 0x00000002 82 | ManufacturerName="" ;TODO: Replace with your manufacturer name 83 | ClassName="Samples" ; TODO: edit ClassName 84 | DiskName = "KernelLuaVm Installation Disk" 85 | KernelLuaVm.DeviceDesc = "KernelLuaVm Device" 86 | KernelLuaVm.SVCDESC = "KernelLuaVm Service" 87 | -------------------------------------------------------------------------------- /KernelLuaVm/runtime.lua: -------------------------------------------------------------------------------- 1 | R"( 2 | function malloc(len) 3 | return nt.ExAllocatePoolWithTag(0, len, 0) 4 | end 5 | function free(ptr) 6 | return nt.ExFreePoolWithTag(ptr, 0) 7 | end 8 | 9 | function tmp(bytes) 10 | local meta = { 11 | __gc = function(self) 12 | if self.pointer ~= 0 then 13 | free(self.pointer) 14 | end 15 | end 16 | } 17 | local self = { 18 | pointer = bytes > 0 and malloc(bytes) or 0, 19 | size = bytes, 20 | 21 | ref = function(self) 22 | return self.pointer 23 | end, 24 | 25 | get = function(self, i) 26 | i = i or 0 27 | if self.size >= (8+i) then 28 | return read8(self.pointer + i) 29 | elseif self.size >= (4+i) then 30 | return read4(self.pointer + i) 31 | elseif self.size >= (2+i) then 32 | return read2(self.pointer + i) 33 | elseif self.size >= (1+i) then 34 | return read1(self.pointer + i) 35 | else 36 | return nil 37 | end 38 | end, 39 | 40 | set = function(self, v, i) 41 | i = i or 0 42 | if self.size >= (8+i) then 43 | return write8(self.pointer + i, v) 44 | elseif self.size >= (4+i) then 45 | return write4(self.pointer + i, v) 46 | elseif self.size >= (2+i) then 47 | return write2(self.pointer + i, v) 48 | elseif self.size >= (1+i) then 49 | return write1(self.pointer + i, v) 50 | else 51 | return nil 52 | end 53 | end 54 | } 55 | setmetatable(self, meta) 56 | return self 57 | end 58 | 59 | function ansi_string(str) 60 | local AnsiString = tmp(0x10 + #str) 61 | write4(AnsiString:ref(), 0x10001 * #str) 62 | write8(AnsiString:ref() + 8, AnsiString:ref() + 0x10) 63 | memcpy(AnsiString:ref() + 0x10, addressof(str), #str) 64 | return AnsiString 65 | end 66 | 67 | function unicode_string(str) 68 | local UnicodeString = tmp(0x10 + (#str*2)) 69 | write4(UnicodeString:ref(), 0x10001 * (#str*2)) 70 | write8(UnicodeString:ref() + 8, UnicodeString:ref() + 0x10) 71 | 72 | local Dst = UnicodeString:ref() + 0x10 73 | local Src = addressof(str) 74 | for i=0,#str do 75 | write2(Dst + i * 2, read1(Src + i)) 76 | end 77 | 78 | return UnicodeString 79 | end 80 | )" 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/__cos.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/k_cos.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* 13 | * __cos( x, y ) 14 | * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 15 | * Input x is assumed to be bounded by ~pi/4 in magnitude. 16 | * Input y is the tail of x. 17 | * 18 | * Algorithm 19 | * 1. Since cos(-x) = cos(x), we need only to consider positive x. 20 | * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. 21 | * 3. cos(x) is approximated by a polynomial of degree 14 on 22 | * [0,pi/4] 23 | * 4 14 24 | * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x 25 | * where the remez error is 26 | * 27 | * | 2 4 6 8 10 12 14 | -58 28 | * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 29 | * | | 30 | * 31 | * 4 6 8 10 12 14 32 | * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then 33 | * cos(x) ~ 1 - x*x/2 + r 34 | * since cos(x+y) ~ cos(x) - sin(x)*y 35 | * ~ cos(x) - x*y, 36 | * a correction term is necessary in cos(x) and hence 37 | * cos(x+y) = 1 - (x*x/2 - (r - x*y)) 38 | * For better accuracy, rearrange to 39 | * cos(x+y) ~ w + (tmp + (r-x*y)) 40 | * where w = 1 - x*x/2 and tmp is a tiny correction term 41 | * (1 - x*x/2 == w + tmp exactly in infinite precision). 42 | * The exactness of w + tmp in infinite precision depends on w 43 | * and tmp having the same precision as x. If they have extra 44 | * precision due to compiler bugs, then the extra precision is 45 | * only good provided it is retained in all terms of the final 46 | * expression for cos(). Retention happens in all cases tested 47 | * under FreeBSD, so don't pessimize things by forcibly clipping 48 | * any extra precision in w. 49 | */ 50 | 51 | #include "..\libmd.h" 52 | 53 | static const double 54 | C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */ 55 | C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */ 56 | C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */ 57 | C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */ 58 | C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */ 59 | C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */ 60 | 61 | double __cos(double x, double y) 62 | { 63 | double_t hz,z,r,w; 64 | 65 | z = x*x; 66 | w = z*z; 67 | r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6)); 68 | hz = 0.5*z; 69 | w = 1.0-hz; 70 | return w + (((1.0-w)-hz) + (z*r-x*y)); 71 | } 72 | -------------------------------------------------------------------------------- /LuaLib/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ 89 | if (g->version) { /* is state fully built? */ 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | } 93 | if (newblock == NULL) 94 | luaD_throw(L, LUA_ERRMEM); 95 | } 96 | lua_assert((nsize == 0) == (newblock == NULL)); 97 | g->GCdebt = (g->GCdebt + nsize) - realosize; 98 | return newblock; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/acos.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_acos.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* acos(x) 13 | * Method : 14 | * acos(x) = pi/2 - asin(x) 15 | * acos(-x) = pi/2 + asin(x) 16 | * For |x|<=0.5 17 | * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) 18 | * For x>0.5 19 | * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) 20 | * = 2asin(sqrt((1-x)/2)) 21 | * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) 22 | * = 2f + (2c + 2s*z*R(z)) 23 | * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term 24 | * for f so that f+c ~ sqrt(z). 25 | * For x<-0.5 26 | * acos(x) = pi - 2asin(sqrt((1-|x|)/2)) 27 | * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) 28 | * 29 | * Special cases: 30 | * if x is NaN, return x itself; 31 | * if |x|>1, return NaN with invalid signal. 32 | * 33 | * Function needed: sqrt 34 | */ 35 | 36 | #include "..\libmd.h" 37 | 38 | static const double 39 | pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ 40 | pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ 41 | pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ 42 | pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ 43 | pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ 44 | pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ 45 | pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ 46 | pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ 47 | qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ 48 | qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ 49 | qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ 50 | qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ 51 | 52 | static double R(double z) 53 | { 54 | double_t p, q; 55 | p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); 56 | q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*qS4))); 57 | return p/q; 58 | } 59 | 60 | double acos(double x) 61 | { 62 | double z,w,s,c,df; 63 | uint32_t hx,ix; 64 | 65 | GET_HIGH_WORD(hx, x); 66 | ix = hx & 0x7fffffff; 67 | /* |x| >= 1 or nan */ 68 | if (ix >= 0x3ff00000) { 69 | uint32_t lx; 70 | 71 | GET_LOW_WORD(lx,x); 72 | if (((ix-0x3ff00000) | lx) == 0) { 73 | /* acos(1)=0, acos(-1)=pi */ 74 | if (hx >> 31) 75 | return 2*pio2_hi + 0x1p-120f; 76 | return 0; 77 | } 78 | return 0/(x-x); 79 | } 80 | /* |x| < 0.5 */ 81 | if (ix < 0x3fe00000) { 82 | if (ix <= 0x3c600000) /* |x| < 2**-57 */ 83 | return pio2_hi + 0x1p-120f; 84 | return pio2_hi - (x - (pio2_lo-x*R(x*x))); 85 | } 86 | /* x < -0.5 */ 87 | if (hx >> 31) { 88 | z = (1.0+x)*0.5; 89 | s = sqrt(z); 90 | w = R(z)*s-pio2_lo; 91 | return 2*(pio2_hi - (s+w)); 92 | } 93 | /* x > 0.5 */ 94 | z = (1.0-x)*0.5; 95 | s = sqrt(z); 96 | df = s; 97 | SET_LOW_WORD(df,0); 98 | c = (z-df*df)/(s+df); 99 | w = R(z)*s+c; 100 | return 2*(df+w); 101 | } 102 | -------------------------------------------------------------------------------- /LuaLib/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | /* get (pointer to) instruction of given 'expdesc' */ 44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 45 | 46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 47 | 48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 49 | 50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 51 | 52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 74 | LUAI_FUNC int luaK_jump (FuncState *fs); 75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 80 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 84 | expdesc *v2, int line); 85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 86 | 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/atan2.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | * 12 | */ 13 | /* atan2(y,x) 14 | * Method : 15 | * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 16 | * 2. Reduce x to positive by (if x and y are unexceptional): 17 | * ARG (x+iy) = arctan(y/x) ... if x > 0, 18 | * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 19 | * 20 | * Special cases: 21 | * 22 | * ATAN2((anything), NaN ) is NaN; 23 | * ATAN2(NAN , (anything) ) is NaN; 24 | * ATAN2(+-0, +(anything but NaN)) is +-0 ; 25 | * ATAN2(+-0, -(anything but NaN)) is +-pi ; 26 | * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; 27 | * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; 28 | * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; 29 | * ATAN2(+-INF,+INF ) is +-pi/4 ; 30 | * ATAN2(+-INF,-INF ) is +-3pi/4; 31 | * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; 32 | * 33 | * Constants: 34 | * The hexadecimal values are the intended ones for the following 35 | * constants. The decimal values may be used, provided that the 36 | * compiler will convert from decimal to binary accurately enough 37 | * to produce the hexadecimal values shown. 38 | */ 39 | 40 | #include "..\libmd.h" 41 | 42 | static const double 43 | pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ 44 | pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ 45 | 46 | double atan2(double y, double x) 47 | { 48 | double z; 49 | uint32_t m,lx,ly,ix,iy; 50 | 51 | if (isnan(x) || isnan(y)) 52 | return x+y; 53 | EXTRACT_WORDS(ix, lx, x); 54 | EXTRACT_WORDS(iy, ly, y); 55 | if (((ix-0x3ff00000) | lx) == 0) /* x = 1.0 */ 56 | return atan(y); 57 | m = ((iy>>31)&1) | ((ix>>30)&2); /* 2*sign(x)+sign(y) */ 58 | ix = ix & 0x7fffffff; 59 | iy = iy & 0x7fffffff; 60 | 61 | /* when y = 0 */ 62 | if ((iy|ly) == 0) { 63 | switch(m) { 64 | case 0: 65 | case 1: return y; /* atan(+-0,+anything)=+-0 */ 66 | case 2: return pi; /* atan(+0,-anything) = pi */ 67 | case 3: return -pi; /* atan(-0,-anything) =-pi */ 68 | } 69 | } 70 | /* when x = 0 */ 71 | if ((ix|lx) == 0) 72 | return m&1 ? -pi/2 : pi/2; 73 | /* when x is INF */ 74 | if (ix == 0x7ff00000) { 75 | if (iy == 0x7ff00000) { 76 | switch(m) { 77 | case 0: return pi/4; /* atan(+INF,+INF) */ 78 | case 1: return -pi/4; /* atan(-INF,+INF) */ 79 | case 2: return 3*pi/4; /* atan(+INF,-INF) */ 80 | case 3: return -3*pi/4; /* atan(-INF,-INF) */ 81 | } 82 | } else { 83 | switch(m) { 84 | case 0: return 0.0; /* atan(+...,+INF) */ 85 | case 1: return -0.0; /* atan(-...,+INF) */ 86 | case 2: return pi; /* atan(+...,-INF) */ 87 | case 3: return -pi; /* atan(-...,-INF) */ 88 | } 89 | } 90 | } 91 | /* |y/x| > 0x1p64 */ 92 | if (ix+(64<<20) < iy || iy == 0x7ff00000) 93 | return m&1 ? -pi/2 : pi/2; 94 | 95 | /* z = atan(|y/x|) without spurious underflow */ 96 | if ((m&2) && iy+(64<<20) < ix) /* |y/x| < 0x1p-64, x<0 */ 97 | z = 0; 98 | else 99 | z = atan(fabs(y/x)); 100 | switch (m) { 101 | case 0: return z; /* atan(+,+) */ 102 | case 1: return -z; /* atan(-,+) */ 103 | case 2: return pi - (z-pi_lo); /* atan(+,-) */ 104 | default: /* case 3 */ 105 | return (z-pi_lo) - pi; /* atan(-,-) */ 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/asin.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_asin.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* asin(x) 13 | * Method : 14 | * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ... 15 | * we approximate asin(x) on [0,0.5] by 16 | * asin(x) = x + x*x^2*R(x^2) 17 | * where 18 | * R(x^2) is a rational approximation of (asin(x)-x)/x^3 19 | * and its remez error is bounded by 20 | * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75) 21 | * 22 | * For x in [0.5,1] 23 | * asin(x) = pi/2-2*asin(sqrt((1-x)/2)) 24 | * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2; 25 | * then for x>0.98 26 | * asin(x) = pi/2 - 2*(s+s*z*R(z)) 27 | * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) 28 | * For x<=0.98, let pio4_hi = pio2_hi/2, then 29 | * f = hi part of s; 30 | * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) 31 | * and 32 | * asin(x) = pi/2 - 2*(s+s*z*R(z)) 33 | * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) 34 | * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) 35 | * 36 | * Special cases: 37 | * if x is NaN, return x itself; 38 | * if |x|>1, return NaN with invalid signal. 39 | * 40 | */ 41 | 42 | #include "..\libmd.h" 43 | 44 | static const double 45 | pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ 46 | pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ 47 | /* coefficients for R(x^2) */ 48 | pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ 49 | pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ 50 | pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ 51 | pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ 52 | pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ 53 | pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ 54 | qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ 55 | qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ 56 | qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ 57 | qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ 58 | 59 | static double R(double z) 60 | { 61 | double_t p, q; 62 | p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); 63 | q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*qS4))); 64 | return p/q; 65 | } 66 | 67 | double asin(double x) 68 | { 69 | double z,r,s; 70 | uint32_t hx,ix; 71 | 72 | GET_HIGH_WORD(hx, x); 73 | ix = hx & 0x7fffffff; 74 | /* |x| >= 1 or nan */ 75 | if (ix >= 0x3ff00000) { 76 | uint32_t lx; 77 | GET_LOW_WORD(lx, x); 78 | if (((ix-0x3ff00000) | lx) == 0) 79 | /* asin(1) = +-pi/2 with inexact */ 80 | return x*pio2_hi + 0x1p-120f; 81 | return 0/(x-x); 82 | } 83 | /* |x| < 0.5 */ 84 | if (ix < 0x3fe00000) { 85 | /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */ 86 | if (ix < 0x3e500000 && ix >= 0x00100000) 87 | return x; 88 | return x + x*R(x*x); 89 | } 90 | /* 1 > |x| >= 0.5 */ 91 | z = (1 - fabs(x))*0.5; 92 | s = sqrt(z); 93 | r = R(z); 94 | if (ix >= 0x3fef3333) { /* if |x| > 0.975 */ 95 | x = pio2_hi-(2*(s+s*r)-pio2_lo); 96 | } else { 97 | double f,c; 98 | /* f+c = sqrt(z) */ 99 | f = s; 100 | SET_LOW_WORD(f,0); 101 | c = (z-f*f)/(s+f); 102 | x = 0.5*pio2_hi - (2*s*r - (pio2_lo-2*c) - (0.5*pio2_hi-2*f)); 103 | } 104 | if (hx >> 31) 105 | return -x; 106 | return x; 107 | } 108 | -------------------------------------------------------------------------------- /LuaLib/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /LuaLib/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | #define tonumber(o,n) \ 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 42 | 43 | #define tointeger(o,i) \ 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 45 | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 47 | 48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 49 | 50 | 51 | /* 52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, 53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, 54 | ** return 0 (meaning it will have to check metamethod) with 'slot' 55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). 56 | ** 'f' is the raw get function to use. 57 | */ 58 | #define luaV_fastget(L,t,k,slot,f) \ 59 | (!ttistable(t) \ 60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 62 | !ttisnil(slot))) /* result not nil? */ 63 | 64 | /* 65 | ** standard implementation for 'gettable' 66 | */ 67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \ 68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 69 | else luaV_finishget(L,t,k,v,slot); } 70 | 71 | 72 | /* 73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil, 74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise, 75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or 76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro 77 | ** returns true, there is no need to 'invalidateTMcache', because the 78 | ** call is not creating a new entry. 79 | */ 80 | #define luaV_fastset(L,t,k,slot,f,v) \ 81 | (!ttistable(t) \ 82 | ? (slot = NULL, 0) \ 83 | : (slot = f(hvalue(t), k), \ 84 | ttisnil(slot) ? 0 \ 85 | : (luaC_barrierback(L, hvalue(t), v), \ 86 | setobj2t(L, cast(TValue *,slot), v), \ 87 | 1))) 88 | 89 | 90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \ 91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 92 | luaV_finishset(L,t,k,v,slot); } 93 | 94 | 95 | 96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); 101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 102 | StkId val, const TValue *slot); 103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 104 | StkId val, const TValue *slot); 105 | LUAI_FUNC void luaV_finishOp (lua_State *L); 106 | LUAI_FUNC void luaV_execute (lua_State *L); 107 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /ActualConsole/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "../KernelLuaVm/driver_io.hpp" 9 | 10 | HANDLE device = CreateFileA 11 | ( 12 | "\\\\.\\NtLua", 13 | GENERIC_READ | GENERIC_WRITE, 14 | FILE_SHARE_READ | FILE_SHARE_WRITE, 15 | NULL, 16 | OPEN_EXISTING, 17 | FILE_ATTRIBUTE_NORMAL, 18 | NULL 19 | ); 20 | bool execute( const char* str, bool silent ) 21 | { 22 | // Issue the IOCTL. 23 | // 24 | DWORD discarded = 0; 25 | ntlua_result result = { nullptr, nullptr }; 26 | DeviceIoControl( device, NTLUA_RUN, ( void* ) str, strlen( str ) + 1, &result, sizeof( result ), &discarded, nullptr ); 27 | bool had_result = result.outputs != nullptr; 28 | 29 | // If silent, free result and return. 30 | // 31 | if ( silent ) 32 | { 33 | if ( result.outputs ) VirtualFree( result.outputs, 0, MEM_RELEASE ); 34 | if ( result.errors ) VirtualFree( result.errors, 0, MEM_RELEASE ); 35 | } 36 | // Print each buffer to the console. 37 | // 38 | else 39 | { 40 | for ( auto& [buffer, color] : { std::pair{ result.errors, 12 }, 41 | std::pair{ result.outputs, 15 } } ) 42 | { 43 | if ( !buffer ) continue; 44 | SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color ); 45 | puts( buffer ); 46 | VirtualFree( buffer, 0, MEM_RELEASE ); 47 | } 48 | } 49 | return had_result; 50 | } 51 | 52 | void worker_thread() 53 | { 54 | bool prev_success = false; 55 | while ( 1 ) 56 | { 57 | Sleep( prev_success ? 100 : 5000 ); 58 | static constexpr char worker_script[] = R"( 59 | if worker then 60 | worker() 61 | print("-") 62 | end 63 | )"; 64 | prev_success = execute( worker_script, true ); 65 | } 66 | } 67 | 68 | 69 | int main( int argc, const char** argv ) 70 | { 71 | if ( device == INVALID_HANDLE_VALUE ) return 1; 72 | 73 | // If any arguments are given, assume they're lua files and execute them. 74 | // 75 | if ( argc >= 2 ) 76 | { 77 | for ( size_t n = 1; n != argc; n++ ) 78 | { 79 | printf( "Running '%s'...\n", argv[ n ] ); 80 | 81 | std::ifstream fs( argv[ n ] ); 82 | std::string buffer{ std::istreambuf_iterator( fs ), {} }; 83 | execute( buffer.data(), false ); 84 | } 85 | return 0; 86 | } 87 | 88 | // Start the worker thread. 89 | // 90 | std::thread thr( &worker_thread ); 91 | 92 | // Enter REPL: 93 | // 94 | while ( 1 ) 95 | { 96 | // Reset colors and ask user for input. 97 | // 98 | SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 7 ); 99 | std::string buffer; 100 | std::cout << "=> "; 101 | std::getline( std::cin, buffer ); 102 | 103 | // While shift is being held, allow multiple lines to be inputted. 104 | // 105 | while ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) 106 | { 107 | std::string buffer2; 108 | std::cout << " "; 109 | std::getline( std::cin, buffer2 ); 110 | buffer += "\n" + buffer2; 111 | } 112 | 113 | // Handle special commands: 114 | // 115 | if ( buffer == "clear" ) 116 | { 117 | system( "cls" ); 118 | } 119 | else if ( buffer == "cmd" ) 120 | { 121 | return system( "cmd" ); 122 | } 123 | else if ( buffer == "exit" ) 124 | { 125 | return exit( 0 ); 126 | } 127 | else if ( buffer == "reset" ) 128 | { 129 | DWORD discarded = 0; 130 | DeviceIoControl( 131 | device, 132 | NTLUA_RESET, 133 | &buffer[ 0 ], buffer.size() + 1, 134 | &discarded, sizeof( discarded ), 135 | &discarded, nullptr 136 | ); 137 | } 138 | else 139 | { 140 | execute( buffer.data(), false ); 141 | } 142 | } 143 | } -------------------------------------------------------------------------------- /LuaLib/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure (lua_State *L, int n) { 26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 27 | CClosure *c = gco2ccl(o); 28 | c->nupvalues = cast_byte(n); 29 | return c; 30 | } 31 | 32 | 33 | LClosure *luaF_newLclosure (lua_State *L, int n) { 34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 35 | LClosure *c = gco2lcl(o); 36 | c->p = NULL; 37 | c->nupvalues = cast_byte(n); 38 | while (n--) c->upvals[n] = NULL; 39 | return c; 40 | } 41 | 42 | /* 43 | ** fill a closure with new closed upvalues 44 | */ 45 | void luaF_initupvals (lua_State *L, LClosure *cl) { 46 | int i; 47 | for (i = 0; i < cl->nupvalues; i++) { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | uv->refcount = 1; 50 | uv->v = &uv->u.value; /* make it closed */ 51 | setnilvalue(uv->v); 52 | cl->upvals[i] = uv; 53 | } 54 | } 55 | 56 | 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { 58 | UpVal **pp = &L->openupval; 59 | UpVal *p; 60 | UpVal *uv; 61 | lua_assert(isintwups(L) || L->openupval == NULL); 62 | while (*pp != NULL && (p = *pp)->v >= level) { 63 | lua_assert(upisopen(p)); 64 | if (p->v == level) /* found a corresponding upvalue? */ 65 | return p; /* return it */ 66 | pp = &p->u.open.next; 67 | } 68 | /* not found: create a new upvalue */ 69 | uv = luaM_new(L, UpVal); 70 | uv->refcount = 0; 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 72 | uv->u.open.touched = 1; 73 | *pp = uv; 74 | uv->v = level; /* current value lives in the stack */ 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 76 | L->twups = G(L)->twups; /* link it to the list */ 77 | G(L)->twups = L; 78 | } 79 | return uv; 80 | } 81 | 82 | 83 | void luaF_close (lua_State *L, StkId level) { 84 | UpVal *uv; 85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) { 86 | lua_assert(upisopen(uv)); 87 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 88 | if (uv->refcount == 0) /* no references? */ 89 | luaM_free(L, uv); /* free upvalue */ 90 | else { 91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 92 | uv->v = &uv->u.value; /* now current value lives here */ 93 | luaC_upvalbarrier(L, uv); 94 | } 95 | } 96 | } 97 | 98 | 99 | Proto *luaF_newproto (lua_State *L) { 100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 101 | Proto *f = gco2p(o); 102 | f->k = NULL; 103 | f->sizek = 0; 104 | f->p = NULL; 105 | f->sizep = 0; 106 | f->code = NULL; 107 | f->cache = NULL; 108 | f->sizecode = 0; 109 | f->lineinfo = NULL; 110 | f->sizelineinfo = 0; 111 | f->upvalues = NULL; 112 | f->sizeupvalues = 0; 113 | f->numparams = 0; 114 | f->is_vararg = 0; 115 | f->maxstacksize = 0; 116 | f->locvars = NULL; 117 | f->sizelocvars = 0; 118 | f->linedefined = 0; 119 | f->lastlinedefined = 0; 120 | f->source = NULL; 121 | return f; 122 | } 123 | 124 | 125 | void luaF_freeproto (lua_State *L, Proto *f) { 126 | luaM_freearray(L, f->code, f->sizecode); 127 | luaM_freearray(L, f->p, f->sizep); 128 | luaM_freearray(L, f->k, f->sizek); 129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 130 | luaM_freearray(L, f->locvars, f->sizelocvars); 131 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 132 | luaM_free(L, f); 133 | } 134 | 135 | 136 | /* 137 | ** Look for n-th local variable at line 'line' in function 'func'. 138 | ** Returns NULL if not found. 139 | */ 140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 141 | int i; 142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 143 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 144 | local_number--; 145 | if (local_number == 0) 146 | return getstr(f->locvars[i].varname); 147 | } 148 | } 149 | return NULL; /* not found */ 150 | } 151 | 152 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/log1p.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_log1p.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* double log1p(double x) 13 | * Return the natural logarithm of 1+x. 14 | * 15 | * Method : 16 | * 1. Argument Reduction: find k and f such that 17 | * 1+x = 2^k * (1+f), 18 | * where sqrt(2)/2 < 1+f < sqrt(2) . 19 | * 20 | * Note. If k=0, then f=x is exact. However, if k!=0, then f 21 | * may not be representable exactly. In that case, a correction 22 | * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 23 | * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 24 | * and add back the correction term c/u. 25 | * (Note: when x > 2**53, one can simply return log(x)) 26 | * 27 | * 2. Approximation of log(1+f): See log.c 28 | * 29 | * 3. Finally, log1p(x) = k*ln2 + log(1+f) + c/u. See log.c 30 | * 31 | * Special cases: 32 | * log1p(x) is NaN with signal if x < -1 (including -INF) ; 33 | * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 34 | * log1p(NaN) is that NaN with no signal. 35 | * 36 | * Accuracy: 37 | * according to an error analysis, the error is always less than 38 | * 1 ulp (unit in the last place). 39 | * 40 | * Constants: 41 | * The hexadecimal values are the intended ones for the following 42 | * constants. The decimal values may be used, provided that the 43 | * compiler will convert from decimal to binary accurately enough 44 | * to produce the hexadecimal values shown. 45 | * 46 | * Note: Assuming log() return accurate answer, the following 47 | * algorithm can be used to compute log1p(x) to within a few ULP: 48 | * 49 | * u = 1+x; 50 | * if(u==1.0) return x ; else 51 | * return log(u)*(x/(u-1.0)); 52 | * 53 | * See HP-15C Advanced Functions Handbook, p.193. 54 | */ 55 | 56 | #include "..\libmd.h" 57 | 58 | static const double 59 | ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 60 | ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 61 | Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 62 | Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 63 | Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 64 | Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 65 | Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 66 | Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 67 | Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 68 | 69 | double log1p(double x) 70 | { 71 | union {double f; uint64_t i;} u = {x}; 72 | double_t hfsq,f,c,s,z,R,w,t1,t2,dk; 73 | uint32_t hx,hu; 74 | int k; 75 | 76 | hx = u.i>>32; 77 | k = 1; 78 | if (hx < 0x3fda827a || hx>>31) { /* 1+x < sqrt(2)+ */ 79 | if (hx >= 0xbff00000) { /* x <= -1.0 */ 80 | if (x == -1) 81 | return x/0.0; /* log1p(-1) = -inf */ 82 | return (x-x)/0.0; /* log1p(x<-1) = NaN */ 83 | } 84 | if (hx<<1 < 0x3ca00000<<1) { /* |x| < 2**-53 */ 85 | /* underflow if subnormal */ 86 | if ((hx&0x7ff00000) == 0) 87 | FORCE_EVAL((float)x); 88 | return x; 89 | } 90 | if (hx <= 0xbfd2bec4) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 91 | k = 0; 92 | c = 0; 93 | f = x; 94 | } 95 | } else if (hx >= 0x7ff00000) 96 | return x; 97 | if (k) { 98 | u.f = 1 + x; 99 | hu = u.i>>32; 100 | hu += 0x3ff00000 - 0x3fe6a09e; 101 | k = (int)(hu>>20) - 0x3ff; 102 | /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */ 103 | if (k < 54) { 104 | c = k >= 2 ? 1-(u.f-x) : x-(u.f-1); 105 | c /= u.f; 106 | } else 107 | c = 0; 108 | /* reduce u into [sqrt(2)/2, sqrt(2)] */ 109 | hu = (hu&0x000fffff) + 0x3fe6a09e; 110 | u.i = (uint64_t)hu<<32 | (u.i&0xffffffff); 111 | f = u.f - 1; 112 | } 113 | hfsq = 0.5*f*f; 114 | s = f/(2.0+f); 115 | z = s*s; 116 | w = z*z; 117 | t1 = w*(Lg2+w*(Lg4+w*Lg6)); 118 | t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); 119 | R = t2 + t1; 120 | dk = k; 121 | return s*(hfsq+R) + (dk*ln2_lo+c) - hfsq + f + dk*ln2_hi; 122 | } 123 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/__tan.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/k_tan.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. 5 | * 6 | * Permission to use, copy, modify, and distribute this 7 | * software is freely granted, provided that this notice 8 | * is preserved. 9 | * ==================================================== 10 | */ 11 | /* __tan( x, y, k ) 12 | * kernel tan function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854 13 | * Input x is assumed to be bounded by ~pi/4 in magnitude. 14 | * Input y is the tail of x. 15 | * Input odd indicates whether tan (if odd = 0) or -1/tan (if odd = 1) is returned. 16 | * 17 | * Algorithm 18 | * 1. Since tan(-x) = -tan(x), we need only to consider positive x. 19 | * 2. Callers must return tan(-0) = -0 without calling here since our 20 | * odd polynomial is not evaluated in a way that preserves -0. 21 | * Callers may do the optimization tan(x) ~ x for tiny x. 22 | * 3. tan(x) is approximated by a odd polynomial of degree 27 on 23 | * [0,0.67434] 24 | * 3 27 25 | * tan(x) ~ x + T1*x + ... + T13*x 26 | * where 27 | * 28 | * |tan(x) 2 4 26 | -59.2 29 | * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 30 | * | x | 31 | * 32 | * Note: tan(x+y) = tan(x) + tan'(x)*y 33 | * ~ tan(x) + (1+x*x)*y 34 | * Therefore, for better accuracy in computing tan(x+y), let 35 | * 3 2 2 2 2 36 | * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) 37 | * then 38 | * 3 2 39 | * tan(x+y) = x + (T1*x + (x *(r+y)+y)) 40 | * 41 | * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then 42 | * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y)) 43 | * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y))) 44 | */ 45 | 46 | #include "..\libmd.h" 47 | 48 | static const double T[] = { 49 | 3.33333333333334091986e-01, /* 3FD55555, 55555563 */ 50 | 1.33333333333201242699e-01, /* 3FC11111, 1110FE7A */ 51 | 5.39682539762260521377e-02, /* 3FABA1BA, 1BB341FE */ 52 | 2.18694882948595424599e-02, /* 3F9664F4, 8406D637 */ 53 | 8.86323982359930005737e-03, /* 3F8226E3, E96E8493 */ 54 | 3.59207910759131235356e-03, /* 3F6D6D22, C9560328 */ 55 | 1.45620945432529025516e-03, /* 3F57DBC8, FEE08315 */ 56 | 5.88041240820264096874e-04, /* 3F4344D8, F2F26501 */ 57 | 2.46463134818469906812e-04, /* 3F3026F7, 1A8D1068 */ 58 | 7.81794442939557092300e-05, /* 3F147E88, A03792A6 */ 59 | 7.14072491382608190305e-05, /* 3F12B80F, 32F0A7E9 */ 60 | -1.85586374855275456654e-05, /* BEF375CB, DB605373 */ 61 | 2.59073051863633712884e-05, /* 3EFB2A70, 74BF7AD4 */ 62 | }, 63 | pio4 = 7.85398163397448278999e-01, /* 3FE921FB, 54442D18 */ 64 | pio4lo = 3.06161699786838301793e-17; /* 3C81A626, 33145C07 */ 65 | 66 | double __tan(double x, double y, int odd) 67 | { 68 | double_t z, r, v, w, s, a; 69 | double w0, a0; 70 | uint32_t hx; 71 | int big, sign; 72 | 73 | GET_HIGH_WORD(hx,x); 74 | big = (hx&0x7fffffff) >= 0x3FE59428; /* |x| >= 0.6744 */ 75 | if (big) { 76 | sign = hx>>31; 77 | if (sign) { 78 | x = -x; 79 | y = -y; 80 | } 81 | x = (pio4 - x) + (pio4lo - y); 82 | y = 0.0; 83 | } 84 | z = x * x; 85 | w = z * z; 86 | /* 87 | * Break x^5*(T[1]+x^2*T[2]+...) into 88 | * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + 89 | * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) 90 | */ 91 | r = T[1] + w*(T[3] + w*(T[5] + w*(T[7] + w*(T[9] + w*T[11])))); 92 | v = z*(T[2] + w*(T[4] + w*(T[6] + w*(T[8] + w*(T[10] + w*T[12]))))); 93 | s = z * x; 94 | r = y + z*(s*(r + v) + y) + s*T[0]; 95 | w = x + r; 96 | if (big) { 97 | s = 1 - 2*odd; 98 | v = s - 2.0 * (x + (r - w*w/(w + s))); 99 | return sign ? -v : v; 100 | } 101 | if (!odd) 102 | return w; 103 | /* -1.0/(x+r) has up to 2ulp error, so compute it accurately */ 104 | w0 = w; 105 | SET_LOW_WORD(w0, 0); 106 | v = r - (w0 - x); /* w0+v = r+x */ 107 | a0 = a = -1.0 / w; 108 | SET_LOW_WORD(a0, 0); 109 | return a0 + a*(1.0 + a0*w0 + a0*v); 110 | } 111 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/atan.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_atan.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* atan(x) 13 | * Method 14 | * 1. Reduce x to positive by atan(x) = -atan(-x). 15 | * 2. According to the integer k=4t+0.25 chopped, t=x, the argument 16 | * is further reduced to one of the following intervals and the 17 | * arctangent of t is evaluated by the corresponding formula: 18 | * 19 | * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) 20 | * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) 21 | * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) 22 | * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) 23 | * [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) 24 | * 25 | * Constants: 26 | * The hexadecimal values are the intended ones for the following 27 | * constants. The decimal values may be used, provided that the 28 | * compiler will convert from decimal to binary accurately enough 29 | * to produce the hexadecimal values shown. 30 | */ 31 | 32 | 33 | #include "..\libmd.h" 34 | 35 | static const double atanhi[] = { 36 | 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ 37 | 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ 38 | 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ 39 | 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ 40 | }; 41 | 42 | static const double atanlo[] = { 43 | 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ 44 | 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ 45 | 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ 46 | 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ 47 | }; 48 | 49 | static const double aT[] = { 50 | 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */ 51 | -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */ 52 | 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */ 53 | -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */ 54 | 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */ 55 | -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */ 56 | 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */ 57 | -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */ 58 | 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */ 59 | -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */ 60 | 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */ 61 | }; 62 | 63 | double atan(double x) 64 | { 65 | double_t w,s1,s2,z; 66 | uint32_t ix,sign; 67 | int id; 68 | 69 | GET_HIGH_WORD(ix, x); 70 | sign = ix >> 31; 71 | ix &= 0x7fffffff; 72 | if (ix >= 0x44100000) { /* if |x| >= 2^66 */ 73 | if (isnan(x)) 74 | return x; 75 | z = atanhi[3] + 0x1p-120f; 76 | return sign ? -z : z; 77 | } 78 | if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ 79 | if (ix < 0x3e400000) { /* |x| < 2^-27 */ 80 | if (ix < 0x00100000) 81 | /* raise underflow for subnormal x */ 82 | FORCE_EVAL((float)x); 83 | return x; 84 | } 85 | id = -1; 86 | } else { 87 | x = fabs(x); 88 | if (ix < 0x3ff30000) { /* |x| < 1.1875 */ 89 | if (ix < 0x3fe60000) { /* 7/16 <= |x| < 11/16 */ 90 | id = 0; 91 | x = (2.0*x-1.0)/(2.0+x); 92 | } else { /* 11/16 <= |x| < 19/16 */ 93 | id = 1; 94 | x = (x-1.0)/(x+1.0); 95 | } 96 | } else { 97 | if (ix < 0x40038000) { /* |x| < 2.4375 */ 98 | id = 2; 99 | x = (x-1.5)/(1.0+1.5*x); 100 | } else { /* 2.4375 <= |x| < 2^66 */ 101 | id = 3; 102 | x = -1.0/x; 103 | } 104 | } 105 | } 106 | /* end of argument reduction */ 107 | z = x*x; 108 | w = z*z; 109 | /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ 110 | s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10]))))); 111 | s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9])))); 112 | if (id < 0) 113 | return x - x*(s1+s2); 114 | z = atanhi[id] - (x*(s1+s2) - atanlo[id] - x); 115 | return sign ? -z : z; 116 | } 117 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/log.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_log.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* log(x) 13 | * Return the logarithm of x 14 | * 15 | * Method : 16 | * 1. Argument Reduction: find k and f such that 17 | * x = 2^k * (1+f), 18 | * where sqrt(2)/2 < 1+f < sqrt(2) . 19 | * 20 | * 2. Approximation of log(1+f). 21 | * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 22 | * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 23 | * = 2s + s*R 24 | * We use a special Remez algorithm on [0,0.1716] to generate 25 | * a polynomial of degree 14 to approximate R The maximum error 26 | * of this polynomial approximation is bounded by 2**-58.45. In 27 | * other words, 28 | * 2 4 6 8 10 12 14 29 | * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s 30 | * (the values of Lg1 to Lg7 are listed in the program) 31 | * and 32 | * | 2 14 | -58.45 33 | * | Lg1*s +...+Lg7*s - R(z) | <= 2 34 | * | | 35 | * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 36 | * In order to guarantee error in log below 1ulp, we compute log 37 | * by 38 | * log(1+f) = f - s*(f - R) (if f is not too large) 39 | * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) 40 | * 41 | * 3. Finally, log(x) = k*ln2 + log(1+f). 42 | * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 43 | * Here ln2 is split into two floating point number: 44 | * ln2_hi + ln2_lo, 45 | * where n*ln2_hi is always exact for |n| < 2000. 46 | * 47 | * Special cases: 48 | * log(x) is NaN with signal if x < 0 (including -INF) ; 49 | * log(+INF) is +INF; log(0) is -INF with signal; 50 | * log(NaN) is that NaN with no signal. 51 | * 52 | * Accuracy: 53 | * according to an error analysis, the error is always less than 54 | * 1 ulp (unit in the last place). 55 | * 56 | * Constants: 57 | * The hexadecimal values are the intended ones for the following 58 | * constants. The decimal values may be used, provided that the 59 | * compiler will convert from decimal to binary accurately enough 60 | * to produce the hexadecimal values shown. 61 | */ 62 | 63 | #include "..\libmd.h" 64 | 65 | static const double 66 | ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 67 | ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 68 | Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 69 | Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 70 | Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 71 | Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 72 | Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 73 | Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 74 | Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 75 | 76 | double log(double x) 77 | { 78 | union {double f; uint64_t i;} u = {x}; 79 | double_t hfsq,f,s,z,R,w,t1,t2,dk; 80 | uint32_t hx; 81 | int k; 82 | 83 | hx = u.i>>32; 84 | k = 0; 85 | if (hx < 0x00100000 || hx>>31) { 86 | if (u.i<<1 == 0) 87 | return -1/(x*x); /* log(+-0)=-inf */ 88 | if (hx>>31) 89 | return (x-x)/0.0; /* log(-#) = NaN */ 90 | /* subnormal number, scale x up */ 91 | k -= 54; 92 | x *= 0x1p54; 93 | u.f = x; 94 | hx = u.i>>32; 95 | } else if (hx >= 0x7ff00000) { 96 | return x; 97 | } else if (hx == 0x3ff00000 && u.i<<32 == 0) 98 | return 0; 99 | 100 | /* reduce x into [sqrt(2)/2, sqrt(2)] */ 101 | hx += 0x3ff00000 - 0x3fe6a09e; 102 | k += (int)(hx>>20) - 0x3ff; 103 | hx = (hx&0x000fffff) + 0x3fe6a09e; 104 | u.i = (uint64_t)hx<<32 | (u.i&0xffffffff); 105 | x = u.f; 106 | 107 | f = x - 1.0; 108 | hfsq = 0.5*f*f; 109 | s = f/(2.0+f); 110 | z = s*s; 111 | w = z*z; 112 | t1 = w*(Lg2+w*(Lg4+w*Lg6)); 113 | t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); 114 | R = t2 + t1; 115 | dk = k; 116 | return s*(hfsq+R) + dk*ln2_lo - hfsq + f + dk*ln2_hi; 117 | } 118 | -------------------------------------------------------------------------------- /LuaLib/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argcheck(L, co, 1, "thread expected"); 24 | return co; 25 | } 26 | 27 | 28 | static int auxresume (lua_State *L, lua_State *co, int narg) { 29 | int status; 30 | if (!lua_checkstack(co, narg)) { 31 | lua_pushliteral(L, "too many arguments to resume"); 32 | return -1; /* error flag */ 33 | } 34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 35 | lua_pushliteral(L, "cannot resume dead coroutine"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg); 40 | if (status == LUA_OK || status == LUA_YIELD) { 41 | int nres = lua_gettop(co); 42 | if (!lua_checkstack(L, nres + 1)) { 43 | lua_pop(co, nres); /* remove results anyway */ 44 | lua_pushliteral(L, "too many results to resume"); 45 | return -1; /* error flag */ 46 | } 47 | lua_xmove(co, L, nres); /* move yielded values */ 48 | return nres; 49 | } 50 | else { 51 | lua_xmove(co, L, 1); /* move error message */ 52 | return -1; /* error flag */ 53 | } 54 | } 55 | 56 | 57 | static int luaB_coresume (lua_State *L) { 58 | lua_State *co = getco(L); 59 | int r; 60 | r = auxresume(L, co, lua_gettop(L) - 1); 61 | if (r < 0) { 62 | lua_pushboolean(L, 0); 63 | lua_insert(L, -2); 64 | return 2; /* return false + error message */ 65 | } 66 | else { 67 | lua_pushboolean(L, 1); 68 | lua_insert(L, -(r + 1)); 69 | return r + 1; /* return true + 'resume' returns */ 70 | } 71 | } 72 | 73 | 74 | static int luaB_auxwrap (lua_State *L) { 75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 76 | int r = auxresume(L, co, lua_gettop(L)); 77 | if (r < 0) { 78 | if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ 79 | luaL_where(L, 1); /* add extra info */ 80 | lua_insert(L, -2); 81 | lua_concat(L, 2); 82 | } 83 | return lua_error(L); /* propagate error */ 84 | } 85 | return r; 86 | } 87 | 88 | 89 | static int luaB_cocreate (lua_State *L) { 90 | lua_State *NL; 91 | luaL_checktype(L, 1, LUA_TFUNCTION); 92 | NL = lua_newthread(L); 93 | lua_pushvalue(L, 1); /* move function to top */ 94 | lua_xmove(L, NL, 1); /* move function from L to NL */ 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_cowrap (lua_State *L) { 100 | luaB_cocreate(L); 101 | lua_pushcclosure(L, luaB_auxwrap, 1); 102 | return 1; 103 | } 104 | 105 | 106 | static int luaB_yield (lua_State *L) { 107 | return lua_yield(L, lua_gettop(L)); 108 | } 109 | 110 | 111 | static int luaB_costatus (lua_State *L) { 112 | lua_State *co = getco(L); 113 | if (L == co) lua_pushliteral(L, "running"); 114 | else { 115 | switch (lua_status(co)) { 116 | case LUA_YIELD: 117 | lua_pushliteral(L, "suspended"); 118 | break; 119 | case LUA_OK: { 120 | lua_Debug ar; 121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 122 | lua_pushliteral(L, "normal"); /* it is running */ 123 | else if (lua_gettop(co) == 0) 124 | lua_pushliteral(L, "dead"); 125 | else 126 | lua_pushliteral(L, "suspended"); /* initial state */ 127 | break; 128 | } 129 | default: /* some error occurred */ 130 | lua_pushliteral(L, "dead"); 131 | break; 132 | } 133 | } 134 | return 1; 135 | } 136 | 137 | 138 | static int luaB_yieldable (lua_State *L) { 139 | lua_pushboolean(L, lua_isyieldable(L)); 140 | return 1; 141 | } 142 | 143 | 144 | static int luaB_corunning (lua_State *L) { 145 | int ismain = lua_pushthread(L); 146 | lua_pushboolean(L, ismain); 147 | return 2; 148 | } 149 | 150 | 151 | static const luaL_Reg co_funcs[] = { 152 | {"create", luaB_cocreate}, 153 | {"resume", luaB_coresume}, 154 | {"running", luaB_corunning}, 155 | {"status", luaB_costatus}, 156 | {"wrap", luaB_cowrap}, 157 | {"yield", luaB_yield}, 158 | {"isyieldable", luaB_yieldable}, 159 | {NULL, NULL} 160 | }; 161 | 162 | 163 | 164 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 165 | luaL_newlib(L, co_funcs); 166 | return 1; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /ActualConsole/ActualConsole.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Release 6 | Win32 7 | 8 | 9 | Release 10 | x64 11 | 12 | 13 | 14 | 16.0 15 | Win32Proj 16 | {82b12053-4da0-440b-8af4-55f49658299e} 17 | ActualConsole 18 | 10.0 19 | 20 | 21 | 22 | Application 23 | false 24 | v142 25 | true 26 | Unicode 27 | 28 | 29 | Application 30 | false 31 | v142 32 | true 33 | Unicode 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | false 49 | 50 | 51 | false 52 | ntlua 53 | 54 | 55 | 56 | Level3 57 | true 58 | true 59 | true 60 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 61 | true 62 | 63 | 64 | Console 65 | true 66 | true 67 | true 68 | 69 | 70 | 71 | 72 | Level3 73 | true 74 | true 75 | true 76 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 77 | true 78 | MultiThreaded 79 | stdcpplatest 80 | 81 | 82 | Console 83 | true 84 | true 85 | true 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/exp.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_exp.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Permission to use, copy, modify, and distribute this 7 | * software is freely granted, provided that this notice 8 | * is preserved. 9 | * ==================================================== 10 | */ 11 | /* exp(x) 12 | * Returns the exponential of x. 13 | * 14 | * Method 15 | * 1. Argument reduction: 16 | * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 17 | * Given x, find r and integer k such that 18 | * 19 | * x = k*ln2 + r, |r| <= 0.5*ln2. 20 | * 21 | * Here r will be represented as r = hi-lo for better 22 | * accuracy. 23 | * 24 | * 2. Approximation of exp(r) by a special rational function on 25 | * the interval [0,0.34658]: 26 | * Write 27 | * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 28 | * We use a special Remez algorithm on [0,0.34658] to generate 29 | * a polynomial of degree 5 to approximate R. The maximum error 30 | * of this polynomial approximation is bounded by 2**-59. In 31 | * other words, 32 | * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 33 | * (where z=r*r, and the values of P1 to P5 are listed below) 34 | * and 35 | * | 5 | -59 36 | * | 2.0+P1*z+...+P5*z - R(z) | <= 2 37 | * | | 38 | * The computation of exp(r) thus becomes 39 | * 2*r 40 | * exp(r) = 1 + ---------- 41 | * R(r) - r 42 | * r*c(r) 43 | * = 1 + r + ----------- (for better accuracy) 44 | * 2 - c(r) 45 | * where 46 | * 2 4 10 47 | * c(r) = r - (P1*r + P2*r + ... + P5*r ). 48 | * 49 | * 3. Scale back to obtain exp(x): 50 | * From step 1, we have 51 | * exp(x) = 2^k * exp(r) 52 | * 53 | * Special cases: 54 | * exp(INF) is INF, exp(NaN) is NaN; 55 | * exp(-INF) is 0, and 56 | * for finite argument, only exp(0)=1 is exact. 57 | * 58 | * Accuracy: 59 | * according to an error analysis, the error is always less than 60 | * 1 ulp (unit in the last place). 61 | * 62 | * Misc. info. 63 | * For IEEE double 64 | * if x > 709.782712893383973096 then exp(x) overflows 65 | * if x < -745.133219101941108420 then exp(x) underflows 66 | */ 67 | 68 | #include "..\libmd.h" 69 | 70 | static const double 71 | half[2] = {0.5,-0.5}, 72 | ln2hi = 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 73 | ln2lo = 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 74 | invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 75 | P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 76 | P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 77 | P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 78 | P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 79 | P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 80 | 81 | double exp(double x) 82 | { 83 | double_t hi, lo, c, xx, y; 84 | int k, sign; 85 | uint32_t hx; 86 | 87 | GET_HIGH_WORD(hx, x); 88 | sign = hx>>31; 89 | hx &= 0x7fffffff; /* high word of |x| */ 90 | 91 | /* special cases */ 92 | if (hx >= 0x4086232b) { /* if |x| >= 708.39... */ 93 | if (isnan(x)) 94 | return x; 95 | if (x > 709.782712893383973096) { 96 | /* overflow if x!=inf */ 97 | x *= 0x1p1023; 98 | return x; 99 | } 100 | if (x < -708.39641853226410622) { 101 | /* underflow if x!=-inf */ 102 | FORCE_EVAL((float)(-0x1p-149/x)); 103 | if (x < -745.13321910194110842) 104 | return 0; 105 | } 106 | } 107 | 108 | /* argument reduction */ 109 | if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 110 | if (hx >= 0x3ff0a2b2) /* if |x| >= 1.5 ln2 */ 111 | k = (int)(invln2*x + half[sign]); 112 | else 113 | k = 1 - sign - sign; 114 | hi = x - k*ln2hi; /* k*ln2hi is exact here */ 115 | lo = k*ln2lo; 116 | x = hi - lo; 117 | } else if (hx > 0x3e300000) { /* if |x| > 2**-28 */ 118 | k = 0; 119 | hi = x; 120 | lo = 0; 121 | } else { 122 | /* inexact if x!=0 */ 123 | FORCE_EVAL(0x1p1023 + x); 124 | return 1 + x; 125 | } 126 | 127 | /* x is now in primary range */ 128 | xx = x*x; 129 | c = x - xx*(P1+xx*(P2+xx*(P3+xx*(P4+xx*P5)))); 130 | y = 1 + (x*c/(2-c) - lo + hi); 131 | if (k == 0) 132 | return y; 133 | return scalbn(y, k); 134 | } 135 | -------------------------------------------------------------------------------- /LuaLib/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum { 26 | VVOID, /* when 'expdesc' describes the last expression a list, 27 | this kind means an empty list (so, no expression) */ 28 | VNIL, /* constant nil */ 29 | VTRUE, /* constant true */ 30 | VFALSE, /* constant false */ 31 | VK, /* constant in 'k'; info = index of constant in 'k' */ 32 | VKFLT, /* floating constant; nval = numerical float value */ 33 | VKINT, /* integer constant; nval = numerical integer value */ 34 | VNONRELOC, /* expression has its value in a fixed register; 35 | info = result register */ 36 | VLOCAL, /* local variable; info = local register */ 37 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 38 | VINDEXED, /* indexed variable; 39 | ind.vt = whether 't' is register or upvalue; 40 | ind.t = table register or upvalue; 41 | ind.idx = key's R/K index */ 42 | VJMP, /* expression is a test/comparison; 43 | info = pc of corresponding jump instruction */ 44 | VRELOCABLE, /* expression can put result in any register; 45 | info = instruction pc */ 46 | VCALL, /* expression is a function call; info = instruction pc */ 47 | VVARARG /* vararg expression; info = instruction pc */ 48 | } expkind; 49 | 50 | 51 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 52 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 53 | 54 | typedef struct expdesc { 55 | expkind k; 56 | union { 57 | lua_Integer ival; /* for VKINT */ 58 | lua_Number nval; /* for VKFLT */ 59 | int info; /* for generic use */ 60 | struct { /* for indexed variables (VINDEXED) */ 61 | short idx; /* index (R/K) */ 62 | lu_byte t; /* table (register or upvalue) */ 63 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 64 | } ind; 65 | } u; 66 | int t; /* patch list of 'exit when true' */ 67 | int f; /* patch list of 'exit when false' */ 68 | } expdesc; 69 | 70 | 71 | /* description of active local variable */ 72 | typedef struct Vardesc { 73 | short idx; /* variable index in stack */ 74 | } Vardesc; 75 | 76 | 77 | /* description of pending goto statements and label statements */ 78 | typedef struct Labeldesc { 79 | TString *name; /* label identifier */ 80 | int pc; /* position in code */ 81 | int line; /* line where it appeared */ 82 | lu_byte nactvar; /* local level where it appears in current block */ 83 | } Labeldesc; 84 | 85 | 86 | /* list of labels or gotos */ 87 | typedef struct Labellist { 88 | Labeldesc *arr; /* array */ 89 | int n; /* number of entries in use */ 90 | int size; /* array size */ 91 | } Labellist; 92 | 93 | 94 | /* dynamic structures used by the parser */ 95 | typedef struct Dyndata { 96 | struct { /* list of active local variables */ 97 | Vardesc *arr; 98 | int n; 99 | int size; 100 | } actvar; 101 | Labellist gt; /* list of pending gotos */ 102 | Labellist label; /* list of active labels */ 103 | } Dyndata; 104 | 105 | 106 | /* control of blocks */ 107 | struct BlockCnt; /* defined in lparser.c */ 108 | 109 | 110 | /* state needed to generate code for a given function */ 111 | typedef struct FuncState { 112 | Proto *f; /* current function header */ 113 | struct FuncState *prev; /* enclosing function */ 114 | struct LexState *ls; /* lexical state */ 115 | struct BlockCnt *bl; /* chain of current blocks */ 116 | int pc; /* next position to code (equivalent to 'ncode') */ 117 | int lasttarget; /* 'label' of last 'jump label' */ 118 | int jpc; /* list of pending jumps to 'pc' */ 119 | int nk; /* number of elements in 'k' */ 120 | int np; /* number of elements in 'p' */ 121 | int firstlocal; /* index of first local var (in Dyndata array) */ 122 | short nlocvars; /* number of elements in 'f->locvars' */ 123 | lu_byte nactvar; /* number of active local variables */ 124 | lu_byte nups; /* number of upvalues */ 125 | lu_byte freereg; /* first free register */ 126 | } FuncState; 127 | 128 | 129 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 130 | Dyndata *dyd, const char *name, int firstchar); 131 | 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/__rem_pio2.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_rem_pio2.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | * 12 | * Optimized by Bruce D. Evans. 13 | */ 14 | /* __rem_pio2(x,y) 15 | * 16 | * return the remainder of x rem pi/2 in y[0]+y[1] 17 | * use __rem_pio2_large() for large x 18 | */ 19 | 20 | #include "..\libmd.h" 21 | 22 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 23 | #define EPS DBL_EPSILON 24 | #elif FLT_EVAL_METHOD==2 25 | #define EPS LDBL_EPSILON 26 | #endif 27 | 28 | /* 29 | * invpio2: 53 bits of 2/pi 30 | * pio2_1: first 33 bit of pi/2 31 | * pio2_1t: pi/2 - pio2_1 32 | * pio2_2: second 33 bit of pi/2 33 | * pio2_2t: pi/2 - (pio2_1+pio2_2) 34 | * pio2_3: third 33 bit of pi/2 35 | * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) 36 | */ 37 | static const double 38 | toint = 1.5/EPS, 39 | invpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */ 40 | pio2_1 = 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */ 41 | pio2_1t = 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */ 42 | pio2_2 = 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */ 43 | pio2_2t = 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */ 44 | pio2_3 = 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */ 45 | pio2_3t = 8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */ 46 | 47 | /* caller must handle the case when reduction is not needed: |x| ~<= pi/4 */ 48 | int __rem_pio2(double x, double *y) 49 | { 50 | union {double f; uint64_t i;} u = {x}; 51 | double_t z,w,t,r,fn; 52 | double tx[3],ty[2]; 53 | uint32_t ix; 54 | int sign, n, ex, ey, i; 55 | 56 | sign = u.i>>63; 57 | ix = u.i>>32 & 0x7fffffff; 58 | if (ix <= 0x400f6a7a) { /* |x| ~<= 5pi/4 */ 59 | if ((ix & 0xfffff) == 0x921fb) /* |x| ~= pi/2 or 2pi/2 */ 60 | goto medium; /* cancellation -- use medium case */ 61 | if (ix <= 0x4002d97c) { /* |x| ~<= 3pi/4 */ 62 | if (!sign) { 63 | z = x - pio2_1; /* one round good to 85 bits */ 64 | y[0] = z - pio2_1t; 65 | y[1] = (z-y[0]) - pio2_1t; 66 | return 1; 67 | } else { 68 | z = x + pio2_1; 69 | y[0] = z + pio2_1t; 70 | y[1] = (z-y[0]) + pio2_1t; 71 | return -1; 72 | } 73 | } else { 74 | if (!sign) { 75 | z = x - 2*pio2_1; 76 | y[0] = z - 2*pio2_1t; 77 | y[1] = (z-y[0]) - 2*pio2_1t; 78 | return 2; 79 | } else { 80 | z = x + 2*pio2_1; 81 | y[0] = z + 2*pio2_1t; 82 | y[1] = (z-y[0]) + 2*pio2_1t; 83 | return -2; 84 | } 85 | } 86 | } 87 | if (ix <= 0x401c463b) { /* |x| ~<= 9pi/4 */ 88 | if (ix <= 0x4015fdbc) { /* |x| ~<= 7pi/4 */ 89 | if (ix == 0x4012d97c) /* |x| ~= 3pi/2 */ 90 | goto medium; 91 | if (!sign) { 92 | z = x - 3*pio2_1; 93 | y[0] = z - 3*pio2_1t; 94 | y[1] = (z-y[0]) - 3*pio2_1t; 95 | return 3; 96 | } else { 97 | z = x + 3*pio2_1; 98 | y[0] = z + 3*pio2_1t; 99 | y[1] = (z-y[0]) + 3*pio2_1t; 100 | return -3; 101 | } 102 | } else { 103 | if (ix == 0x401921fb) /* |x| ~= 4pi/2 */ 104 | goto medium; 105 | if (!sign) { 106 | z = x - 4*pio2_1; 107 | y[0] = z - 4*pio2_1t; 108 | y[1] = (z-y[0]) - 4*pio2_1t; 109 | return 4; 110 | } else { 111 | z = x + 4*pio2_1; 112 | y[0] = z + 4*pio2_1t; 113 | y[1] = (z-y[0]) + 4*pio2_1t; 114 | return -4; 115 | } 116 | } 117 | } 118 | if (ix < 0x413921fb) { /* |x| ~< 2^20*(pi/2), medium size */ 119 | medium: 120 | /* rint(x/(pi/2)), Assume round-to-nearest. */ 121 | fn = (double_t)x*invpio2 + toint - toint; 122 | n = (int32_t)fn; 123 | r = x - fn*pio2_1; 124 | w = fn*pio2_1t; /* 1st round, good to 85 bits */ 125 | y[0] = r - w; 126 | u.f = y[0]; 127 | ey = u.i>>52 & 0x7ff; 128 | ex = ix>>20; 129 | if (ex - ey > 16) { /* 2nd round, good to 118 bits */ 130 | t = r; 131 | w = fn*pio2_2; 132 | r = t - w; 133 | w = fn*pio2_2t - ((t-r)-w); 134 | y[0] = r - w; 135 | u.f = y[0]; 136 | ey = u.i>>52 & 0x7ff; 137 | if (ex - ey > 49) { /* 3rd round, good to 151 bits, covers all cases */ 138 | t = r; 139 | w = fn*pio2_3; 140 | r = t - w; 141 | w = fn*pio2_3t - ((t-r)-w); 142 | y[0] = r - w; 143 | } 144 | } 145 | y[1] = (r - y[0]) - w; 146 | return n; 147 | } 148 | /* 149 | * all other (large) arguments 150 | */ 151 | if (ix >= 0x7ff00000) { /* x is inf or NaN */ 152 | y[0] = y[1] = x - x; 153 | return 0; 154 | } 155 | /* set z = scalbn(|x|,-ilogb(x)+23) */ 156 | u.f = x; 157 | u.i &= (uint64_t)-1>>12; 158 | u.i |= (uint64_t)(0x3ff + 23)<<52; 159 | z = u.f; 160 | for (i=0; i < 2; i++) { 161 | tx[i] = (double)(int32_t)z; 162 | z = (z-tx[i])*0x1p24; 163 | } 164 | tx[i] = z; 165 | /* skip zero terms, first term is non-zero */ 166 | while (tx[i] == 0.0) 167 | i--; 168 | n = __rem_pio2_large(tx,ty,(int)(ix>>20)-(0x3ff+23),i+1,1); 169 | if (sign) { 170 | y[0] = -ty[0]; 171 | y[1] = -ty[1]; 172 | return -n; 173 | } 174 | y[0] = ty[0]; 175 | y[1] = ty[1]; 176 | return n; 177 | } 178 | -------------------------------------------------------------------------------- /LuaLib/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | /* 105 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 106 | ** allows some adjustments to be done only when needed. macro 107 | ** 'condchangemem' is used only for heavy tests (forcing a full 108 | ** GC cycle on every opportunity) 109 | */ 110 | #define luaC_condGC(L,pre,pos) \ 111 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 112 | condchangemem(L,pre,pos); } 113 | 114 | /* more often than not, 'pre'/'pos' are empty */ 115 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 116 | 117 | 118 | #define luaC_barrier(L,p,v) ( \ 119 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 120 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 121 | 122 | #define luaC_barrierback(L,p,v) ( \ 123 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 124 | luaC_barrierback_(L,p) : cast_void(0)) 125 | 126 | #define luaC_objbarrier(L,p,o) ( \ 127 | (isblack(p) && iswhite(o)) ? \ 128 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 129 | 130 | #define luaC_upvalbarrier(L,uv) ( \ 131 | (iscollectable((uv)->v) && !upisopen(uv)) ? \ 132 | luaC_upvalbarrier_(L,uv) : cast_void(0)) 133 | 134 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 135 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 136 | LUAI_FUNC void luaC_step (lua_State *L); 137 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 138 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 139 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 142 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 145 | 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /LuaLib/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 30 | "no value", 31 | "nil", "boolean", udatatypename, "number", 32 | "string", "table", "function", udatatypename, "thread", 33 | "proto" /* this last case is used for tests only */ 34 | }; 35 | 36 | 37 | void luaT_init (lua_State *L) { 38 | static const char *const luaT_eventname[] = { /* ORDER TM */ 39 | "__index", "__newindex", 40 | "__gc", "__mode", "__len", "__eq", 41 | "__add", "__sub", "__mul", "__mod", "__pow", 42 | "__div", "__idiv", 43 | "__band", "__bor", "__bxor", "__shl", "__shr", 44 | "__unm", "__bnot", "__lt", "__le", 45 | "__concat", "__call" 46 | }; 47 | int i; 48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 51 | } 52 | } 53 | 54 | 55 | /* 56 | ** function to be used with macro "fasttm": optimized for absence of 57 | ** tag methods 58 | */ 59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 60 | const TValue *tm = luaH_getshortstr(events, ename); 61 | lua_assert(event <= TM_EQ); 62 | if (ttisnil(tm)) { /* no tag method? */ 63 | events->flags |= cast_byte(1u<metatable; 75 | break; 76 | case LUA_TUSERDATA: 77 | mt = uvalue(o)->metatable; 78 | break; 79 | default: 80 | mt = G(L)->mt[ttnov(o)]; 81 | } 82 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); 83 | } 84 | 85 | 86 | /* 87 | ** Return the name of the type of an object. For tables and userdata 88 | ** with metatable, use their '__name' metafield, if present. 89 | */ 90 | const char *luaT_objtypename (lua_State *L, const TValue *o) { 91 | Table *mt; 92 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 93 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { 94 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 95 | if (ttisstring(name)) /* is '__name' a string? */ 96 | return getstr(tsvalue(name)); /* use it as type name */ 97 | } 98 | return ttypename(ttnov(o)); /* else use standard type name */ 99 | } 100 | 101 | 102 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 103 | const TValue *p2, TValue *p3, int hasres) { 104 | ptrdiff_t result = savestack(L, p3); 105 | StkId func = L->top; 106 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 107 | setobj2s(L, func + 1, p1); /* 1st argument */ 108 | setobj2s(L, func + 2, p2); /* 2nd argument */ 109 | L->top += 3; 110 | if (!hasres) /* no result? 'p3' is third argument */ 111 | setobj2s(L, L->top++, p3); /* 3rd argument */ 112 | /* metamethod may yield only when called from Lua code */ 113 | if (isLua(L->ci)) 114 | luaD_call(L, func, hasres); 115 | else 116 | luaD_callnoyield(L, func, hasres); 117 | if (hasres) { /* if has result, move it to its place */ 118 | p3 = restorestack(L, result); 119 | setobjs2s(L, p3, --L->top); 120 | } 121 | } 122 | 123 | 124 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 125 | StkId res, TMS event) { 126 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 127 | if (ttisnil(tm)) 128 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 129 | if (ttisnil(tm)) return 0; 130 | luaT_callTM(L, tm, p1, p2, res, 1); 131 | return 1; 132 | } 133 | 134 | 135 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 136 | StkId res, TMS event) { 137 | if (!luaT_callbinTM(L, p1, p2, res, event)) { 138 | switch (event) { 139 | case TM_CONCAT: 140 | luaG_concaterror(L, p1, p2); 141 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 142 | case TM_BAND: case TM_BOR: case TM_BXOR: 143 | case TM_SHL: case TM_SHR: case TM_BNOT: { 144 | lua_Number dummy; 145 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 146 | luaG_tointerror(L, p1, p2); 147 | else 148 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 149 | } 150 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 151 | default: 152 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 153 | } 154 | } 155 | } 156 | 157 | 158 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 159 | TMS event) { 160 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 161 | return -1; /* no metamethod */ 162 | else 163 | return !l_isfalse(L->top); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /LuaLib/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through DumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 36 | 37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 38 | 39 | 40 | static void DumpBlock (const void *b, size_t size, DumpState *D) { 41 | if (D->status == 0 && size > 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define DumpVar(x,D) DumpVector(&x,1,D) 50 | 51 | 52 | static void DumpByte (int y, DumpState *D) { 53 | lu_byte x = (lu_byte)y; 54 | DumpVar(x, D); 55 | } 56 | 57 | 58 | static void DumpInt (int x, DumpState *D) { 59 | DumpVar(x, D); 60 | } 61 | 62 | 63 | static void DumpNumber (lua_Number x, DumpState *D) { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpInteger (lua_Integer x, DumpState *D) { 69 | DumpVar(x, D); 70 | } 71 | 72 | 73 | static void DumpString (const TString *s, DumpState *D) { 74 | if (s == NULL) 75 | DumpByte(0, D); 76 | else { 77 | size_t size = tsslen(s) + 1; /* include trailing '\0' */ 78 | const char *str = getstr(s); 79 | if (size < 0xFF) 80 | DumpByte(cast_int(size), D); 81 | else { 82 | DumpByte(0xFF, D); 83 | DumpVar(size, D); 84 | } 85 | DumpVector(str, size - 1, D); /* no need to save '\0' */ 86 | } 87 | } 88 | 89 | 90 | static void DumpCode (const Proto *f, DumpState *D) { 91 | DumpInt(f->sizecode, D); 92 | DumpVector(f->code, f->sizecode, D); 93 | } 94 | 95 | 96 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 97 | 98 | static void DumpConstants (const Proto *f, DumpState *D) { 99 | int i; 100 | int n = f->sizek; 101 | DumpInt(n, D); 102 | for (i = 0; i < n; i++) { 103 | const TValue *o = &f->k[i]; 104 | DumpByte(ttype(o), D); 105 | switch (ttype(o)) { 106 | case LUA_TNIL: 107 | break; 108 | case LUA_TBOOLEAN: 109 | DumpByte(bvalue(o), D); 110 | break; 111 | case LUA_TNUMFLT: 112 | DumpNumber(fltvalue(o), D); 113 | break; 114 | case LUA_TNUMINT: 115 | DumpInteger(ivalue(o), D); 116 | break; 117 | case LUA_TSHRSTR: 118 | case LUA_TLNGSTR: 119 | DumpString(tsvalue(o), D); 120 | break; 121 | default: 122 | lua_assert(0); 123 | } 124 | } 125 | } 126 | 127 | 128 | static void DumpProtos (const Proto *f, DumpState *D) { 129 | int i; 130 | int n = f->sizep; 131 | DumpInt(n, D); 132 | for (i = 0; i < n; i++) 133 | DumpFunction(f->p[i], f->source, D); 134 | } 135 | 136 | 137 | static void DumpUpvalues (const Proto *f, DumpState *D) { 138 | int i, n = f->sizeupvalues; 139 | DumpInt(n, D); 140 | for (i = 0; i < n; i++) { 141 | DumpByte(f->upvalues[i].instack, D); 142 | DumpByte(f->upvalues[i].idx, D); 143 | } 144 | } 145 | 146 | 147 | static void DumpDebug (const Proto *f, DumpState *D) { 148 | int i, n; 149 | n = (D->strip) ? 0 : f->sizelineinfo; 150 | DumpInt(n, D); 151 | DumpVector(f->lineinfo, n, D); 152 | n = (D->strip) ? 0 : f->sizelocvars; 153 | DumpInt(n, D); 154 | for (i = 0; i < n; i++) { 155 | DumpString(f->locvars[i].varname, D); 156 | DumpInt(f->locvars[i].startpc, D); 157 | DumpInt(f->locvars[i].endpc, D); 158 | } 159 | n = (D->strip) ? 0 : f->sizeupvalues; 160 | DumpInt(n, D); 161 | for (i = 0; i < n; i++) 162 | DumpString(f->upvalues[i].name, D); 163 | } 164 | 165 | 166 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { 167 | if (D->strip || f->source == psource) 168 | DumpString(NULL, D); /* no debug info or same source as its parent */ 169 | else 170 | DumpString(f->source, D); 171 | DumpInt(f->linedefined, D); 172 | DumpInt(f->lastlinedefined, D); 173 | DumpByte(f->numparams, D); 174 | DumpByte(f->is_vararg, D); 175 | DumpByte(f->maxstacksize, D); 176 | DumpCode(f, D); 177 | DumpConstants(f, D); 178 | DumpUpvalues(f, D); 179 | DumpProtos(f, D); 180 | DumpDebug(f, D); 181 | } 182 | 183 | 184 | static void DumpHeader (DumpState *D) { 185 | DumpLiteral(LUA_SIGNATURE, D); 186 | DumpByte(LUAC_VERSION, D); 187 | DumpByte(LUAC_FORMAT, D); 188 | DumpLiteral(LUAC_DATA, D); 189 | DumpByte(sizeof(int), D); 190 | DumpByte(sizeof(size_t), D); 191 | DumpByte(sizeof(Instruction), D); 192 | DumpByte(sizeof(lua_Integer), D); 193 | DumpByte(sizeof(lua_Number), D); 194 | DumpInteger(LUAC_INT, D); 195 | DumpNumber(LUAC_NUM, D); 196 | } 197 | 198 | 199 | /* 200 | ** dump Lua function as precompiled chunk 201 | */ 202 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 203 | int strip) { 204 | DumpState D; 205 | D.L = L; 206 | D.writer = w; 207 | D.data = data; 208 | D.strip = strip; 209 | D.status = 0; 210 | DumpHeader(&D); 211 | DumpByte(f->sizeupvalues, &D); 212 | DumpFunction(f, NULL, &D); 213 | return D.status; 214 | } 215 | 216 | -------------------------------------------------------------------------------- /LuaLib/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 24 | 25 | 26 | /* number of bits to consider in a number */ 27 | #if !defined(LUA_NBITS) 28 | #define LUA_NBITS 32 29 | #endif 30 | 31 | 32 | /* 33 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 34 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 35 | ** number of bits in a lua_Unsigned.) 36 | */ 37 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 38 | 39 | 40 | /* macro to trim extra bits */ 41 | #define trim(x) ((x) & ALLONES) 42 | 43 | 44 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 45 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 46 | 47 | 48 | 49 | static lua_Unsigned andaux (lua_State *L) { 50 | int i, n = lua_gettop(L); 51 | lua_Unsigned r = ~(lua_Unsigned)0; 52 | for (i = 1; i <= n; i++) 53 | r &= checkunsigned(L, i); 54 | return trim(r); 55 | } 56 | 57 | 58 | static int b_and (lua_State *L) { 59 | lua_Unsigned r = andaux(L); 60 | pushunsigned(L, r); 61 | return 1; 62 | } 63 | 64 | 65 | static int b_test (lua_State *L) { 66 | lua_Unsigned r = andaux(L); 67 | lua_pushboolean(L, r != 0); 68 | return 1; 69 | } 70 | 71 | 72 | static int b_or (lua_State *L) { 73 | int i, n = lua_gettop(L); 74 | lua_Unsigned r = 0; 75 | for (i = 1; i <= n; i++) 76 | r |= checkunsigned(L, i); 77 | pushunsigned(L, trim(r)); 78 | return 1; 79 | } 80 | 81 | 82 | static int b_xor (lua_State *L) { 83 | int i, n = lua_gettop(L); 84 | lua_Unsigned r = 0; 85 | for (i = 1; i <= n; i++) 86 | r ^= checkunsigned(L, i); 87 | pushunsigned(L, trim(r)); 88 | return 1; 89 | } 90 | 91 | 92 | static int b_not (lua_State *L) { 93 | lua_Unsigned r = ~checkunsigned(L, 1); 94 | pushunsigned(L, trim(r)); 95 | return 1; 96 | } 97 | 98 | 99 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 100 | if (i < 0) { /* shift right? */ 101 | i = -i; 102 | r = trim(r); 103 | if (i >= LUA_NBITS) r = 0; 104 | else r >>= i; 105 | } 106 | else { /* shift left */ 107 | if (i >= LUA_NBITS) r = 0; 108 | else r <<= i; 109 | r = trim(r); 110 | } 111 | pushunsigned(L, r); 112 | return 1; 113 | } 114 | 115 | 116 | static int b_lshift (lua_State *L) { 117 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); 118 | } 119 | 120 | 121 | static int b_rshift (lua_State *L) { 122 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 123 | } 124 | 125 | 126 | static int b_arshift (lua_State *L) { 127 | lua_Unsigned r = checkunsigned(L, 1); 128 | lua_Integer i = luaL_checkinteger(L, 2); 129 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 130 | return b_shift(L, r, -i); 131 | else { /* arithmetic shift for 'negative' number */ 132 | if (i >= LUA_NBITS) r = ALLONES; 133 | else 134 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 135 | pushunsigned(L, r); 136 | return 1; 137 | } 138 | } 139 | 140 | 141 | static int b_rot (lua_State *L, lua_Integer d) { 142 | lua_Unsigned r = checkunsigned(L, 1); 143 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 144 | r = trim(r); 145 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 146 | r = (r << i) | (r >> (LUA_NBITS - i)); 147 | pushunsigned(L, trim(r)); 148 | return 1; 149 | } 150 | 151 | 152 | static int b_lrot (lua_State *L) { 153 | return b_rot(L, luaL_checkinteger(L, 2)); 154 | } 155 | 156 | 157 | static int b_rrot (lua_State *L) { 158 | return b_rot(L, -luaL_checkinteger(L, 2)); 159 | } 160 | 161 | 162 | /* 163 | ** get field and width arguments for field-manipulation functions, 164 | ** checking whether they are valid. 165 | ** ('luaL_error' called without 'return' to avoid later warnings about 166 | ** 'width' being used uninitialized.) 167 | */ 168 | static int fieldargs (lua_State *L, int farg, int *width) { 169 | lua_Integer f = luaL_checkinteger(L, farg); 170 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 171 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 172 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 173 | if (f + w > LUA_NBITS) 174 | luaL_error(L, "trying to access non-existent bits"); 175 | *width = (int)w; 176 | return (int)f; 177 | } 178 | 179 | 180 | static int b_extract (lua_State *L) { 181 | int w; 182 | lua_Unsigned r = trim(checkunsigned(L, 1)); 183 | int f = fieldargs(L, 2, &w); 184 | r = (r >> f) & mask(w); 185 | pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static int b_replace (lua_State *L) { 191 | int w; 192 | lua_Unsigned r = trim(checkunsigned(L, 1)); 193 | lua_Unsigned v = trim(checkunsigned(L, 2)); 194 | int f = fieldargs(L, 3, &w); 195 | lua_Unsigned m = mask(w); 196 | r = (r & ~(m << f)) | ((v & m) << f); 197 | pushunsigned(L, r); 198 | return 1; 199 | } 200 | 201 | 202 | static const luaL_Reg bitlib[] = { 203 | {"arshift", b_arshift}, 204 | {"band", b_and}, 205 | {"bnot", b_not}, 206 | {"bor", b_or}, 207 | {"bxor", b_xor}, 208 | {"btest", b_test}, 209 | {"extract", b_extract}, 210 | {"lrotate", b_lrot}, 211 | {"lshift", b_lshift}, 212 | {"replace", b_replace}, 213 | {"rrotate", b_rrot}, 214 | {"rshift", b_rshift}, 215 | {NULL, NULL} 216 | }; 217 | 218 | 219 | 220 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 221 | luaL_newlib(L, bitlib); 222 | return 1; 223 | } 224 | 225 | 226 | #else /* }{ */ 227 | 228 | 229 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 230 | return luaL_error(L, "library 'bit32' has been deprecated"); 231 | } 232 | 233 | #endif /* } */ 234 | -------------------------------------------------------------------------------- /KernelLuaVm/lua/state.cpp: -------------------------------------------------------------------------------- 1 | #include "state.hpp" 2 | 3 | namespace lua 4 | { 5 | struct allocation_header 6 | { 7 | uint64_t size; 8 | 9 | static allocation_header* of( void* p ) { return ( ( allocation_header* ) p ) - 1; } 10 | 11 | void* data() { return this + 1; } 12 | const void* data() const { return this + 1; } 13 | }; 14 | 15 | static void* allocator( void*, void* odata, size_t osize, size_t nsize ) 16 | { 17 | // If new size is zero: 18 | // 19 | if ( !nsize ) 20 | { 21 | // Deallocate previous if relevant, return null. 22 | // 23 | if ( osize ) free( allocation_header::of( odata ) ); 24 | return nullptr; 25 | } 26 | 27 | // If no old data: 28 | // 29 | if ( !odata ) 30 | { 31 | // Simply allocate as requested and return. 32 | // 33 | return ( ( allocation_header* ) malloc( nsize + sizeof( allocation_header ) ) )->data(); 34 | } 35 | 36 | // Resolve allocation header of the old data. 37 | // 38 | allocation_header* hdr = allocation_header::of( odata ); 39 | 40 | // If it can accomadate curent data and is not "substantially" different in size, return as is. 41 | // 42 | if ( hdr->size >= nsize && nsize >= ( hdr->size / 2 ) ) 43 | return odata; 44 | 45 | // Calculate the new "ideal" allocation size. 46 | // 47 | nsize = max( min( pow( nsize, 1.2 ), PAGE_SIZE ), nsize ); 48 | 49 | // Allocate from non-paged pool and write the size. 50 | // 51 | allocation_header* nhdr = ( allocation_header* ) malloc( nsize + sizeof( allocation_header ) ); 52 | nhdr->size = nsize; 53 | 54 | // Relocate the old data and free. 55 | // 56 | memcpy( nhdr->data(), odata, min( osize, nsize ) ); 57 | free( hdr ); 58 | 59 | // Return pointer to the new data. 60 | // 61 | return nhdr->data(); 62 | } 63 | 64 | static int panic( lua_State* L ) 65 | { 66 | const char* message = lua_tostring( L, -1 ); 67 | logger::error( "Runtime error: %s\n", message ); 68 | longjmp( get_context( L )->panic_jump, 1 ); 69 | return 0; 70 | } 71 | 72 | // Initializes a Lua state. 73 | // 74 | lua_State* init() 75 | { 76 | lua_State* L = lua_newstate( &allocator, new lua_context ); 77 | if ( !L ) return nullptr; 78 | lua_atpanic( L, &panic ); 79 | luaL_openlibs( L ); 80 | return L; 81 | } 82 | 83 | // Destroys a Lua state. 84 | // 85 | void destroy( lua_State* L ) 86 | { 87 | delete get_context( L ); 88 | lua_close( L ); 89 | } 90 | 91 | // Gets current context from a Lua state. 92 | // 93 | lua_context* get_context( lua_State* L ) 94 | { 95 | void* ctx; 96 | lua_getallocf( L, &ctx ); 97 | return ( lua_context* ) ctx; 98 | } 99 | 100 | // Executes code in given Lua state. 101 | // 102 | void execute( lua_State* L, const char* code, bool user_input ) 103 | { 104 | size_t len = strlen( code ); 105 | if ( !len ) return; 106 | 107 | // Reset the Lua stack. 108 | // 109 | if ( user_input ) 110 | lua_settop( L, 0 ); 111 | 112 | // Guard against Lua panic. 113 | // 114 | if ( setjmp( lua::get_context( L )->panic_jump ) == 0 ) 115 | { 116 | // Try to load the buffer. 117 | // 118 | if ( luaL_loadbuffer( L, code, len, "line" ) ) 119 | { 120 | logger::error( "Lua parser error: %s\n", lua_tostring( L, -1 ) ); 121 | return; 122 | } 123 | 124 | // Guard against any exceptions. 125 | // 126 | __try 127 | { 128 | // Guard against any virtual exceptions. 129 | // 130 | if ( lua_pcall( L, 0, user_input ? LUA_MULTRET : 0, 0 ) ) 131 | { 132 | logger::error( "Lua runtime error: %s\n", lua_tostring( L, -1 ) ); 133 | } 134 | // If not internal and we have something left on stack: 135 | // 136 | else if ( user_input && lua_gettop( L ) > 0 ) 137 | { 138 | // Redirect to print. 139 | // 140 | lua_getglobal( L, "print" ); 141 | lua_insert( L, 1 ); 142 | lua_pcall( L, lua_gettop( L ) - 1, 0, 0 ); 143 | } 144 | } 145 | __except ( 1 ) 146 | { 147 | logger::error( "Lua SEH error: %x\n", GetExceptionCode() ); 148 | } 149 | } 150 | else 151 | { 152 | logger::error( "Lua Panic!" ); 153 | } 154 | } 155 | }; 156 | 157 | // Some helpers we need in Lua style. 158 | // 159 | uint64_t lua_asintrinsic( lua_State* L, int i ) 160 | { 161 | switch ( lua_type( L, i ) ) 162 | { 163 | case LUA_TSTRING: 164 | return ( uint64_t ) lua_tostring( L, i ); 165 | case LUA_TLIGHTUSERDATA: 166 | case LUA_TTABLE: 167 | case LUA_TFUNCTION: 168 | case LUA_TUSERDATA: 169 | case LUA_TTHREAD: 170 | return ( uint64_t ) lua_topointer( L, i ); 171 | case LUA_TNIL: 172 | case LUA_TBOOLEAN: 173 | case LUA_TNUMBER: 174 | default: 175 | return lua_tounsigned( L, i ); 176 | } 177 | } 178 | 179 | void* lua_adressof( lua_State* L, int i ) 180 | { 181 | switch ( lua_type( L, i ) ) 182 | { 183 | case LUA_TSTRING: 184 | return ( void* ) lua_tostring( L, i ); 185 | case LUA_TLIGHTUSERDATA: 186 | case LUA_TTABLE: 187 | case LUA_TFUNCTION: 188 | case LUA_TUSERDATA: 189 | case LUA_TTHREAD: 190 | return ( void* ) lua_topointer( L, i ); 191 | case LUA_TNIL: 192 | case LUA_TBOOLEAN: 193 | case LUA_TNUMBER: 194 | default: 195 | return 0; 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/sqrt.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_sqrt.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* sqrt(x) 13 | * Return correctly rounded sqrt. 14 | * ------------------------------------------ 15 | * | Use the hardware sqrt if you have one | 16 | * ------------------------------------------ 17 | * Method: 18 | * Bit by bit method using integer arithmetic. (Slow, but portable) 19 | * 1. Normalization 20 | * Scale x to y in [1,4) with even powers of 2: 21 | * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then 22 | * sqrt(x) = 2^k * sqrt(y) 23 | * 2. Bit by bit computation 24 | * Let q = sqrt(y) truncated to i bit after binary point (q = 1), 25 | * i 0 26 | * i+1 2 27 | * s = 2*q , and y = 2 * ( y - q ). (1) 28 | * i i i i 29 | * 30 | * To compute q from q , one checks whether 31 | * i+1 i 32 | * 33 | * -(i+1) 2 34 | * (q + 2 ) <= y. (2) 35 | * i 36 | * -(i+1) 37 | * If (2) is false, then q = q ; otherwise q = q + 2 . 38 | * i+1 i i+1 i 39 | * 40 | * With some algebric manipulation, it is not difficult to see 41 | * that (2) is equivalent to 42 | * -(i+1) 43 | * s + 2 <= y (3) 44 | * i i 45 | * 46 | * The advantage of (3) is that s and y can be computed by 47 | * i i 48 | * the following recurrence formula: 49 | * if (3) is false 50 | * 51 | * s = s , y = y ; (4) 52 | * i+1 i i+1 i 53 | * 54 | * otherwise, 55 | * -i -(i+1) 56 | * s = s + 2 , y = y - s - 2 (5) 57 | * i+1 i i+1 i i 58 | * 59 | * One may easily use induction to prove (4) and (5). 60 | * Note. Since the left hand side of (3) contain only i+2 bits, 61 | * it does not necessary to do a full (53-bit) comparison 62 | * in (3). 63 | * 3. Final rounding 64 | * After generating the 53 bits result, we compute one more bit. 65 | * Together with the remainder, we can decide whether the 66 | * result is exact, bigger than 1/2ulp, or less than 1/2ulp 67 | * (it will never equal to 1/2ulp). 68 | * The rounding mode can be detected by checking whether 69 | * huge + tiny is equal to huge, and whether huge - tiny is 70 | * equal to huge for some floating point number "huge" and "tiny". 71 | * 72 | * Special cases: 73 | * sqrt(+-0) = +-0 ... exact 74 | * sqrt(inf) = inf 75 | * sqrt(-ve) = NaN ... with invalid signal 76 | * sqrt(NaN) = NaN ... with invalid signal for signaling NaN 77 | */ 78 | 79 | #include "..\libmd.h" 80 | 81 | static const double tiny = 1.0e-300; 82 | 83 | double sqrt(double x) 84 | { 85 | double z; 86 | int32_t sign = (int)0x80000000; 87 | int32_t ix0,s0,q,m,t,i; 88 | uint32_t r,t1,s1,ix1,q1; 89 | 90 | EXTRACT_WORDS(ix0, ix1, x); 91 | 92 | /* take care of Inf and NaN */ 93 | if ((ix0&0x7ff00000) == 0x7ff00000) { 94 | return x*x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */ 95 | } 96 | /* take care of zero */ 97 | if (ix0 <= 0) { 98 | if (((ix0&~sign)|ix1) == 0) 99 | return x; /* sqrt(+-0) = +-0 */ 100 | if (ix0 < 0) 101 | return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ 102 | } 103 | /* normalize x */ 104 | m = ix0>>20; 105 | if (m == 0) { /* subnormal x */ 106 | while (ix0 == 0) { 107 | m -= 21; 108 | ix0 |= (ix1>>11); 109 | ix1 <<= 21; 110 | } 111 | for (i=0; (ix0&0x00100000) == 0; i++) 112 | ix0<<=1; 113 | m -= i - 1; 114 | ix0 |= ix1>>(32-i); 115 | ix1 <<= i; 116 | } 117 | m -= 1023; /* unbias exponent */ 118 | ix0 = (ix0&0x000fffff)|0x00100000; 119 | if (m & 1) { /* odd m, double x to make it even */ 120 | ix0 += ix0 + ((ix1&sign)>>31); 121 | ix1 += ix1; 122 | } 123 | m >>= 1; /* m = [m/2] */ 124 | 125 | /* generate sqrt(x) bit by bit */ 126 | ix0 += ix0 + ((ix1&sign)>>31); 127 | ix1 += ix1; 128 | q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ 129 | r = 0x00200000; /* r = moving bit from right to left */ 130 | 131 | while (r != 0) { 132 | t = s0 + r; 133 | if (t <= ix0) { 134 | s0 = t + r; 135 | ix0 -= t; 136 | q += r; 137 | } 138 | ix0 += ix0 + ((ix1&sign)>>31); 139 | ix1 += ix1; 140 | r >>= 1; 141 | } 142 | 143 | r = sign; 144 | while (r != 0) { 145 | t1 = s1 + r; 146 | t = s0; 147 | if (t < ix0 || (t == ix0 && t1 <= ix1)) { 148 | s1 = t1 + r; 149 | if ((t1&sign) == sign && (s1&sign) == 0) 150 | s0++; 151 | ix0 -= t; 152 | if (ix1 < t1) 153 | ix0--; 154 | ix1 -= t1; 155 | q1 += r; 156 | } 157 | ix0 += ix0 + ((ix1&sign)>>31); 158 | ix1 += ix1; 159 | r >>= 1; 160 | } 161 | 162 | /* use floating add to find out rounding direction */ 163 | if ((ix0|ix1) != 0) { 164 | z = 1.0 - tiny; /* raise inexact flag */ 165 | if (z >= 1.0) { 166 | z = 1.0 + tiny; 167 | if (q1 == (uint32_t)0xffffffff) { 168 | q1 = 0; 169 | q++; 170 | } else if (z > 1.0) { 171 | if (q1 == (uint32_t)0xfffffffe) 172 | q++; 173 | q1 += 2; 174 | } else 175 | q1 += q1 & 1; 176 | } 177 | } 178 | ix0 = (q>>1) + 0x3fe00000; 179 | ix1 = q1>>1; 180 | if (q&1) 181 | ix1 |= sign; 182 | ix0 += m << 20; 183 | INSERT_WORDS(z, ix0, ix1); 184 | return z; 185 | } 186 | -------------------------------------------------------------------------------- /KernelLuaVm/crt/libmd/tgamma.c: -------------------------------------------------------------------------------- 1 | /* 2 | "A Precision Approximation of the Gamma Function" - Cornelius Lanczos (1964) 3 | "Lanczos Implementation of the Gamma Function" - Paul Godfrey (2001) 4 | "An Analysis of the Lanczos Gamma Approximation" - Glendon Ralph Pugh (2004) 5 | 6 | approximation method: 7 | 8 | (x - 0.5) S(x) 9 | Gamma(x) = (x + g - 0.5) * ---------------- 10 | exp(x + g - 0.5) 11 | 12 | with 13 | a1 a2 a3 aN 14 | S(x) ~= [ a0 + ----- + ----- + ----- + ... + ----- ] 15 | x + 1 x + 2 x + 3 x + N 16 | 17 | with a0, a1, a2, a3,.. aN constants which depend on g. 18 | 19 | for x < 0 the following reflection formula is used: 20 | 21 | Gamma(x)*Gamma(-x) = -pi/(x sin(pi x)) 22 | 23 | most ideas and constants are from boost and python 24 | */ 25 | #include "..\libmd.h" 26 | 27 | static const double pi = 3.141592653589793238462643383279502884; 28 | 29 | /* sin(pi x) with x > 0x1p-100, if sin(pi*x)==0 the sign is arbitrary */ 30 | static double sinpi(double x) 31 | { 32 | int n; 33 | 34 | /* argument reduction: x = |x| mod 2 */ 35 | /* spurious inexact when x is odd int */ 36 | x = x * 0.5; 37 | x = 2 * (x - floor(x)); 38 | 39 | /* reduce x into [-.25,.25] */ 40 | n = 4 * x; 41 | n = (n+1)/2; 42 | x -= n * 0.5; 43 | 44 | x *= pi; 45 | switch (n) { 46 | default: /* case 4 */ 47 | case 0: 48 | return __sin(x, 0, 0); 49 | case 1: 50 | return __cos(x, 0); 51 | case 2: 52 | return __sin(-x, 0, 0); 53 | case 3: 54 | return -__cos(x, 0); 55 | } 56 | } 57 | 58 | #define N 12 59 | //static const double g = 6.024680040776729583740234375; 60 | static const double gmhalf = 5.524680040776729583740234375; 61 | static const double Snum[N+1] = { 62 | 23531376880.410759688572007674451636754734846804940, 63 | 42919803642.649098768957899047001988850926355848959, 64 | 35711959237.355668049440185451547166705960488635843, 65 | 17921034426.037209699919755754458931112671403265390, 66 | 6039542586.3520280050642916443072979210699388420708, 67 | 1439720407.3117216736632230727949123939715485786772, 68 | 248874557.86205415651146038641322942321632125127801, 69 | 31426415.585400194380614231628318205362874684987640, 70 | 2876370.6289353724412254090516208496135991145378768, 71 | 186056.26539522349504029498971604569928220784236328, 72 | 8071.6720023658162106380029022722506138218516325024, 73 | 210.82427775157934587250973392071336271166969580291, 74 | 2.5066282746310002701649081771338373386264310793408, 75 | }; 76 | static const double Sden[N+1] = { 77 | 0, 39916800, 120543840, 150917976, 105258076, 45995730, 13339535, 78 | 2637558, 357423, 32670, 1925, 66, 1, 79 | }; 80 | /* n! for small integer n */ 81 | static const double fact[] = { 82 | 1, 1, 2, 6, 24, 120, 720, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0, 83 | 479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, 20922789888000.0, 84 | 355687428096000.0, 6402373705728000.0, 121645100408832000.0, 85 | 2432902008176640000.0, 51090942171709440000.0, 1124000727777607680000.0, 86 | }; 87 | 88 | /* S(x) rational function for positive x */ 89 | static double S(double x) 90 | { 91 | double_t num = 0, den = 0; 92 | int i; 93 | 94 | /* to avoid overflow handle large x differently */ 95 | if (x < 8) 96 | for (i = N; i >= 0; i--) { 97 | num = num * x + Snum[i]; 98 | den = den * x + Sden[i]; 99 | } 100 | else 101 | for (i = 0; i <= N; i++) { 102 | num = num / x + Snum[i]; 103 | den = den / x + Sden[i]; 104 | } 105 | return num/den; 106 | } 107 | 108 | double tgamma(double x) 109 | { 110 | union {double f; uint64_t i;} u = {x}; 111 | double absx, y; 112 | double_t dy, z, r; 113 | uint32_t ix = u.i>>32 & 0x7fffffff; 114 | int sign = u.i>>63; 115 | 116 | /* special cases */ 117 | if (ix >= 0x7ff00000) 118 | /* tgamma(nan)=nan, tgamma(inf)=inf, tgamma(-inf)=nan with invalid */ 119 | return x + INFINITY; 120 | if (ix < (0x3ff-54)<<20) 121 | /* |x| < 2^-54: tgamma(x) ~ 1/x, +-0 raises div-by-zero */ 122 | return 1/x; 123 | 124 | /* integer arguments */ 125 | /* raise inexact when non-integer */ 126 | if (x == floor(x)) { 127 | if (sign) 128 | return NAN; 129 | if (x <= sizeof fact/sizeof *fact) 130 | return fact[(int)x - 1]; 131 | } 132 | 133 | /* x >= 172: tgamma(x)=inf with overflow */ 134 | /* x =< -184: tgamma(x)=+-0 with underflow */ 135 | if (ix >= 0x40670000) { /* |x| >= 184 */ 136 | if (sign) { 137 | FORCE_EVAL((float)(0x1p-126/x)); 138 | if (floor(x) * 0.5 == floor(x * 0.5)) 139 | return 0; 140 | return -0.0; 141 | } 142 | x *= 0x1p1023; 143 | return x; 144 | } 145 | 146 | absx = sign ? -x : x; 147 | 148 | /* handle the error of x + g - 0.5 */ 149 | y = absx + gmhalf; 150 | if (absx > gmhalf) { 151 | dy = y - absx; 152 | dy -= gmhalf; 153 | } else { 154 | dy = y - gmhalf; 155 | dy -= absx; 156 | } 157 | 158 | z = absx - 0.5; 159 | r = S(absx) * exp(-y); 160 | if (x < 0) { 161 | /* reflection formula for negative x */ 162 | /* sinpi(absx) is not 0, integers are already handled */ 163 | r = -pi / (sinpi(absx) * absx * r); 164 | dy = -dy; 165 | z = -z; 166 | } 167 | r += dy * (gmhalf+0.5) * r / y; 168 | z = pow(y, 0.5*z); 169 | y = r * z * z; 170 | return y; 171 | } 172 | 173 | #if 1 174 | double __lgamma_r(double x, int *sign) 175 | { 176 | double r, absx; 177 | 178 | *sign = 1; 179 | 180 | /* special cases */ 181 | if (!isfinite(x)) 182 | /* lgamma(nan)=nan, lgamma(+-inf)=inf */ 183 | return x*x; 184 | 185 | /* integer arguments */ 186 | if (x == floor(x) && x <= 2) { 187 | /* n <= 0: lgamma(n)=inf with divbyzero */ 188 | /* n == 1,2: lgamma(n)=0 */ 189 | if (x <= 0) 190 | return INFINITY; 191 | return 0; 192 | } 193 | 194 | absx = fabs(x); 195 | 196 | /* lgamma(x) ~ -log(|x|) for tiny |x| */ 197 | if (absx < 0x1p-54) { 198 | *sign = 1 - 2*!!signbit(x); 199 | return -log(absx); 200 | } 201 | 202 | /* use tgamma for smaller |x| */ 203 | if (absx < 128) { 204 | x = tgamma(x); 205 | *sign = 1 - 2*!!signbit(x); 206 | return log(fabs(x)); 207 | } 208 | 209 | /* second term (log(S)-g) could be more precise here.. */ 210 | /* or with stirling: (|x|-0.5)*(log(|x|)-1) + poly(1/|x|) */ 211 | r = (absx-0.5)*(log(absx+gmhalf)-1) + (log(S(absx)) - (gmhalf+0.5)); 212 | if (x < 0) { 213 | /* reflection formula for negative x */ 214 | x = sinpi(absx); 215 | *sign = 2*!!signbit(x) - 1; 216 | r = log(pi/(fabs(x)*absx)) - r; 217 | } 218 | return r; 219 | } 220 | 221 | //weak_alias(__lgamma_r, lgamma_r); 222 | #endif 223 | -------------------------------------------------------------------------------- /KernelLuaVm/KernelLuaVm.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | crt\libmd 7 | 8 | 9 | crt\libmd 10 | 11 | 12 | crt\libmd 13 | 14 | 15 | crt\libmd 16 | 17 | 18 | crt\libmd 19 | 20 | 21 | crt\libmd 22 | 23 | 24 | crt\libmd 25 | 26 | 27 | crt\libmd 28 | 29 | 30 | crt\libmd 31 | 32 | 33 | crt\libmd 34 | 35 | 36 | crt\libmd 37 | 38 | 39 | crt\libmd 40 | 41 | 42 | crt\libmd 43 | 44 | 45 | crt\libmd 46 | 47 | 48 | crt\libmd 49 | 50 | 51 | crt\libmd 52 | 53 | 54 | crt\libmd 55 | 56 | 57 | crt\libmd 58 | 59 | 60 | crt\libmd 61 | 62 | 63 | crt\libmd 64 | 65 | 66 | crt\libmd 67 | 68 | 69 | crt\libmd 70 | 71 | 72 | crt\libmd 73 | 74 | 75 | crt\libmd 76 | 77 | 78 | crt\libmd 79 | 80 | 81 | crt\libmd 82 | 83 | 84 | crt\libmd 85 | 86 | 87 | crt\libmd 88 | 89 | 90 | crt\libmd 91 | 92 | 93 | crt\libmd 94 | 95 | 96 | crt\libmd 97 | 98 | 99 | crt\libmd 100 | 101 | 102 | crt\libmd 103 | 104 | 105 | crt\libmd 106 | 107 | 108 | crt\libmd 109 | 110 | 111 | crt\libmd 112 | 113 | 114 | crt\libmd 115 | 116 | 117 | crt\libmd 118 | 119 | 120 | crt\libmd 121 | 122 | 123 | crt\libmd 124 | 125 | 126 | crt\libmd 127 | 128 | 129 | crt\libmd 130 | 131 | 132 | crt\libmd 133 | 134 | 135 | crt\libmd 136 | 137 | 138 | crt\misc 139 | 140 | 141 | crt\misc 142 | 143 | 144 | crt\misc 145 | 146 | 147 | lua 148 | 149 | 150 | crt\misc 151 | 152 | 153 | lua 154 | 155 | 156 | lua 157 | 158 | 159 | 160 | 161 | {34fc80ff-2c2f-48ce-bc9d-e58bed8bd8c5} 162 | 163 | 164 | {d020440f-50cc-4ecd-bb74-30ed1469974b} 165 | 166 | 167 | {51d27d14-edb0-4590-bb60-8e20b89d3a8a} 168 | 169 | 170 | {c93e1f28-6e3d-4491-9813-a370a623d96b} 171 | 172 | 173 | 174 | 175 | crt 176 | 177 | 178 | crt\misc 179 | 180 | 181 | crt\misc 182 | 183 | 184 | 185 | lua 186 | 187 | 188 | lua 189 | 190 | 191 | 192 | lua 193 | 194 | 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /LuaLib/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstring.h" 23 | #include "lundump.h" 24 | #include "lzio.h" 25 | 26 | 27 | #if !defined(luai_verifycode) 28 | #define luai_verifycode(L,b,f) /* empty */ 29 | #endif 30 | 31 | 32 | typedef struct { 33 | lua_State *L; 34 | ZIO *Z; 35 | const char *name; 36 | } LoadState; 37 | 38 | 39 | static l_noret error(LoadState *S, const char *why) { 40 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); 41 | luaD_throw(S->L, LUA_ERRSYNTAX); 42 | } 43 | 44 | 45 | /* 46 | ** All high-level loads go through LoadVector; you can change it to 47 | ** adapt to the endianness of the input 48 | */ 49 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 50 | 51 | static void LoadBlock (LoadState *S, void *b, size_t size) { 52 | if (luaZ_read(S->Z, b, size) != 0) 53 | error(S, "truncated"); 54 | } 55 | 56 | 57 | #define LoadVar(S,x) LoadVector(S,&x,1) 58 | 59 | 60 | static lu_byte LoadByte (LoadState *S) { 61 | lu_byte x; 62 | LoadVar(S, x); 63 | return x; 64 | } 65 | 66 | 67 | static int LoadInt (LoadState *S) { 68 | int x; 69 | LoadVar(S, x); 70 | return x; 71 | } 72 | 73 | 74 | static lua_Number LoadNumber (LoadState *S) { 75 | lua_Number x; 76 | LoadVar(S, x); 77 | return x; 78 | } 79 | 80 | 81 | static lua_Integer LoadInteger (LoadState *S) { 82 | lua_Integer x; 83 | LoadVar(S, x); 84 | return x; 85 | } 86 | 87 | 88 | static TString *LoadString (LoadState *S) { 89 | size_t size = LoadByte(S); 90 | if (size == 0xFF) 91 | LoadVar(S, size); 92 | if (size == 0) 93 | return NULL; 94 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ 95 | char buff[LUAI_MAXSHORTLEN]; 96 | LoadVector(S, buff, size); 97 | return luaS_newlstr(S->L, buff, size); 98 | } 99 | else { /* long string */ 100 | TString *ts = luaS_createlngstrobj(S->L, size); 101 | LoadVector(S, getstr(ts), size); /* load directly in final place */ 102 | return ts; 103 | } 104 | } 105 | 106 | 107 | static void LoadCode (LoadState *S, Proto *f) { 108 | int n = LoadInt(S); 109 | f->code = luaM_newvector(S->L, n, Instruction); 110 | f->sizecode = n; 111 | LoadVector(S, f->code, n); 112 | } 113 | 114 | 115 | static void LoadFunction(LoadState *S, Proto *f, TString *psource); 116 | 117 | 118 | static void LoadConstants (LoadState *S, Proto *f) { 119 | int i; 120 | int n = LoadInt(S); 121 | f->k = luaM_newvector(S->L, n, TValue); 122 | f->sizek = n; 123 | for (i = 0; i < n; i++) 124 | setnilvalue(&f->k[i]); 125 | for (i = 0; i < n; i++) { 126 | TValue *o = &f->k[i]; 127 | int t = LoadByte(S); 128 | switch (t) { 129 | case LUA_TNIL: 130 | setnilvalue(o); 131 | break; 132 | case LUA_TBOOLEAN: 133 | setbvalue(o, LoadByte(S)); 134 | break; 135 | case LUA_TNUMFLT: 136 | setfltvalue(o, LoadNumber(S)); 137 | break; 138 | case LUA_TNUMINT: 139 | setivalue(o, LoadInteger(S)); 140 | break; 141 | case LUA_TSHRSTR: 142 | case LUA_TLNGSTR: 143 | setsvalue2n(S->L, o, LoadString(S)); 144 | break; 145 | default: 146 | lua_assert(0); 147 | } 148 | } 149 | } 150 | 151 | 152 | static void LoadProtos (LoadState *S, Proto *f) { 153 | int i; 154 | int n = LoadInt(S); 155 | f->p = luaM_newvector(S->L, n, Proto *); 156 | f->sizep = n; 157 | for (i = 0; i < n; i++) 158 | f->p[i] = NULL; 159 | for (i = 0; i < n; i++) { 160 | f->p[i] = luaF_newproto(S->L); 161 | LoadFunction(S, f->p[i], f->source); 162 | } 163 | } 164 | 165 | 166 | static void LoadUpvalues (LoadState *S, Proto *f) { 167 | int i, n; 168 | n = LoadInt(S); 169 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); 170 | f->sizeupvalues = n; 171 | for (i = 0; i < n; i++) 172 | f->upvalues[i].name = NULL; 173 | for (i = 0; i < n; i++) { 174 | f->upvalues[i].instack = LoadByte(S); 175 | f->upvalues[i].idx = LoadByte(S); 176 | } 177 | } 178 | 179 | 180 | static void LoadDebug (LoadState *S, Proto *f) { 181 | int i, n; 182 | n = LoadInt(S); 183 | f->lineinfo = luaM_newvector(S->L, n, int); 184 | f->sizelineinfo = n; 185 | LoadVector(S, f->lineinfo, n); 186 | n = LoadInt(S); 187 | f->locvars = luaM_newvector(S->L, n, LocVar); 188 | f->sizelocvars = n; 189 | for (i = 0; i < n; i++) 190 | f->locvars[i].varname = NULL; 191 | for (i = 0; i < n; i++) { 192 | f->locvars[i].varname = LoadString(S); 193 | f->locvars[i].startpc = LoadInt(S); 194 | f->locvars[i].endpc = LoadInt(S); 195 | } 196 | n = LoadInt(S); 197 | for (i = 0; i < n; i++) 198 | f->upvalues[i].name = LoadString(S); 199 | } 200 | 201 | 202 | static void LoadFunction (LoadState *S, Proto *f, TString *psource) { 203 | f->source = LoadString(S); 204 | if (f->source == NULL) /* no source in dump? */ 205 | f->source = psource; /* reuse parent's source */ 206 | f->linedefined = LoadInt(S); 207 | f->lastlinedefined = LoadInt(S); 208 | f->numparams = LoadByte(S); 209 | f->is_vararg = LoadByte(S); 210 | f->maxstacksize = LoadByte(S); 211 | LoadCode(S, f); 212 | LoadConstants(S, f); 213 | LoadUpvalues(S, f); 214 | LoadProtos(S, f); 215 | LoadDebug(S, f); 216 | } 217 | 218 | 219 | static void checkliteral (LoadState *S, const char *s, const char *msg) { 220 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 221 | size_t len = strlen(s); 222 | LoadVector(S, buff, len); 223 | if (memcmp(s, buff, len) != 0) 224 | error(S, msg); 225 | } 226 | 227 | 228 | static void fchecksize (LoadState *S, size_t size, const char *tname) { 229 | if (LoadByte(S) != size) 230 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); 231 | } 232 | 233 | 234 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 235 | 236 | static void checkHeader (LoadState *S) { 237 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ 238 | if (LoadByte(S) != LUAC_VERSION) 239 | error(S, "version mismatch in"); 240 | if (LoadByte(S) != LUAC_FORMAT) 241 | error(S, "format mismatch in"); 242 | checkliteral(S, LUAC_DATA, "corrupted"); 243 | checksize(S, int); 244 | checksize(S, size_t); 245 | checksize(S, Instruction); 246 | checksize(S, lua_Integer); 247 | checksize(S, lua_Number); 248 | if (LoadInteger(S) != LUAC_INT) 249 | error(S, "endianness mismatch in"); 250 | if (LoadNumber(S) != LUAC_NUM) 251 | error(S, "float format mismatch in"); 252 | } 253 | 254 | 255 | /* 256 | ** load precompiled chunk 257 | */ 258 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { 259 | LoadState S; 260 | LClosure *cl; 261 | if (*name == '@' || *name == '=') 262 | S.name = name + 1; 263 | else if (*name == LUA_SIGNATURE[0]) 264 | S.name = "binary string"; 265 | else 266 | S.name = name; 267 | S.L = L; 268 | S.Z = Z; 269 | checkHeader(&S); 270 | cl = luaF_newLclosure(L, LoadByte(&S)); 271 | setclLvalue(L, L->top, cl); 272 | luaD_inctop(L); 273 | cl->p = luaF_newproto(L); 274 | LoadFunction(&S, cl->p, NULL); 275 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 276 | luai_verifycode(L, buff, cl->p); 277 | return cl; 278 | } 279 | 280 | --------------------------------------------------------------------------------