Skip to content

Commit

Permalink
JS: Add parse_int(), to_upper_case(), to_lower_case() (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL authored Mar 17, 2024
2 parents 6f9e50d + 7f07417 commit d63a07f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions applications/system/js_app/examples/apps/Scripts/stringutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let sampleText = "Hello, World!";

let lengthOfText = "Length of text: " + to_string(sampleText.length);
print(lengthOfText);

let start = 7;
let end = 12;
let substringResult = sampleText.slice(start, end);
print(substringResult);

let searchStr = "World";
let result2 = to_string(sampleText.indexOf(searchStr));
print(result2);

let upperCaseText = "Text in upper case: " + to_upper_case(sampleText);
print(upperCaseText);

let lowerCaseText = "Text in lower case: " + to_lower_case(sampleText);
print(lowerCaseText);
85 changes: 85 additions & 0 deletions applications/system/js_app/js_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,88 @@ static void js_global_to_hex_string(struct mjs* mjs) {
mjs_return(mjs, ret);
}

static void js_parse_int(struct mjs* mjs) {
mjs_val_t arg = mjs_arg(mjs, 0);
if(!mjs_is_string(arg)) {
mjs_return(mjs, mjs_mk_number(mjs, 0));
return;
}
size_t str_len = 0;
const char* str = mjs_get_string(mjs, &arg, &str_len);
if((str_len == 0) || (str == NULL)) {
mjs_return(mjs, mjs_mk_number(mjs, 0));
return;
}

int32_t num = 0;
int32_t sign = 1;
size_t i = 0;

if(str[0] == '-') {
sign = -1;
i = 1;
} else if(str[0] == '+') {
i = 1;
}

for(; i < str_len; i++) {
if(str[i] >= '0' && str[i] <= '9') {
num = num * 10 + (str[i] - '0');
} else {
break;
}
}
num *= sign;

mjs_return(mjs, mjs_mk_number(mjs, num));
}

static void js_to_upper_case(struct mjs* mjs) {
mjs_val_t arg0 = mjs_arg(mjs, 0);

size_t str_len;
const char* str = NULL;
if(mjs_is_string(arg0)) {
str = mjs_get_string(mjs, &arg0, &str_len);
}
if(!str) {
mjs_return(mjs, MJS_UNDEFINED);
return;
}

char* upperStr = strdup(str);
for(size_t i = 0; i < str_len; i++) {
upperStr[i] = toupper(upperStr[i]);
}

mjs_val_t resultStr = mjs_mk_string(mjs, upperStr, ~0, true);
free(upperStr);
mjs_return(mjs, resultStr);
}

static void js_to_lower_case(struct mjs* mjs) {
mjs_val_t arg0 = mjs_arg(mjs, 0);

size_t str_len;
const char* str = NULL;
if(mjs_is_string(arg0)) {
str = mjs_get_string(mjs, &arg0, &str_len);
}
if(!str) {
mjs_return(mjs, MJS_UNDEFINED);
return;
}

char* lowerStr = strdup(str);
for(size_t i = 0; i < str_len; i++) {
lowerStr[i] = tolower(lowerStr[i]);
}

mjs_val_t resultStr = mjs_mk_string(mjs, lowerStr, ~0, true);
free(lowerStr);
mjs_return(mjs, resultStr);
}

#ifdef JS_DEBUG
static void js_dump_write_callback(void* ctx, const char* format, ...) {
File* file = ctx;
Expand Down Expand Up @@ -243,6 +325,9 @@ static int32_t js_thread(void* arg) {
mjs_set(mjs, global, "to_hex_string", ~0, MJS_MK_FN(js_global_to_hex_string));
mjs_set(mjs, global, "ffi_address", ~0, MJS_MK_FN(js_ffi_address));
mjs_set(mjs, global, "require", ~0, MJS_MK_FN(js_require));
mjs_set(mjs, global, "parse_int", ~0, MJS_MK_FN(js_parse_int));
mjs_set(mjs, global, "to_upper_case", ~0, MJS_MK_FN(js_to_upper_case));
mjs_set(mjs, global, "to_lower_case", ~0, MJS_MK_FN(js_to_lower_case));

mjs_val_t console_obj = mjs_mk_object(mjs);
mjs_set(mjs, console_obj, "log", ~0, MJS_MK_FN(js_console_log));
Expand Down

0 comments on commit d63a07f

Please sign in to comment.