|
| 1 | +# Common methods to handle properties |
| 2 | + |
| 3 | +## jerryx_property_entry |
| 4 | + |
| 5 | +**Summary** |
| 6 | + |
| 7 | +Structure to define an array of properties with `name` and `value` fields which |
| 8 | +can be registered to a target object. |
| 9 | + |
| 10 | +The engine must be initialied before specifying the `jerry_value_t` in the struct. |
| 11 | + |
| 12 | + |
| 13 | +**Prototype** |
| 14 | + |
| 15 | +```c |
| 16 | +typedef struct { |
| 17 | + const char *name; |
| 18 | + jerry_value_t value; |
| 19 | +} jerryx_function_list_entry; |
| 20 | +``` |
| 21 | + |
| 22 | +**See also** |
| 23 | + |
| 24 | +- [jerryx_set_properties](#jerryx_set_properties) |
| 25 | + |
| 26 | + |
| 27 | +## jerryx_register_result |
| 28 | + |
| 29 | +**Summary** |
| 30 | + |
| 31 | +Structure returned as the result of the [jerryx_set_properties](#jerryx_set_properties) operation. |
| 32 | +The `result` field will either be a JavaScript undefined value or an error object. |
| 33 | +In every case the `registered` field is used to indicated the number of |
| 34 | +successfully registered methods. |
| 35 | + |
| 36 | +This must be passed for the [jerryx_free_property_entry](#jerryx_free_property_entry) method |
| 37 | +after the property registration. |
| 38 | + |
| 39 | +**Prototype** |
| 40 | + |
| 41 | +```c |
| 42 | +typedef struct { |
| 43 | + jerry_value_t result; |
| 44 | + uint32_t registered; |
| 45 | +} jerryx_register_result; |
| 46 | +``` |
| 47 | + |
| 48 | +**See also** |
| 49 | + |
| 50 | +- [jerryx_set_properties](#jerryx_set_properties) |
| 51 | +- [jerryx_free_property_entry](#jerryx_free_property_entry) |
| 52 | + |
| 53 | + |
| 54 | +## jerryx_set_properties |
| 55 | + |
| 56 | +**Summary** |
| 57 | + |
| 58 | +Set multiple properties on a target object. |
| 59 | + |
| 60 | +The properties are an array of (name, jerry_value_t) pairs and |
| 61 | +this list must end with a (NULL, 0) entry. |
| 62 | + |
| 63 | +Important notes: |
| 64 | +* Each property value in the input array is released after a successful property registration. |
| 65 | +* The method [jerryx_free_property_entry](#jerryx_free_property_entry) must be called if there is any failed registration |
| 66 | + to release the values in the entries array. |
| 67 | + It is safe to call this cleanup method in every case not just in case of failure. |
| 68 | +* If the error value is reported via the result it must be freed manually. |
| 69 | + |
| 70 | +**Prototype** |
| 71 | + |
| 72 | +```c |
| 73 | +jerryx_register_result |
| 74 | +jerryx_set_properties (const jerry_value_t target_object, |
| 75 | + const jerryx_property_entry entries[]); |
| 76 | +``` |
| 77 | +
|
| 78 | +- `target_object` - object on which the entries will be set. |
| 79 | +- `entries` - array of (name, jerry_value_t) pairs. |
| 80 | +- return a [jerryx_register_result](#jerryx_register_result). |
| 81 | + - if everything is ok, the struct's `result` field is set to a JS undefined value. |
| 82 | + - otherwise the `result` field is an error object indicating the problem. |
| 83 | + - in every case the `registered` field contains the number of successfully registered properties. |
| 84 | +
|
| 85 | +**Example** |
| 86 | +
|
| 87 | +[doctest]: # () |
| 88 | +
|
| 89 | +```c |
| 90 | +#include "jerryscript.h" |
| 91 | +#include "jerryscript-ext/handler.h" |
| 92 | +
|
| 93 | +static jerry_value_t |
| 94 | +handler (const jerry_value_t function_obj, |
| 95 | + const jerry_value_t this_val, |
| 96 | + const jerry_value_t args_p[], |
| 97 | + const jerry_length_t args_cnt) |
| 98 | +{ |
| 99 | + printf ("native handler called!\n"); |
| 100 | +
|
| 101 | + return jerry_create_boolean (true); |
| 102 | +} |
| 103 | +
|
| 104 | +int |
| 105 | +main (int argc, char **argv) |
| 106 | +{ |
| 107 | + jerry_init (JERRY_INIT_EMPTY); |
| 108 | +
|
| 109 | + jerryx_property_entry methods[] = |
| 110 | + { |
| 111 | + { "demo", jerry_create_external_function (handler) }, |
| 112 | + { NULL, 0 }, |
| 113 | + }; |
| 114 | +
|
| 115 | + jerry_value_t global = jerry_get_global_object (); |
| 116 | + jerryx_register_result reg = jerryx_set_properties (global, methods); |
| 117 | + /* if `reg.result` is undefined all methods are registered */ |
| 118 | + if (jerry_value_is_error (reg.result)) |
| 119 | + { |
| 120 | + printf ("Only registered %d properties\r\n", reg.registered); |
| 121 | + /* clean up not registered property values */ |
| 122 | + jerryx_free_property_entry (methods, reg); |
| 123 | +
|
| 124 | + /* clean up the error */ |
| 125 | + jerry_release_value (reg.result); |
| 126 | + } |
| 127 | +
|
| 128 | + jerry_release_value (global); |
| 129 | +
|
| 130 | + jerry_cleanup(); |
| 131 | +
|
| 132 | + return 0; |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +**Convenience macros** |
| 137 | + |
| 138 | +To make property registration convenient, there are a set of macros to use |
| 139 | +when setting a property entry: |
| 140 | + |
| 141 | +* `JERRYX_PROPERTY_NUMBER(NAME, NUMBER)` - creates a number entry. |
| 142 | +* `JERRYX_PROPERTY_STRING(NAME, STR)` - creates an utf8 string entry. |
| 143 | +* `JERRYX_PROPERTY_STRING_SZ(NAME, STR, SIZE)` - creates an utf8 string entry using only `SIZE` bytes from the string. |
| 144 | +* `JERRYX_PROPERTY_BOOLEAN(NAME, VALUE)` - creates a boolean entry. |
| 145 | +* `JERRYX_PROPERTY_FUNCTION(NAME, NATIVE)` - creates a native C function entry. |
| 146 | +* `JERRYX_PROPERTY_UNDEFINED(NAME)` - creates an undefined property entry. |
| 147 | +* `JERRYX_PROPERTY_LIST_END()` - indicates the end of the property list. |
| 148 | + |
| 149 | +**Example usage of Convenience macros** |
| 150 | + |
| 151 | +[doctest]: # () |
| 152 | + |
| 153 | +```c |
| 154 | +#include "jerryscript.h" |
| 155 | +#include "jerryscript-ext/handler.h" |
| 156 | + |
| 157 | +static jerry_value_t |
| 158 | +handler (const jerry_value_t function_obj, |
| 159 | + const jerry_value_t this_val, |
| 160 | + const jerry_value_t args_p[], |
| 161 | + const jerry_length_t args_cnt) |
| 162 | +{ |
| 163 | + printf ("native handler called!\n"); |
| 164 | + |
| 165 | + return jerry_create_boolean (true); |
| 166 | +} |
| 167 | + |
| 168 | +int |
| 169 | +main (int argc, char **argv) |
| 170 | +{ |
| 171 | + jerry_init (JERRY_INIT_EMPTY); |
| 172 | + |
| 173 | + jerryx_property_entry methods[] = |
| 174 | + { |
| 175 | + JERRYX_PROPERTY_FUNCTION ("demo", handler), |
| 176 | + JERRYX_PROPERTY_NUMBER ("test_num", 2.3), |
| 177 | + JERRYX_PROPERTY_UNDEFINED ("this_is_undefined"), |
| 178 | + JERRYX_PROPERTY_LIST_END(), |
| 179 | + }; |
| 180 | + |
| 181 | + jerry_value_t global = jerry_get_global_object (); |
| 182 | + jerryx_register_result reg = jerryx_set_properties (global, methods); |
| 183 | + /* if `reg.result` is undefined all methods are registered */ |
| 184 | + if (jerry_value_is_error (reg.result)) |
| 185 | + { |
| 186 | + printf ("Only registered %d properties\r\n", reg.registered); |
| 187 | + /* clean up not registered property values */ |
| 188 | + jerryx_free_property_entry (methods, reg); |
| 189 | + |
| 190 | + /* clean up the error */ |
| 191 | + jerry_release_value (reg.result); |
| 192 | + } |
| 193 | + |
| 194 | + jerry_release_value (global); |
| 195 | + |
| 196 | + jerry_cleanup(); |
| 197 | + |
| 198 | + return 0; |
| 199 | +} |
| 200 | +``` |
| 201 | +
|
| 202 | +
|
| 203 | +**See also** |
| 204 | +
|
| 205 | +- [jerryx_property_entry](#jerryx_property_entry) |
| 206 | +- [jerryx_free_property_entry](#jerryx_free_property_entry) |
| 207 | +- [jerryx_register_result](#jerryx_register_result) |
| 208 | +
|
| 209 | +## jerryx_free_property_entry |
| 210 | +
|
| 211 | +**Summary** |
| 212 | +
|
| 213 | +Release all `jerry_value_t` in a `jerryx_property_entry` array based on a previous [jerryx_set_properties](#jerryx_set_properties) call |
| 214 | +and also the error value (if any) in the `jerryx_register_result` structure. |
| 215 | +In case of a successful registration it is safe to call this method. |
| 216 | +
|
| 217 | +After the method call the `ęntries` array should not be used as all values are released. |
| 218 | +
|
| 219 | +**Prototype** |
| 220 | +
|
| 221 | +``` |
| 222 | +void |
| 223 | +jerryx_free_property_entry (const jerryx_property_entry entries[], |
| 224 | + const jerryx_register_result register_result); |
| 225 | +``` |
| 226 | +
|
| 227 | +- `entires` - array of [jerryx_property_entry](#jerryx_property_entry). |
| 228 | +- `register_result` - result of a previous [jerryx_set_properties](#jerryx_set_properties) call. |
| 229 | +
|
| 230 | +**Example** |
| 231 | +
|
| 232 | +For example usage see [jerryx_set_properties](#jerryx_set_properties). |
| 233 | +
|
| 234 | +
|
| 235 | +## jerryx_set_property_str |
| 236 | +
|
| 237 | +**Summary** |
| 238 | +
|
| 239 | +Set a property on a target object with a given name. |
| 240 | +
|
| 241 | +The property name must be provided as a zero terminated string. |
| 242 | +
|
| 243 | +*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it |
| 244 | +is no longer needed. |
| 245 | +
|
| 246 | +
|
| 247 | +**Prototype** |
| 248 | +
|
| 249 | +```c |
| 250 | +jerry_value_t |
| 251 | +jerryx_set_property_str (const jerry_value_t target_object, |
| 252 | + const char *name, |
| 253 | + const jerry_value_t value); |
| 254 | +``` |
| 255 | + |
| 256 | +- `target_object` - the object where the property should be set |
| 257 | +- `name` - name of the property |
| 258 | +- `value` - property value to be set |
| 259 | +- return value |
| 260 | + - JS true value, if success |
| 261 | + - thrown error, otherwise |
| 262 | + |
| 263 | +**Example** |
| 264 | + |
| 265 | +[doctest]: # () |
| 266 | + |
| 267 | +```c |
| 268 | +#include "jerryscript.h" |
| 269 | +#include "jerryscript-ext/handler.h" |
| 270 | + |
| 271 | +int |
| 272 | +main (int argc, char **argv) |
| 273 | +{ |
| 274 | + jerry_init (JERRY_INIT_EMPTY); |
| 275 | + |
| 276 | + jerry_value_t global = jerry_get_global_object (); |
| 277 | + |
| 278 | + jerry_value_t value = jerry_create_number (3.3); |
| 279 | + jerry_value_t result = jerryx_set_property_str (global, "value", value); |
| 280 | + if (jerry_value_is_error (result)) |
| 281 | + { |
| 282 | + printf ("Error during property configuration\r\n"); |
| 283 | + } |
| 284 | + |
| 285 | + jerry_release_value (result); |
| 286 | + jerry_release_value (value); |
| 287 | + jerry_release_value (global); |
| 288 | + jerry_cleanup(); |
| 289 | + |
| 290 | + return 0; |
| 291 | +} |
| 292 | +``` |
| 293 | +
|
| 294 | +
|
| 295 | +## jerryx_get_property_str |
| 296 | +
|
| 297 | +**Summary** |
| 298 | +
|
| 299 | +Get the value of a property from the specified object with the given name. |
| 300 | +
|
| 301 | +*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it |
| 302 | +is no longer needed. |
| 303 | +
|
| 304 | +
|
| 305 | +**Prototype** |
| 306 | +
|
| 307 | +``` |
| 308 | +jerry_value_t |
| 309 | +jerryx_get_property_str (const jerry_value_t target_object, |
| 310 | + const char *name); |
| 311 | +``` |
| 312 | +
|
| 313 | +- `target_object` - object on which the property name is accessed |
| 314 | +- `name` - property name as a `char*` |
| 315 | +- return value |
| 316 | + - value of property, if success |
| 317 | + - thrown error, otherwise |
| 318 | +
|
| 319 | +**Example** |
| 320 | +
|
| 321 | +[doctest]: # () |
| 322 | +
|
| 323 | +```c |
| 324 | +#include "jerryscript.h" |
| 325 | +#include "jerryscript-ext/handler.h" |
| 326 | +
|
| 327 | +int |
| 328 | +main (int argc, char **argv) |
| 329 | +{ |
| 330 | + jerry_init (JERRY_INIT_EMPTY); |
| 331 | +
|
| 332 | + jerry_value_t global = jerry_get_global_object (); |
| 333 | +
|
| 334 | + jerry_value_t math_object = jerryx_get_property_str (global, "Math"); |
| 335 | +
|
| 336 | + /* use math_object */ |
| 337 | +
|
| 338 | + jerry_release_value (math_object); |
| 339 | + jerry_release_value (global); |
| 340 | + jerry_cleanup(); |
| 341 | +
|
| 342 | + return 0; |
| 343 | +} |
| 344 | +``` |
| 345 | + |
| 346 | + |
1 | 347 | # Common external function handlers
|
2 | 348 |
|
3 | 349 | ## jerryx_handler_assert_fatal
|
|
0 commit comments