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