From 631e9f9f809335779be8bd159d19db4d87e4516c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Lang=C3=B3?= Date: Tue, 30 May 2017 09:59:49 +0200 Subject: [PATCH] Update the API examples to use print from extensions (#1846) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com --- docs/03.API-EXAMPLE.md | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/docs/03.API-EXAMPLE.md b/docs/03.API-EXAMPLE.md index 91662ce08..d8499fd3f 100644 --- a/docs/03.API-EXAMPLE.md +++ b/docs/03.API-EXAMPLE.md @@ -11,7 +11,7 @@ This guide is intended to introduce you to JerryScript embedding API through cre int main (int argc, char *argv[]) { - const jerry_char_t script[] = "print ('Hello, World!');"; + const jerry_char_t script[] = "var str = 'Hello, World!';"; size_t script_size = strlen ((const char *) script); bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY); @@ -20,11 +20,7 @@ main (int argc, char *argv[]) } ``` -The application will generate the following output: - -```bash -Hello, World! -``` +The application will return with zero exit code. ## Step 2. Split engine initialization and script execution @@ -39,6 +35,7 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se ```c #include #include "jerryscript.h" +#include "jerryscript-ext/handler.h" int main (int argc, char *argv[]) @@ -49,6 +46,10 @@ main (int argc, char *argv[]) /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + /* Setup Global scope code */ jerry_value_t parsed_code = jerry_parse (script, script_size, false); @@ -78,6 +79,7 @@ Our code is more complex now, but it introduces possibilities to interact with J ```c #include #include "jerryscript.h" +#include "jerryscript-ext/handler.h" int main (int argc, char *argv[]) @@ -88,6 +90,10 @@ main (int argc, char *argv[]) /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + jerry_value_t eval_ret; /* Evaluate script1 */ @@ -120,6 +126,7 @@ This way, we execute two independent script parts in one execution environment. ```c #include #include "jerryscript.h" +#include "jerryscript-ext/handler.h" int main (int argc, char *argv[]) { @@ -129,6 +136,10 @@ main (int argc, char *argv[]) { /* Initializing JavaScript environment */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + /* Getting pointer to the Global object */ jerry_value_t global_object = jerry_get_global_object (); @@ -176,7 +187,6 @@ The following example function will output a JavaScript value: #include #include #include - #include "jerryscript.h" static void @@ -246,8 +256,8 @@ Shell operation can be described with the following loop: #include #include #include - #include "jerryscript.h" +#include "jerryscript-ext/handler.h" static void print_value (const jerry_value_t); @@ -259,6 +269,10 @@ main (int argc, char *argv[]) /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + while (!is_done) { char cmd[256] = {}; @@ -325,6 +339,7 @@ In this example we demonstrate how to use native function and structures in Java ```c #include #include "jerryscript.h" +#include "jerryscript-ext/handler.h" struct my_struct { @@ -349,6 +364,10 @@ main (int argc, char *argv[]) /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + /* Do something with the native object */ my_struct.msg = "Hello World"; @@ -411,6 +430,7 @@ Here we create a JS Object with `jerry_eval`, then extend it with a native funct ```c #include #include "jerryscript.h" +#include "jerryscript-ext/handler.h" /** * Add param to 'this.x' @@ -451,6 +471,10 @@ main (int argc, char *argv[]) /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); + /* Register 'print' function from the extensions */ + jerryx_handler_register_global ((const jerry_char_t *) "print", + jerryx_handler_print); + /* Create a JS object */ const jerry_char_t my_js_object[] = " \ MyObject = \