Update the API examples to use print from extensions (#1846)

JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
This commit is contained in:
László Langó
2017-05-30 09:59:49 +02:00
committed by GitHub
parent 783dcec6df
commit 631e9f9f80
+32 -8
View File
@@ -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 <string.h>
#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 <string.h>
#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 <string.h>
#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerryscript.h"
static void
@@ -246,8 +256,8 @@ Shell operation can be described with the following loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <string.h>
#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 <string.h>
#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 = \