MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

use computed goto for ffi

+161 -62
+1 -1
maidfile.toml
··· 21 21 [tasks.setup] 22 22 script = ['meson subprojects download', ''' 23 23 bash -c 'CC="ccache $(which clang)" \ 24 - meson setup build --wipe -Dc_args=-DNO_EXECUTE_PERMISSION' 24 + meson setup build --wipe' 25 25 '''] 26 26 27 27 [tasks.install]
+4 -2
meson.build
··· 79 79 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 80 80 81 81 version_conf = configuration_data() 82 - version_conf.set('ANT_VERSION', '0.2.2.4') 82 + version_conf.set('ANT_VERSION', '0.2.2.5') 83 83 version_conf.set('ANT_GIT_HASH', git_hash) 84 84 version_conf.set('ANT_BUILD_DATE', build_date) 85 85 ··· 93 93 94 94 add_project_arguments( 95 95 '-D JS_DUMP', 96 + '-D NO_EXECUTE_PERMISSION', 96 97 '-Wno-unused-function', 98 + '-Wno-deprecated-declarations', 97 99 language: 'c') 98 100 99 101 if host_machine.system() == 'linux' ··· 105 107 'gen_snapshot', 106 108 files('src/tools/gen_snapshot.c'), 107 109 include_directories: [include, build_include], 108 - c_args: ['-DANT_SNAPSHOT_GENERATOR'], 110 + c_args: ['-D ANT_SNAPSHOT_GENERATOR'], 109 111 native: true 110 112 ) 111 113
+156 -59
src/modules/ffi.c
··· 782 782 return js_mkundef(); 783 783 } 784 784 785 - static ffi_type *get_ffi_type(const char *type_str) { 786 - if (strcmp(type_str, "void") == 0) 787 - return &ffi_type_void; 788 - if (strcmp(type_str, "int8") == 0) 789 - return &ffi_type_sint8; 790 - if (strcmp(type_str, "int16") == 0) 791 - return &ffi_type_sint16; 792 - if (strcmp(type_str, "int") == 0) 793 - return &ffi_type_sint32; 794 - if (strcmp(type_str, "int64") == 0) 795 - return &ffi_type_sint64; 796 - if (strcmp(type_str, "uint8") == 0) 797 - return &ffi_type_uint8; 798 - if (strcmp(type_str, "uint16") == 0) 799 - return &ffi_type_uint16; 785 + typedef enum { 786 + JS_FFI_VOID = 0, 787 + JS_FFI_INT8, 788 + JS_FFI_INT16, 789 + JS_FFI_INT, 790 + JS_FFI_INT64, 791 + JS_FFI_UINT8, 792 + JS_FFI_UINT16, 793 + JS_FFI_UINT64, 794 + JS_FFI_FLOAT, 795 + JS_FFI_DOUBLE, 796 + JS_FFI_POINTER, 797 + JS_FFI_STRING, 798 + JS_FFI_UNKNOWN, 799 + JS_FFI_COUNT 800 + } js_ffi_type_id; 800 801 801 - if (strcmp(type_str, "uint64") == 0) 802 - return &ffi_type_uint64; 803 - if (strcmp(type_str, "float") == 0) 804 - return &ffi_type_float; 805 - if (strcmp(type_str, "double") == 0) 806 - return &ffi_type_double; 807 - if (strcmp(type_str, "pointer") == 0) 808 - return &ffi_type_pointer; 809 - if (strcmp(type_str, "string") == 0) 810 - return &ffi_type_pointer; 802 + static js_ffi_type_id get_ffi_type_id(const char *type_str) { 803 + if (!type_str) return JS_FFI_UNKNOWN; 811 804 812 - return NULL; 805 + switch (type_str[0]) { 806 + case 'v': if (strcmp(type_str, "void") == 0) return JS_FFI_VOID; break; 807 + case 'i': 808 + if (type_str[1] == 'n' && type_str[2] == 't') { 809 + if (type_str[3] == '\0') return JS_FFI_INT; 810 + if (strcmp(type_str + 3, "8") == 0) return JS_FFI_INT8; 811 + if (strcmp(type_str + 3, "16") == 0) return JS_FFI_INT16; 812 + if (strcmp(type_str + 3, "64") == 0) return JS_FFI_INT64; 813 + } 814 + break; 815 + case 'u': 816 + if (type_str[1] == 'i' && type_str[2] == 'n' && type_str[3] == 't') { 817 + if (strcmp(type_str + 4, "8") == 0) return JS_FFI_UINT8; 818 + if (strcmp(type_str + 4, "16") == 0) return JS_FFI_UINT16; 819 + if (strcmp(type_str + 4, "64") == 0) return JS_FFI_UINT64; 820 + } 821 + break; 822 + case 'f': if (strcmp(type_str, "float") == 0) return JS_FFI_FLOAT; break; 823 + case 'd': if (strcmp(type_str, "double") == 0) return JS_FFI_DOUBLE; break; 824 + case 'p': if (strcmp(type_str, "pointer") == 0) return JS_FFI_POINTER; break; 825 + case 's': if (strcmp(type_str, "string") == 0) return JS_FFI_STRING; break; 826 + } 827 + return JS_FFI_UNKNOWN; 828 + } 829 + 830 + static ffi_type *get_ffi_type(const char *type_str) { 831 + static ffi_type *type_map[] = { 832 + [JS_FFI_VOID] = &ffi_type_void, 833 + [JS_FFI_INT8] = &ffi_type_sint8, 834 + [JS_FFI_INT16] = &ffi_type_sint16, 835 + [JS_FFI_INT] = &ffi_type_sint32, 836 + [JS_FFI_INT64] = &ffi_type_sint64, 837 + [JS_FFI_UINT8] = &ffi_type_uint8, 838 + [JS_FFI_UINT16] = &ffi_type_uint16, 839 + [JS_FFI_UINT64] = &ffi_type_uint64, 840 + [JS_FFI_FLOAT] = &ffi_type_float, 841 + [JS_FFI_DOUBLE] = &ffi_type_double, 842 + [JS_FFI_POINTER] = &ffi_type_pointer, 843 + [JS_FFI_STRING] = &ffi_type_pointer, 844 + [JS_FFI_UNKNOWN] = NULL, 845 + }; 846 + 847 + js_ffi_type_id id = get_ffi_type_id(type_str); 848 + return type_map[id]; 813 849 } 814 850 815 851 static void *js_to_ffi_value(struct js *js, jsval_t val, ffi_type *type, void *buffer) { 816 - if (type == &ffi_type_sint8) { 852 + static const void *dispatch[] = { 853 + &&do_sint8, &&do_sint16, &&do_sint32, &&do_sint64, 854 + &&do_uint8, &&do_uint16, &&do_uint64, 855 + &&do_float, &&do_double, &&do_pointer, &&do_done 856 + }; 857 + 858 + int idx; 859 + if (type == &ffi_type_sint8) idx = 0; 860 + else if (type == &ffi_type_sint16) idx = 1; 861 + else if (type == &ffi_type_sint32) idx = 2; 862 + else if (type == &ffi_type_sint64) idx = 3; 863 + else if (type == &ffi_type_uint8) idx = 4; 864 + else if (type == &ffi_type_uint16) idx = 5; 865 + else if (type == &ffi_type_uint64) idx = 6; 866 + else if (type == &ffi_type_float) idx = 7; 867 + else if (type == &ffi_type_double) idx = 8; 868 + else if (type == &ffi_type_pointer) idx = 9; 869 + else idx = 10; 870 + 871 + goto *dispatch[idx]; 872 + 873 + do_sint8: { 817 874 int8_t v = (int8_t)js_getnum(val); 818 875 memcpy(buffer, &v, sizeof(v)); 819 - } else if (type == &ffi_type_sint16) { 876 + goto do_done; 877 + } 878 + do_sint16: { 820 879 int16_t v = (int16_t)js_getnum(val); 821 880 memcpy(buffer, &v, sizeof(v)); 822 - } else if (type == &ffi_type_sint32) { 881 + goto do_done; 882 + } 883 + do_sint32: { 823 884 int32_t v = (int32_t)js_getnum(val); 824 885 memcpy(buffer, &v, sizeof(v)); 825 - } else if (type == &ffi_type_sint64) { 886 + goto do_done; 887 + } 888 + do_sint64: { 826 889 int64_t v = (int64_t)js_getnum(val); 827 890 memcpy(buffer, &v, sizeof(v)); 828 - } else if (type == &ffi_type_uint8) { 891 + goto do_done; 892 + } 893 + do_uint8: { 829 894 uint8_t v = (uint8_t)js_getnum(val); 830 895 memcpy(buffer, &v, sizeof(v)); 831 - } else if (type == &ffi_type_uint16) { 896 + goto do_done; 897 + } 898 + do_uint16: { 832 899 uint16_t v = (uint16_t)js_getnum(val); 833 900 memcpy(buffer, &v, sizeof(v)); 834 - } else if (type == &ffi_type_uint64) { 901 + goto do_done; 902 + } 903 + do_uint64: { 835 904 uint64_t v = (uint64_t)js_getnum(val); 836 905 memcpy(buffer, &v, sizeof(v)); 837 - } else if (type == &ffi_type_float) { 906 + goto do_done; 907 + } 908 + do_float: { 838 909 float v = (float)js_getnum(val); 839 910 memcpy(buffer, &v, sizeof(v)); 840 - } else if (type == &ffi_type_double) { 911 + goto do_done; 912 + } 913 + do_double: { 841 914 double v = js_getnum(val); 842 915 memcpy(buffer, &v, sizeof(v)); 843 - } else if (type == &ffi_type_pointer) { 916 + goto do_done; 917 + } 918 + do_pointer: { 844 919 if (js_type(val) == JS_STR) { 845 920 size_t str_len; 846 921 const char *str = js_getstr(js, val, &str_len); ··· 850 925 void *ptr = (void *)(uint64_t)js_getnum(val); 851 926 memcpy(buffer, &ptr, sizeof(ptr)); 852 927 } 928 + goto do_done; 853 929 } 854 - 930 + do_done: 855 931 return buffer; 856 932 } 857 933 858 934 static jsval_t ffi_to_js_value(struct js *js, void *val, ffi_type *type, const char *type_str) { 859 - if (type == &ffi_type_void) { 860 - return js_mkundef(); 861 - } else if (type == &ffi_type_sint8) { 862 - return js_mknum((double)(*(int8_t *)val)); 863 - } else if (type == &ffi_type_sint16) { 864 - return js_mknum((double)(*(int16_t *)val)); 865 - } else if (type == &ffi_type_sint32) { 866 - return js_mknum((double)(*(int32_t *)val)); 867 - } else if (type == &ffi_type_sint64) { 868 - return js_mknum((double)(*(int64_t *)val)); 869 - } else if (type == &ffi_type_uint8) { 870 - return js_mknum((double)(*(uint8_t *)val)); 871 - } else if (type == &ffi_type_uint16) { 872 - return js_mknum((double)(*(uint16_t *)val)); 935 + static const void *dispatch[] = { 936 + &&ret_void, &&ret_sint8, &&ret_sint16, &&ret_sint32, &&ret_sint64, 937 + &&ret_uint8, &&ret_uint16, &&ret_uint64, 938 + &&ret_float, &&ret_double, &&ret_pointer, &&ret_undef 939 + }; 940 + 941 + int idx; 942 + if (type == &ffi_type_void) idx = 0; 943 + else if (type == &ffi_type_sint8) idx = 1; 944 + else if (type == &ffi_type_sint16) idx = 2; 945 + else if (type == &ffi_type_sint32) idx = 3; 946 + else if (type == &ffi_type_sint64) idx = 4; 947 + else if (type == &ffi_type_uint8) idx = 5; 948 + else if (type == &ffi_type_uint16) idx = 6; 949 + else if (type == &ffi_type_uint64) idx = 7; 950 + else if (type == &ffi_type_float) idx = 8; 951 + else if (type == &ffi_type_double) idx = 9; 952 + else if (type == &ffi_type_pointer) idx = 10; 953 + else idx = 11; 954 + 955 + goto *dispatch[idx]; 873 956 874 - } else if (type == &ffi_type_uint64) { 875 - return js_mknum((double)(*(uint64_t *)val)); 876 - } else if (type == &ffi_type_float) { 877 - return js_mknum((double)(*(float *)val)); 878 - } else if (type == &ffi_type_double) { 879 - return js_mknum((double)(*(double *)val)); 880 - } else if (type == &ffi_type_pointer) { 957 + ret_void: 958 + return js_mkundef(); 959 + ret_sint8: 960 + return js_mknum((double)(*(int8_t *)val)); 961 + ret_sint16: 962 + return js_mknum((double)(*(int16_t *)val)); 963 + ret_sint32: 964 + return js_mknum((double)(*(int32_t *)val)); 965 + ret_sint64: 966 + return js_mknum((double)(*(int64_t *)val)); 967 + ret_uint8: 968 + return js_mknum((double)(*(uint8_t *)val)); 969 + ret_uint16: 970 + return js_mknum((double)(*(uint16_t *)val)); 971 + ret_uint64: 972 + return js_mknum((double)(*(uint64_t *)val)); 973 + ret_float: 974 + return js_mknum((double)(*(float *)val)); 975 + ret_double: 976 + return js_mknum((double)(*(double *)val)); 977 + ret_pointer: { 881 978 void *ptr = *(void **)val; 882 979 if (type_str && strcmp(type_str, "string") == 0 && ptr) { 883 980 const char *str = (const char *)ptr; ··· 885 982 } 886 983 return js_mknum((double)(uint64_t)ptr); 887 984 } 888 - 985 + ret_undef: 889 986 return js_mkundef(); 890 987 }