1 /* 2 * Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef UCODE_TYPES_H 18 #define UCODE_TYPES_H 19 20 #include <stdbool.h> 21 #include <stdint.h> 22 #include <regex.h> 23 #include <signal.h> 24 #include <json-c/json.h> 25 26 #include "util.h" 27 #include "platform.h" 28 29 30 /* Value types and generic value header */ 31 32 typedef enum uc_type { 33 UC_NULL, 34 UC_INTEGER, 35 UC_BOOLEAN, 36 UC_STRING, 37 UC_DOUBLE, 38 UC_ARRAY, 39 UC_OBJECT, 40 UC_REGEXP, 41 UC_CFUNCTION, 42 UC_CLOSURE, 43 UC_UPVALUE, 44 UC_RESOURCE, 45 UC_PROGRAM, 46 UC_SOURCE 47 } uc_type_t; 48 49 typedef struct uc_value { 50 uint32_t type:4; 51 uint32_t mark:1; 52 uint32_t ext_flag:1; 53 uint32_t refcount:26; 54 } uc_value_t; 55 56 57 /* Constant list defintions */ 58 59 typedef struct { 60 size_t isize; 61 size_t dsize; 62 uint64_t *index; 63 char *data; 64 } uc_value_list_t; 65 66 67 /* Source buffer defintions */ 68 69 uc_declare_vector(uc_lineinfo_t, uint8_t); 70 71 typedef struct { 72 uc_value_t header; 73 char *filename, *runpath, *buffer; 74 FILE *fp; 75 size_t off; 76 uc_lineinfo_t lineinfo; 77 struct { 78 size_t count, offset; 79 uc_value_t **entries; 80 } exports; 81 } uc_source_t; 82 83 84 /* Bytecode chunk defintions */ 85 86 typedef struct { 87 size_t from, to, target, slot; 88 } uc_ehrange_t; 89 90 typedef struct { 91 size_t from, to, slot, nameidx; 92 } uc_varrange_t; 93 94 uc_declare_vector(uc_ehranges_t, uc_ehrange_t); 95 uc_declare_vector(uc_variables_t, uc_varrange_t); 96 uc_declare_vector(uc_offsetinfo_t, uint8_t); 97 98 typedef struct { 99 size_t count; 100 uint8_t *entries; 101 uc_ehranges_t ehranges; 102 struct { 103 uc_variables_t variables; 104 uc_value_list_t varnames; 105 uc_offsetinfo_t offsets; 106 } debuginfo; 107 } uc_chunk_t; 108 109 110 /* Value type structures */ 111 112 typedef struct uc_weakref { 113 struct uc_weakref *prev; 114 struct uc_weakref *next; 115 } uc_weakref_t; 116 117 typedef struct uc_function { 118 uc_weakref_t progref; 119 bool arrow, vararg, strict, module; 120 size_t nargs; 121 size_t nupvals; 122 size_t srcidx; 123 size_t srcpos; 124 uc_chunk_t chunk; 125 struct uc_program *program; 126 char name[]; 127 } uc_function_t; 128 129 typedef struct { 130 uc_value_t header; 131 double dbl; 132 } uc_double_t; 133 134 typedef struct { 135 uc_value_t header; 136 union { 137 int64_t s64; 138 uint64_t u64; 139 } i; 140 } uc_integer_t; 141 142 typedef struct { 143 uc_value_t header; 144 size_t length; 145 char str[]; 146 } uc_string_t; 147 148 typedef struct { 149 uc_value_t header; 150 uc_weakref_t ref; 151 size_t count; 152 uc_value_t *proto; 153 uc_value_t **entries; 154 } uc_array_t; 155 156 typedef struct { 157 uc_value_t header; 158 uc_weakref_t ref; 159 uc_value_t *proto; 160 struct lh_table *table; 161 } uc_object_t; 162 163 typedef struct { 164 uc_value_t header; 165 regex_t regexp; 166 bool icase, newline, global; 167 char source[]; 168 } uc_regexp_t; 169 170 typedef struct uc_upval_tref { 171 uc_value_t header; 172 size_t slot; 173 bool closed; 174 uc_value_t *value; 175 struct uc_upval_tref *next; 176 } uc_upvalref_t; 177 178 typedef struct { 179 uc_value_t header; 180 uc_weakref_t ref; 181 bool is_arrow; 182 uc_function_t *function; 183 uc_upvalref_t **upvals; 184 } uc_closure_t; 185 186 typedef struct uc_vm uc_vm_t; 187 typedef uc_value_t *(*uc_cfn_ptr_t)(uc_vm_t *, size_t); 188 189 typedef struct { 190 uc_value_t header; 191 uc_cfn_ptr_t cfn; 192 char name[]; 193 } uc_cfunction_t; 194 195 typedef struct { 196 const char *name; 197 uc_value_t *proto; 198 void (*free)(void *); 199 } uc_resource_type_t; 200 201 typedef struct { 202 uc_value_t header; 203 uc_resource_type_t *type; 204 void *data; 205 } uc_resource_t; 206 207 typedef struct { 208 uc_value_t header; 209 uc_weakref_t ref; 210 uc_resource_type_t *type; 211 212 uint32_t reserved:3; 213 uint32_t persistent:1; 214 uint32_t uvcount:8; 215 uint32_t datasize:20; 216 217 uint32_t _pad; 218 } uc_resource_ext_t; 219 220 uc_declare_vector(uc_resource_types_t, uc_resource_type_t *); 221 222 typedef struct { 223 uc_list_t list; 224 struct lh_table *table; 225 union { 226 struct lh_entry *pos; 227 struct { 228 const void *k; 229 unsigned long hash; 230 } kh; 231 } u; 232 } uc_object_iterator_t; 233 234 235 /* Program structure definitions */ 236 237 uc_declare_vector(uc_sources_t, uc_source_t *); 238 uc_declare_vector(uc_modexports_t, uc_upvalref_t *); 239 240 typedef struct uc_program { 241 uc_value_t header; 242 uc_value_list_t constants; 243 uc_weakref_t functions; 244 uc_sources_t sources; 245 uc_modexports_t exports; 246 } uc_program_t; 247 248 249 /* Parser definitions */ 250 251 uc_declare_vector(uc_search_path_t, char *); 252 253 typedef struct { 254 bool lstrip_blocks; 255 bool trim_blocks; 256 bool strict_declarations; 257 bool raw_mode; 258 uc_search_path_t module_search_path; 259 uc_search_path_t force_dynlink_list; 260 bool setup_signal_handlers; 261 bool compile_module; 262 } uc_parse_config_t; 263 264 extern uc_parse_config_t uc_default_parse_config; 265 266 void uc_search_path_init(uc_search_path_t *search_path); 267 268 static inline void 269 uc_search_path_add(uc_search_path_t *search_path, char *path) { 270 uc_vector_push(search_path, xstrdup(path)); 271 } 272 273 static inline void 274 uc_search_path_free(uc_search_path_t *search_path) { 275 while (search_path->count > 0) 276 free(search_path->entries[--search_path->count]); 277 278 uc_vector_clear(search_path); 279 } 280 281 282 /* TLS data */ 283 284 typedef struct { 285 /* VM owning installed signal handlers */ 286 uc_vm_t *signal_handler_vm; 287 288 /* Object iteration */ 289 uc_list_t object_iterators; 290 } uc_thread_context_t; 291 292 __hidden uc_thread_context_t *uc_thread_context_get(void); 293 294 295 /* VM definitions */ 296 297 typedef enum { 298 EXCEPTION_NONE, 299 EXCEPTION_SYNTAX, 300 EXCEPTION_RUNTIME, 301 EXCEPTION_TYPE, 302 EXCEPTION_REFERENCE, 303 EXCEPTION_USER, 304 EXCEPTION_EXIT 305 } uc_exception_type_t; 306 307 typedef struct { 308 uc_exception_type_t type; 309 uc_value_t *stacktrace; 310 char *message; 311 } uc_exception_t; 312 313 typedef struct { 314 uint8_t *ip; 315 uc_closure_t *closure; 316 uc_cfunction_t *cfunction; 317 size_t stackframe; 318 uc_value_t *ctx; 319 bool mcall, strict; 320 } uc_callframe_t; 321 322 uc_declare_vector(uc_callframes_t, uc_callframe_t); 323 uc_declare_vector(uc_stack_t, uc_value_t *); 324 325 typedef struct printbuf uc_stringbuf_t; 326 327 typedef void (uc_exception_handler_t)(uc_vm_t *, uc_exception_t *); 328 329 struct uc_vm { 330 uc_stack_t stack; 331 uc_exception_t exception; 332 uc_callframes_t callframes; 333 uc_upvalref_t *open_upvals; 334 uc_parse_config_t *config; 335 uc_value_t *globals; 336 uc_value_t *registry; 337 uc_source_t *sources; 338 uc_weakref_t values; 339 uc_resource_types_t restypes; 340 char _reserved[sizeof(uc_modexports_t)]; 341 union { 342 uint32_t u32; 343 int32_t s32; 344 uint16_t u16; 345 int16_t s16; 346 uint8_t u8; 347 int8_t s8; 348 } arg; 349 size_t alloc_refs; 350 uint8_t trace; 351 uint8_t gc_flags; 352 uint16_t gc_interval; 353 uc_stringbuf_t *strbuf; 354 uc_exception_handler_t *exhandler; 355 FILE *output; 356 struct { 357 uint64_t raised[((UC_SYSTEM_SIGNAL_COUNT + 63) & ~63) / 64]; 358 uc_value_t *handler; 359 struct sigaction sa; 360 int sigpipe[2]; 361 } signal; 362 }; 363 364 365 /* Value API */ 366 367 __hidden void ucv_free(uc_value_t *, bool); 368 __hidden void ucv_unref(uc_weakref_t *); 369 __hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *); 370 371 uc_value_t *ucv_get(uc_value_t *uv); 372 void ucv_put(uc_value_t *); 373 374 uc_type_t ucv_type(uc_value_t *); 375 const char *ucv_typename(uc_value_t *); 376 377 uc_value_t *ucv_boolean_new(bool); 378 bool ucv_boolean_get(uc_value_t *); 379 380 uc_value_t *ucv_string_alloc(char **, size_t); 381 uc_value_t *ucv_string_new(const char *); 382 uc_value_t *ucv_string_new_length(const char *, size_t); 383 size_t ucv_string_length(uc_value_t *); 384 385 char *_ucv_string_get(uc_value_t **); 386 #define ucv_string_get(uv) _ucv_string_get((uc_value_t **)&uv) 387 388 uc_stringbuf_t *ucv_stringbuf_new(void); 389 uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *); 390 391 void _ucv_stringbuf_append(uc_stringbuf_t *, const char *, size_t); 392 393 #define _ucv_is_literal(str) ("" str) 394 #define ucv_stringbuf_append(buf, str) _ucv_stringbuf_append(buf, _ucv_is_literal(str), sizeof(str) - 1) 395 #define ucv_stringbuf_addstr(buf, str, len) _ucv_stringbuf_append(buf, str, len) 396 #define ucv_stringbuf_printf(buf, fmt, ...) sprintbuf(buf, fmt, __VA_ARGS__) 397 398 uc_value_t *ucv_int64_new(int64_t); 399 uc_value_t *ucv_uint64_new(uint64_t); 400 int64_t ucv_int64_get(uc_value_t *); 401 uint64_t ucv_uint64_get(uc_value_t *); 402 403 uc_value_t *ucv_double_new(double); 404 double ucv_double_get(uc_value_t *); 405 406 uc_value_t *ucv_array_new(uc_vm_t *); 407 uc_value_t *ucv_array_new_length(uc_vm_t *, size_t); 408 uc_value_t *ucv_array_get(uc_value_t *, size_t); 409 uc_value_t *ucv_array_pop(uc_value_t *); 410 uc_value_t *ucv_array_push(uc_value_t *, uc_value_t *); 411 uc_value_t *ucv_array_shift(uc_value_t *); 412 uc_value_t *ucv_array_unshift(uc_value_t *, uc_value_t *); 413 void ucv_array_sort(uc_value_t *, int (*)(const void *, const void *)); 414 void ucv_array_sort_r(uc_value_t *, int (*)(uc_value_t *, uc_value_t *, void *), void *); 415 bool ucv_array_delete(uc_value_t *, size_t, size_t); 416 bool ucv_array_set(uc_value_t *, size_t, uc_value_t *); 417 size_t ucv_array_length(uc_value_t *); 418 419 uc_value_t *ucv_object_new(uc_vm_t *); 420 uc_value_t *ucv_object_get(uc_value_t *, const char *, bool *); 421 bool ucv_object_add(uc_value_t *, const char *, uc_value_t *); 422 void ucv_object_sort(uc_value_t *, int (*)(const void *, const void *)); 423 void ucv_object_sort_r(uc_value_t *, int (*)(const char *, uc_value_t *, const char *, uc_value_t *, void *), void *); 424 bool ucv_object_delete(uc_value_t *, const char *); 425 size_t ucv_object_length(uc_value_t *); 426 427 #define ucv_object_foreach(obj, key, val) \ 428 char *key = NULL; \ 429 uc_value_t *val = NULL; \ 430 struct lh_entry *entry##key; \ 431 struct lh_entry *entry_next##key = NULL; \ 432 for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL; \ 433 (entry##key ? (key = (char *)lh_entry_k(entry##key), \ 434 val = (uc_value_t *)lh_entry_v(entry##key), \ 435 entry_next##key = entry##key->next, entry##key) \ 436 : 0); \ 437 entry##key = entry_next##key) 438 439 uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t); 440 441 uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool); 442 443 uc_resource_type_t *ucv_resource_type_add(uc_vm_t *, const char *, uc_value_t *, void (*)(void *)); 444 uc_resource_type_t *ucv_resource_type_lookup(uc_vm_t *, const char *); 445 446 uc_value_t *ucv_resource_new(uc_resource_type_t *, void *); 447 uc_value_t *ucv_resource_new_ex(uc_vm_t *, uc_resource_type_t *, void **, size_t, size_t); 448 void *ucv_resource_data(uc_value_t *uv, const char *); 449 void **ucv_resource_dataptr(uc_value_t *, const char *); 450 uc_value_t *ucv_resource_value_get(uc_value_t *, size_t); 451 bool ucv_resource_value_set(uc_value_t *, size_t, uc_value_t *); 452 453 static inline uc_resource_type_t * 454 ucv_resource_type(uc_value_t *uv) 455 { 456 if (uv->ext_flag) { 457 uc_resource_ext_t *res = (uc_resource_ext_t *)uv; 458 459 return res->type; 460 } 461 else { 462 uc_resource_t *res = (uc_resource_t *)uv; 463 464 return res->type; 465 } 466 } 467 468 static inline uc_value_t * 469 ucv_resource_create(uc_vm_t *vm, const char *type, void *value) 470 { 471 uc_resource_type_t *t = NULL; 472 473 if (type && (t = ucv_resource_type_lookup(vm, type)) == NULL) 474 return NULL; 475 476 return ucv_resource_new(t, value); 477 } 478 479 static inline uc_value_t * 480 ucv_resource_create_ex(uc_vm_t *vm, const char *type, void **data, size_t uvcount, size_t datasize) 481 { 482 uc_resource_type_t *t = NULL; 483 484 if (type && (t = ucv_resource_type_lookup(vm, type)) == NULL) 485 return NULL; 486 487 return ucv_resource_new_ex(vm, t, data, uvcount, datasize); 488 } 489 490 static inline bool 491 ucv_resource_is_extended(uc_value_t *uv) 492 { 493 return (((uintptr_t)uv & 3) == 0 && uv != NULL && 494 uv->ext_flag == true && uv->type == UC_RESOURCE); 495 } 496 497 static inline bool 498 ucv_resource_is_persistent(uc_value_t *uv) 499 { 500 uc_resource_ext_t *res = (uc_resource_ext_t *)uv; 501 502 return ucv_resource_is_extended(uv) && res->persistent; 503 } 504 505 static inline bool 506 ucv_resource_persistent_set(uc_value_t *uv, bool persistent) 507 { 508 uc_resource_ext_t *res = (uc_resource_ext_t *)uv; 509 510 if (!ucv_resource_is_extended(uv) || res->persistent == persistent) 511 return false; 512 513 res->persistent = persistent; 514 515 return true; 516 } 517 518 uc_value_t *ucv_regexp_new(const char *, bool, bool, bool, char **); 519 520 uc_value_t *ucv_upvalref_new(size_t); 521 522 uc_value_t *ucv_prototype_get(uc_value_t *); 523 bool ucv_prototype_set(uc_value_t *, uc_value_t *); 524 525 uc_value_t *ucv_property_get(uc_value_t *, const char *); 526 527 uc_value_t *ucv_from_json(uc_vm_t *, json_object *); 528 json_object *ucv_to_json(uc_value_t *); 529 530 char *ucv_to_string(uc_vm_t *, uc_value_t *); 531 char *ucv_to_jsonstring_formatted(uc_vm_t *, uc_value_t *, char, size_t); 532 void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_t, char, size_t); 533 534 #define ucv_to_jsonstring(vm, val) ucv_to_jsonstring_formatted(vm, val, '\1', 0) 535 #define ucv_to_stringbuf(vm, buf, val, json) ucv_to_stringbuf_formatted(vm, buf, val, 0, json ? '\1' : '\0', 0) 536 537 uc_type_t ucv_cast_number(uc_value_t *, int64_t *, double *); 538 539 uc_value_t *ucv_to_number(uc_value_t *); 540 541 static inline double 542 ucv_to_double(uc_value_t *v) 543 { 544 uc_value_t *nv; 545 double d; 546 547 nv = ucv_to_number(v); 548 d = ucv_double_get(nv); 549 ucv_put(nv); 550 551 return d; 552 } 553 554 static inline int64_t 555 ucv_to_integer(uc_value_t *v) 556 { 557 uc_value_t *nv; 558 int64_t n; 559 560 nv = ucv_to_number(v); 561 n = ucv_int64_get(nv); 562 ucv_put(nv); 563 564 return n; 565 } 566 567 static inline uint64_t 568 ucv_to_unsigned(uc_value_t *v) 569 { 570 uc_value_t *nv; 571 uint64_t u; 572 573 nv = ucv_to_number(v); 574 u = ucv_uint64_get(nv); 575 ucv_put(nv); 576 577 return u; 578 } 579 580 static inline bool 581 ucv_is_callable(uc_value_t *uv) 582 { 583 switch (ucv_type(uv)) { 584 case UC_CLOSURE: 585 case UC_CFUNCTION: 586 return true; 587 588 default: 589 return false; 590 } 591 } 592 593 static inline bool 594 ucv_is_arrowfn(uc_value_t *uv) 595 { 596 uc_closure_t *closure = (uc_closure_t *)uv; 597 598 return (ucv_type(uv) == UC_CLOSURE && closure->is_arrow); 599 } 600 601 static inline bool 602 ucv_is_u64(uc_value_t *uv) 603 { 604 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->ext_flag == true && 605 uv->type == UC_INTEGER); 606 } 607 608 static inline bool 609 ucv_is_constant(uc_value_t *uv) 610 { 611 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->ext_flag == true && 612 (uv->type == UC_ARRAY || uv->type == UC_OBJECT)); 613 } 614 615 static inline bool 616 ucv_set_constant(uc_value_t *uv, bool constant) 617 { 618 if (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->ext_flag != constant && 619 (uv->type == UC_ARRAY || uv->type == UC_OBJECT)) { 620 uv->ext_flag = constant; 621 622 return true; 623 } 624 625 return false; 626 } 627 628 static inline bool 629 ucv_is_scalar(uc_value_t *uv) 630 { 631 switch (ucv_type(uv)) { 632 case UC_NULL: 633 case UC_BOOLEAN: 634 case UC_DOUBLE: 635 case UC_INTEGER: 636 case UC_STRING: 637 return true; 638 639 default: 640 return false; 641 } 642 } 643 644 bool ucv_is_equal(uc_value_t *, uc_value_t *); 645 bool ucv_is_truish(uc_value_t *); 646 647 bool ucv_compare(int, uc_value_t *, uc_value_t *, int *); 648 649 uc_value_t *ucv_key_get(uc_vm_t *, uc_value_t *, uc_value_t *); 650 uc_value_t *ucv_key_set(uc_vm_t *, uc_value_t *, uc_value_t *, uc_value_t *); 651 bool ucv_key_delete(uc_vm_t *, uc_value_t *, uc_value_t *); 652 653 654 static inline bool 655 ucv_is_marked(uc_value_t *uv) 656 { 657 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->mark == true); 658 } 659 660 static inline void 661 ucv_set_mark(uc_value_t *uv) 662 { 663 if (((uintptr_t)uv & 3) == 0 && uv != NULL) 664 uv->mark = true; 665 } 666 667 static inline void 668 ucv_clear_mark(uc_value_t *uv) 669 { 670 if (((uintptr_t)uv & 3) == 0 && uv != NULL) 671 uv->mark = false; 672 } 673 674 void ucv_gc(uc_vm_t *); 675 676 __hidden void ucv_freeall(uc_vm_t *); 677 678 #endif /* UCODE_TYPES_H */ 679
This page was automatically generated by LXR 0.3.1. • OpenWrt