@@ -168,7 +168,7 @@ This method will be implemented in C and will be called from the JavaScript code
168
168
For this a few extra API methods are required:
169
169
170
170
- ` jerry_current_realm `
171
- - ` jerry_string_sz `
171
+ - ` jerry_sz_ascii `
172
172
- ` jerry_object_set `
173
173
- ` jerry_function_external `
174
174
@@ -207,7 +207,7 @@ main (void)
207
207
/* Get the "global" object * /
208
208
jerry_value_t global_object = jerry_current_realm ();
209
209
/* Create a "print" JS string * /
210
- jerry_value_t property_name_print = jerry_string_sz ("print");
210
+ jerry_value_t property_name_print = jerry_sz_ascii ("print");
211
211
/* Create a function from a native C method (this function will be called from JS) * /
212
212
jerry_value_t property_value_func = jerry_function_external (print_handler);
213
213
/* Add the "print" property with the function value to the "global" object * /
@@ -326,7 +326,7 @@ main (void)
326
326
/* Get the "global" object * /
327
327
jerry_value_t global_object = jerry_current_realm ();
328
328
/* Create a "print" JS string * /
329
- jerry_value_t property_name_print = jerry_string_sz ("print");
329
+ jerry_value_t property_name_print = jerry_sz_ascii ("print");
330
330
/* Create a function from a native C method (this function will be called from JS) * /
331
331
jerry_value_t property_value_func = jerry_function_external (print_handler);
332
332
/* Add the "print" property with the function value to the "global" object * /
@@ -393,9 +393,17 @@ In this example the following extension methods are used:
393
393
394
394
In further examples this "print" handler will be used.
395
395
396
+ The ` api-example-6.c ` file should contain the following code:
397
+
398
+ [ doctest ] : # ( )
399
+
396
400
``` c
401
+ #include < stdio.h>
402
+
397
403
#include " jerryscript.h"
404
+
398
405
#include " jerryscript-ext/handlers.h"
406
+ #include " jerryscript-ext/properties.h"
399
407
400
408
int
401
409
main (void)
@@ -407,7 +415,7 @@ main (void)
407
415
jerry_init (JERRY_INIT_EMPTY);
408
416
409
417
/* Register 'print' function from the extensions to the global object * /
410
- jerryx_register_global ("print", jerryx_handler_print);
418
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
411
419
412
420
/* Setup Global scope code * /
413
421
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
@@ -470,14 +478,14 @@ main (void)
470
478
jerry_init (JERRY_INIT_EMPTY);
471
479
472
480
/* Register 'print' function from the extensions * /
473
- jerryx_register_global ("print", jerryx_handler_print);
481
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
474
482
475
483
/* Getting pointer to the Global object * /
476
484
jerry_value_t global_object = jerry_current_realm ();
477
485
478
486
/* Constructing strings * /
479
- jerry_value_t prop_name = jerry_string_sz ("my_var");
480
- jerry_value_t prop_value = jerry_string_sz ("Hello from C!");
487
+ jerry_value_t prop_name = jerry_sz_ascii ("my_var");
488
+ jerry_value_t prop_value = jerry_sz_ascii ("Hello from C!");
481
489
482
490
/* Setting the string value as a property of the Global object * /
483
491
jerry_value_t set_result = jerry_object_set (global_object, prop_name, prop_value);
@@ -729,7 +737,7 @@ main (void)
729
737
jerry_init (JERRY_INIT_EMPTY);
730
738
731
739
/* Register 'print' function from the extensions */
732
- jerryx_register_global ("print", jerryx_handler_print);
740
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
733
741
734
742
while (!is_done)
735
743
{
@@ -808,10 +816,8 @@ In this example (`api-example-9.c`) an object with a native function is added to
808
816
#include " jerryscript-ext/handlers.h"
809
817
#include " jerryscript-ext/properties.h"
810
818
811
- struct my_struct
812
- {
813
- const char * msg;
814
- } my_struct;
819
+
820
+ jerry_string_t my_struct;
815
821
816
822
/* *
817
823
* Get a string from a native object
@@ -821,7 +827,7 @@ get_msg_handler (const jerry_call_info_t *call_info_p, /**< call information */
821
827
const jerry_value_t * args_p, /** < function arguments * /
822
828
const jerry_length_t args_cnt) /** < number of function arguments * /
823
829
{
824
- return jerry_string_sz (my_struct.msg );
830
+ return jerry_string_ascii (my_struct.ptr, my_struct.size );
825
831
} /* get_msg_handler * /
826
832
827
833
int
@@ -831,10 +837,13 @@ main (void)
831
837
jerry_init (JERRY_INIT_EMPTY);
832
838
833
839
/* Register 'print' function from the extensions * /
834
- jerryx_register_global ("print", jerryx_handler_print);
840
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
835
841
836
842
/* Do something with the native object * /
837
- my_struct.msg = "Hello, World!";
843
+ {
844
+ static const jerry_string_t hello = { JERRY_ZSTR_ARG ("Hello, World!") };
845
+ my_struct = hello;
846
+ }
838
847
839
848
/* Create an empty JS object * /
840
849
jerry_value_t object = jerry_object ();
@@ -843,7 +852,7 @@ main (void)
843
852
jerry_value_t func_obj = jerry_function_external (get_msg_handler);
844
853
845
854
/* Set the native function as a property of the empty JS object * /
846
- jerry_value_t prop_name = jerry_string_sz ("myFunc");
855
+ jerry_value_t prop_name = jerry_sz_ascii ("myFunc");
847
856
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
848
857
jerry_value_free (prop_name);
849
858
jerry_value_free (func_obj);
@@ -852,7 +861,7 @@ main (void)
852
861
jerry_value_t global_object = jerry_current_realm ();
853
862
854
863
/* Add the JS object to the global context * /
855
- prop_name = jerry_string_sz ("MyObject");
864
+ prop_name = jerry_sz_ascii ("MyObject");
856
865
jerry_value_free (jerry_object_set (global_object, prop_name, object));
857
866
jerry_value_free (prop_name);
858
867
jerry_value_free (object);
@@ -928,7 +937,7 @@ add_handler (const jerry_call_info_t *call_info_p, /**< call information */
928
937
/* Note: that the argument count check is ignored for the example's case * /
929
938
930
939
/* Get 'this.x' * /
931
- jerry_value_t prop_name = jerry_string_sz ("x");
940
+ jerry_value_t prop_name = jerry_sz_ascii ("x");
932
941
jerry_value_t x_val = jerry_object_get (call_info_p->this_value, prop_name);
933
942
934
943
if (!jerry_value_is_exception (x_val))
@@ -958,7 +967,7 @@ main (void)
958
967
jerry_init (JERRY_INIT_EMPTY);
959
968
960
969
/* Register 'print' function from the extensions * /
961
- jerryx_register_global ("print", jerryx_handler_print);
970
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
962
971
963
972
/* Create a JS object * /
964
973
const jerry_char_t my_js_object[ ] = " \
@@ -983,7 +992,7 @@ main (void)
983
992
jerry_value_t add_func_obj = jerry_function_external (add_handler);
984
993
985
994
/* Set the native function as a property of previously created MyObject * /
986
- jerry_value_t prop_name = jerry_string_sz ("add2x");
995
+ jerry_value_t prop_name = jerry_sz_ascii ("add2x");
987
996
jerry_value_free (jerry_object_set (my_js_obj_val, prop_name, add_func_obj));
988
997
jerry_value_free (add_func_obj);
989
998
jerry_value_free (prop_name);
@@ -1058,7 +1067,7 @@ main (void)
1058
1067
jerry_init (JERRY_INIT_EMPTY);
1059
1068
1060
1069
/* Register the print function * /
1061
- jerryx_register_global ("print", jerryx_handler_print);
1070
+ jerryx_register_global (jerry_sz_ascii ( "print") , jerryx_handler_print);
1062
1071
1063
1072
/* Evaluate the script * /
1064
1073
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
0 commit comments