-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjswrap_number.c
152 lines (138 loc) · 3.85 KB
/
jswrap_number.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
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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* JavaScript methods for Numbers
* ----------------------------------------------------------------------------
*/
#include "jswrap_number.h"
/*JSON{
"type" : "class",
"class" : "Number",
"check" : "jsvIsNumeric(var)"
}
This is the built-in JavaScript class for numbers.
*/
/*JSON{
"type" : "constructor",
"class" : "Number",
"name" : "Number",
"generate" : "jswrap_number_constructor",
"params" : [
["value","JsVarArray","A single value to be converted to a number"]
],
"return" : ["JsVar","A Number object"]
}
Creates a number
*/
JsVar *jswrap_number_constructor(JsVar *args) {
if (jsvGetArrayLength(args)==0) return jsvNewFromInteger(0);
JsVar *val = jsvGetArrayItem(args, 0);
JsVar *result = 0;
if (jsvIsArray(val)) {
JsVarInt l = jsvGetArrayLength(val);
if (l==0) result = jsvNewFromInteger(0);
else if (l==1) {
JsVar *n = jsvGetArrayItem(val, 0);
if (jsvIsString(n) && jsvIsEmptyString(n)) result = jsvNewFromInteger(0);
else if (!jsvIsBoolean(n)) result=jsvAsNumber(n);
jsvUnLock(n);
} // else NaN
} else if (jsvIsUndefined(val) || jsvIsObject(val))
result = 0;
else {
if (jsvIsString(val) && jsvIsEmptyString(val)) {
result = jsvNewFromInteger(0);
} else
result = jsvAsNumber(val);
}
jsvUnLock(val);
if (result) return result;
return jsvNewFromFloat(NAN);
}
/*JSON{
"type" : "variable",
"name" : "NaN",
"generate_full" : "NAN",
"return" : ["float","Not a Number"]
}*/
/*JSON{
"type" : "variable",
"name" : "Infinity",
"generate_full" : "INFINITY",
"return" : ["float","Positive Infinity (1/0)"]
}*/
/*JSON{
"type" : "staticproperty",
"class" : "Number",
"name" : "NaN",
"generate_full" : "NAN",
"return" : ["float","Not a Number"]
}*/
/*JSON{
"type" : "staticproperty",
"class" : "Number",
"name" : "MAX_VALUE",
"generate_full" : "DBL_MAX",
"return" : ["float","Maximum representable value"]
}*/
/*JSON{
"type" : "staticproperty",
"class" : "Number",
"name" : "MIN_VALUE",
"generate_full" : "DBL_MIN",
"return" : ["float","Smallest representable value"]
}*/
/*JSON{
"type" : "staticproperty",
"class" : "Number",
"name" : "NEGATIVE_INFINITY",
"generate_full" : "-INFINITY",
"return" : ["float","Negative Infinity (-1/0)"]
}*/
/*JSON{
"type" : "staticproperty",
"class" : "Number",
"name" : "POSITIVE_INFINITY",
"generate_full" : "INFINITY",
"return" : ["float","Positive Infinity (1/0)"]
}*/
/*JSON{
"type" : "method",
"class" : "Number",
"name" : "toFixed",
"generate" : "jswrap_number_toFixed",
"params" : [
["decimalPlaces","int32","A number between 0 and 20 specifying the number of decimal digits after the decimal point"]
],
"return" : ["JsVar","A string"]
}
Format the number as a fixed point number
*/
JsVar *jswrap_number_toFixed(JsVar *parent, int decimals) {
if (decimals<0) decimals=0;
if (decimals>20) decimals=20;
char buf[JS_NUMBER_BUFFER_SIZE];
ftoa_bounded_extra(jsvGetFloat(parent), buf, sizeof(buf), 10, decimals);
return jsvNewFromString(buf);
}
/*JSON{
"type" : "variable",
"name" : "HIGH",
"generate_full" : "1",
"return" : ["int32","Logic 1 for Arduino compatibility - this is the same as just typing `1`"]
}*/
/*JSON{
"type" : "variable",
"name" : "LOW",
"generate_full" : "0",
"return" : ["int32","Logic 0 for Arduino compatibility - this is the same as just typing `0`"]
}*/