Skip to content

Commit 631e9f9

Browse files
authored
Update the API examples to use print from extensions (#1846)
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 783dcec commit 631e9f9

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

docs/03.API-EXAMPLE.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This guide is intended to introduce you to JerryScript embedding API through cre
1111
int
1212
main (int argc, char *argv[])
1313
{
14-
const jerry_char_t script[] = "print ('Hello, World!');";
14+
const jerry_char_t script[] = "var str = 'Hello, World!';";
1515
size_t script_size = strlen ((const char *) script);
1616

1717
bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
@@ -20,11 +20,7 @@ main (int argc, char *argv[])
2020
}
2121
```
2222
23-
The application will generate the following output:
24-
25-
```bash
26-
Hello, World!
27-
```
23+
The application will return with zero exit code.
2824
2925
## Step 2. Split engine initialization and script execution
3026
@@ -39,6 +35,7 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se
3935
```c
4036
#include <string.h>
4137
#include "jerryscript.h"
38+
#include "jerryscript-ext/handler.h"
4239
4340
int
4441
main (int argc, char *argv[])
@@ -49,6 +46,10 @@ main (int argc, char *argv[])
4946
/* Initialize engine */
5047
jerry_init (JERRY_INIT_EMPTY);
5148
49+
/* Register 'print' function from the extensions */
50+
jerryx_handler_register_global ((const jerry_char_t *) "print",
51+
jerryx_handler_print);
52+
5253
/* Setup Global scope code */
5354
jerry_value_t parsed_code = jerry_parse (script, script_size, false);
5455
@@ -78,6 +79,7 @@ Our code is more complex now, but it introduces possibilities to interact with J
7879
```c
7980
#include <string.h>
8081
#include "jerryscript.h"
82+
#include "jerryscript-ext/handler.h"
8183

8284
int
8385
main (int argc, char *argv[])
@@ -88,6 +90,10 @@ main (int argc, char *argv[])
8890
/* Initialize engine */
8991
jerry_init (JERRY_INIT_EMPTY);
9092

93+
/* Register 'print' function from the extensions */
94+
jerryx_handler_register_global ((const jerry_char_t *) "print",
95+
jerryx_handler_print);
96+
9197
jerry_value_t eval_ret;
9298

9399
/* Evaluate script1 */
@@ -120,6 +126,7 @@ This way, we execute two independent script parts in one execution environment.
120126
```c
121127
#include <string.h>
122128
#include "jerryscript.h"
129+
#include "jerryscript-ext/handler.h"
123130
124131
int
125132
main (int argc, char *argv[]) {
@@ -129,6 +136,10 @@ main (int argc, char *argv[]) {
129136
/* Initializing JavaScript environment */
130137
jerry_init (JERRY_INIT_EMPTY);
131138
139+
/* Register 'print' function from the extensions */
140+
jerryx_handler_register_global ((const jerry_char_t *) "print",
141+
jerryx_handler_print);
142+
132143
/* Getting pointer to the Global object */
133144
jerry_value_t global_object = jerry_get_global_object ();
134145
@@ -176,7 +187,6 @@ The following example function will output a JavaScript value:
176187
#include <stdio.h>
177188
#include <stdlib.h>
178189
#include <string.h>
179-
180190
#include "jerryscript.h"
181191

182192
static void
@@ -246,8 +256,8 @@ Shell operation can be described with the following loop:
246256
#include <stdio.h>
247257
#include <stdlib.h>
248258
#include <string.h>
249-
250259
#include "jerryscript.h"
260+
#include "jerryscript-ext/handler.h"
251261
252262
static void print_value (const jerry_value_t);
253263
@@ -259,6 +269,10 @@ main (int argc, char *argv[])
259269
/* Initialize engine */
260270
jerry_init (JERRY_INIT_EMPTY);
261271
272+
/* Register 'print' function from the extensions */
273+
jerryx_handler_register_global ((const jerry_char_t *) "print",
274+
jerryx_handler_print);
275+
262276
while (!is_done)
263277
{
264278
char cmd[256] = {};
@@ -325,6 +339,7 @@ In this example we demonstrate how to use native function and structures in Java
325339
```c
326340
#include <string.h>
327341
#include "jerryscript.h"
342+
#include "jerryscript-ext/handler.h"
328343

329344
struct my_struct
330345
{
@@ -349,6 +364,10 @@ main (int argc, char *argv[])
349364
/* Initialize engine */
350365
jerry_init (JERRY_INIT_EMPTY);
351366

367+
/* Register 'print' function from the extensions */
368+
jerryx_handler_register_global ((const jerry_char_t *) "print",
369+
jerryx_handler_print);
370+
352371
/* Do something with the native object */
353372
my_struct.msg = "Hello World";
354373

@@ -411,6 +430,7 @@ Here we create a JS Object with `jerry_eval`, then extend it with a native funct
411430
```c
412431
#include <string.h>
413432
#include "jerryscript.h"
433+
#include "jerryscript-ext/handler.h"
414434

415435
/**
416436
* Add param to 'this.x'
@@ -451,6 +471,10 @@ main (int argc, char *argv[])
451471
/* Initialize engine */
452472
jerry_init (JERRY_INIT_EMPTY);
453473

474+
/* Register 'print' function from the extensions */
475+
jerryx_handler_register_global ((const jerry_char_t *) "print",
476+
jerryx_handler_print);
477+
454478
/* Create a JS object */
455479
const jerry_char_t my_js_object[] = " \
456480
MyObject = \

0 commit comments

Comments
 (0)