### Example rBoot Installation Command Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp8266/rboot/rboot/readme.txt This command flashes rBoot to the first sector of the flash, followed by two user ROMs for an 8Mbit flash system. Ensure flash size and other options like '-fm dio' are set correctly for your device. ```bash esptool.py -fs 8m 0x0000 rboot.bin 0x2000 user1.bin 0x82000 user2.bin ``` -------------------------------- ### Initializing mjs_create_opts Source: https://github.com/cesanta/mjs/blob/master/_autodocs/configuration.md Example of initializing the mjs_create_opts structure and creating an mJS instance with default bytecode. ```c struct mjs_create_opts opts = {0}; opts.code = NULL; /* Use default bytecode */ struct mjs *mjs = mjs_create_opt(opts); ``` -------------------------------- ### Compile and Run Embedding Example Source: https://github.com/cesanta/mjs/blob/master/README.md Command to compile the C embedding example and run the resulting executable. ```bash $ cc main.c mjs.c -o /tmp/x && /tmp/x ``` -------------------------------- ### Main Function Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Entry point for the main C function. Contains setup and calls to other functions. ```assembly 4000114c <_s_ets_main_c>: ffca90 excw 4000114f: 3f .byte 0x3f 40001150: 1d18 l32i.n a1, a13, 4 40001152: 904000 addx2 a4, a0, a0 40001155: 001d mov.n a1, a0 40001157: ca9c40 depbits a4, a12, 12, 10 4000115a: ff .byte 0xff 4000115b: 3f .byte 0x3f ``` -------------------------------- ### Example of Partial and Full GC Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/garbage-collection.md Demonstrates executing code, performing partial GC, and then full GC. Ensure MJS is initialized and destroyed properly. ```c #include "mjs.h" int main(void) { struct mjs *mjs = mjs_create(); // Execute code that creates many temporary objects mjs_exec(mjs, "for (let i = 0; i < 1000; i++) { let obj = {x: i}; }", NULL); // Perform partial garbage collection mjs_gc(mjs, 0); printf("After partial GC\n"); // Perform full garbage collection with heap trimming mjs_gc(mjs, 1); printf("After full GC\n"); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Example C Struct Descriptor Source: https://github.com/cesanta/mjs/blob/master/_autodocs/types.md An example demonstrating how to define a descriptor array for a C struct using mjs_c_struct_member. The array must be NULL-terminated. ```c struct Person { int age; const char *name; double height; bool active; }; static const struct mjs_c_struct_member person_descriptor[] = { {"age", offsetof(struct Person, age), MJS_STRUCT_FIELD_TYPE_INT, NULL}, {"name", offsetof(struct Person, name), MJS_STRUCT_FIELD_TYPE_CHAR_PTR, NULL}, {"height", offsetof(struct Person, height), MJS_STRUCT_FIELD_TYPE_DOUBLE, NULL}, {"active", offsetof(struct Person, active), MJS_STRUCT_FIELD_TYPE_BOOL, NULL}, {NULL, 0, MJS_STRUCT_FIELD_TYPE_INVALID, NULL}, /* Terminator */ }; ``` -------------------------------- ### Example: Enabling and Using JSC File Generation Source: https://github.com/cesanta/mjs/blob/master/_autodocs/configuration.md Demonstrates enabling JSC file generation and executing a script. Subsequent executions of the same script will use the generated .jsc file for faster loading. ```c struct mjs *mjs = mjs_create(); /* Enable JSC file generation */ mjs_set_generate_jsc(mjs, 1); /* Execute script - will generate .jsc file */ mjs_exec_file(mjs, "script.js", NULL); /* Next time script.js is executed, the faster .jsc is used */ ``` -------------------------------- ### Example: Custom Static FFI Resolver for Embedded Systems Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Illustrates setting a custom static resolver function for FFI on embedded systems. This approach avoids dynamic linking and allows lookup of functions from a predefined table. ```c void *static_dlsym(void *handle, const char *name) { (void)handle; if (strcmp(name, "my_function") == 0) { return (void *)my_function; } if (strcmp(name, "another_function") == 0) { return (void *)another_function; } return NULL; } int main(void) { struct mjs *mjs = mjs_create(); mjs_set_ffi_resolver(mjs, static_dlsym); // JavaScript can now call my_function and another_function const char *code = "let f = ffi('void my_function(int)'); f(42);"; mjs_exec(mjs, code, NULL); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Example: Floating Point Operations with FFI Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Shows how to call C functions that perform floating-point calculations (`sqrt`, `pow`) from JavaScript. The `double` return and parameter types are directly supported. ```javascript let sqrt = ffi('double sqrt(double)'); let pow = ffi('double pow(double, double)'); print(sqrt(16)); // Prints: 4 print(pow(2, 8)); // Prints: 256 ``` -------------------------------- ### Example: Using dlsym as FFI Resolver Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Demonstrates how to set the standard POSIX `dlsym` function as the FFI symbol resolver. This allows JavaScript to call C functions like `sin` directly after setting up the resolver. ```c #include "mjs.h" #include void *my_dlsym(void *handle, const char *name) { // Use dlsym to resolve the symbol return dlsym(RTLD_DEFAULT, name); } int main(void) { struct mjs *mjs = mjs_create(); // Set the resolver mjs_set_ffi_resolver(mjs, my_dlsym); // Now JavaScript can use ffi() to call C functions const char *code = "let sin_func = ffi('double sin(double)'); " "print(sin_func(1.0));"; mjs_exec(mjs, code, NULL); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Example: Function with Multiple Arguments using FFI Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Demonstrates calling a C function that accepts multiple arguments (`max`) from JavaScript. The FFI signature string specifies each argument type, and they are passed as separate arguments in the JavaScript call. ```javascript let max_func = ffi('int max(int, int)'); print(max_func(10, 20)); // Prints: 20 ``` -------------------------------- ### Garbage Collection Strategy Example Source: https://github.com/cesanta/mjs/blob/master/_autodocs/configuration.md Illustrates a typical strategy for memory management using MJS garbage collection. It combines periodic partial collections with a final full collection. ```c while (processing) { process_item(); if (item_count % 1000 == 0) { mjs_gc(mjs, 0); /* Lightweight collection every 1000 items */ } } mjs_gc(mjs, 1); /* Final cleanup */ ``` -------------------------------- ### Example: Convert C struct Person to JavaScript Object Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/objects.md Demonstrates how to define a descriptor for a C struct and use mjs_struct_to_obj to create a corresponding JavaScript object. ```c #include #include "mjs.h" struct Person { int age; const char *name; double height; }; static const struct mjs_c_struct_member person_descriptor[] = { {"age", offsetof(struct Person, age), MJS_STRUCT_FIELD_TYPE_INT, NULL}, {"name", offsetof(struct Person, name), MJS_STRUCT_FIELD_TYPE_CHAR_PTR, NULL}, {"height", offsetof(struct Person, height), MJS_STRUCT_FIELD_TYPE_DOUBLE, NULL}, {NULL, 0, MJS_STRUCT_FIELD_TYPE_INVALID, NULL}, }; int main(void) { struct mjs *mjs = mjs_create(); struct Person alice = {30, "Alice", 5.8}; mjs_val_t obj = mjs_struct_to_obj(mjs, &alice, person_descriptor); // obj now has properties: obj.age == 30, obj.name == "Alice", obj.height == 5.8 mjs_destroy(mjs); return 0; } ``` -------------------------------- ### MJS VM Bytecode Dump Source: https://github.com/cesanta/mjs/blob/master/README.md Example output of the MJS VM bytecode dump for the expression '2 + 2'. Shows stack operations and exit codes. ```text ------- MJS VM DUMP BEGIN DATA_STACK (0 elems): CALL_STACK (0 elems): SCOPES (1 elems): [] LOOP_OFFSETS (0 elems): CODE: 0 BCODE_HDR [] size:28 21 PUSH_INT 2 23 PUSH_INT 2 25 EXPR + 27 EXIT 28 NOP ------- MJS VM DUMP END ``` -------------------------------- ### Error Handling Example in mJS Source: https://github.com/cesanta/mjs/blob/master/_autodocs/types.md Demonstrates typical usage for checking and reporting errors returned by mJS execution functions. It shows how to check the return value of mjs_exec() and print formatted error messages. ```c mjs_err_t err = mjs_exec(mjs, "let x = 1/0;", NULL); if (err != MJS_OK) { printf("Error: %s\n", mjs_strerror(mjs, err)); mjs_print_error(mjs, stderr, "Execution failed", 1); } ``` -------------------------------- ### Example: Integer Arithmetic with FFI Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Demonstrates calling a C function that performs integer arithmetic (`abs`) from JavaScript. The `int` return type and parameter type are directly supported by FFI. ```javascript let abs = ffi('int abs(int)'); print(abs(-42)); // Prints: 42 ``` -------------------------------- ### ESP31B ROM strstr Function - Part 2 Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt This snippet continues the strstr function implementation, handling entry points and initial register setup for string comparison. ```assembly 40006578: 004136 entry a1, 32 4000657b: e88b81 l32r a8, 400007a8 <_c_0x00ff0000+0x10> 4000657e: ffaf92 movi a9, -1 40006581: e88871 l32r a7, 400007a4 <_c_0x00ff0000+0xc> 40006584: 746030 extui a6, a3, 0, 8 40006587: 03a032 movi a3, 3 4000658a: 08f616 beqz a6, 4000661d 4000658d: 180327 bnone a3, a2, 400065a9 40006590: f03d nop.n 40006592: 0f8076 loop a0, 400065a5 40006595: 000232 l8ui a3, a2, 0 40006598: 0c8316 beqz a3, 40006664 4000659b: 791637 beq a6, a3, 40006618 4000659e: 221b addi.n a2, a2, 1 400065a0: 144020 extui a4, a2, 0, 2 400065a3: 248c beqz.n a4, 400065a9 400065a5: fff9c6 j 40006590 400065a8: 063d00 excw ``` -------------------------------- ### JavaScript Example: Calling C sin() Function Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Demonstrates calling a C math function (`sin`) from JavaScript using the `ffi()` built-in. The function signature is specified as a string, and the C return type `double` is mapped to a JavaScript number. ```javascript // Calling C sin() function let sin_func = ffi('double sin(double)'); print(sin_func(1.0)); // Prints: 0.841471 ``` -------------------------------- ### Configure FFI with Embedded Resolver Source: https://github.com/cesanta/mjs/blob/master/_autodocs/configuration.md Example of setting up a custom embedded resolver for FFI. This approach uses a hardcoded symbol table to map JavaScript function calls to specific C functions. ```c void *embedded_resolver(void *handle, const char *name) { (void)handle; /* Hardcoded symbol table */ if (strcmp(name, "device_init") == 0) return (void *)device_init; if (strcmp(name, "device_read") == 0) return (void *)device_read; if (strcmp(name, "device_write") == 0) return (void *)device_write; return NULL; /* Symbol not found */ } int main(void) { struct mjs *mjs = mjs_create(); mjs_set_ffi_resolver(mjs, embedded_resolver); const char *code = "let init = ffi('void device_init(void)'); init();"; mjs_exec(mjs, code, NULL); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### JavaScript Example: C Callback Function Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/ffi.md Shows how to pass a JavaScript function as a callback to a C function using FFI. The C function `timer` expects a callback with a specific signature, which is defined in JavaScript using `ffi()` and a `userdata` parameter. ```javascript /* JavaScript side */ let Timer = { set: ffi('void timer(int, void (*)(int, userdata), userdata)') }; Timer.set(200, function(t) { print('Time now: ', t); }, null); ``` -------------------------------- ### Initialize Flash Write for OTA Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp8266/rboot/rboot/readme-api.txt Call this function once before writing data to flash. It initializes the write process, specifying the starting address on the SPI flash. It returns a status structure that must be used in subsequent write operations. ```c rboot_write_status rboot_write_init(uint32 start_addr); ``` -------------------------------- ### Get byte value at string index Source: https://github.com/cesanta/mjs/blob/master/README.md The at method returns the numeric byte value at a specific index within a string. Example: 'abc'.at(0) === 0x61. ```javascript 'abc'.at(0); ``` -------------------------------- ### Find index of substring Source: https://github.com/cesanta/mjs/blob/master/README.md The indexOf method returns the starting index of the first occurrence of a substring within a string, or -1 if not found. Example: 'abc'.indexOf('bc') === 1. ```javascript 'abc'.indexOf(substr[, fromIndex]); ``` -------------------------------- ### Get character from ASCII code Source: https://github.com/cesanta/mjs/blob/master/README.md The chr function returns a single-byte string corresponding to the given ASCII integer code. Returns null for invalid input. Example: chr(0x61) === 'a'. ```javascript chr(n); ``` -------------------------------- ### ESP31B ROM Main Entry Point (_X_start) Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt The main entry point of the ESP31B ROM. It initializes the stack, BSS section, and then calls the user's main function. Includes a loop for BSS clearing and a call to _X_main. ```assembly 400007ac <_X_start>: 400007ac: 00a002 movi a0, 0 400007af: ffd411 l32r a1, 40000700 <_c_stack> 400007b2: ffd431 l32r a3, 40000704 <_c_stack+0x4> 400007b5: f03d nop.n 400007b7: 13e630 wsr.ps a3 400007ba: 002010 rsync 400007bd: ffd261 l32r a6, 40000708 <_c_bss_start> 400007c0: ffd371 l32r a7, 4000070c <_c_bss_end> 400007c3: 06b677 bgeu a6, a7, 400007cd <_X_start+0x21> 400007c6: 0609 s32i.n a0, a6, 0 400007c8: 664b addi.n a6, a6, 4 400007ca: f83677 bltu a6, a7, 400007c6 <_X_start+0x1a> 400007cd: 0098d5 call4 4000115c <_X_main> 400007d0: 120c movi.n a2, 1 400007d2: 005100 simcall 400007d5: 0041f0 break 1, 15 400007d8: fffd06 j 400007d0 <_X_start+0x24> 400007db: 413600 srli a3, a0, 6 400007de: d2f600 quos a15, a6, a0 400007e1: 2e .byte 0x2e 400007e2: 090c movi.n a9, 0 400007e4: ffcd61 l32r a6, 40000718 <_c_bss_end+0xc> 400007e7: ffce41 l32r a4, 40000720 <_c_bss_end+0x14> 400007ea: ffcc81 l32r a8, 4000071c <_c_bss_end+0x10> 400007ed: ffc8a1 l32r a10, 40000710 <_c_bss_end+0x4> ``` -------------------------------- ### Create and Execute an mjs Interpreter Source: https://github.com/cesanta/mjs/blob/master/_autodocs/README.md Demonstrates the basic steps to create an mjs interpreter instance, execute a JavaScript string, and handle potential errors. This is the entry point for using mjs. ```c #include "mjs.h" int main(void) { struct mjs *mjs = mjs_create(); /* Execute JavaScript */ mjs_val_t result; mjs_err_t err = mjs_exec(mjs, "1 + 2", &result); if (err == MJS_OK) { double value = mjs_get_double(mjs, result); printf("Result: %f\n", value); } else { mjs_print_error(mjs, stderr, "Error", 1); } mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Get Type of MJS Value Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/utilities.md Use mjs_typeof to get a string representation of an MJS value's type. This is useful for debugging and conditional logic based on value types. ```c #include "mjs.h" int main(void) { struct mjs *mjs = mjs_create(); mjs_val_t num = mjs_mk_number(mjs, 42); mjs_val_t str = mjs_mk_string(mjs, "hello", ~0, 1); mjs_val_t obj = mjs_mk_object(mjs); mjs_val_t arr = mjs_mk_array(mjs); printf("typeof 42: %s\n", mjs_typeof(num)); // "number" printf("typeof 'hello': %s\n", mjs_typeof(str)); // "string" printf("typeof {}: %s\n", mjs_typeof(obj)); // "object" printf("typeof []: %s\n", mjs_typeof(arr)); // "array" printf("typeof null: %s\n", mjs_typeof(MJS_NULL)); // "null" mjs_destroy(mjs); return 0; } ``` -------------------------------- ### ESP31B ROM _start Function Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Entry point of the ESP31B ROM. Initializes registers and sets up exception handling. ```assembly 40000000 <_start>: 40000000: 49c500 s32e a0, a5, -16 40000003: 49d510 s32e a1, a5, -12 40000006: 49e520 s32e a2, a5, -8 40000009: 49f530 s32e a3, a5, -4 4000000c: 003400 rfwo 4000000f: 412800 srli a2, a0, 8 40000012: 5138 l32i.n a3, a1, 20 40000014: 6148 l32i.n a4, a1, 24 40000016: 01d112 addmi a1, a1, 0x100 40000019: 13d100 wsr.excsave1 a0 4000001c: 034800 rsr.windowbase a0 4000001f: 4080f0 rotw -1 40000022: 03e620 rsr.ps a2 40000025: 343820 extui a3, a2, 8, 4 40000028: 303340 xor a3, a3, a4 4000002b: 000846 j 40000050 <_XX_ExcVec50> ``` -------------------------------- ### Get MJS Global Object Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/core.md Retrieves the global object of an MJS interpreter. This object holds all global variables and functions. ```c mjs_val_t mjs_get_global(struct mjs *mjs); ``` ```c struct mjs *mjs = mjs_create(); mjs_val_t global = mjs_get_global(mjs); // global now points to the global object ``` -------------------------------- ### ESP31B ROM: _c_0x00001800 Snippet Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Assembly code for the _c_0x00001800 function in ESP31B ROM. This snippet appears to be a setup routine. ```assembly 400030ec: 001800 movsp a0, a8 ``` -------------------------------- ### Read Configuration from JavaScript File Source: https://github.com/cesanta/mjs/blob/master/_autodocs/README.md Shows how to load configuration settings from a JavaScript file into a C struct. This allows for flexible configuration management using JavaScript. ```c struct Config { const char *name; int port; double timeout; }; int load_config(const char *js_file, struct Config *cfg) { struct mjs *mjs = mjs_create(); mjs_exec_file(mjs, js_file, NULL); mjs_val_t global = mjs_get_global(mjs); mjs_val_t config_obj = mjs_get(mjs, global, "config", ~0); cfg->name = mjs_get_cstring(mjs, &(mjs_val_t){mjs_get(mjs, config_obj, "name", ~0)}); cfg->port = mjs_get_int(mjs, mjs_get(mjs, config_obj, "port", ~0)); cfg->timeout = mjs_get_double(mjs, mjs_get(mjs, config_obj, "timeout", ~0)); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Create MJS Instance (Default) Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/core.md Creates a new MJS interpreter instance with default settings. Use this for basic MJS operations. Ensure to destroy the instance when done. ```c struct mjs *mjs_create(void); ``` ```c #include "mjs.h" int main(void) { struct mjs *mjs = mjs_create(); // Use mjs... mjs_destroy(mjs); return 0; } ``` -------------------------------- ### ESP31B ROM: _X_ets_install_external_printf Function Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Assembly code for `_X_ets_install_external_printf` in the ESP31B ROM, handling the installation of external printing functions. ```assembly 40002794 <_X_ets_install_external_printf>: 40002794: 004136 entry a1, 32 40002797: fe6e51 l32r a5, 40002150 <_st_0x3fffda9c> 4000279a: 5529 s32i.n a2, a5, 20 4000279c: 838c beqz.n a3, 400027a8 <_X_ets_install_external_printf+0x14> 4000279e: 20a330 or a10, a3, a3 400027a1: 000125 call8 400027b4 <_X_ets_install_putc2> 400027a4: 6549 s32i.n a4, a5, 24 400027a6: f01d retw.n 400027a8: fffaa1 l32r a10, 40002790 <_c_0x400027dc> 400027ab: 0000a5 call8 400027b4 <_X_ets_install_putc2> 400027ae: 6549 s32i.n a4, a5, 24 400027b0: f01d retw.n ``` -------------------------------- ### rboot_get_current_rom Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp8266/rboot/rboot/readme-api.txt Gets the currently selected boot ROM. This reflects the ROM that is currently running, assuming no changes were made since boot. ```APIDOC ## rboot_get_current_rom ### Description Retrieves the identifier of the currently executing boot ROM. ### Returns uint8 - The identifier of the current boot ROM. ``` -------------------------------- ### Build MJS Stand-alone Binary Source: https://github.com/cesanta/mjs/blob/master/README.md Command to build the stand-alone mJS binary using make. ```bash $ make ``` -------------------------------- ### Get rBoot Configuration Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp8266/rboot/rboot/readme-api.txt Retrieves the current rBoot configuration structure. This structure can be modified to change settings, including the ROM layout. ```c rboot_config rboot_get_config(); ``` -------------------------------- ### Get JavaScript Array Length Source: https://github.com/cesanta/mjs/blob/master/_autodocs/INDEX.md Retrieves the number of elements currently in a JavaScript array. This is a common operation when working with array data. ```c size_t len = mjs_array_length(mjs, arr); ``` -------------------------------- ### ESP31B ROM: UART Initialization and Boot Strap Logic Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt This snippet shows the initial UART setup and conditional branching logic within the ESP31B ROM boot process. It involves calling UART initialization functions and checking various system parameters. ```assembly 40001269 <_l_strap_init_uart0>: 40001269: 020da5 call8 40003344 <_X_uart_attach> 4000126c: 00a0a2 movi a10, 0 4000126f: 022c65 call8 40003534 <_X_uart_init> 40001272: 015125 call8 40002784 <_X_ets_install_uart_printf> 40001275: ba2c movi.n a10, 43 40001277: 0020c0 memw 4000127a: 8e2292 l32i a9, a2, 0x238 4000127d: 1099a0 and a9, a9, a10 40001280: fec992 addi a9, a9, -2 40001283: 25f916 beqz a9, 400014e6 <_l_strap_0x0x10> 40001286: 0020c0 memw 40001289: 8e22b2 l32i a11, a2, 0x238 4000128c: 54b0b0 extui a11, a11, 0, 6 4000128f: c0bb40 sub a11, a11, a4 40001292: 233b16 beqz a11, 400014c9 <_l_strap_0x0x11_loader> 40001295: 0020c0 memw 40001298: 8e22c2 l32i a12, a2, 0x238 4000129b: 10cca0 and a12, a12, a10 4000129e: fdccc2 addi a12, a12, -3 400012a1: 224c16 beqz a12, 400014c9 <_l_strap_0x0x11_loader> 400012a4: 0020c0 memw 400012a7: 8e22d2 l32i a13, a2, 0x238 400012aa: 340da7 bnone a13, a10, 400012e2 <_l_strap_0x0x00> 400012ad: 0020c0 memw 400012b0: 8e22e2 l32i a14, a2, 0x238 400012b3: 54e0e0 extui a14, a14, 0, 6 400012b6: c0ee50 sub a14, a14, a5 400012b9: 217e16 beqz a14, 400014d4 <_l_strap_0x0x01> 400012bc: 0020c0 memw 400012bf: 8e22f2 l32i a15, a2, 0x238 400012c2: 10ffa0 and a15, a15, a10 400012c5: ff0b addi.n a15, a15, -1 400012c7: 209f16 beqz a15, 400014d4 <_l_strap_0x0x01> 400012ca: 0020c0 memw 400012cd: 8e2282 l32i a8, a2, 0x238 400012d0: 548080 extui a8, a8, 0, 6 400012d3: 139866 bnei a8, 10, 400012ea <_l_boot> 400012d6: 04a0a2 movi a10, 4 400012d9: 03e965 call8 40005170 <_X_sip_init_attach> 400012dc: 000286 j 400012ea <_l_boot> 400012df: 000000 ill ``` -------------------------------- ### ESP31B ROM: _X_ets_install_uart_printf Function Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Assembly code for `_X_ets_install_uart_printf` in the ESP31B ROM, likely involved in installing UART printing functionality. ```assembly 40002784 <_X_ets_install_uart_printf>: 40002784: 004136 entry a1, 32 40002787: fffea1 l32r a10, 40002780 <_c_0x4000223c_ets_uart_putc> 4000278a: fffea5 call8 40002774 4000278d: f01d retw.n ``` -------------------------------- ### Extract Unsigned Integer Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Extracts a specified number of bits from a register, starting at a given bit position. Used for bit manipulation. ```assembly 75b080 extui a11, a8, 16, 8 74e880 extui a14, a8, 8, 8 756880 extui a6, a8, 24, 8 744040 extui a4, a4, 0, 8 ``` -------------------------------- ### Call C Functions from JavaScript using FFI Source: https://github.com/cesanta/mjs/blob/master/_autodocs/README.md Demonstrates how to enable JavaScript to call C functions by implementing a symbol resolver and using the `ffi` function in JavaScript. Requires `sqrt` and `sin` functions to be available. ```c /* Set up symbol resolver */ void *my_resolver(void *handle, const char *name) { if (strcmp(name, "sqrt") == 0) return (void *)sqrt; if (strcmp(name, "sin") == 0) return (void *)sin; return NULL; } int main(void) { struct mjs *mjs = mjs_create(); mjs_set_ffi_resolver(mjs, my_resolver); /* JavaScript can now call C functions */ mjs_exec(mjs, "let result = ffi('double sqrt(double)')(16); print(result);", NULL); mjs_destroy(mjs); return 0; } ``` -------------------------------- ### Extract substring from a string Source: https://github.com/cesanta/mjs/blob/master/README.md The slice method returns a portion of a string between two specified indices. Example: 'abcdef'.slice(1,3) === 'bc'. ```javascript 'some_string'.slice(start, end); ``` -------------------------------- ### Get the length of a JavaScript array Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/arrays.md Use `mjs_array_length` to retrieve the number of elements in a JavaScript array. It returns 0 if the provided value is not an array. ```c mjs_val_t arr = mjs_mk_array(mjs); mjs_array_push(mjs, arr, mjs_mk_number(mjs, 42)); unsigned long len = mjs_array_length(mjs, arr); printf("Length: %lu\n", len); // Prints: 1 ``` -------------------------------- ### Typical MJS Interpreter Lifecycle Source: https://github.com/cesanta/mjs/blob/master/_autodocs/types.md Demonstrates the standard lifecycle of creating, using, and destroying an MJS interpreter context. ```c /* Create */ struct mjs *mjs = mjs_create(); /* Use */ mjs_exec(mjs, "let x = 42;", NULL); /* Destroy */ mjs_destroy(mjs); ``` -------------------------------- ### Get Boolean Value - MJS C API Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/primitives.md Extracts a boolean value from an MJS value. Returns non-zero for true and zero for false. ```c int mjs_get_bool(struct mjs *mjs, mjs_val_t v); ``` ```c mjs_val_t bool_val = mjs_mk_boolean(mjs, 1); int result = mjs_get_bool(mjs, bool_val); // result == 1 ``` -------------------------------- ### Call Standard C Functions via FFI Source: https://github.com/cesanta/mjs/blob/master/README.md Demonstrates calling a standard C math function (sin) from mJS using the FFI. ```bash $ ./build/mjs -e 'ffi("double sin(double)")(1.23)' ``` -------------------------------- ### Check mjs_exec Return Value Source: https://github.com/cesanta/mjs/blob/master/_autodocs/README.md Always check the return value of mjs_exec for errors. Use mjs_strerror to get a human-readable error message. ```c mjs_err_t err = mjs_exec(mjs, code, NULL); if (err != MJS_OK) { fprintf(stderr, "Error: %s\n", mjs_strerror(mjs, err)); return err; } ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 40000857: entry a1, 64 ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 40000878: entry a1, 48 ``` -------------------------------- ### Syntax Error Handling Source: https://github.com/cesanta/mjs/blob/master/_autodocs/errors.md Detect and handle JavaScript syntax errors during script execution. Use mjs_print_error to get more details about the parsing issue. ```c mjs_err_t err = mjs_exec(mjs, "let x = ;", NULL); if (err == MJS_SYNTAX_ERROR) { printf("Syntax error in script\n"); mjs_print_error(mjs, stderr, "Parse error", 0); } ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 40000880: entry a1, 48 ``` -------------------------------- ### ESP31B ROM: _X_ets_install_putc2 Function Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Assembly code for `_X_ets_install_putc2` in the ESP31B ROM, likely another function for installing character output routines. ```assembly 400027b4 <_X_ets_install_putc2>: 400027b4: 004136 entry a1, 32 400027b7: fe6631 l32r a3, 40002150 <_st_0x3fffda9c> 400027ba: 4329 s32i.n a2, a3, 16 400027bc: f01d retw.n ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 40000885: entry a1, 48 ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 4000088a: entry a1, 16 ``` -------------------------------- ### Get MJS Error String Representation Source: https://github.com/cesanta/mjs/blob/master/_autodocs/api-reference/core.md Retrieves a human-readable string for a given MJS error code. Useful for displaying error messages to the user. ```c const char *mjs_strerror(struct mjs *mjs, enum mjs_err err); ``` ```c mjs_err_t err = MJS_TYPE_ERROR; printf("Error: %s\n", mjs_strerror(mjs, err)); ``` -------------------------------- ### ESP31B ROM: _X_FlashDwnLdStartMsgProc Snippet Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Assembly code for the _X_FlashDwnLdStartMsgProc function in ESP31B ROM. This function handles the start message processing for flash downloads. ```assembly 400030f0: 004136 entry a1, 32 400030f3: 011282 l16ui a8, a2, 2 400030f6: 05a0a2 movi a10, 5 400030f9: 05b826 beqi a8, 16, 40003102 <_X_FlashDwnLdStartMsgProc+0x12> 400030fc: 120c movi.n a2, 1 400030fe: 03a9 s32i.n a10, a3, 0 40003100: f01d retw.n 40003102: 0422b2 l32i a11, a2, 16 40003105: fff991 l32r a9, 400030ec <_c_0x00001800> 40003108: f039b7 bltu a9, a11, 400030fc <_X_FlashDwnLdStartMsgProc+0xc> 4000310b: 0264b2 s32i a11, a4, 8 4000310e: 0a0c movi.n a10, 0 40003110: 32c8 l32i.n a12, a2, 12 40003112: 22d8 l32i.n a13, a2, 8 40003114: 04d9 s32i.n a13, a4, 0 40003116: 14c9 s32i.n a12, a4, 4 40003118: 34a9 s32i.n a10, a4, 12 4000311a: 44a9 s32i.n a10, a4, 16 4000311c: 52b8 l32i.n a11, a2, 20 4000311e: 54b9 s32i.n a11, a4, 20 40003120: 0132a5 call8 4000444c 40003123: 54a8 l32i.n a10, a4, 20 40003125: 04b8 l32i.n a11, a4, 0 40003127: 0178e5 call8 400048b4 <_X_SPIEraseArea> 4000312a: 6a8c beqz.n a10, 40003134 <_X_FlashDwnLdStartMsgProc+0x44> 4000312c: 120c movi.n a2, 1 4000312e: 6e0c movi.n a14, 6 40003130: 03e9 s32i.n a14, a3, 0 40003132: f01d retw.n 40003134: 020c movi.n a2, 0 40003136: f01d retw.n 40003138: 001810 movsp a1, a8 ``` -------------------------------- ### Entry Point Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Marks an entry point for code execution with a specified stack size. ```assembly 400008bd: entry a1, 0x100 ``` -------------------------------- ### ESP31B ROM Assembly Snippet 6 Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Entry point and memory operations. ```assembly 400016e8: 00e136 entry a1, 112 400016eb: 1a2c movi.n a10, 33 400016ed: fe9081 l32r a8, 40001130 <_c_0x60003e00> 400016f0: 0020c0 memw ``` -------------------------------- ### Call function returning NUL-terminated string Source: https://github.com/cesanta/mjs/blob/master/_autodocs/types.md Illustrates calling a C function that returns a NUL-terminated string via the FFI. This example uses the MJS_FFI_CTYPE_CHAR_PTR type. ```javascript /* Call function returning NUL-terminated string */ let strdup = ffi('char *strdup(char *)'); strdup("hello"); // Uses MJS_FFI_CTYPE_CHAR_PTR ``` -------------------------------- ### ESP31B ROM Entry and Multiplication Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt Demonstrates the entry point for a routine and a 16-bit unsigned multiplication operation. ```assembly 40002100: 004136 entry a1, 32 c13230 mul16u a3, a2, a3 f4a030 extui a10, a3, 0, 16 fff165 call8 40002020 <_X_ets_memcmp+0x23c> 202aa0 or a2, a10, a10 007a16 beqz a10, 4000211a <_X_ets_memcmp+0x336> 20c330 or a12, a3, a3 0b0c movi.n a11, 0 ffc9e5 call8 40001db4 <_X_ets_memset> 4000211a: f01d retw.n ``` -------------------------------- ### ESP31B ROM: Entry Point and Memory Initialization Source: https://github.com/cesanta/mjs/blob/master/src/common/platforms/esp31/rom/ESP31B_ROM.txt This snippet defines an entry point and performs memory initialization in the ESP31B ROM assembly. It sets up registers and stores values into memory locations. ```assembly 40002324: 00e136 entry a1, 112 40002327: 106162 s32i a6, a1, 64 4000232a: 04dd mov.n a13, a4 4000232c: 05cd mov.n a12, a5 4000232e: 000352 l8ui a5, a3, 0 40002331: 431b addi.n a4, a3, 1 40002333: 42b516 beqz a5, 40002762 <_X_ets_unk225c+0x506> 40002336: 030c movi.n a3, 0 40002338: a1d9 s32i.n a13, a1, 40 4000233a: 91c9 s32i.n a12, a1, 36 4000233c: 080c movi.n a8, 0 4000233e: 060c movi.n a6, 0 40002340: 090c movi.n a9, 0 40002342: 116192 s32i a9, a1, 68 40002345: d169 s32i.n a6, a1, 52 40002347: e189 s32i.n a8, a1, 56 40002349: 016d mov.n a6, a1 4000234b: 582c movi.n a8, 37 4000234d: 1b1587 beq a5, a8, 4000236c <_X_ets_unk225c+0x110> ```