aboutsummaryrefslogtreecommitdiff
path: root/src/lmathlib.js
blob: fd1871110a38502034b153bd12d64baca8f5cadd (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"use strict";

const assert     = require('assert');
const seedrandom = require('seedrandom');

const lua        = require('./lua.js');
const lapi       = require('./lapi.js');
const lauxlib    = require('./lauxlib.js');
const lstate     = require('./lstate.js');
const ldo        = require('./ldo.js');
const ldebug     = require('./ldebug.js');
const llimit     = require('./llimit.js');
const luaconf    = require('./luaconf.js');
const CT         = lua.constant_types;
const TS         = lua.thread_status;

var RNG          = seedrandom();

const math_randomseed = function(L) {
    RNG = seedrandom(Math.abs(lauxlib.luaL_checknumber(L, 1)));
};

const math_random = function(L) {
    let low, up;
    let r = RNG();
    switch (lapi.lua_gettop(L)) {  /* check number of arguments */
        case 0:
            lapi.lua_pushnumber(L, r);  /* Number between 0 and 1 */
            return 1;
        case 1: {
            low = 1;
            up = lauxlib.luaL_checkinteger(L, 1);
            break;
        }
        case 2: {
            low = lauxlib.luaL_checkinteger(L, 1);
            up = lauxlib.luaL_checkinteger(L, 2);
            break;
        }
        default: return lauxlib.luaL_error(L, lua.to_luastring("wrong number of arguments", true));
    }

    /* random integer in the interval [low, up] */
    lauxlib.luaL_argcheck(L, low <= up, 1, lua.to_luastring("interval is empty", true));
    lauxlib.luaL_argcheck(L, low >= 0 || up <= llimit.MAX_INT + low, 1,
            lua.to_luastring("interval too large", true));

    r *= (up - low) + 1;
    lapi.lua_pushinteger(L, r + low);
    return 1;
};

const math_abs = function(L) {
    if (lapi.lua_isinteger(L, 1))
        lapi.lua_pushinteger(L, Math.abs(lapi.lua_tointeger(L, 1)));
    else
        lapi.lua_pushnumber(L, Math.abs(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_sin = function(L) {
    lapi.lua_pushnumber(L, Math.sin(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_cos = function(L) {
    lapi.lua_pushnumber(L, Math.cos(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_tan = function(L) {
    lapi.lua_pushnumber(L, Math.tan(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_asin = function(L) {
    lapi.lua_pushnumber(L, Math.asin(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_acos = function(L) {
    lapi.lua_pushnumber(L, Math.acos(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_atan = function(L) {
    lapi.lua_pushnumber(L, Math.atan(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_toint = function(L) {
    let n = lapi.lua_tointegerx(L, 1);
    if (n !== false)
        lapi.lua_pushinteger(L, n);
    else {
        lauxlib.luaL_checkany(L, 1);
        lapi.lua_pushnil(L);  /* value is not convertible to integer */
    }
    return 1;
};

const pushnumint = function(L, d) {
    let n = luaconf.lua_numbertointeger(d);
    if (n !== false)  /* does 'd' fit in an integer? */
        lapi.lua_pushinteger(L, n);  /* result is integer */
    else
        lapi.lua_pushnumber(L, d);  /* result is float */
};

const math_floor = function(L) {
    if (lapi.lua_isinteger(L, 1))
        lapi.lua_settop(L, 1);
    else
        pushnumint(L, Math.floor(lauxlib.luaL_checknumber(L, 1)));

    return 1;
};

const math_ceil = function(L) {
    if (lapi.lua_isinteger(L, 1))
        lapi.lua_settop(L, 1);
    else
        pushnumint(L, Math.ceil(lauxlib.luaL_checknumber(L, 1)));

    return 1;
};

const math_sqrt = function(L) {
    lapi.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_ult = function(L) {
    let a = lauxlib.luaL_checkinteger(L, 1);
    let b = lauxlib.luaL_checkinteger(L, 2);
    lapi.lua_pushboolean(L, Math.abs(a) < Math.abs(b));
    return 1;
};

const math_log = function(L) {
    let x = lauxlib.luaL_checknumber(L, 1);
    let res;
    if (lapi.lua_isnoneornil(L, 2))
        res = Math.log(x);
    else {
        let base = lauxlib.luaL_checknumber(L, 2);
        if (base === 2)
            res = Math.log2(x);
        else if (base === 10)
            res = Math.log10(x);
        else
            res = Math.log(x)/Math.log(base);
    }
    lapi.lua_pushnumber(L, res);
    return 1;
};

const math_exp = function(L) {
    lapi.lua_pushnumber(L, Math.exp(lauxlib.luaL_checknumber(L, 1)));
    return 1;
};

const math_deg = function(L) {
    lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (180 / Math.PI));
    return 1;
};

const math_rad = function(L) {
    lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (Math.PI / 180));
    return 1;
};

const math_min = function(L) {
    let n = lapi.lua_gettop(L);  /* number of arguments */
    let imin = 1;  /* index of current minimum value */
    lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true));
    for (let i = 2; i <= n; i++){
        if (lapi.lua_compare(L, i, imin, lua.LUA_OPLT))
            imin = i;
    }
    lapi.lua_pushvalue(L, imin);
    return 1;
};

const math_max = function(L) {
    let n = lapi.lua_gettop(L);  /* number of arguments */
    let imax = 1;  /* index of current minimum value */
    lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true));
    for (let i = 2; i <= n; i++){
        if (lapi.lua_compare(L, imax, i, lua.LUA_OPLT))
            imax = i;
    }
    lapi.lua_pushvalue(L, imax);
    return 1;
};

const math_type = function(L) {
    if (lapi.lua_type(L, 1) === CT.LUA_TNUMBER) {
        if (lapi.lua_isinteger(L, 1))
            lapi.lua_pushliteral(L, "integer");
        else
            lapi.lua_pushliteral(L, "float");
    } else {
        lauxlib.luaL_checkany(L, 1);
        lapi.lua_pushnil(L);
    }
    return 1;
};

const math_fmod = function(L) {
    if (lapi.lua_isinteger(L, 1) && lapi.lua_isinteger(L, 2)) {
        let d = lapi.lua_tointeger(L, 2);
        if (Math.abs(d) + 1 <= 1) {
            lauxlib.luaL_argcheck(L, d !== 0, 2, lua.to_luastring("zero", true));
            lapi.lua_pushinteger(L, 0);
        } else
            lapi.lua_pushinteger(L, lapi.lua_tointeger(L, 1) % d);
    } else {
        let a = lauxlib.luaL_checknumber(L, 1);
        let b = lauxlib.luaL_checknumber(L, 2);
        lapi.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8)));
    }
    return 1;
};

const math_modf = function(L) {
    if (lapi.lua_isinteger(L, 1)) {
        lapi.lua_settop(L, 1);  /* number is its own integer part */
        lapi.lua_pushnumber(L, 0);  /* no fractional part */
    } else {
        let n = lauxlib.luaL_checknumber(L, 1);
        let ip = n < 0 ? Math.ceil(n) : Math.floor(n);
        pushnumint(L, ip);
        lapi.lua_pushnumber(L, n === ip ? 0 : n - ip);
    }
    return 2;
};

const mathlib = {
    "abs":        math_abs,
    "acos":       math_acos,
    "asin":       math_asin,
    "atan":       math_atan,
    "ceil":       math_ceil,
    "cos":        math_cos,
    "deg":        math_deg,
    "exp":        math_exp,
    "floor":      math_floor,
    "fmod":       math_fmod,
    "log":        math_log,
    "max":        math_max,
    "min":        math_min,
    "modf":       math_modf,
    "rad":        math_rad,
    "random":     math_random,
    "randomseed": math_randomseed,
    "sin":        math_sin,
    "sqrt":       math_sqrt,
    "tan":        math_tan,
    "tointeger":  math_toint,
    "type":       math_type,
    "ult":        math_ult
};

const luaopen_math = function(L) {
    lauxlib.luaL_newlib(L, mathlib);
    lapi.lua_pushnumber(L, Math.PI);
    lapi.lua_setfield(L, -2, lua.to_luastring("pi", true));
    lapi.lua_pushnumber(L, Number.MAX_VALUE);
    lapi.lua_setfield(L, -2, lua.to_luastring("huge", true));
    lapi.lua_pushinteger(L, llimit.MAX_INT);
    lapi.lua_setfield(L, -2, lua.to_luastring("maxinteger", true));
    lapi.lua_pushinteger(L, llimit.MIN_INT);
    lapi.lua_setfield(L, -2, lua.to_luastring("mininteger", true));
    return 1;
};

module.exports.luaopen_math = luaopen_math;