Lines Matching +full:js +full:- +full:native +full:- +full:api
1 …iding the way to run JavaScript in a large range of environments - from desktops to low-memory mic…
3 This guide is intended to introduce you to JerryScript embedding API and to create a minimal JavaSc…
4 The examples are not using all API methods please also check out the API reference document which c…
18 $ git clone https://github.com/jerryscript-project/jerryscript.git
19 $ jerryscript/tools/build.py --builddir=$(pwd)/example_build --cmake-param="-DCMAKE_INSTALL_PREFIX=…
20 $ make -C $(pwd)/example_build install
25 In this guide we will use `pkg-config` to ease the usage of headers and libraries.
32 Test if the `pkg-config` works for JerryScript:
35 $ pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-ext libjerry-libm
40 The most basic example to test the engine is to create an `api-example-1.c` file containing the fol…
52 bool ret_value = jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
61 $ gcc api-example-1.c -o api-example-1 $(pkg-config --cflags --libs libjerry-core libjerry-port-def…
67 $ ./api-example-1
76 returned by the API methods is freed with the `jerry_release_value` method.
81 Use the following code for the `api-example-2.c` file:
92 const jerry_length_t script_size = sizeof (script) - 1;
121 $ gcc api-example-2.c -o api-example-2 $(pkg-config --cflags --libs libjerry-core libjerry-port-def…
127 $ ./api-example-2
133 In this example the `jerry_eval` is replaced with a more common API calls:
135 - script code setup - `jerry_parse`.
136 - script execution - `jerry_run`.
138 The `api-example-3.c` file should contain the following code:
156 …jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS…
158 /* Check if there is any JS code parse error */
184 $ gcc api-example-3.c -o api-example-3 $(pkg-config --cflags --libs libjerry-core libjerry-port-def…
190 $ ./api-example-3
201 For this a few extra API methods are required:
203 - `jerry_get_global_object`
204 - `jerry_create_string`
205 - `jerry_set_property`
206 - `jerry_create_external_function`
208 The `api-example-4.c` file should contain the following code:
234 const jerry_length_t script_size = sizeof (script) - 1;
243 /* Create a "print" JS string */
245 /* Create a function from a native C method (this function will be called from JS) */
255 /* Release all jerry_value_t-s */
288 $ gcc api-example-4.c -o api-example-4 $(pkg-config --cflags --libs libjerry-core libjerry-port-def…
294 $ ./api-example-4
297 ## Example 5. Passing and processing arguments for native C code
303 argument (which probably comes from a JavaScript source) to a JS string and prints it out to the st…
305 New API methods used:
307 - `jerry_value_to_string`
308 - `jerry_string_to_utf8_char_buffer`
310 The `api-example-5.c` file should contain the following code:
327 /* Convert the first argument to a string (JS "toString" operation) */
335 * More details on the API reference page
337 …size_t copied_bytes = jerry_string_to_utf8_char_buffer (string_value, buffer, sizeof (buffer) - 1);
353 const jerry_char_t script[] = "print ('Hello from JS!');";
354 const jerry_length_t script_size = sizeof (script) - 1;
363 /* Create a "print" JS string */
365 /* Create a function from a native C method (this function will be called from JS) */
375 /* Release all jerry_value_t-s */
408 $ gcc api-example-5.c -o api-example-5 $(pkg-config --cflags --libs libjerry-core libjerry-port-def…
411 …s correct the application should print out the string passed for the `print` method in the JS code:
414 $ ./api-example-5
426 - `jerryx_handler_register_global`
427 - `jerryx_handler_print`
433 #include "jerryscript-ext/handler.h"
438 const jerry_char_t script[] = "print ('Hello from JS with ext!');";
439 const jerry_length_t script_size = sizeof (script) - 1;
473 (**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `p…
476 $ gcc api-example-6.c -o api-example-6 $(pkg-config --cflags --libs libjerry-core libjerry-ext libj…
479 …s correct the application should print out the string passed for the `print` method in the JS code:
482 $ ./api-example-6
485 ## Example 7. Interaction with JavaScript environment - adding a string property
490 Use the following code as the `api-example-7.c` file:
496 #include "jerryscript-ext/handler.h"
534 sizeof (script) - 1,
549 (**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `p…
552 $ gcc api-example-7.c -o api-example-7 $(pkg-config --cflags --libs libjerry-core libjerry-ext libj…
558 $ ./api-example-7
565 … which wraps another value. This "error" can be created by throwing a JavaScript value from JS code
566 or via API method(s). It is advised to check for this error with the `jerry_value_is_error` method …
567 can process error values. To extract the value from the "error" the API method `jerry_get_value_fro…
568 If an error object is created via API method (for example with `jerry_create_error`) the "error" va…
571 - The error object is a object which was constructed via one of the `Error` objects (available from…
572 For example in JS such object be created with the following code example:
574 ```js
578 - The error value is not an object on its own. This is the exception raised/thrown either via API m…
579 For example, creating such error value in JS would look like this:
581 ```js
656 printf ("[JS object]");
670 - read command;
671 - if command is 'quit'
672 - exit loop;
673 - else
674 - eval (command);
675 - print result of eval;
676 - loop.
678 See the following `api-example-8-shell.c` file:
687 #include "jerryscript-ext/handler.h"
750 printf ("[JS object]");
795 if (!strncmp (cmd, "quit\n", sizeof ("quit\n") - 1))
822 (**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `p…
825 $ gcc api-example-8-shell.c -o api-example-8-shell $(pkg-config --cflags --libs libjerry-core libje…
831 $ ./api-example-8-shell
835 ## Example 9. Creating JS object in global context
837 In this example (`api-example-9.c`) an object with a native function is added to the global object.
843 #include "jerryscript-ext/handler.h"
851 * Get a string from a native object
872 /* Do something with the native object */
875 /* Create an empty JS object */
878 /* Create a JS function object and wrap into a jerry value */
881 /* Set the native function as a property of the empty JS object */
887 /* Wrap the JS object (not empty anymore) into a jerry api value */
890 /* Add the JS object to the global context */
899 * Equivalent JS code:
908 jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
922 (**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `p…
925 $ gcc api-example-9.c -o api-example-9 $(pkg-config --cflags --libs libjerry-core libjerry-ext libj…
931 $ ./api-example-9
940 ## Example 10. Extending JS Objects with native functions
942 The example creates a JS Object with `jerry_eval`, then it is extended from C with a native functio…
943 In addition this native function shows how to get a property value from the object and how to manip…
946 Use the following code for `api-example-10.c`:
952 #include "jerryscript-ext/handler.h"
963 /* The the 'this_val' is the 'MyObject' from the JS code below */
972 /* Convert Jerry API values to double */
1000 /* Create a JS object */
1016 sizeof (my_js_object) - 1,
1019 /* Create a JS function object and wrap into a jerry value */
1022 /* Set the native function as a property of previously created MyObject */
1039 jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
1053 (**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `p…
1056 $ gcc api-example-10.c -o api-example-10 $(pkg-config --cflags --libs libjerry-core libjerry-ext li…
1062 $ ./api-example-10
1080 #include "jerryscript-port.h"
1081 #include "jerryscript-ext/handler.h"
1101 jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
1115 …API description, please visit [API Reference page](https://jerryscript-project.github.io/jerryscri…