Merge pull request #7794 from kaspar030/bump_jerryscript_version

pkg/jerryscript: bump jerryscript version
This commit is contained in:
Kaspar Schleiser 2017-11-07 14:31:03 +01:00 committed by GitHub
commit 72add88004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 62 deletions

View File

@ -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)

View File

@ -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 \'.

View File

@ -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;
} }

View File

@ -0,0 +1 @@
print("Hello from JerryScript!");

View File

@ -1,10 +1,12 @@
PKG_NAME=jerryscript PKG_NAME=jerryscript
PKG_URL=https://github.com/jerryscript-project/jerryscript.git PKG_URL=https://github.com/jerryscript-project/jerryscript.git
PKG_VERSION=56802c22a10b0d684ac93921ac28df891e320b4a PKG_VERSION=e62b5b601bc1caa3e4d8172824988536ed6138f3
PKG_LICENSE=Apache-2.0 PKG_LICENSE=Apache-2.0
.PHONY: all .PHONY: all
CFLAGS += -Wno-implicit-fallthrough
all: git-download all: git-download
@cp Makefile.jerryscript $(PKG_BUILDDIR)/Makefile @cp Makefile.jerryscript $(PKG_BUILDDIR)/Makefile
$(MAKE) -C $(PKG_BUILDDIR) $(MAKE) -C $(PKG_BUILDDIR)

View File

@ -1,3 +1,4 @@
ifneq (,$(filter jerryscript,$(USEPKG))) ifneq (,$(filter jerryscript,$(USEPKG)))
USEMODULE += jerryport-minimal USEMODULE += jerryport-minimal
USEMODULE += jerryscript-ext
endif endif

View File

@ -1 +1,2 @@
INCLUDES += -I$(PKGDIRBASE)/jerryscript/jerry-core/ INCLUDES += -I$(PKGDIRBASE)/jerryscript/jerry-core/include
INCLUDES += -I$(PKGDIRBASE)/jerryscript/jerry-ext/include

View File

@ -26,8 +26,9 @@ libjerry:
-DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \ -DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \
-DMEM_HEAP_SIZE_KB=$(JERRYHEAP) -DMEM_HEAP_SIZE_KB=$(JERRYHEAP)
make -C $(BUILD_DIR) jerry-core jerry-port-default-minimal make -C $(BUILD_DIR) jerry-core jerry-ext jerry-port-default-minimal
cp $(BUILD_DIR)/lib/libjerry-core.a $(BINDIR)/jerryscript.a cp $(BUILD_DIR)/lib/libjerry-core.a $(BINDIR)/jerryscript.a
cp $(BUILD_DIR)/lib/libjerry-ext.a $(BINDIR)/jerryscript-ext.a
cp $(BUILD_DIR)/lib/libjerry-port-default-minimal.a $(BINDIR)/jerryport-minimal.a cp $(BUILD_DIR)/lib/libjerry-port-default-minimal.a $(BINDIR)/jerryport-minimal.a
include $(RIOTBASE)/Makefile.base include $(RIOTBASE)/Makefile.base