mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
examples/javascript: adapt to updated jerryscript package
This commit is contained in:
parent
9ba90e2ae6
commit
118a88e60b
@ -24,11 +24,24 @@ CFLAGS += -DDEVELHELP
|
|||||||
# Set stack size to something (conservatively) enormous
|
# Set stack size to something (conservatively) enormous
|
||||||
CFLAGS += -DTHREAD_STACKSIZE_MAIN=9092
|
CFLAGS += -DTHREAD_STACKSIZE_MAIN=9092
|
||||||
|
|
||||||
# Add the shell and some shell commands
|
|
||||||
USEMODULE += shell
|
|
||||||
USEMODULE += shell_commands
|
|
||||||
|
|
||||||
# Add the package for Jerryscript
|
# Add the package for Jerryscript
|
||||||
USEPKG += jerryscript
|
USEPKG += jerryscript
|
||||||
|
|
||||||
include $(CURDIR)/../../Makefile.include
|
include $(CURDIR)/../../Makefile.include
|
||||||
|
|
||||||
|
JS_PATH := $(BINDIR)/js/$(MODULE)
|
||||||
|
# add directory of generated *.js.h files to include path
|
||||||
|
CFLAGS += -I$(JS_PATH)
|
||||||
|
|
||||||
|
# generate .js.h header files of .js files
|
||||||
|
JS = $(wildcard *.js)
|
||||||
|
JS_H := $(JS:%.js=$(JS_PATH)/%.js.h)
|
||||||
|
|
||||||
|
$(JS_PATH)/:
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
$(JS_H): | $(JS_PATH)/
|
||||||
|
$(JS_H): $(JS_PATH)/%.js.h: %.js
|
||||||
|
xxd -i $< | sed 's/^unsigned/const unsigned/g' > $@
|
||||||
|
|
||||||
|
$(RIOTBUILD_CONFIG_HEADER_C): $(JS_H)
|
||||||
|
@ -1,39 +1,23 @@
|
|||||||
### About
|
### About
|
||||||
|
|
||||||
This example enables to execute arbitrary Javascript directly from the command line on the RIOT shell.
|
This example shows how to write IoT applications using javascript.
|
||||||
|
|
||||||
### Acknowledgement
|
### Acknowledgement
|
||||||
This example is based on [Jerryscript](https://github.com/jerryscript-project/jerryscript) which does all the heavy lifting, providing full ECMAScript 5.1 profile on your IoT device (kudos guys!).
|
This example is based on [Jerryscript](https://github.com/jerryscript-project/jerryscript) which does all the heavy lifting, providing full ECMAScript 5.1 profile on your IoT device (kudos guys!).
|
||||||
|
|
||||||
### Caveats
|
### Caveats
|
||||||
|
|
||||||
- On your local board: best used with a serial communication program such [minicom](https://help.ubuntu.com/community/Minicom) instead of PyTerm (see why below).
|
- currently, the only actual function available is "print"
|
||||||
|
|
||||||
- On a testbed: you can try it on [IoT-lab](https://www.iot-lab.info). Tested and works so far on [IoT-lab M3](https://www.iot-lab.info/hardware/m3/), upload and flash the .elf, ssh into the node, and script away!
|
### How to use
|
||||||
|
|
||||||
- Except in the `print` instruction in your script, you have to replace single brackets `'` with `\'`.
|
Just put your javascript code in "main.js" (check the example). The file will
|
||||||
|
automatically be included when compiling the application, which will execute
|
||||||
|
the file right after booting.
|
||||||
|
|
||||||
- Expect some issues with PyTerm which interprets by default the first `;` as the end of the script command. Furthermore, if the script is long, PyTerm seems to get confused (hence the recommended use of minicom). To start playing with PyTerm, first edit `;` line 256 of RIOT/dist/tools/pyterm/pyterm
|
### How to run
|
||||||
|
|
||||||
### How to build
|
Type `make flash term`.
|
||||||
|
|
||||||
Type `make flash`. Then use your preferred serial communication tool to land in the RIOT shell.
|
Note: you may have to press `RESET` on the board (after the flash) if the board
|
||||||
Note: you may have to type `reboot` or to press `RESET` on the board (after the flash).
|
reboots faster than the terminal program can start..
|
||||||
|
|
||||||
### Running the example
|
|
||||||
|
|
||||||
In the RIOT shell, `help` will provide the list of available commands.
|
|
||||||
|
|
||||||
The `script` command will run the test script code that you input in the command line.
|
|
||||||
Some examples of scripts you can try:
|
|
||||||
```
|
|
||||||
script print ('hello');
|
|
||||||
```
|
|
||||||
```
|
|
||||||
script var x = Math.sqrt (64); var txt = \'\'; while (x>1) { txt += --x + \'\\n\';} print (txt);
|
|
||||||
```
|
|
||||||
```
|
|
||||||
script var person = { fname:\'John\', lname:\'Doe\', age:25 }; var text = \'\'; var x; for (x in person) { text += person[x] + \'\\n\'; } print (text);
|
|
||||||
```
|
|
||||||
|
|
||||||
Remark: outside of the print command, you may have to replace single brackets ' with \'.
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2017 Inria
|
* Copyright (C) 2017 Inria
|
||||||
|
* 2017 Kaspar Schleiser <kaspar@schleiser.de>
|
||||||
*
|
*
|
||||||
* This file is subject to the terms and conditions of the GNU Lesser
|
* This file is subject to the terms and conditions of the GNU Lesser
|
||||||
* General Public License v2.1. See the file LICENSE in the top level
|
* General Public License v2.1. See the file LICENSE in the top level
|
||||||
@ -11,58 +12,64 @@
|
|||||||
* @{
|
* @{
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
* @brief Showing an example of scripting (javascript) from command line
|
* @brief Example of how to use javascript on RIOT
|
||||||
*
|
*
|
||||||
* @author Emmanuel Baccelli <emmanuel.baccelli@inria.fr>
|
* @author Emmanuel Baccelli <emmanuel.baccelli@inria.fr>
|
||||||
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||||
*
|
*
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "shell.h"
|
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
int shell_script(int argc, char **argv)
|
/* include header generated from main.js */
|
||||||
|
#include "main.js.h"
|
||||||
|
|
||||||
|
int js_run(const jerry_char_t *script, size_t script_size)
|
||||||
{
|
{
|
||||||
if (argc < 2) {
|
jerry_value_t ret_value;
|
||||||
puts("Usage: script <your script here!> \n"
|
|
||||||
"For example, try: \n"
|
/* Initialize engine */
|
||||||
"script var txt = \\'\\'; txt += Math.PI; print (\\'Pi=\\'+txt); \n"
|
jerry_init(JERRY_INIT_EMPTY);
|
||||||
"Note: you need to substitute usual quotes with \\' \n");
|
|
||||||
return -1;
|
/* Register the print function in the global object. */
|
||||||
|
jerryx_handler_register_global((const jerry_char_t *) "print", jerryx_handler_print);
|
||||||
|
|
||||||
|
/* Setup Global scope code */
|
||||||
|
ret_value = jerry_parse(script, script_size, false);
|
||||||
|
|
||||||
|
if (!jerry_value_has_error_flag(ret_value)) {
|
||||||
|
/* Execute the parsed source code in the Global scope */
|
||||||
|
ret_value = jerry_run(ret_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
jerry_char_t script[(2 * SHELL_DEFAULT_BUFSIZE + 1)];
|
int res = 0;
|
||||||
*script = '\0';
|
|
||||||
for(int i = 1; i < argc; i++) {
|
if (jerry_value_has_error_flag(ret_value)) {
|
||||||
if (i>1) {
|
printf("js_run(): Script Error!");
|
||||||
strcat((char *)script, " ");
|
res = -1;
|
||||||
}
|
|
||||||
strcat((char *)script, argv[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t script_size = strlen((char *) script);
|
jerry_release_value(ret_value);
|
||||||
printf("Executing script: [%s]\n\n", script);
|
|
||||||
bool ret_value = jerry_run_simple(script, script_size, JERRY_INIT_EMPTY);
|
|
||||||
|
|
||||||
return (ret_value != 0);
|
/* Cleanup engine */
|
||||||
|
jerry_cleanup();
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const shell_command_t shell_commands[] = {
|
|
||||||
{ "script", "Shell scripting ", shell_script },
|
|
||||||
{ NULL, NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
|
printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
|
||||||
printf("This board features a(n) %s MCU.\n", RIOT_MCU);
|
printf("This board features a(n) %s MCU.\n", RIOT_MCU);
|
||||||
|
|
||||||
/* start the shell */
|
printf("Executing main.js:\n");
|
||||||
char line_buf[2 * SHELL_DEFAULT_BUFSIZE];
|
|
||||||
/* for longer script support shell buffer should be bigger */
|
js_run(main_js, main_js_len);
|
||||||
shell_run(shell_commands, line_buf, sizeof(line_buf));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
1
examples/javascript/main.js
Normal file
1
examples/javascript/main.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
print("Hello from JerryScript!");
|
Loading…
Reference in New Issue
Block a user