-
Notifications
You must be signed in to change notification settings - Fork 4
/
bnumber.c
64 lines (53 loc) · 1.98 KB
/
bnumber.c
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
#include <math.h>
#include "bstring.h"
#include "berror.h"
#include "bparser.h"
#include "bnumber.h"
static TValue * primitive_Number_toExponential(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
assert_with_message(ttisnumber(thisVal), "The calling object must be number.");
char buff[MAX_STRING_BUFFER];
TValue * ret = TV_MALLOC;
bean_Number value = nvalue(thisVal);
sprintf(buff, "%e", value);
TString * ts = beanS_newlstr(B, buff, strlen(buff));
setsvalue(ret, ts);
return ret;
}
static TValue * primitive_Number_toFixed(bean_State * B, TValue * thisVal, TValue * args, int argc) {
assert_with_message(ttisnumber(thisVal), "The calling object must be number.");
char * buff;
char * template = "%%.%df";
char * format = malloc(sizeof(char) * strlen(template));
if (argc >= 1) {
assert_with_message(ttisnumber(&args[0]), "The paramter which you passed must be a number.");
}
long bit = (long)fabs(argc > 0 ? nvalue(&args[0]) : 2);
assert_with_message(bit > 0, "The paramter which you passed must be a positive number.");
TValue * ret = TV_MALLOC;
sprintf(format, template, bit);
bean_Number value = nvalue(thisVal);
/* Auto enlarge if space not enough */
size_t i = 0;
size_t size = MAX_STRING_BUFFER;
size_t result;
do {
size = size * (++i);
buff = malloc(size);
result = snprintf(buff, size, format, value);
} while(result >= size);
TString * ts = beanS_newlstr(B, buff, strlen(buff));
setsvalue(ret, ts);
return ret;
}
TValue * init_Number(bean_State * B) {
global_State * G = B->l_G;
TValue * proto = TV_MALLOC;
Hash * h = init_hash(B);
sethashvalue(proto, h);
set_prototype_function(B, "toFixed", 7, primitive_Number_toFixed, hhvalue(proto));
set_prototype_function(B, "toExponential", 13, primitive_Number_toExponential, hhvalue(proto));
TValue * number = TV_MALLOC;
setsvalue(number, beanS_newlstr(B, "Number", 6));
hash_set(B, G->globalScope->variables, number, proto);
return proto;
}