diff --git a/.gitignore b/.gitignore index 4e6b2e3..be19517 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /build /traces +/src/strip/target /javascript-zoo /vendor/*/ diff --git a/examples/ffi/basic/printf.js b/examples/ffi/basic/printf.ts similarity index 74% rename from examples/ffi/basic/printf.js rename to examples/ffi/basic/printf.ts index 1b34bea..09e63bf 100644 --- a/examples/ffi/basic/printf.js +++ b/examples/ffi/basic/printf.ts @@ -1,17 +1,20 @@ import { dlopen, suffix, FFIType } from 'ant:ffi'; -let libcName; +let libcName: string; if (process.platform === 'darwin') { libcName = `libSystem.${suffix}`; } else if (process.platform === 'linux') { libcName = `libc.${suffix}`; } else if (process.platform === 'win32') { libcName = `msvcrt.${suffix}`; -} else { - throw new Error(`Unsupported platform: ${process.platform}`); +} else throw new Error(`Unsupported platform: ${process.platform}`); + +interface libC { + putchar(c: number): number; + printf(format: string, ...args: unknown[]): number; } -const libc = dlopen(libcName); +const libc = dlopen(libcName); libc.define('putchar', { args: [FFIType.int], diff --git a/meson.build b/meson.build index 958cda4..b74e50e 100644 --- a/meson.build +++ b/meson.build @@ -81,6 +81,25 @@ libuv_dep = subproject('libuv', default_options: [ 'build_benchmarks=false' ]).get_variable('libuv_dep') +cargo = find_program('cargo', required: true) +cp = find_program('cp', required: true) +strip_include = include_directories('src/strip') + +oxc_lib = custom_target( + 'oxc_strip', + output: 'liboxc.a', + command: [ + 'sh', '-c', + cargo.full_path() + ' build --release ' + + '--manifest-path ' + meson.project_source_root() / 'src' / 'strip' / 'Cargo.toml' + ' ' + + '--target-dir ' + meson.project_build_root() / 'oxc-target' + + ' && ' + cp.full_path() + ' ' + meson.project_build_root() / 'oxc-target' / 'release' / 'liboxc.a' + ' @OUTPUT@' + ], + build_by_default: true +) + +oxc_dep = declare_dependency(link_with: oxc_lib) + git = find_program('git', required: false) if git.found() git_commit = run_command(git, 'rev-parse', '--short=10', 'HEAD', check: false) @@ -96,7 +115,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.3.3.8') +version_conf.set('ANT_VERSION', '0.3.3.9') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) @@ -146,10 +165,10 @@ snapshot_h = custom_target( ant_deps = [ libffi_dep, bdwgc_dep, uuid_dep, - llhttp, pcre2_dep, libuv_dep, + llhttp, pcre2_dep, libuv_dep, oxc_dep, argtable3_dep, tlsuv_dep, libsodium_dep, yyjson_dep, minicoro_dep, uuidv7_dep, - openssl_dep, zlib_dep, uthash_dep + openssl_dep, zlib_dep, uthash_dep, ] if host_machine.system() == 'darwin' @@ -159,6 +178,6 @@ endif executable( 'ant', sources + [snapshot_h], - include_directories: [include, build_include], + include_directories: [include, build_include, strip_include], dependencies: ant_deps ) diff --git a/src/main.c b/src/main.c index 28c32b1..21b34fa 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -42,8 +43,22 @@ #include "modules/navigator.h" #include "modules/child_process.h" + int js_result = EXIT_SUCCESS; +static int is_typescript_file(const char *filename) { + if (filename == NULL) return 0; + size_t len = strlen(filename); + if (len < 3) return 0; + + const char *ext = filename + len; + while (ext > filename && *(ext - 1) != '.' && *(ext - 1) != '/') ext--; + if (ext == filename || *(ext - 1) != '.') return 0; + ext--; + + return (strcmp(ext, ".ts") == 0 || strcmp(ext, ".mts") == 0 || strcmp(ext, ".cts") == 0); +} + static void eval_code(struct js *js, struct arg_str *eval, struct arg_lit *print) { const char *script = eval->sval[0]; size_t len = strlen(script); @@ -111,13 +126,26 @@ static int execute_module(struct js *js, const char *filename) { if (realpath(filename, abs_path) != NULL) { use_path = abs_path; } else use_path = filename; + + char *js_code = buffer; + size_t js_len = len; + + if (is_typescript_file(filename)) { + int result = OXC_strip_types(buffer, filename, buffer, len + 1); + if (result < 0) { + fprintf(stderr, "TypeScript error: strip failed (%d)\n", result); + free(buffer); + return EXIT_FAILURE; + } + js_len = (size_t)result; + } js_set_filename(js, use_path); js_setup_import_meta(js, use_path); js_mkscope(js); - jsval_t result = js_eval(js, buffer, len); - free(buffer); + jsval_t result = js_eval(js, js_code, js_len); + free(js_code); if (js_type(result) == JS_ERR) { fprintf(stderr, "%s\n", js_str(js, result)); diff --git a/src/strip/Cargo.lock b/src/strip/Cargo.lock new file mode 100644 index 0000000..103dc2c --- /dev/null +++ b/src/strip/Cargo.lock @@ -0,0 +1,1007 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64-simd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339abbe78e73178762e23bea9dfd08e697eb3f3301cd4be981c0f78ba5859195" +dependencies = [ + "outref", + "vsimd", +] + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" +dependencies = [ + "allocator-api2", +] + +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror", +] + +[[package]] +name = "compact_str" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb1325a1cece981e8a296ab8f0f9b63ae357bd0784a9faaf548cc7b480707a" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "rustversion", + "ryu", + "static_assertions", +] + +[[package]] +name = "cow-utils" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "deranged" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ececcb659e7ba858fb4f10388c250a7252eb0a27373f1a72b8748afdd248e587" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "dragonbox_ecma" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d742b56656e8b14d63e7ea9806597b1849ae25412584c8adf78c0f67bd985e66" + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "flate2" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfe33edd8e85a12a67454e37f8c75e730830d83e313556ab9ebf9ee7fbeb3bfb" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "allocator-api2", +] + +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" + +[[package]] +name = "json-escape-simd" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3c2a6c0b4b5637c41719973ef40c6a1cf564f9db6958350de6193fbee9c23f5" + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + +[[package]] +name = "nonmax" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51" + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "outref" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e" + +[[package]] +name = "owo-colors" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c6901729fa79e91a0913333229e9ca5dc725089d1c363b2f4b4760709dc4a52" + +[[package]] +name = "oxc" +version = "0.0.0" +dependencies = [ + "oxc_allocator", + "oxc_codegen", + "oxc_parser", + "oxc_semantic", + "oxc_span", + "oxc_transformer", +] + +[[package]] +name = "oxc-browserslist" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d55ecaab3af16fee2d0195f2e7d26f27cab0044d0b6d74073514c7feee6beb" +dependencies = [ + "flate2", + "nom", + "postcard", + "rustc-hash", + "serde", + "serde_json", + "thiserror", + "time", +] + +[[package]] +name = "oxc-miette" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a7ba54c704edefead1f44e9ef09c43e5cfae666bdc33516b066011f0e6ebf7" +dependencies = [ + "cfg-if", + "owo-colors", + "oxc-miette-derive", + "textwrap", + "thiserror", + "unicode-segmentation", + "unicode-width", +] + +[[package]] +name = "oxc-miette-derive" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4faecb54d0971f948fbc1918df69b26007e6f279a204793669542e1e8b75eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "oxc_allocator" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "377063b29ba9762002f07bd4bc5cdd60373a4e3b094e15ea201eec07556d65ff" +dependencies = [ + "allocator-api2", + "bumpalo", + "hashbrown", + "oxc_data_structures", + "rustc-hash", +] + +[[package]] +name = "oxc_ast" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4d2aed570578cfe5249d1e50736971b85c4eedc39f9777f735914e10d42dd7" +dependencies = [ + "bitflags", + "oxc_allocator", + "oxc_ast_macros", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_estree", + "oxc_regular_expression", + "oxc_span", + "oxc_syntax", +] + +[[package]] +name = "oxc_ast_macros" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d5f0089ba8705ef065f8acb371c3ab44e3a7fb90372cccac6788b677e763f1" +dependencies = [ + "phf", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "oxc_ast_visit" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45305537949b2471ce9afe251a2051af6ed2188492cd079b1d83b96caa6714b5" +dependencies = [ + "oxc_allocator", + "oxc_ast", + "oxc_span", + "oxc_syntax", +] + +[[package]] +name = "oxc_codegen" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf3d7a97d49d6557d315f7b806a246a6627aec4942b9bd50e670686be6d3b00" +dependencies = [ + "bitflags", + "cow-utils", + "dragonbox_ecma", + "itoa", + "oxc_allocator", + "oxc_ast", + "oxc_data_structures", + "oxc_index", + "oxc_semantic", + "oxc_sourcemap", + "oxc_span", + "oxc_syntax", + "rustc-hash", +] + +[[package]] +name = "oxc_compat" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d772c9461d67a0bd1b8177674f73d7d666f8120bab90bfc37383eace93bcd82" +dependencies = [ + "cow-utils", + "oxc-browserslist", + "oxc_syntax", + "rustc-hash", + "serde", +] + +[[package]] +name = "oxc_data_structures" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0264dbee1c186f744a433955a900a34f7a8bf6e4381d00100528d2b23043b21" +dependencies = [ + "ropey", +] + +[[package]] +name = "oxc_diagnostics" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd5d75130504c4b6792eba9a687349ec92cc7e220ddfc1cba35ef540c194b6ed" +dependencies = [ + "cow-utils", + "oxc-miette", + "percent-encoding", +] + +[[package]] +name = "oxc_ecmascript" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12400e655a18fe9ba1359324d7a634964f62d9a151008a48671e46abd9b728e1" +dependencies = [ + "cow-utils", + "num-bigint", + "num-traits", + "oxc_allocator", + "oxc_ast", + "oxc_span", + "oxc_syntax", +] + +[[package]] +name = "oxc_estree" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2059abb4c194b588d128c1933b6c20fde1b443b053e51da818caed17c272f65" + +[[package]] +name = "oxc_index" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3e6120999627ec9703025eab7c9f410ebb7e95557632a8902ca48210416c2b" +dependencies = [ + "nonmax", + "serde", +] + +[[package]] +name = "oxc_parser" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090741583620244210f9988d13e7ddec78d8c22b13f885eb8d31c6566528c943" +dependencies = [ + "bitflags", + "cow-utils", + "memchr", + "num-bigint", + "num-traits", + "oxc_allocator", + "oxc_ast", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_regular_expression", + "oxc_span", + "oxc_syntax", + "rustc-hash", + "seq-macro", +] + +[[package]] +name = "oxc_regular_expression" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47fe9458baea58cfe0d514b32a3258839f9cad1af738a92c1c55ccea97c5ceb7" +dependencies = [ + "bitflags", + "oxc_allocator", + "oxc_ast_macros", + "oxc_diagnostics", + "oxc_span", + "phf", + "rustc-hash", + "unicode-id-start", +] + +[[package]] +name = "oxc_semantic" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e425a107259ff3417682df38a9964491013de0080aecbaab7e43eb1f452b6de1" +dependencies = [ + "itertools", + "memchr", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_index", + "oxc_span", + "oxc_syntax", + "rustc-hash", + "self_cell", +] + +[[package]] +name = "oxc_sourcemap" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36801dbbd025f2fa133367494e38eef75a53d334ae6746ba0c889fc4e76fa3a3" +dependencies = [ + "base64-simd", + "json-escape-simd", + "rustc-hash", + "serde", + "serde_json", +] + +[[package]] +name = "oxc_span" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb492189296521cf459f4caaee8d7525c70341daf10083c53c962c13e4748ee" +dependencies = [ + "compact_str", + "oxc-miette", + "oxc_allocator", + "oxc_ast_macros", + "oxc_estree", +] + +[[package]] +name = "oxc_syntax" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c09691f16fb7ed90acb442e88faca68751f2222dbed892122a9b50fc7d161c8d" +dependencies = [ + "bitflags", + "cow-utils", + "dragonbox_ecma", + "nonmax", + "oxc_allocator", + "oxc_ast_macros", + "oxc_data_structures", + "oxc_estree", + "oxc_index", + "oxc_span", + "phf", + "unicode-id-start", +] + +[[package]] +name = "oxc_transformer" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11b2b72d1e073c39b4993d7dfd71daeb7ea6fb445f5c8b445be6998022ee4f76" +dependencies = [ + "base64", + "compact_str", + "indexmap", + "itoa", + "memchr", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_compat", + "oxc_data_structures", + "oxc_diagnostics", + "oxc_ecmascript", + "oxc_regular_expression", + "oxc_semantic", + "oxc_span", + "oxc_syntax", + "oxc_traverse", + "rustc-hash", + "serde", + "serde_json", + "sha1", +] + +[[package]] +name = "oxc_traverse" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dcaa9168b7ae1f7df2461e33210605bbe60dd2b638af08a7093310c232f1bda" +dependencies = [ + "itoa", + "oxc_allocator", + "oxc_ast", + "oxc_ast_visit", + "oxc_data_structures", + "oxc_ecmascript", + "oxc_semantic", + "oxc_span", + "oxc_syntax", + "rustc-hash", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_macros", + "phf_shared", + "serde", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_macros" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "proc-macro2" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ropey" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93411e420bcd1a75ddd1dc3caf18c23155eda2c090631a85af21ba19e97093b5" +dependencies = [ + "smallvec", + "str_indices", +] + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" + +[[package]] +name = "self_cell" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89" + +[[package]] +name = "seq-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "simd-adler32" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2" + +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "smawk" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "str_indices" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "textwrap" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.3.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b" + +[[package]] +name = "typenum" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" + +[[package]] +name = "unicode-id-start" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b79ad29b5e19de4260020f8919b443b2ef0277d242ce532ec7b7a2cc8b6007" + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "vsimd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3082ca00d5a5ef149bb8b555a72ae84c9c59f7250f013ac822ac2e49b19c64" + +[[package]] +name = "zmij" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fc5a66a20078bf1251bde995aa2fdcc4b800c70b5d92dd2c62abc5c60f679f8" diff --git a/src/strip/Cargo.toml b/src/strip/Cargo.toml new file mode 100644 index 0000000..51fc76c --- /dev/null +++ b/src/strip/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "oxc" +edition = "2024" + +[lib] +path = "oxc.rs" +crate-type = ["staticlib", "cdylib"] + +[dependencies] +oxc_allocator = "0.107.0" +oxc_parser = "0.107.0" +oxc_span = "0.107.0" +oxc_codegen = "0.107.0" +oxc_transformer = "0.107.0" +oxc_semantic = "0.107.0" + +[profile.release] +lto = true +codegen-units = 1 +panic = "abort" +opt-level = "z" +strip = true diff --git a/src/strip/oxc.h b/src/strip/oxc.h new file mode 100644 index 0000000..d0eb268 --- /dev/null +++ b/src/strip/oxc.h @@ -0,0 +1,19 @@ +#ifndef OXC_STRIP_H +#define OXC_STRIP_H + +#include + +#define OXC_ERR_NULL_INPUT -1 +#define OXC_ERR_INVALID_UTF8 -2 +#define OXC_ERR_PARSE_FAILED -3 +#define OXC_ERR_TRANSFORM_FAILED -4 +#define OXC_ERR_OUTPUT_TOO_LARGE -5 + +int OXC_strip_types( + const char *input, + const char *filename, + char *output, + size_t output_len +); + +#endif diff --git a/src/strip/oxc.rs b/src/strip/oxc.rs new file mode 100644 index 0000000..6ebb6bf --- /dev/null +++ b/src/strip/oxc.rs @@ -0,0 +1,90 @@ +use std::ffi::{c_char, c_int, CStr}; +use std::path::Path; +use std::ptr; + +use oxc_allocator::Allocator; +use oxc_codegen::Codegen; +use oxc_parser::Parser; +use oxc_semantic::SemanticBuilder; +use oxc_span::SourceType; +use oxc_transformer::{TransformOptions, Transformer, TypeScriptOptions}; + +pub const OXC_ERR_NULL_INPUT: c_int = -1; +pub const OXC_ERR_INVALID_UTF8: c_int = -2; +pub const OXC_ERR_TRANSFORM_FAILED: c_int = -4; +pub const OXC_ERR_OUTPUT_TOO_LARGE: c_int = -5; + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn OXC_strip_types( + input: *const c_char, + filename: *const c_char, + output: *mut c_char, + output_len: usize, +) -> c_int { + if input.is_null() || output.is_null() { + return OXC_ERR_NULL_INPUT; + } + + let filename_str = match unsafe { CStr::from_ptr(filename).to_str() } { + Ok(s) => s, + Err(_) => return OXC_ERR_INVALID_UTF8, + }; + + let input_str = match unsafe { CStr::from_ptr(input).to_str() } { + Ok(s) => s, + Err(_) => return OXC_ERR_INVALID_UTF8, + }; + + match strip_types_internal(input_str, filename_str) { + Ok(result) => { + let bytes = result.as_bytes(); + if bytes.len() + 1 > output_len { return OXC_ERR_OUTPUT_TOO_LARGE; } + unsafe { + ptr::copy_nonoverlapping(bytes.as_ptr(), output as *mut u8, bytes.len()); + *output.add(bytes.len()) = 0; + } + bytes.len() as c_int + } + Err(_) => OXC_ERR_TRANSFORM_FAILED, + } +} + +fn strip_types_internal(source: &str, filename: &str) -> Result { + let allocator = Allocator::default(); + let source_type = SourceType::from_path(filename).unwrap_or_else(|_| SourceType::ts()); + let parser_ret = Parser::new(&allocator, source, source_type).parse(); + + if !parser_ret.errors.is_empty() { + let errors: Vec = parser_ret.errors.iter().map(|e| e.to_string()).collect(); + return Err(format!("Parse errors: {}", errors.join("; "))); + } + + let mut program = parser_ret.program; + let semantic_ret = SemanticBuilder::new().build(&program); + + if !semantic_ret.errors.is_empty() { + let errors: Vec = semantic_ret.errors.iter().map(|e| e.to_string()).collect(); + return Err(format!("Semantic errors: {}", errors.join("; "))); + } + + let scoping = semantic_ret.semantic.into_scoping(); + let transform_options = TransformOptions { + typescript: TypeScriptOptions { + only_remove_type_imports: true, + ..Default::default() + }, + ..Default::default() + }; + + let source_path = Path::new(filename); + let transformer = Transformer::new(&allocator, source_path, &transform_options); + let ret = transformer.build_with_scoping(scoping, &mut program); + + if !ret.errors.is_empty() { + let errors: Vec = ret.errors.iter().map(|e| e.to_string()).collect(); + return Err(format!("Transform errors: {}", errors.join("; "))); + } + + let output = Codegen::new().build(&program).code; + Ok(output) +} diff --git a/src/types/modules/child_process.d.ts b/src/types/modules/child_process.d.ts index 1f75ced..f84a0bd 100644 --- a/src/types/modules/child_process.d.ts +++ b/src/types/modules/child_process.d.ts @@ -48,3 +48,11 @@ declare module 'child_process' { function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): SpawnResult; function fork(modulePath: string, options?: ForkOptions): ChildProcess & Promise; } + +declare module 'ant:child_process' { + export * from 'events'; +} + +declare module 'node:child_process' { + export * from 'events'; +} diff --git a/src/types/modules/crypto.d.ts b/src/types/modules/crypto.d.ts index d5c1b57..b694a3a 100644 --- a/src/types/modules/crypto.d.ts +++ b/src/types/modules/crypto.d.ts @@ -13,3 +13,11 @@ declare module 'crypto' { function randomUUID(): string; function getRandomValues(array: T): T; } + +declare module 'ant:crypto' { + export * from 'events'; +} + +declare module 'node:crypto' { + export * from 'events'; +} diff --git a/src/types/modules/events.d.ts b/src/types/modules/events.d.ts index 73e028b..53ffac8 100644 --- a/src/types/modules/events.d.ts +++ b/src/types/modules/events.d.ts @@ -11,3 +11,11 @@ declare module 'events' { eventNames(): string[]; } } + +declare module 'ant:events' { + export * from 'events'; +} + +declare module 'node:events' { + export * from 'events'; +} diff --git a/src/types/modules/ffi.d.ts b/src/types/modules/ffi.d.ts index cf49828..47eb686 100644 --- a/src/types/modules/ffi.d.ts +++ b/src/types/modules/ffi.d.ts @@ -1,19 +1,4 @@ -declare module 'ffi' { - type FFIType = - | 'void' - | 'int8' - | 'int16' - | 'int' - | 'int64' - | 'uint8' - | 'uint16' - | 'uint64' - | 'float' - | 'double' - | 'pointer' - | 'string' - | '...'; - +declare module 'ant:ffi' { interface FFITypeConstants { void: 'void'; int8: 'int8'; @@ -30,26 +15,29 @@ declare module 'ffi' { spread: '...'; } + type FFIType = FFITypeConstants[keyof FFITypeConstants]; + interface FunctionSignature { returns: FFIType; args: FFIType[]; } interface FFILibrary { - define(name: string, signature: FunctionSignature | [FFIType, FFIType[]]): (...args: unknown[]) => unknown; + define(name: string, signature: FunctionSignature | [FFIType, FFIType[]]): void; call(name: string, ...args: unknown[]): unknown; } const suffix: 'dylib' | 'so' | 'dll'; const FFIType: FFITypeConstants; - function dlopen(libraryPath: string): FFILibrary; function alloc(size: number): number; function free(ptr: number): void; function read(ptr: number, size: number, type?: FFIType): unknown; function write(ptr: number, value: unknown, type?: FFIType): void; function pointer(value: unknown): number; - function callback(signature: FunctionSignature | [FFIType, FFIType[]], fn: (...args: unknown[]) => unknown): number; function freeCallback(ptr: number): void; function readPtr(ptr: number): number; + + function callback(signature: FunctionSignature | [FFIType, FFIType[]], fn: (...args: unknown[]) => unknown): number; + function dlopen(libraryPath: string): FFILibrary & T; } diff --git a/src/types/modules/fs.d.ts b/src/types/modules/fs.d.ts index c24572b..1969540 100644 --- a/src/types/modules/fs.d.ts +++ b/src/types/modules/fs.d.ts @@ -28,3 +28,11 @@ declare module 'fs' { function readdir(path: string): Promise; function readdirSync(path: string): string[]; } + +declare module 'ant:fs' { + export * from 'fs'; +} + +declare module 'node:fs' { + export * from 'fs'; +} diff --git a/src/types/modules/os.d.ts b/src/types/modules/os.d.ts index 68aa43c..8e076f2 100644 --- a/src/types/modules/os.d.ts +++ b/src/types/modules/os.d.ts @@ -106,3 +106,11 @@ declare module 'os' { function setPriority(priority: number): void; function setPriority(pid: number, priority: number): void; } + +declare module 'ant:os' { + export * from 'os'; +} + +declare module 'node:os' { + export * from 'os'; +} diff --git a/src/types/modules/path.d.ts b/src/types/modules/path.d.ts index 1d58cac..43a0482 100644 --- a/src/types/modules/path.d.ts +++ b/src/types/modules/path.d.ts @@ -21,3 +21,11 @@ declare module 'path' { function parse(path: string): ParsedPath; function format(pathObject: Partial): string; } + +declare module 'ant:path' { + export * from 'path'; +} + +declare module 'node:path' { + export * from 'path'; +} diff --git a/src/types/modules/shell.d.ts b/src/types/modules/shell.d.ts index 3294837..7db925d 100644 --- a/src/types/modules/shell.d.ts +++ b/src/types/modules/shell.d.ts @@ -1,4 +1,4 @@ -declare module 'shell' { +declare module 'ant:shell' { interface ShellResult { stdout: string; stderr: string;