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 u64_or_constant: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 uc_declare_vector(uc_resource_types_t, uc_resource_type_t *); 208 209 210 /* Object iteration */ 211 212 extern uc_list_t uc_object_iterators; 213 214 typedef struct { 215 uc_list_t list; 216 struct lh_entry *pos; 217 } uc_object_iterator_t; 218 219 220 /* Program structure definitions */ 221 222 uc_declare_vector(uc_sources_t, uc_source_t *); 223 uc_declare_vector(uc_modexports_t, uc_upvalref_t *); 224 225 typedef struct uc_program { 226 uc_value_t header; 227 uc_value_list_t constants; 228 uc_weakref_t functions; 229 uc_sources_t sources; 230 uc_modexports_t exports; 231 } uc_program_t; 232 233 234 /* Parser definitions */ 235 236 uc_declare_vector(uc_search_path_t, char *); 237 238 typedef struct { 239 bool lstrip_blocks; 240 bool trim_blocks; 241 bool strict_declarations; 242 bool raw_mode; 243 uc_search_path_t module_search_path; 244 uc_search_path_t force_dynlink_list; 245 bool setup_signal_handlers; 246 } uc_parse_config_t; 247 248 extern uc_parse_config_t uc_default_parse_config; 249 250 void uc_search_path_init(uc_search_path_t *search_path); 251 252 static inline void 253 uc_search_path_add(uc_search_path_t *search_path, char *path) { 254 uc_vector_push(search_path, xstrdup(path)); 255 } 256 257 static inline void 258 uc_search_path_free(uc_search_path_t *search_path) { 259 while (search_path->count > 0) 260 free(search_path->entries[--search_path->count]); 261 262 uc_vector_clear(search_path); 263 } 264 265 266 /* VM definitions */ 267 268 typedef enum { 269 EXCEPTION_NONE, 270 EXCEPTION_SYNTAX, 271 EXCEPTION_RUNTIME, 272 EXCEPTION_TYPE, 273 EXCEPTION_REFERENCE, 274 EXCEPTION_USER, 275 EXCEPTION_EXIT 276 } uc_exception_type_t; 277 278 typedef struct { 279 uc_exception_type_t type; 280 uc_value_t *stacktrace; 281 char *message; 282 } uc_exception_t; 283 284 typedef struct { 285 uint8_t *ip; 286 uc_closure_t *closure; 287 uc_cfunction_t *cfunction; 288 size_t stackframe; 289 uc_value_t *ctx; 290 bool mcall, strict; 291 } uc_callframe_t; 292 293 uc_declare_vector(uc_callframes_t, uc_callframe_t); 294 uc_declare_vector(uc_stack_t, uc_value_t *); 295 296 typedef struct printbuf uc_stringbuf_t; 297 298 typedef void (uc_exception_handler_t)(uc_vm_t *, uc_exception_t *); 299 300 struct uc_vm { 301 uc_stack_t stack; 302 uc_exception_t exception; 303 uc_callframes_t callframes; 304 uc_upvalref_t *open_upvals; 305 uc_parse_config_t *config; 306 uc_value_t *globals; 307 uc_value_t *registry; 308 uc_source_t *sources; 309 uc_weakref_t values; 310 uc_resource_types_t restypes; 311 char _reserved[sizeof(uc_modexports_t)]; 312 union { 313 uint32_t u32; 314 int32_t s32; 315 uint16_t u16; 316 int16_t s16; 317 uint8_t u8; 318 int8_t s8; 319 } arg; 320 size_t alloc_refs; 321 uint8_t trace; 322 uint8_t gc_flags; 323 uint16_t gc_interval; 324 uc_stringbuf_t *strbuf; 325 uc_exception_handler_t *exhandler; 326 FILE *output; 327 struct { 328 uint64_t raised[((UC_SYSTEM_SIGNAL_COUNT + 63) & ~63) / 64]; 329 uc_value_t *handler; 330 struct sigaction sa; 331 int sigpipe[2]; 332 } signal; 333 }; 334 335 336 /* Value API */ 337 338 __hidden void ucv_free(uc_value_t *, bool); 339 __hidden void ucv_unref(uc_weakref_t *); 340 __hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *); 341 342 uc_value_t *ucv_get(uc_value_t *uv); 343 void ucv_put(uc_value_t *); 344 345 uc_type_t ucv_type(uc_value_t *); 346 const char *ucv_typename(uc_value_t *); 347 348 uc_value_t *ucv_boolean_new(bool); 349 bool ucv_boolean_get(uc_value_t *); 350 351 uc_value_t *ucv_string_new(const char *); 352 uc_value_t *ucv_string_new_length(const char *, size_t); 353 size_t ucv_string_length(uc_value_t *); 354 355 char *_ucv_string_get(uc_value_t **); 356 #define ucv_string_get(uv) _ucv_string_get((uc_value_t **)&uv) 357 358 uc_stringbuf_t *ucv_stringbuf_new(void); 359 uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *); 360 361 void _ucv_stringbuf_append(uc_stringbuf_t *, const char *, size_t); 362 363 #define _ucv_is_literal(str) ("" str) 364 #define ucv_stringbuf_append(buf, str) _ucv_stringbuf_append(buf, _ucv_is_literal(str), sizeof(str) - 1) 365 #define ucv_stringbuf_addstr(buf, str, len) _ucv_stringbuf_append(buf, str, len) 366 #define ucv_stringbuf_printf(buf, fmt, ...) sprintbuf(buf, fmt, __VA_ARGS__) 367 368 uc_value_t *ucv_int64_new(int64_t); 369 uc_value_t *ucv_uint64_new(uint64_t); 370 int64_t ucv_int64_get(uc_value_t *); 371 uint64_t ucv_uint64_get(uc_value_t *); 372 373 uc_value_t *ucv_double_new(double); 374 double ucv_double_get(uc_value_t *); 375 376 uc_value_t *ucv_array_new(uc_vm_t *); 377 uc_value_t *ucv_array_new_length(uc_vm_t *, size_t); 378 uc_value_t *ucv_array_get(uc_value_t *, size_t); 379 uc_value_t *ucv_array_pop(uc_value_t *); 380 uc_value_t *ucv_array_push(uc_value_t *, uc_value_t *); 381 uc_value_t *ucv_array_shift(uc_value_t *); 382 uc_value_t *ucv_array_unshift(uc_value_t *, uc_value_t *); 383 void ucv_array_sort(uc_value_t *, int (*)(const void *, const void *)); 384 bool ucv_array_delete(uc_value_t *, size_t, size_t); 385 bool ucv_array_set(uc_value_t *, size_t, uc_value_t *); 386 size_t ucv_array_length(uc_value_t *); 387 388 uc_value_t *ucv_object_new(uc_vm_t *); 389 uc_value_t *ucv_object_get(uc_value_t *, const char *, bool *); 390 bool ucv_object_add(uc_value_t *, const char *, uc_value_t *); 391 void ucv_object_sort(uc_value_t *, int (*)(const void *, const void *)); 392 bool ucv_object_delete(uc_value_t *, const char *); 393 size_t ucv_object_length(uc_value_t *); 394 395 #define ucv_object_foreach(obj, key, val) \ 396 char *key = NULL; \ 397 uc_value_t *val = NULL; \ 398 struct lh_entry *entry##key; \ 399 struct lh_entry *entry_next##key = NULL; \ 400 for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL; \ 401 (entry##key ? (key = (char *)lh_entry_k(entry##key), \ 402 val = (uc_value_t *)lh_entry_v(entry##key), \ 403 entry_next##key = entry##key->next, entry##key) \ 404 : 0); \ 405 entry##key = entry_next##key) 406 407 uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t); 408 409 uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool); 410 411 uc_resource_type_t *ucv_resource_type_add(uc_vm_t *, const char *, uc_value_t *, void (*)(void *)); 412 uc_resource_type_t *ucv_resource_type_lookup(uc_vm_t *, const char *); 413 414 uc_value_t *ucv_resource_new(uc_resource_type_t *, void *); 415 void *ucv_resource_data(uc_value_t *uv, const char *); 416 void **ucv_resource_dataptr(uc_value_t *, const char *); 417 418 uc_value_t *ucv_regexp_new(const char *, bool, bool, bool, char **); 419 420 uc_value_t *ucv_upvalref_new(size_t); 421 422 uc_value_t *ucv_prototype_get(uc_value_t *); 423 bool ucv_prototype_set(uc_value_t *, uc_value_t *); 424 425 uc_value_t *ucv_property_get(uc_value_t *, const char *); 426 427 uc_value_t *ucv_from_json(uc_vm_t *, json_object *); 428 json_object *ucv_to_json(uc_value_t *); 429 430 char *ucv_to_string(uc_vm_t *, uc_value_t *); 431 char *ucv_to_jsonstring_formatted(uc_vm_t *, uc_value_t *, char, size_t); 432 void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_t, char, size_t); 433 434 #define ucv_to_jsonstring(vm, val) ucv_to_jsonstring_formatted(vm, val, '\1', 0) 435 #define ucv_to_stringbuf(vm, buf, val, json) ucv_to_stringbuf_formatted(vm, buf, val, 0, json ? '\1' : '\0', 0) 436 437 uc_type_t ucv_cast_number(uc_value_t *, int64_t *, double *); 438 439 uc_value_t *ucv_to_number(uc_value_t *); 440 441 static inline double 442 ucv_to_double(uc_value_t *v) 443 { 444 uc_value_t *nv; 445 double d; 446 447 nv = ucv_to_number(v); 448 d = ucv_double_get(nv); 449 ucv_put(nv); 450 451 return d; 452 } 453 454 static inline int64_t 455 ucv_to_integer(uc_value_t *v) 456 { 457 uc_value_t *nv; 458 int64_t n; 459 460 nv = ucv_to_number(v); 461 n = ucv_int64_get(nv); 462 ucv_put(nv); 463 464 return n; 465 } 466 467 static inline uint64_t 468 ucv_to_unsigned(uc_value_t *v) 469 { 470 uc_value_t *nv; 471 uint64_t u; 472 473 nv = ucv_to_number(v); 474 u = ucv_uint64_get(nv); 475 ucv_put(nv); 476 477 return u; 478 } 479 480 static inline bool 481 ucv_is_callable(uc_value_t *uv) 482 { 483 switch (ucv_type(uv)) { 484 case UC_CLOSURE: 485 case UC_CFUNCTION: 486 return true; 487 488 default: 489 return false; 490 } 491 } 492 493 static inline bool 494 ucv_is_arrowfn(uc_value_t *uv) 495 { 496 uc_closure_t *closure = (uc_closure_t *)uv; 497 498 return (ucv_type(uv) == UC_CLOSURE && closure->is_arrow); 499 } 500 501 static inline bool 502 ucv_is_u64(uc_value_t *uv) 503 { 504 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true && 505 uv->type == UC_INTEGER); 506 } 507 508 static inline bool 509 ucv_is_constant(uc_value_t *uv) 510 { 511 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true && 512 (uv->type == UC_ARRAY || uv->type == UC_OBJECT)); 513 } 514 515 static inline bool 516 ucv_set_constant(uc_value_t *uv, bool constant) 517 { 518 if (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant != constant && 519 (uv->type == UC_ARRAY || uv->type == UC_OBJECT)) { 520 uv->u64_or_constant = constant; 521 522 return true; 523 } 524 525 return false; 526 } 527 528 static inline bool 529 ucv_is_scalar(uc_value_t *uv) 530 { 531 switch (ucv_type(uv)) { 532 case UC_NULL: 533 case UC_BOOLEAN: 534 case UC_DOUBLE: 535 case UC_INTEGER: 536 case UC_STRING: 537 return true; 538 539 default: 540 return false; 541 } 542 } 543 544 bool ucv_is_equal(uc_value_t *, uc_value_t *); 545 bool ucv_is_truish(uc_value_t *); 546 547 bool ucv_compare(int, uc_value_t *, uc_value_t *, int *); 548 549 uc_value_t *ucv_key_get(uc_vm_t *, uc_value_t *, uc_value_t *); 550 uc_value_t *ucv_key_set(uc_vm_t *, uc_value_t *, uc_value_t *, uc_value_t *); 551 bool ucv_key_delete(uc_vm_t *, uc_value_t *, uc_value_t *); 552 553 554 static inline bool 555 ucv_is_marked(uc_value_t *uv) 556 { 557 return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->mark == true); 558 } 559 560 static inline void 561 ucv_set_mark(uc_value_t *uv) 562 { 563 if (((uintptr_t)uv & 3) == 0 && uv != NULL) 564 uv->mark = true; 565 } 566 567 static inline void 568 ucv_clear_mark(uc_value_t *uv) 569 { 570 if (((uintptr_t)uv & 3) == 0 && uv != NULL) 571 uv->mark = false; 572 } 573 574 void ucv_gc(uc_vm_t *); 575 576 __hidden void ucv_freeall(uc_vm_t *); 577 578 #endif /* UCODE_TYPES_H */ 579
This page was automatically generated by LXR 0.3.1. • OpenWrt