forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathlib.go
More file actions
594 lines (537 loc) · 13.3 KB
/
Copy pathmathlib.go
File metadata and controls
594 lines (537 loc) · 13.3 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package lua
import (
"math"
"math/rand"
)
func OpenMath(L *LState) int {
mod := L.RegisterModule(MathLibName, mathFuncs).(*LTable)
mod.RawSetString("pi", LNumberFloat(math.Pi))
mod.RawSetString("huge", LNumberFloat(math.Inf(1)))
// Lua 5.3 integer constants
mod.RawSetString("maxinteger", LNumberInt(math.MaxInt64))
mod.RawSetString("mininteger", LNumberInt(math.MinInt64))
L.Push(mod)
return 1
}
var mathFuncs = map[string]LGFunction{
"abs": mathAbs,
"acos": mathAcos,
"asin": mathAsin,
"atan": mathAtan,
"atan2": mathAtan2,
"ceil": mathCeil,
"cos": mathCos,
"cosh": mathCosh,
"deg": mathDeg,
"exp": mathExp,
"floor": mathFloor,
"fmod": mathFmod,
"frexp": mathFrexp,
"ldexp": mathLdexp,
"log": mathLog,
"log10": mathLog10,
"max": mathMax,
"min": mathMin,
"mod": mathMod,
"modf": mathModf,
"pow": mathPow,
"rad": mathRad,
"random": mathRandom,
"randomseed": mathRandomseed,
"sin": mathSin,
"sinh": mathSinh,
"sqrt": mathSqrt,
"tan": mathTan,
"tanh": mathTanh,
// Lua 5.3 functions
"tointeger": mathToInteger,
"type": mathType,
"ult": mathUlt,
// Bitwise operations (Lua 5.3)
"band": mathBand,
"bor": mathBor,
"bxor": mathBxor,
"bnot": mathBnot,
"lshift": mathLshift,
"rshift": mathRshift,
"extract": mathExtract,
"replace": mathReplace,
}
func mathAbs(L *LState) int {
v := L.CheckNumber(1)
L.Push(v.Abs())
return 1
}
func mathAcos(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Acos(v.Float64())))
return 1
}
func mathAsin(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Asin(v.Float64())))
return 1
}
func mathAtan(L *LState) int {
v := L.CheckNumber(1)
// Lua 5.3: math.atan can accept optional second argument
// math.atan(y) returns atan(y)
// math.atan(y, x) returns atan2(y, x)
if L.GetTop() >= 2 {
v2 := L.CheckNumber(2)
L.Push(LNumberFloat(math.Atan2(v.Float64(), v2.Float64())))
} else {
L.Push(LNumberFloat(math.Atan(v.Float64())))
}
return 1
}
func mathAtan2(L *LState) int {
v1 := L.CheckNumber(1)
v2 := L.CheckNumber(2)
L.Push(LNumberFloat(math.Atan2(v1.Float64(), v2.Float64())))
return 1
}
func mathCeil(L *LState) int {
v := L.CheckNumber(1)
L.Push(v.Ceil())
return 1
}
func mathCos(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Cos(v.Float64())))
return 1
}
func mathCosh(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Cosh(v.Float64())))
return 1
}
func mathDeg(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(v.Float64() * 180 / math.Pi))
return 1
}
func mathExp(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Exp(v.Float64())))
return 1
}
func mathFloor(L *LState) int {
v := L.CheckNumber(1)
L.Push(v.Floor())
return 1
}
func mathFmod(L *LState) int {
v1 := L.CheckNumber(1)
v2 := L.CheckNumber(2)
// fmod is different from modulo - it's the remainder of division
// Lua 5.3: if both arguments are integers, return integer
if v1.IsInteger() && v2.IsInteger() {
i1 := v1.Int64()
i2 := v2.Int64()
if i2 == 0 {
L.RaiseError("zero")
}
// Integer fmod: same as C fmod for integers
result := i1 % i2
L.Push(LNumberInt(result))
} else {
r := math.Mod(v1.Float64(), v2.Float64())
L.Push(LNumberFloat(r))
}
return 1
}
func mathFrexp(L *LState) int {
v := L.CheckNumber(1)
f1, f2 := math.Frexp(v.Float64())
L.Push(LNumberFloat(f1))
L.Push(LNumberInt(int64(f2)))
return 2
}
func mathLdexp(L *LState) int {
v := L.CheckNumber(1)
exp := L.CheckInt(2)
L.Push(LNumberFloat(math.Ldexp(v.Float64(), exp)))
return 1
}
func mathLog(L *LState) int {
v := L.CheckNumber(1)
base := L.OptNumber(2, LNumberFloat(math.E)) // Default to natural log
// Lua 5.3: math.log(x, base) = log(x) / log(base)
result := math.Log(v.Float64()) / math.Log(base.Float64())
L.Push(LNumberFloat(result))
return 1
}
func mathLog10(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Log10(v.Float64())))
return 1
}
func mathMax(L *LState) int {
if L.GetTop() == 0 {
L.RaiseError("value expected")
}
max := L.CheckNumber(1)
top := L.GetTop()
for i := 2; i <= top; i++ {
v := L.CheckNumber(i)
// Lua 5.3: preserve integer type when possible
if max.IsInteger() && v.IsInteger() {
if v.Int64() > max.Int64() {
max = v
}
} else {
if v.Float64() > max.Float64() {
max = v
}
}
}
L.Push(max)
return 1
}
func mathMin(L *LState) int {
if L.GetTop() == 0 {
L.RaiseError("value expected")
}
min := L.CheckNumber(1)
top := L.GetTop()
for i := 2; i <= top; i++ {
v := L.CheckNumber(i)
// Lua 5.3: preserve integer type when possible
if min.IsInteger() && v.IsInteger() {
if v.Int64() < min.Int64() {
min = v
}
} else {
if v.Float64() < min.Float64() {
min = v
}
}
}
L.Push(min)
return 1
}
func mathMod(L *LState) int {
lhs := L.CheckNumber(1)
rhs := L.CheckNumber(2)
L.Push(lhs.Mod(rhs))
return 1
}
func mathModf(L *LState) int {
v := L.CheckNumber(1)
f := v.Float64()
// Handle special cases: NaN and Inf
if math.IsNaN(f) {
L.Push(LNumberFloat(math.NaN()))
L.Push(LNumberFloat(math.NaN()))
return 2
}
if math.IsInf(f, 0) {
L.Push(LNumberFloat(f))
L.Push(LNumberFloat(0.0))
return 2
}
intPart, fracPart := math.Modf(f)
// Return integer part as integer if possible
var intLNum LNumber
if intPart == float64(int64(intPart)) {
intLNum = LNumberInt(int64(intPart))
} else {
intLNum = LNumberFloat(intPart)
}
L.Push(intLNum)
L.Push(LNumberFloat(fracPart))
return 2
}
func mathPow(L *LState) int {
v1 := L.CheckNumber(1)
v2 := L.CheckNumber(2)
L.Push(v1.Pow(v2))
return 1
}
func mathRad(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(v.Float64() * math.Pi / 180))
return 1
}
func mathRandom(L *LState) int {
// Lua 5.3: math.random() accepts 0, 1, or 2 arguments
if L.GetTop() > 2 {
L.ArgError(3, "wrong number of arguments")
return 0
}
switch L.GetTop() {
case 0:
L.Push(LNumberFloat(rand.Float64()))
case 1:
n := L.CheckInt64(1)
if n <= 0 {
L.ArgError(1, "interval is empty")
return 0
}
// Use Int63n for positive numbers, handle large values
if n > math.MaxInt64 {
// For very large numbers, use float64 approximation
L.Push(LNumberInt(int64(rand.Float64()*float64(n)) + 1))
} else {
L.Push(LNumberInt(rand.Int63n(n) + 1))
}
default:
min := L.CheckInt64(1)
max := L.CheckInt64(2)
if max < min {
L.ArgError(2, "interval is empty")
return 0
}
// Lua 5.3: reject intervals that are too large for uniform distribution
// The range (max - min) must fit in a positive int64
if min < 0 && max >= 0 {
// Range crosses zero (or is [minint, 0]). The range is max - min + 1.
// Special case: [minint, 0] is too large
if min == math.MinInt64 && max == 0 {
L.ArgError(1, "interval too large")
return 0
}
// For other cases where max > 0, check if the range overflows
if max > 0 {
// This overflows if max > math.MaxInt64 + min (i.e., max + (-min) > math.MaxInt64)
// Since min < 0, -min > 0. We need to check if max + (-min) > math.MaxInt64.
// But -min can overflow if min == math.MinInt64 (handled above).
if min == math.MinInt64 {
// Already handled [minint, 0] above, so this is [minint, -1] or similar
// [minint, -1] is handled specially below
L.ArgError(1, "interval too large")
return 0
}
// Now -min is safe (doesn't overflow)
if max > math.MaxInt64+min {
// max - min > math.MaxInt64
L.ArgError(1, "interval too large")
return 0
}
}
}
// Handle the full int64 range specially
if min == math.MinInt64 && max == math.MaxInt64 {
// For full range, use random bytes
var buf [8]byte
rand.Read(buf[:])
L.Push(LNumberInt(int64(buf[0]) | int64(buf[1])<<8 | int64(buf[2])<<16 |
int64(buf[3])<<24 | int64(buf[4])<<32 | int64(buf[5])<<40 |
int64(buf[6])<<48 | int64(buf[7])<<56))
} else if min == 0 && max == math.MaxInt64 {
// Special case for [0, maxint]: max+1 overflows, so use Int63() directly
L.Push(LNumberInt(rand.Int63()))
} else if min == math.MinInt64 && max == -1 {
// Special case for [minint, -1]: use random negative number
// -minint overflows, so use Int63() and negate
L.Push(LNumberInt(-rand.Int63() - 1))
} else {
// Calculate range carefully to avoid overflow
range_ := max - min
if range_ < 0 {
// Overflow in subtraction, use two-step approach
// Generate random sign and magnitude
if rand.Float64() < 0.5 {
L.Push(LNumberInt(rand.Int63n(-min+1) + min))
} else {
L.Push(LNumberInt(rand.Int63n(max + 1)))
}
} else if range_ > math.MaxInt64/2 {
// Very large range, use combination of Int63n calls
// Split range into two halves
half := range_ / 2
if rand.Float64() < 0.5 {
L.Push(LNumberInt(rand.Int63n(half+1) + min))
} else {
L.Push(LNumberInt(rand.Int63n(range_-half) + min + half + 1))
}
} else {
// Normal case: range_ + 1 to include max
L.Push(LNumberInt(rand.Int63n(range_+1) + min))
}
}
}
return 1
}
func mathRandomseed(L *LState) int {
rand.Seed(L.CheckInt64(1))
return 0
}
func mathSin(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Sin(v.Float64())))
return 1
}
func mathSinh(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Sinh(v.Float64())))
return 1
}
func mathSqrt(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Sqrt(v.Float64())))
return 1
}
func mathTan(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Tan(v.Float64())))
return 1
}
func mathTanh(L *LState) int {
v := L.CheckNumber(1)
L.Push(LNumberFloat(math.Tanh(v.Float64())))
return 1
}
// Lua 5.3 functions
// math.tointeger converts a number to integer if possible
func mathToInteger(L *LState) int {
// Lua 5.3: accepts numbers and strings, returns nil for invalid values
arg := L.Get(1)
// Try to convert to number first (handles both numbers and numeric strings)
var v LNumber
switch val := arg.(type) {
case LNumber:
v = val
case LString:
// Try to parse string as number
num, err := parseNumber(string(val))
if err != nil {
L.Push(LNil)
return 1
}
v = num
default:
// Not a number or string, return nil
L.Push(LNil)
return 1
}
if v.IsInteger() {
L.Push(v)
} else {
f := v.Float64()
if f == float64(int64(f)) && !math.IsInf(f, 0) && !math.IsNaN(f) {
L.Push(LNumberInt(int64(f)))
} else {
L.Push(LNil)
}
}
return 1
}
// math.type returns "integer", "float", or nil
// Lua 5.3: returns nil for non-numeric values (doesn't raise error)
func mathType(L *LState) int {
v := L.CheckAny(1)
if num, ok := v.(LNumber); ok {
if num.IsInteger() {
L.Push(LString("integer"))
} else {
L.Push(LString("float"))
}
} else {
// Return nil for non-numeric values (Lua 5.3 behavior)
L.Push(LNil)
}
return 1
}
// math.ult returns true if a < b when compared as unsigned integers
func mathUlt(L *LState) int {
a := L.CheckNumber(1)
b := L.CheckNumber(2)
// Convert to uint64 for unsigned comparison
// This handles negative numbers correctly as large positive values
ua := uint64(a.Int64())
ub := uint64(b.Int64())
if ua < ub {
L.Push(LTrue)
} else {
L.Push(LFalse)
}
return 1
}
// Bitwise operations (Lua 5.3)
// math.band performs bitwise AND
func mathBand(L *LState) int {
if L.GetTop() < 2 {
L.RaiseError("wrong number of arguments")
}
result := L.CheckNumber(1)
for i := 2; i <= L.GetTop(); i++ {
result = result.Band(L.CheckNumber(i))
}
L.Push(result)
return 1
}
// math.bor performs bitwise OR
func mathBor(L *LState) int {
if L.GetTop() < 2 {
L.RaiseError("wrong number of arguments")
}
result := L.CheckNumber(1)
for i := 2; i <= L.GetTop(); i++ {
result = result.Bor(L.CheckNumber(i))
}
L.Push(result)
return 1
}
// math.bxor performs bitwise XOR
func mathBxor(L *LState) int {
if L.GetTop() < 2 {
L.RaiseError("wrong number of arguments")
}
result := L.CheckNumber(1)
for i := 2; i <= L.GetTop(); i++ {
result = result.Bxor(L.CheckNumber(i))
}
L.Push(result)
return 1
}
// math.bnot performs bitwise NOT
func mathBnot(L *LState) int {
v := L.CheckNumber(1)
L.Push(v.Bnot())
return 1
}
// math.lshift performs bitwise left shift
func mathLshift(L *LState) int {
v := L.CheckNumber(1)
shift := L.CheckNumber(2)
L.Push(v.Shl(shift))
return 1
}
// math.rshift performs bitwise right shift
func mathRshift(L *LState) int {
v := L.CheckNumber(1)
shift := L.CheckNumber(2)
L.Push(v.Shr(shift))
return 1
}
// math.extract extracts bits from a number
func mathExtract(L *LState) int {
v := L.CheckNumber(1)
field := L.CheckInt(2)
width := L.OptInt(3, 1)
if field < 0 || width < 1 || field+width > 64 {
L.RaiseError("invalid field width")
}
mask := (int64(1) << uint(width)) - 1
result := (v.Int64() >> uint(field)) & mask
L.Push(LNumberInt(result))
return 1
}
// math.replace replaces bits in a number
func mathReplace(L *LState) int {
v := L.CheckNumber(1)
value := L.CheckNumber(2)
field := L.CheckInt(3)
width := L.OptInt(4, 1)
if field < 0 || width < 1 || field+width > 64 {
L.RaiseError("invalid field width")
}
mask := (int64(1) << uint(width)) - 1
maskedValue := value.Int64() & mask
maskedV := v.Int64() &^ (mask << uint(field))
result := maskedV | (maskedValue << uint(field))
L.Push(LNumberInt(result))
return 1
}