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:
+32
-8
@@ -11,7 +11,7 @@ This guide is intended to introduce you to JerryScript embedding API through cre
|
|||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
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);
|
size_t script_size = strlen ((const char *) script);
|
||||||
|
|
||||||
bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
|
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:
|
The application will return with zero exit code.
|
||||||
|
|
||||||
```bash
|
|
||||||
Hello, World!
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2. Split engine initialization and script execution
|
## 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
|
```c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
@@ -49,6 +46,10 @@ main (int argc, char *argv[])
|
|||||||
/* Initialize engine */
|
/* Initialize engine */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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 */
|
/* Setup Global scope code */
|
||||||
jerry_value_t parsed_code = jerry_parse (script, script_size, false);
|
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
|
```c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
@@ -88,6 +90,10 @@ main (int argc, char *argv[])
|
|||||||
/* Initialize engine */
|
/* Initialize engine */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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;
|
jerry_value_t eval_ret;
|
||||||
|
|
||||||
/* Evaluate script1 */
|
/* Evaluate script1 */
|
||||||
@@ -120,6 +126,7 @@ This way, we execute two independent script parts in one execution environment.
|
|||||||
```c
|
```c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[]) {
|
main (int argc, char *argv[]) {
|
||||||
@@ -129,6 +136,10 @@ main (int argc, char *argv[]) {
|
|||||||
/* Initializing JavaScript environment */
|
/* Initializing JavaScript environment */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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 */
|
/* Getting pointer to the Global object */
|
||||||
jerry_value_t global_object = jerry_get_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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -246,8 +256,8 @@ Shell operation can be described with the following loop:
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
static void print_value (const jerry_value_t);
|
static void print_value (const jerry_value_t);
|
||||||
|
|
||||||
@@ -259,6 +269,10 @@ main (int argc, char *argv[])
|
|||||||
/* Initialize engine */
|
/* Initialize engine */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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)
|
while (!is_done)
|
||||||
{
|
{
|
||||||
char cmd[256] = {};
|
char cmd[256] = {};
|
||||||
@@ -325,6 +339,7 @@ In this example we demonstrate how to use native function and structures in Java
|
|||||||
```c
|
```c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
struct my_struct
|
struct my_struct
|
||||||
{
|
{
|
||||||
@@ -349,6 +364,10 @@ main (int argc, char *argv[])
|
|||||||
/* Initialize engine */
|
/* Initialize engine */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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 */
|
/* Do something with the native object */
|
||||||
my_struct.msg = "Hello World";
|
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
|
```c
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jerryscript.h"
|
#include "jerryscript.h"
|
||||||
|
#include "jerryscript-ext/handler.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add param to 'this.x'
|
* Add param to 'this.x'
|
||||||
@@ -451,6 +471,10 @@ main (int argc, char *argv[])
|
|||||||
/* Initialize engine */
|
/* Initialize engine */
|
||||||
jerry_init (JERRY_INIT_EMPTY);
|
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 */
|
/* Create a JS object */
|
||||||
const jerry_char_t my_js_object[] = " \
|
const jerry_char_t my_js_object[] = " \
|
||||||
MyObject = \
|
MyObject = \
|
||||||
|
|||||||
Reference in New Issue
Block a user