unleashed-firmware/lib/mjs/mjs_string_public.h
Nikolay Minaylov 0154018363
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor

Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 15:54:32 +07:00

78 lines
2.5 KiB
C

/*
* Copyright (c) 2016 Cesanta Software Limited
* All rights reserved
*/
#ifndef MJS_STRING_PUBLIC_H_
#define MJS_STRING_PUBLIC_H_
#include "mjs_core_public.h"
#define MJS_STRING_LITERAL_MAX_LEN 128
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*
* Creates a string primitive value.
* `str` must point to the utf8 string of length `len`.
* If `len` is ~0, `str` is assumed to be NUL-terminated and `strlen(str)` is
* used.
*
* If `copy` is non-zero, the string data is copied and owned by the GC. The
* caller can free the string data afterwards. Otherwise (`copy` is zero), the
* caller owns the string data, and is responsible for not freeing it while it
* is used.
*/
mjs_val_t mjs_mk_string(struct mjs* mjs, const char* str, size_t len, int copy);
/* Returns true if given value is a primitive string value */
int mjs_is_string(mjs_val_t v);
/*
* Returns a pointer to the string stored in `mjs_val_t`.
*
* String length returned in `len`, which is allowed to be NULL. Returns NULL
* if the value is not a string.
*
* JS strings can contain embedded NUL chars and may or may not be NUL
* terminated.
*
* CAUTION: creating new JavaScript object, array, or string may kick in a
* garbage collector, which in turn may relocate string data and invalidate
* pointer returned by `mjs_get_string()`.
*
* Short JS strings are embedded inside the `mjs_val_t` value itself. This
* is why a pointer to a `mjs_val_t` is required. It also means that the string
* data will become invalid once that `mjs_val_t` value goes out of scope.
*/
const char* mjs_get_string(struct mjs* mjs, mjs_val_t* v, size_t* len);
/*
* Returns a pointer to the string stored in `mjs_val_t`.
*
* Returns NULL if the value is not a string or if the string is not compatible
* with a C string.
*
* C compatible strings contain exactly one NUL char, in terminal position.
*
* All strings owned by the MJS engine (see `mjs_mk_string()`) are guaranteed to
* be NUL terminated. Out of these, those that don't include embedded NUL chars
* are guaranteed to be C compatible.
*/
const char* mjs_get_cstring(struct mjs* mjs, mjs_val_t* v);
/*
* Returns the standard strcmp comparison code after comparing a JS string a
* with a possibly non null-terminated string b. NOTE: the strings are equal
* only if their length is equal, i.e. the len field doesn't imply strncmp
* behaviour.
*/
int mjs_strcmp(struct mjs* mjs, mjs_val_t* a, const char* b, size_t len);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* MJS_STRING_PUBLIC_H_ */