1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 12:42:30 +04:00

merge mjs

This commit is contained in:
MX
2024-10-20 16:02:15 +03:00
parent 48acd9f075
commit 75625ffd5e
3 changed files with 53 additions and 0 deletions

View File

@@ -475,6 +475,22 @@ static int getprop_builtin_string(
return 0;
}
static int getprop_builtin_number(
struct mjs* mjs,
mjs_val_t val,
const char* name,
size_t name_len,
mjs_val_t* res) {
if(strcmp(name, "toString") == 0) {
*res = mjs_mk_foreign_func(mjs, (mjs_func_ptr_t)mjs_number_to_string);
return 1;
}
(void)val;
(void)name_len;
return 0;
}
static int getprop_builtin_array(
struct mjs* mjs,
mjs_val_t val,
@@ -589,6 +605,8 @@ static int getprop_builtin(struct mjs* mjs, mjs_val_t val, mjs_val_t name, mjs_v
} else if(s != NULL && n == 5 && strncmp(s, "apply", n) == 0) {
*res = mjs_mk_foreign_func(mjs, (mjs_func_ptr_t)mjs_apply_);
handled = 1;
} else if(mjs_is_number(val)) {
handled = getprop_builtin_number(mjs, val, s, n, res);
} else if(mjs_is_array(val)) {
handled = getprop_builtin_array(mjs, val, s, n, res);
} else if(mjs_is_foreign(val)) {

View File

@@ -6,6 +6,8 @@
#include "mjs_core.h"
#include "mjs_internal.h"
#include "mjs_primitive.h"
#include "mjs_string_public.h"
#include "mjs_util.h"
mjs_val_t mjs_mk_null(void) {
return MJS_NULL;
@@ -158,3 +160,31 @@ MJS_PRIVATE void mjs_op_isnan(struct mjs* mjs) {
mjs_return(mjs, ret);
}
MJS_PRIVATE void mjs_number_to_string(struct mjs* mjs) {
mjs_val_t ret = MJS_UNDEFINED;
mjs_val_t base_v = MJS_UNDEFINED;
int32_t base = 10;
int32_t num;
/* get number from `this` */
if(!mjs_check_arg(mjs, -1 /*this*/, "this", MJS_TYPE_NUMBER, NULL)) {
goto clean;
}
num = mjs_get_int32(mjs, mjs->vals.this_obj);
if(mjs_nargs(mjs) >= 1) {
/* get base from arg 0 */
if(!mjs_check_arg(mjs, 0, "base", MJS_TYPE_NUMBER, &base_v)) {
goto clean;
}
base = mjs_get_int(mjs, base_v);
}
char tmp_str[] = "-2147483648";
itoa(num, tmp_str, base);
ret = mjs_mk_string(mjs, tmp_str, ~0, true);
clean:
mjs_return(mjs, ret);
}

View File

@@ -34,6 +34,11 @@ MJS_PRIVATE void* get_ptr(mjs_val_t v);
*/
MJS_PRIVATE void mjs_op_isnan(struct mjs* mjs);
/*
* Implementation for JS Number.toString()
*/
MJS_PRIVATE void mjs_number_to_string(struct mjs* mjs);
#if defined(__cplusplus)
}
#endif /* __cplusplus */