diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-07-30 14:02:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-30 14:02:03 +0200 |
commit | e55965a3d170f60776ffa2d82b2711d9ea3a0211 (patch) | |
tree | b73977b8e71445de9e5947d2db3bf941cc174f42 /include | |
parent | 1219d7efa170bf38fb1bf6a10fa0d1f96e62f091 (diff) | |
parent | 156d584e4d0af46c39234ee68a98a16ab4cbe225 (diff) |
Merge pull request #96 from jow-/module-import-export-support
Module import export support
Diffstat (limited to 'include')
-rw-r--r-- | include/ucode/chunk.h | 14 | ||||
-rw-r--r-- | include/ucode/lexer.h | 40 | ||||
-rw-r--r-- | include/ucode/lib.h | 4 | ||||
-rw-r--r-- | include/ucode/program.h | 20 | ||||
-rw-r--r-- | include/ucode/source.h | 13 | ||||
-rw-r--r-- | include/ucode/types.h | 65 | ||||
-rw-r--r-- | include/ucode/util.h | 5 | ||||
-rw-r--r-- | include/ucode/vallist.h | 14 | ||||
-rw-r--r-- | include/ucode/vm.h | 4 |
9 files changed, 118 insertions, 61 deletions
diff --git a/include/ucode/chunk.h b/include/ucode/chunk.h index 78d5ec6..a5f0b1c 100644 --- a/include/ucode/chunk.h +++ b/include/ucode/chunk.h @@ -24,14 +24,14 @@ #include "util.h" #include "types.h" -void uc_chunk_init(uc_chunk_t *chunk); -void uc_chunk_free(uc_chunk_t *chunk); -size_t uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t line); +__hidden void uc_chunk_init(uc_chunk_t *chunk); +__hidden void uc_chunk_free(uc_chunk_t *chunk); +__hidden size_t uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t line); -void uc_chunk_pop(uc_chunk_t *chunk); +__hidden void uc_chunk_pop(uc_chunk_t *chunk); -size_t uc_chunk_debug_get_srcpos(uc_chunk_t *chunk, size_t off); -void uc_chunk_debug_add_variable(uc_chunk_t *chunk, size_t from, size_t to, size_t slot, bool upval, uc_value_t *name); -uc_value_t *uc_chunk_debug_get_variable(uc_chunk_t *chunk, size_t off, size_t slot, bool upval); +__hidden size_t uc_chunk_debug_get_srcpos(uc_chunk_t *chunk, size_t off); +__hidden void uc_chunk_debug_add_variable(uc_chunk_t *chunk, size_t from, size_t to, size_t slot, bool upval, uc_value_t *name); +__hidden uc_value_t *uc_chunk_debug_get_variable(uc_chunk_t *chunk, size_t off, size_t slot, bool upval); #endif /* UCODE_CHUNK_H */ diff --git a/include/ucode/lexer.h b/include/ucode/lexer.h index 835bc2b..c013aac 100644 --- a/include/ucode/lexer.h +++ b/include/ucode/lexer.h @@ -117,6 +117,10 @@ typedef enum { TK_NULLISH, TK_PLACEH, TK_TEMPLATE, + TK_IMPORT, + TK_EXPORT, + TK_FROM, + TK_AS, TK_EOF, TK_ERROR @@ -124,14 +128,11 @@ typedef enum { typedef enum { UC_LEX_IDENTIFY_BLOCK, - UC_LEX_BLOCK_COMMENT_START, - UC_LEX_BLOCK_EXPRESSION_START, UC_LEX_BLOCK_EXPRESSION_EMIT_TAG, - UC_LEX_BLOCK_STATEMENT_START, UC_LEX_BLOCK_COMMENT, UC_LEX_IDENTIFY_TOKEN, - UC_LEX_PARSE_TOKEN, - UC_LEX_PLACEHOLDER, + UC_LEX_PLACEHOLDER_START, + UC_LEX_PLACEHOLDER_END, UC_LEX_EOF } uc_lex_state_t; @@ -145,19 +146,9 @@ typedef struct { uc_lex_state_t state; uc_parse_config_t *config; uc_source_t *source; - uint8_t eof:1; - uint8_t is_escape:1; - uint8_t is_placeholder:1; uint8_t no_regexp:1; uint8_t no_keyword:1; - size_t buflen; - char *buf, *bufstart, *bufend; - size_t lookbehindlen; - char *lookbehind; - const void *tok; uc_token_t curr; - char esc[5]; - uint8_t esclen; int lead_surrogate; size_t lastoff; enum { @@ -176,19 +167,24 @@ typedef struct { size_t count; size_t *entries; } templates; + struct { + size_t count; + char *entries; + } buffer; + unsigned char *rbuf; + size_t rlen, rpos; } uc_lexer_t; -void uc_lexer_init(uc_lexer_t *lex, uc_parse_config_t *config, uc_source_t *source); -void uc_lexer_free(uc_lexer_t *lex); +__hidden void uc_lexer_init(uc_lexer_t *lex, uc_parse_config_t *config, uc_source_t *source); +__hidden void uc_lexer_free(uc_lexer_t *lex); -uc_token_t *uc_lexer_next_token(uc_lexer_t *lex); +__hidden uc_token_t *uc_lexer_next_token(uc_lexer_t *lex); -bool uc_lexer_is_keyword(uc_value_t *label); +__hidden bool uc_lexer_is_keyword(uc_value_t *label); -bool utf8enc(char **out, int *rem, int code); +__hidden bool utf8enc(char **out, int *rem, int code); -const char * -uc_tokenname(unsigned type); +__hidden const char *uc_tokenname(unsigned type); #endif /* UCODE_LEXER_H */ diff --git a/include/ucode/lib.h b/include/ucode/lib.h index a80844d..4c7a3b0 100644 --- a/include/ucode/lib.h +++ b/include/ucode/lib.h @@ -31,8 +31,8 @@ extern const uc_function_list_t uc_stdlib_functions[]; void uc_stdlib_load(uc_value_t *scope); uc_cfn_ptr_t uc_stdlib_function(const char *name); -bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact); -bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off); +__hidden bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact); +__hidden bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off); /* vm helper */ diff --git a/include/ucode/program.h b/include/ucode/program.h index e8b96ed..9014ae4 100644 --- a/include/ucode/program.h +++ b/include/ucode/program.h @@ -20,7 +20,7 @@ #include "types.h" -uc_program_t *uc_program_new(uc_source_t *); +uc_program_t *uc_program_new(void); static inline uc_program_t * uc_program_get(uc_program_t *prog) { @@ -46,15 +46,19 @@ uc_program_put(uc_program_t *prog) { fn = fn##_tmp, \ fn##_tmp = (uc_function_t *)fn##_tmp->progref.prev) -uc_function_t *uc_program_function_new(uc_program_t *, const char *, size_t); -size_t uc_program_function_id(uc_program_t *, uc_function_t *); -uc_function_t *uc_program_function_load(uc_program_t *, size_t); -size_t uc_program_function_srcpos(uc_function_t *, size_t); -void uc_program_function_free(uc_function_t *); +#define uc_program_function_last(prog) (uc_function_t *)prog->functions.next +__hidden uc_function_t *uc_program_function_new(uc_program_t *, const char *, uc_source_t *, size_t); +__hidden size_t uc_program_function_id(uc_program_t *, uc_function_t *); +__hidden uc_function_t *uc_program_function_load(uc_program_t *, size_t); +__hidden uc_source_t *uc_program_function_source(uc_function_t *); +__hidden size_t uc_program_function_srcpos(uc_function_t *, size_t); +__hidden void uc_program_function_free(uc_function_t *); -uc_value_t *uc_program_get_constant(uc_program_t *, size_t); -ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *); +__hidden ssize_t uc_program_export_lookup(uc_program_t *, uc_source_t *, uc_value_t *); + +__hidden uc_value_t *uc_program_get_constant(uc_program_t *, size_t); +__hidden ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *); void uc_program_write(uc_program_t *, FILE *, bool); uc_program_t *uc_program_load(uc_source_t *, char **); diff --git a/include/ucode/source.h b/include/ucode/source.h index 6f9a8d7..e1fd211 100644 --- a/include/ucode/source.h +++ b/include/ucode/source.h @@ -35,7 +35,7 @@ typedef enum { uc_source_t *uc_source_new_file(const char *path); uc_source_t *uc_source_new_buffer(const char *name, char *buf, size_t len); -size_t uc_source_get_line(uc_source_t *source, size_t *offset); +__hidden size_t uc_source_get_line(uc_source_t *source, size_t *offset); static inline uc_source_t * uc_source_get(uc_source_t *source) { @@ -47,11 +47,14 @@ uc_source_put(uc_source_t *source) { ucv_put(source ? &source->header : NULL); } -uc_source_type_t uc_source_type_test(uc_source_t *source); +__hidden uc_source_type_t uc_source_type_test(uc_source_t *source); -void uc_source_line_next(uc_source_t *source); -void uc_source_line_update(uc_source_t *source, size_t off); +__hidden void uc_source_line_next(uc_source_t *source); +__hidden void uc_source_line_update(uc_source_t *source, size_t off); -void uc_source_runpath_set(uc_source_t *source, const char *runpath); +__hidden void uc_source_runpath_set(uc_source_t *source, const char *runpath); + +__hidden bool uc_source_export_add(uc_source_t *source, uc_value_t *name); +__hidden ssize_t uc_source_export_lookup(uc_source_t *source, uc_value_t *name); #endif /* UCODE_SOURCE_H */ diff --git a/include/ucode/types.h b/include/ucode/types.h index d1e01a1..0b63501 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -47,7 +47,7 @@ typedef enum uc_type { typedef struct uc_value { uint32_t type:4; uint32_t mark:1; - uint32_t u64:1; + uint32_t u64_or_constant:1; uint32_t refcount:26; } uc_value_t; @@ -65,6 +65,7 @@ typedef struct { /* Source buffer defintions */ uc_declare_vector(uc_lineinfo_t, uint8_t); +uc_declare_vector(uc_exports_t, uc_value_t *); typedef struct { uc_value_t header; @@ -72,6 +73,7 @@ typedef struct { FILE *fp; size_t off; uc_lineinfo_t lineinfo; + uc_exports_t exports; } uc_source_t; @@ -113,6 +115,7 @@ typedef struct uc_function { bool arrow, vararg, strict; size_t nargs; size_t nupvals; + size_t srcidx; size_t srcpos; uc_chunk_t chunk; struct uc_program *program; @@ -202,23 +205,45 @@ uc_declare_vector(uc_resource_types_t, uc_resource_type_t *); /* Program structure definitions */ +uc_declare_vector(uc_sources_t, uc_source_t *); + typedef struct uc_program { uc_value_t header; uc_value_list_t constants; uc_weakref_t functions; - uc_source_t *source; + uc_sources_t sources; } uc_program_t; /* Parser definitions */ +uc_declare_vector(uc_search_path_t, char *); + typedef struct { bool lstrip_blocks; bool trim_blocks; bool strict_declarations; bool raw_mode; + uc_search_path_t module_search_path; } uc_parse_config_t; +extern uc_parse_config_t uc_default_parse_config; + +void uc_search_path_init(uc_search_path_t *search_path); + +static inline void +uc_search_path_add(uc_search_path_t *search_path, char *path) { + uc_vector_push(search_path, xstrdup(path)); +} + +static inline void +uc_search_path_free(uc_search_path_t *search_path) { + while (search_path->count > 0) + free(search_path->entries[--search_path->count]); + + uc_vector_clear(search_path); +} + /* VM definitions */ @@ -249,6 +274,7 @@ typedef struct { uc_declare_vector(uc_callframes_t, uc_callframe_t); uc_declare_vector(uc_stack_t, uc_value_t *); +uc_declare_vector(uc_modexports_t, uc_upvalref_t *); typedef struct printbuf uc_stringbuf_t; @@ -265,6 +291,7 @@ struct uc_vm { uc_source_t *sources; uc_weakref_t values; uc_resource_types_t restypes; + uc_modexports_t exports; union { uint32_t u32; int32_t s32; @@ -283,13 +310,12 @@ struct uc_vm { /* Value API */ -void ucv_free(uc_value_t *, bool); -void ucv_put(uc_value_t *); - -void ucv_unref(uc_weakref_t *); -void ucv_ref(uc_weakref_t *, uc_weakref_t *); +__hidden void ucv_free(uc_value_t *, bool); +__hidden void ucv_unref(uc_weakref_t *); +__hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *); uc_value_t *ucv_get(uc_value_t *uv); +void ucv_put(uc_value_t *); uc_type_t ucv_type(uc_value_t *); const char *ucv_typename(uc_value_t *); @@ -448,7 +474,28 @@ ucv_is_arrowfn(uc_value_t *uv) static inline bool ucv_is_u64(uc_value_t *uv) { - return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64 == true); + return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true && + uv->type == UC_INTEGER); +} + +static inline bool +ucv_is_constant(uc_value_t *uv) +{ + return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true && + (uv->type == UC_ARRAY || uv->type == UC_OBJECT)); +} + +static inline bool +ucv_set_constant(uc_value_t *uv, bool constant) +{ + if (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant != constant && + (uv->type == UC_ARRAY || uv->type == UC_OBJECT)) { + uv->u64_or_constant = constant; + + return true; + } + + return false; } static inline bool @@ -499,6 +546,6 @@ ucv_clear_mark(uc_value_t *uv) void ucv_gc(uc_vm_t *); -void ucv_freeall(uc_vm_t *); +__hidden void ucv_freeall(uc_vm_t *); #endif /* UCODE_TYPES_H */ diff --git a/include/ucode/util.h b/include/ucode/util.h index 093951e..52303cc 100644 --- a/include/ucode/util.h +++ b/include/ucode/util.h @@ -26,6 +26,11 @@ #include <json-c/json.h> +#ifndef __hidden +#define __hidden __attribute__((visibility("hidden"))) +#endif + + /* alignment & array size */ #ifndef ALIGN diff --git a/include/ucode/vallist.h b/include/ucode/vallist.h index 3dc5720..78b9a3e 100644 --- a/include/ucode/vallist.h +++ b/include/ucode/vallist.h @@ -33,17 +33,17 @@ typedef enum { TAG_LSTR = 5 } uc_value_type_t; -uc_value_t *uc_number_parse(const char *buf, char **end); -uc_value_t *uc_number_parse_octal(const char *buf, char **end); +__hidden uc_value_t *uc_number_parse(const char *buf, char **end); +__hidden uc_value_t *uc_number_parse_octal(const char *buf, char **end); bool uc_double_pack(double d, char *buf, bool little_endian); double uc_double_unpack(const char *buf, bool little_endian); -void uc_vallist_init(uc_value_list_t *list); -void uc_vallist_free(uc_value_list_t *list); +__hidden void uc_vallist_init(uc_value_list_t *list); +__hidden void uc_vallist_free(uc_value_list_t *list); -ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value); -uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx); -uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx); +__hidden ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value); +__hidden uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx); +__hidden uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx); #endif /* UCODE_VALUE_H */ diff --git a/include/ucode/vm.h b/include/ucode/vm.h index 8377446..cc57fdb 100644 --- a/include/ucode/vm.h +++ b/include/ucode/vm.h @@ -95,7 +95,9 @@ __insn(QMCALL) \ __insn(PRINT) \ __insn(NEXTK) \ __insn(NEXTKV) \ -__insn(DELETE) +__insn(DELETE) \ +__insn(IMPORT) \ +__insn(EXPORT) #undef __insn |