mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-20 04:54:45 -07:00
JS: Fix Number.toString() with decimals --nobuild
This commit is contained in:
@@ -95,6 +95,7 @@
|
|||||||
- Fix crash on ISO15693-3 save when memory is empty or cannot be read (by @Willy-JL)
|
- Fix crash on ISO15693-3 save when memory is empty or cannot be read (by @Willy-JL)
|
||||||
- Infrared: Fix universals sending (by @Willy-JL)
|
- Infrared: Fix universals sending (by @Willy-JL)
|
||||||
- GUI: Fix widget text scroll with 256+ lines (by @Willy-JL)
|
- GUI: Fix widget text scroll with 256+ lines (by @Willy-JL)
|
||||||
|
- JS: Fix `Number.toString()` with decimals (by @Willy-JL)
|
||||||
- Sub-GHz:
|
- Sub-GHz:
|
||||||
- UL: Fix Hollarm protocol with more verification (by @xMasterX)
|
- UL: Fix Hollarm protocol with more verification (by @xMasterX)
|
||||||
- UL: Fix GangQi protocol (by @DoberBit and @mishamyte)
|
- UL: Fix GangQi protocol (by @DoberBit and @mishamyte)
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
#include "mjs_string_public.h"
|
#include "mjs_string_public.h"
|
||||||
#include "mjs_util.h"
|
#include "mjs_util.h"
|
||||||
|
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
mjs_val_t mjs_mk_null(void) {
|
mjs_val_t mjs_mk_null(void) {
|
||||||
return MJS_NULL;
|
return MJS_NULL;
|
||||||
}
|
}
|
||||||
@@ -94,7 +96,7 @@ int mjs_is_boolean(mjs_val_t v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define MJS_IS_POINTER_LEGIT(n) \
|
#define MJS_IS_POINTER_LEGIT(n) \
|
||||||
(((n)&MJS_TAG_MASK) == 0 || ((n)&MJS_TAG_MASK) == (~0 & MJS_TAG_MASK))
|
(((n) & MJS_TAG_MASK) == 0 || ((n) & MJS_TAG_MASK) == (~0 & MJS_TAG_MASK))
|
||||||
|
|
||||||
MJS_PRIVATE mjs_val_t mjs_pointer_to_value(struct mjs* mjs, void* p) {
|
MJS_PRIVATE mjs_val_t mjs_pointer_to_value(struct mjs* mjs, void* p) {
|
||||||
uint64_t n = ((uint64_t)(uintptr_t)p);
|
uint64_t n = ((uint64_t)(uintptr_t)p);
|
||||||
@@ -165,13 +167,13 @@ MJS_PRIVATE void mjs_number_to_string(struct mjs* mjs) {
|
|||||||
mjs_val_t ret = MJS_UNDEFINED;
|
mjs_val_t ret = MJS_UNDEFINED;
|
||||||
mjs_val_t base_v = MJS_UNDEFINED;
|
mjs_val_t base_v = MJS_UNDEFINED;
|
||||||
int32_t base = 10;
|
int32_t base = 10;
|
||||||
int32_t num;
|
double num;
|
||||||
|
|
||||||
/* get number from `this` */
|
/* get number from `this` */
|
||||||
if(!mjs_check_arg(mjs, -1 /*this*/, "this", MJS_TYPE_NUMBER, NULL)) {
|
if(!mjs_check_arg(mjs, -1 /*this*/, "this", MJS_TYPE_NUMBER, NULL)) {
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
num = mjs_get_int32(mjs, mjs->vals.this_obj);
|
num = mjs_get_double(mjs, mjs->vals.this_obj);
|
||||||
|
|
||||||
if(mjs_nargs(mjs) >= 1) {
|
if(mjs_nargs(mjs) >= 1) {
|
||||||
/* get base from arg 0 */
|
/* get base from arg 0 */
|
||||||
@@ -181,9 +183,20 @@ MJS_PRIVATE void mjs_number_to_string(struct mjs* mjs) {
|
|||||||
base = mjs_get_int(mjs, base_v);
|
base = mjs_get_int(mjs, base_v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(base != 10 || floor(num) == num) {
|
||||||
char tmp_str[] = "-2147483648";
|
char tmp_str[] = "-2147483648";
|
||||||
itoa(num, tmp_str, base);
|
itoa((int32_t)num, tmp_str, base);
|
||||||
ret = mjs_mk_string(mjs, tmp_str, ~0, true);
|
ret = mjs_mk_string(mjs, tmp_str, ~0, true);
|
||||||
|
} else {
|
||||||
|
char tmp_str[] = "2.22514337200000e-308";
|
||||||
|
snprintf(tmp_str, sizeof(tmp_str), "%.*g", DBL_DIG, num);
|
||||||
|
size_t len = strlen(tmp_str);
|
||||||
|
while(len && tmp_str[len - 1] == '0') {
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
tmp_str[len] = '\0';
|
||||||
|
ret = mjs_mk_string(mjs, tmp_str, ~0, true);
|
||||||
|
}
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
mjs_return(mjs, ret);
|
mjs_return(mjs, ret);
|
||||||
|
|||||||
Reference in New Issue
Block a user