From 58d9de477f4242f03e08ce73fd2d419133850ae5 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Wed, 8 Apr 2026 12:41:34 -0700 Subject: [PATCH] clean up unused slots --- include/common.h | 153 +++++++++--------- include/oxc.h | 15 -- src/ant.c | 11 -- src/modules/io.c | 71 +++----- src/silver/glue.c | 5 +- src/silver/ops/coercion.h | 12 +- src/silver/ops/upvalues.h | 5 +- src/strip/Cargo.lock | 45 ------ src/strip/Cargo.toml | 6 +- src/strip/src/collector.rs | 113 ------------- src/strip/src/ffi.rs | 124 +------------- src/strip/src/lib.rs | 2 - src/utils.c | 4 +- .../wasm-micro-runtime/meson.build | 2 +- 14 files changed, 108 insertions(+), 460 deletions(-) delete mode 100644 src/strip/src/collector.rs diff --git a/include/common.h b/include/common.h index cb89457..a5a3271 100644 --- a/include/common.h +++ b/include/common.h @@ -5,89 +5,82 @@ #include #include +#define ANT_INTERNAL_SLOT_LIST(X) \ + X(SLOT_NONE) \ + X(SLOT_ASYNC) \ + X(SLOT_CODE) \ + X(SLOT_CODE_LEN) \ + X(SLOT_CFUNC) \ + X(SLOT_CORO) \ + X(SLOT_PROTO) \ + X(SLOT_FUNC_PROTO) \ + X(SLOT_ASYNC_PROTO) \ + X(SLOT_AUX) \ + X(SLOT_TARGET_FUNC) \ + X(SLOT_MODULE_CTX) \ + X(SLOT_MODULE_LOADING) \ + X(SLOT_MAP) \ + X(SLOT_SET) \ + X(SLOT_PRIMITIVE) \ + X(SLOT_PROXY_REF) \ + X(SLOT_BUILTIN) \ + X(SLOT_BRAND) \ + X(SLOT_DATA) \ + X(SLOT_CTOR) \ + X(SLOT_FS_FLAGS) \ + X(SLOT_DEFAULT) \ + X(SLOT_ERROR_BRAND) \ + X(SLOT_ERR_TYPE) \ + X(SLOT_OBSERVABLE_SUBSCRIBER) \ + X(SLOT_SUBSCRIPTION_OBSERVER) \ + X(SLOT_SUBSCRIPTION_CLEANUP) \ + X(SLOT_STRICT_ARGS) \ + X(SLOT_ITER_STATE) \ + X(SLOT_ENTRIES) \ + X(SLOT_SETTLED) \ + X(SLOT_WT_ON_MESSAGE) \ + X(SLOT_WT_ONCE_MESSAGE) \ + X(SLOT_WT_ON_EXIT) \ + X(SLOT_WT_ONCE_EXIT) \ + X(SLOT_WT_PORT_TAG) \ + X(SLOT_WT_PORT_QUEUE) \ + X(SLOT_WT_PORT_HEAD) \ + X(SLOT_WT_PORT_PEER) \ + X(SLOT_WT_PORT_CLOSED) \ + X(SLOT_WT_PORT_STARTED) \ + X(SLOT_WT_PORT_ON_MESSAGE) \ + X(SLOT_WT_PORT_ONCE_MESSAGE) \ + X(SLOT_WT_PORT_PROTO) \ + X(SLOT_WT_ENV_STORE) \ + X(SLOT_NAPI_EXTERNAL_ID) \ + X(SLOT_NAPI_WRAP_ID) \ + X(SLOT_RS_PULL) \ + X(SLOT_RS_CANCEL) \ + X(SLOT_RS_SIZE) \ + X(SLOT_RS_CLOSED) \ + X(SLOT_WS_WRITE) \ + X(SLOT_WS_CLOSE) \ + X(SLOT_WS_ABORT) \ + X(SLOT_WS_READY) \ + X(SLOT_WS_SIGNAL) \ + X(SLOT_HEADERS_GUARD) \ + X(SLOT_REQUEST_HEADERS) \ + X(SLOT_REQUEST_SIGNAL) \ + X(SLOT_REQUEST_ABORT_REASON) \ + X(SLOT_REQUEST_BODY_STREAM) \ + X(SLOT_RESPONSE_HEADERS) \ + X(SLOT_RESPONSE_BODY_STREAM) \ + X(SLOT_PIPE_ABORT_LISTENER) \ + X(SLOT_MATCHALL_RX) \ + X(SLOT_MATCHALL_STR) \ + X(SLOT_MATCHALL_DONE) + +#define ANT_DECLARE_INTERNAL_SLOT(name) name, typedef enum { - SLOT_NONE = 0, - SLOT_PID, - SLOT_ASYNC, - SLOT_WITH, - SLOT_THIS, - SLOT_NEW_TARGET, - SLOT_FIELD_COUNT, - SLOT_FIELDS, - SLOT_STRICT, - SLOT_CODE, - SLOT_CODE_LEN, - SLOT_CFUNC, - SLOT_CORO, - SLOT_PROTO, - SLOT_FUNC_PROTO, - SLOT_ASYNC_PROTO, - SLOT_AUX, - SLOT_TARGET_FUNC, - SLOT_NAME, - SLOT_MODULE_CTX, - SLOT_MODULE_LOADING, - SLOT_MAP, - SLOT_SET, - SLOT_PRIMITIVE, - SLOT_PROXY_REF, - SLOT_BUILTIN, - SLOT_BRAND, - SLOT_DATA, - SLOT_CTOR, - SLOT_FS_FLAGS, - SLOT_DEFAULT, - SLOT_ERROR_BRAND, - SLOT_ERR_TYPE, - SLOT_OBSERVABLE_SUBSCRIBER, - SLOT_SUBSCRIPTION_OBSERVER, - SLOT_SUBSCRIPTION_CLEANUP, - SLOT_HOISTED_VARS, - SLOT_HOISTED_VARS_LEN, - SLOT_STRICT_ARGS, - SLOT_NO_FUNC_DECLS, - SLOT_ITER_STATE, - SLOT_BYTECODE, - SLOT_ENTRIES, - SLOT_SETTLED, - SLOT_WT_ON_MESSAGE, - SLOT_WT_ONCE_MESSAGE, - SLOT_WT_ON_EXIT, - SLOT_WT_ONCE_EXIT, - SLOT_WT_PORT_TAG, - SLOT_WT_PORT_QUEUE, - SLOT_WT_PORT_HEAD, - SLOT_WT_PORT_PEER, - SLOT_WT_PORT_CLOSED, - SLOT_WT_PORT_STARTED, - SLOT_WT_PORT_ON_MESSAGE, - SLOT_WT_PORT_ONCE_MESSAGE, - SLOT_WT_PORT_PROTO, - SLOT_WT_ENV_STORE, - SLOT_NAPI_EXTERNAL_ID, - SLOT_NAPI_WRAP_ID, - SLOT_RS_PULL, - SLOT_RS_CANCEL, - SLOT_RS_SIZE, - SLOT_RS_CLOSED, - SLOT_WS_WRITE, - SLOT_WS_CLOSE, - SLOT_WS_ABORT, - SLOT_WS_READY, - SLOT_WS_SIGNAL, - SLOT_HEADERS_GUARD, - SLOT_REQUEST_HEADERS, - SLOT_REQUEST_SIGNAL, - SLOT_REQUEST_ABORT_REASON, - SLOT_REQUEST_BODY_STREAM, - SLOT_RESPONSE_HEADERS, - SLOT_RESPONSE_BODY_STREAM, - SLOT_PIPE_ABORT_LISTENER, - SLOT_MATCHALL_RX, - SLOT_MATCHALL_STR, - SLOT_MATCHALL_DONE, + ANT_INTERNAL_SLOT_LIST(ANT_DECLARE_INTERNAL_SLOT) SLOT_MAX = 255 } internal_slot_t; +#undef ANT_DECLARE_INTERNAL_SLOT typedef enum { BUILTIN_NONE = 0, diff --git a/include/oxc.h b/include/oxc.h index 48eafbb..4c1931b 100644 --- a/include/oxc.h +++ b/include/oxc.h @@ -18,19 +18,4 @@ char *OXC_strip_types_owned( size_t error_output_len ); -char *OXC_get_hoisted_vars( - const char *input, - size_t input_len, - size_t *out_len -); - -char *OXC_get_func_hoisted_vars( - const char *input, - size_t input_len, - size_t *out_len -); - -void OXC_free_hoisted_vars(char *ptr, size_t len); -void OXC_free_stripped_output(char *ptr, size_t len); - #endif diff --git a/src/ant.c b/src/ant.c index af11f89..d0e05d5 100644 --- a/src/ant.c +++ b/src/ant.c @@ -32,7 +32,6 @@ #include "silver/engine.h" #include -#include #include #include #include @@ -2609,16 +2608,6 @@ static void set_func_code(ant_t *js, ant_value_t func_obj, const char *code, siz const char *arena_code = code_arena_alloc(code, len); if (!arena_code) return; set_func_code_ptr(js, func_obj, arena_code, len); - - if (!memmem(code, len, "var", 3)) return; - - size_t vars_buf_len; - char *vars = OXC_get_func_hoisted_vars(code, len, &vars_buf_len); - - if (vars) { - set_slot(func_obj, SLOT_HOISTED_VARS, mkval(T_CFUNC, (size_t)vars)); - set_slot(func_obj, SLOT_HOISTED_VARS_LEN, tov((double)vars_buf_len)); - } } static const char *get_func_code(ant_t *js, ant_value_t func_obj, ant_offset_t *len) { diff --git a/src/modules/io.c b/src/modules/io.c index 5934f50..27b0be4 100644 --- a/src/modules/io.c +++ b/src/modules/io.c @@ -570,51 +570,16 @@ static ant_value_t js_console_timeEnd(ant_t *js, ant_value_t *args, int nargs) { } static const char *get_slot_name(internal_slot_t slot) { + #define ANT_SLOT_NAME(name) [name] = &#name[5], static const char *slot_names[] = { - [SLOT_NONE] = "NONE", - [SLOT_PID] = "PID", - [SLOT_ASYNC] = "ASYNC", - [SLOT_WITH] = "WITH", - [SLOT_THIS] = "THIS", - [SLOT_NEW_TARGET] = "NEW_TARGET", - [SLOT_FIELD_COUNT] = "FIELD_COUNT", - [SLOT_FIELDS] = "FIELDS", - [SLOT_STRICT] = "STRICT", - [SLOT_CODE] = "CODE", - [SLOT_CODE_LEN] = "CODE_LEN", - [SLOT_CFUNC] = "CFUNC", - [SLOT_CORO] = "CORO", - [SLOT_PROTO] = "PROTO", - [SLOT_FUNC_PROTO] = "FUNC_PROTO", - [SLOT_ASYNC_PROTO] = "ASYNC_PROTO", - [SLOT_AUX] = "AUX", - [SLOT_TARGET_FUNC] = "TARGET_FUNC", - [SLOT_NAME] = "NAME", - [SLOT_MODULE_CTX] = "MODULE_CTX", - [SLOT_MAP] = "MAP", - [SLOT_SET] = "SET", - [SLOT_PRIMITIVE] = "PRIMITIVE", - [SLOT_PROXY_REF] = "PROXY_REF", - [SLOT_BUILTIN] = "BUILTIN", - [SLOT_DATA] = "DATA", - [SLOT_CTOR] = "CTOR", - [SLOT_DEFAULT] = "DEFAULT", - [SLOT_ERROR_BRAND] = "ERROR_BRAND", - [SLOT_ERR_TYPE] = "ERR_TYPE", - [SLOT_OBSERVABLE_SUBSCRIBER] = "OBSERVABLE_SUBSCRIBER", - [SLOT_SUBSCRIPTION_OBSERVER] = "SUBSCRIPTION_OBSERVER", - [SLOT_SUBSCRIPTION_CLEANUP] = "SUBSCRIPTION_CLEANUP", - [SLOT_HOISTED_VARS] = "HOISTED_VARS", - [SLOT_HOISTED_VARS_LEN] = "HOISTED_VARS_LEN", - [SLOT_STRICT_ARGS] = "STRICT_ARGS", - [SLOT_NO_FUNC_DECLS] = "NO_FUNC_DECLS", - [SLOT_ITER_STATE] = "ITER_STATE", - [SLOT_ENTRIES] = "ENTRIES", + ANT_INTERNAL_SLOT_LIST(ANT_SLOT_NAME) }; - + #undef ANT_SLOT_NAME + if (slot < sizeof(slot_names) / sizeof(slot_names[0]) && slot_names[slot]) { return slot_names[slot]; } + return "UNKNOWN"; } @@ -622,20 +587,26 @@ static const char *get_type_name(int type) { static const char *type_names[] = { [T_OBJ] = "object", [T_STR] = "string", - [T_UNDEF] = "undefined", - [T_NULL] = "null", - [T_NUM] = "number", - [T_BOOL] = "boolean", + [T_ARR] = "array", [T_FUNC] = "function", [T_CFUNC] = "function", - [T_ERR] = "error", - [T_ARR] = "array", + [T_CLOSURE] = "closure", [T_PROMISE] = "Promise", - [T_TYPEDARRAY] = "TypedArray", + [T_GENERATOR] = "Generator", + [T_UNDEF] = "undefined", + [T_NULL] = "null", + [T_BOOL] = "boolean", + [T_NUM] = "number", [T_BIGINT] = "bigint", [T_SYMBOL] = "symbol", - [T_GENERATOR] = "Generator", - [T_FFI] = "ffi" + [T_ERR] = "error", + [T_TYPEDARRAY] = "TypedArray", + [T_FFI] = "ffi", + [T_NTARG] = "ntarg", + [T_MAP] = "map", + [T_SET] = "set", + [T_WEAKMAP] = "weakmap", + [T_WEAKSET] = "weakset" }; size_t num_types = sizeof(type_names) / sizeof(type_names[0]); @@ -726,11 +697,9 @@ void inspect_object(ant_t *js, ant_value_t obj, FILE *stream, int depth, inspect switch (slot) { case SLOT_CODE: case SLOT_CFUNC: - case SLOT_HOISTED_VARS: fprintf(stream, "", (uint64_t)vdata(slot_val)); break; case SLOT_CODE_LEN: - case SLOT_HOISTED_VARS_LEN: fprintf(stream, "%.0f", js_getnum(slot_val)); break; default: diff --git a/src/silver/glue.c b/src/silver/glue.c index 05c25ea..0b02c84 100644 --- a/src/silver/glue.c +++ b/src/silver/glue.c @@ -254,10 +254,7 @@ ant_value_t jit_helper_closure( ant_value_t func_val = mkval(T_FUNC, (uintptr_t)closure); if (!child->is_arrow && !child->is_method) sv_setup_function_prototype(js, func_obj, func_val); - - if (child->is_strict) - js_set_slot(func_obj, SLOT_STRICT, js_true); - + if (child->is_async) { js_set_slot(func_obj, SLOT_ASYNC, js_true); ant_value_t async_proto = js_get_slot(js->global, SLOT_ASYNC_PROTO); diff --git a/src/silver/ops/coercion.h b/src/silver/ops/coercion.h index 3b9aad8..fd38674 100644 --- a/src/silver/ops/coercion.h +++ b/src/silver/ops/coercion.h @@ -358,17 +358,11 @@ static inline void sv_op_special_obj( } ant_value_t arr = js_mkarr(js); - if (frame->bp && frame->argc > 0) { - for (int i = 0; i < frame->argc; i++) - js_arr_push(js, arr, frame->bp[i]); + for (int i = 0; i < frame->argc; i++) js_arr_push(js, arr, frame->bp[i]); } - - if (sv_frame_is_strict(frame)) - js_set_slot(arr, SLOT_STRICT_ARGS, tov(1)); - else if (vtype(frame->callee) == T_FUNC) - setprop_cstr(js, arr, "callee", 6, frame->callee); - + if (vtype(frame->callee) == T_FUNC) setprop_cstr(js, arr, "callee", 6, frame->callee); + js_set_sym(js, arr, get_toStringTag_sym(), js_mkstr(js, "Arguments", 9)); ant_value_t array_proto = js_get_ctor_proto(js, "Array", 5); diff --git a/src/silver/ops/upvalues.h b/src/silver/ops/upvalues.h index 320b5c7..aacc6fc 100644 --- a/src/silver/ops/upvalues.h +++ b/src/silver/ops/upvalues.h @@ -121,11 +121,8 @@ static inline void sv_op_closure( js_setprop(js, func_obj, js->length_str, tov((double)child->param_count)); js_set_descriptor(js, func_obj, "length", 6, JS_DESC_C); - if (is_object_type(module_ctx)) - js_set_slot_wb(js, func_obj, SLOT_MODULE_CTX, module_ctx); - + if (is_object_type(module_ctx)) js_set_slot_wb(js, func_obj, SLOT_MODULE_CTX, module_ctx); if (!child->is_arrow && !child->is_method) sv_setup_function_prototype(js, func_obj, func_val); - if (child->is_strict) js_set_slot(func_obj, SLOT_STRICT, js_true); if (child->is_async) { js_set_slot(func_obj, SLOT_ASYNC, js_true); diff --git a/src/strip/Cargo.lock b/src/strip/Cargo.lock index 6578cbf..3a8e2cb 100644 --- a/src/strip/Cargo.lock +++ b/src/strip/Cargo.lock @@ -26,16 +26,6 @@ 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" @@ -232,12 +222,6 @@ 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" @@ -309,12 +293,6 @@ 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" @@ -326,8 +304,6 @@ name = "oxc" version = "0.0.0" dependencies = [ "oxc_allocator", - "oxc_ast", - "oxc_ast_visit", "oxc_codegen", "oxc_parser", "oxc_semantic", @@ -445,7 +421,6 @@ dependencies = [ "oxc_data_structures", "oxc_index", "oxc_semantic", - "oxc_sourcemap", "oxc_span", "oxc_syntax", "rustc-hash", @@ -532,7 +507,6 @@ dependencies = [ "oxc_data_structures", "oxc_diagnostics", "oxc_ecmascript", - "oxc_regular_expression", "oxc_span", "oxc_syntax", "rustc-hash", @@ -577,19 +551,6 @@ dependencies = [ "smallvec", ] -[[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.110.0" @@ -991,12 +952,6 @@ 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" diff --git a/src/strip/Cargo.toml b/src/strip/Cargo.toml index 860c60b..c74615e 100644 --- a/src/strip/Cargo.toml +++ b/src/strip/Cargo.toml @@ -7,13 +7,11 @@ crate-type = ["staticlib"] [dependencies] oxc_allocator = "0.110.0" -oxc_parser = "0.110.0" oxc_span = "0.110.0" -oxc_codegen = "0.110.0" oxc_transformer = "0.110.0" oxc_semantic = "0.110.0" -oxc_ast = "0.110.0" -oxc_ast_visit = "0.110.0" +oxc_codegen = { version = "0.110.0", default-features = false } +oxc_parser = { version = "0.110.0", default-features = false } [profile.release] lto = true diff --git a/src/strip/src/collector.rs b/src/strip/src/collector.rs deleted file mode 100644 index ebd52a2..0000000 --- a/src/strip/src/collector.rs +++ /dev/null @@ -1,113 +0,0 @@ -use oxc_allocator::Allocator; -use oxc_ast::ast::{BindingPattern, VariableDeclarationKind}; -use oxc_ast_visit::{Visit, walk}; -use oxc_parser::Parser; -use oxc_semantic::ScopeFlags; -use oxc_span::SourceType; - -struct VarCollector<'a> { - vars: Vec<&'a str>, - func_depth: usize, - target_depth: usize, -} - -impl<'a> VarCollector<'a> { - fn new() -> Self { - Self { - vars: Vec::new(), - func_depth: 0, - target_depth: 0, - } - } - - fn for_func_body() -> Self { - Self { - vars: Vec::new(), - func_depth: 0, - target_depth: 1, - } - } - - fn extract_binding_names(&mut self, pattern: &BindingPattern<'a>) { - use BindingPattern::*; - match pattern { - BindingIdentifier(id) => self.vars.push(id.name.as_str()), - ObjectPattern(obj) => { - for prop in &obj.properties { - self.extract_binding_names(&prop.value); - } - if let Some(rest) = &obj.rest { - self.extract_binding_names(&rest.argument); - } - } - ArrayPattern(arr) => { - for elem in arr.elements.iter().flatten() { - self.extract_binding_names(elem); - } - if let Some(rest) = &arr.rest { - self.extract_binding_names(&rest.argument); - } - } - AssignmentPattern(assign) => { - self.extract_binding_names(&assign.left); - } - } - } -} - -impl<'a> Visit<'a> for VarCollector<'a> { - fn visit_function(&mut self, func: &oxc_ast::ast::Function<'a>, flags: ScopeFlags) { - self.func_depth += 1; - walk::walk_function(self, func, flags); - self.func_depth -= 1; - } - - fn visit_arrow_function_expression(&mut self, expr: &oxc_ast::ast::ArrowFunctionExpression<'a>) { - self.func_depth += 1; - walk::walk_arrow_function_expression(self, expr); - self.func_depth -= 1; - } - - fn visit_variable_declaration(&mut self, decl: &oxc_ast::ast::VariableDeclaration<'a>) { - if decl.kind == VariableDeclarationKind::Var && self.func_depth == self.target_depth { - for declarator in &decl.declarations { - self.extract_binding_names(&declarator.id); - } - } - walk::walk_variable_declaration(self, decl); - } -} - -pub fn collect_var_names(source: &str) -> Result, String> { - let allocator = Allocator::default(); - let source_type = SourceType::mjs(); - 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 collector = VarCollector::new(); - collector.visit_program(&parser_ret.program); - - Ok(collector.vars.into_iter().map(String::from).collect()) -} - -pub fn collect_var_names_from_func(source: &str) -> Result, String> { - let allocator = Allocator::default(); - let source_type = SourceType::mjs(); - - let wrapped = format!("(function{})", source); - let parser_ret = Parser::new(&allocator, &wrapped, 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 collector = VarCollector::for_func_body(); - collector.visit_program(&parser_ret.program); - - Ok(collector.vars.into_iter().map(String::from).collect()) -} diff --git a/src/strip/src/ffi.rs b/src/strip/src/ffi.rs index 91480b7..882d9a1 100644 --- a/src/strip/src/ffi.rs +++ b/src/strip/src/ffi.rs @@ -1,8 +1,6 @@ -use std::alloc::{Layout, alloc, dealloc}; use std::ffi::{CStr, c_char, c_int}; use std::ptr; -use crate::collector::{collect_var_names, collect_var_names_from_func}; use crate::strip::strip_types_internal; pub const OXC_ERR_NULL_INPUT: c_int = -1; @@ -18,6 +16,10 @@ fn classify_strip_error(err: &str) -> c_int { } } +unsafe extern "C" { + fn malloc(size: usize) -> *mut core::ffi::c_void; +} + unsafe fn write_error(output: *mut c_char, output_len: usize, msg: &str) { if output.is_null() || output_len == 0 { return; @@ -75,8 +77,7 @@ pub unsafe extern "C" fn OXC_strip_types_owned( Ok(result) => { let bytes = result.as_bytes(); let alloc_len = bytes.len() + 1; - let layout = Layout::from_size_align(alloc_len, 1).unwrap(); - let out_ptr = unsafe { alloc(layout) as *mut c_char }; + let out_ptr = unsafe { malloc(alloc_len) as *mut c_char }; if out_ptr.is_null() { unsafe { write_error(error_output, error_output_len, "out of memory allocating strip output") }; if !out_error.is_null() { @@ -104,118 +105,3 @@ pub unsafe extern "C" fn OXC_strip_types_owned( } } } - -#[unsafe(no_mangle)] -pub unsafe extern "C" fn OXC_free_stripped_output(ptr: *mut c_char, len: usize) { - if ptr.is_null() { - return; - } - - let alloc_len = len.saturating_add(1); - let layout = Layout::from_size_align(alloc_len, 1).unwrap(); - - unsafe { - dealloc(ptr as *mut u8, layout); - } -} - -#[unsafe(no_mangle)] -pub unsafe extern "C" fn OXC_get_hoisted_vars(input: *const c_char, input_len: usize, out_len: *mut usize) -> *mut c_char { - if input.is_null() || out_len.is_null() { - return ptr::null_mut(); - } - - let input_slice = unsafe { std::slice::from_raw_parts(input as *const u8, input_len) }; - let input_str = match std::str::from_utf8(input_slice) { - Ok(s) => s, - Err(_) => return ptr::null_mut(), - }; - - match collect_var_names(input_str) { - Ok(vars) => { - if vars.is_empty() { - return ptr::null_mut(); - } - let total_len: usize = vars.iter().map(|s| s.len() + 1).sum::() + 1; - - let layout = std::alloc::Layout::from_size_align(total_len, 1).unwrap(); - let ptr = unsafe { std::alloc::alloc(layout) as *mut c_char }; - if ptr.is_null() { - return ptr::null_mut(); - } - - let mut offset = 0; - for v in &vars { - unsafe { - ptr::copy_nonoverlapping(v.as_ptr(), ptr.add(offset) as *mut u8, v.len()); - *ptr.add(offset + v.len()) = 0; - } - offset += v.len() + 1; - } - unsafe { - *ptr.add(offset) = 0; - *out_len = total_len; - } - - ptr - } - Err(_) => ptr::null_mut(), - } -} - -#[unsafe(no_mangle)] -pub unsafe extern "C" fn OXC_get_func_hoisted_vars(input: *const c_char, input_len: usize, out_len: *mut usize) -> *mut c_char { - if input.is_null() || out_len.is_null() { - return ptr::null_mut(); - } - - let input_slice = unsafe { std::slice::from_raw_parts(input as *const u8, input_len) }; - let input_str = match std::str::from_utf8(input_slice) { - Ok(s) => s, - Err(_) => return ptr::null_mut(), - }; - - match collect_var_names_from_func(input_str) { - Ok(vars) => { - if vars.is_empty() { - return ptr::null_mut(); - } - let total_len: usize = vars.iter().map(|s| s.len() + 1).sum::() + 1; - - let layout = std::alloc::Layout::from_size_align(total_len, 1).unwrap(); - let ptr = unsafe { std::alloc::alloc(layout) as *mut c_char }; - - if ptr.is_null() { - return ptr::null_mut(); - } - - let mut offset = 0; - for v in &vars { - unsafe { - ptr::copy_nonoverlapping(v.as_ptr(), ptr.add(offset) as *mut u8, v.len()); - *ptr.add(offset + v.len()) = 0; - } - offset += v.len() + 1; - } - - unsafe { - *ptr.add(offset) = 0; - *out_len = total_len; - } - - ptr - } - Err(_) => ptr::null_mut(), - } -} - -#[unsafe(no_mangle)] -pub unsafe extern "C" fn OXC_free_hoisted_vars(ptr: *mut c_char, len: usize) { - if ptr.is_null() || len == 0 { - return; - } - let layout = std::alloc::Layout::from_size_align(len, 1).unwrap(); - unsafe { - std::alloc::dealloc(ptr as *mut u8, layout); - } -} diff --git a/src/strip/src/lib.rs b/src/strip/src/lib.rs index cdf212f..8296843 100644 --- a/src/strip/src/lib.rs +++ b/src/strip/src/lib.rs @@ -1,7 +1,5 @@ -mod collector; mod ffi; mod strip; -pub use collector::{collect_var_names, collect_var_names_from_func}; pub use ffi::*; pub use strip::strip_types_internal; diff --git a/src/utils.c b/src/utils.c index 438c840..3780b37 100644 --- a/src/utils.c +++ b/src/utils.c @@ -169,13 +169,13 @@ int strip_typescript_inplace( char *next = realloc(input, stripped_len + 1); if (!next) { - OXC_free_stripped_output(stripped, stripped_len); + free(stripped); if (error_detail) *error_detail = "out of memory while resizing strip output buffer"; return OXC_ERR_OUTPUT_TOO_LARGE; } memcpy(next, stripped, stripped_len + 1); - OXC_free_stripped_output(stripped, stripped_len); + free(stripped); *buffer = next; if (out_len) *out_len = stripped_len; diff --git a/vendor/packagefiles/wasm-micro-runtime/meson.build b/vendor/packagefiles/wasm-micro-runtime/meson.build index 1311781..26cdfbc 100644 --- a/vendor/packagefiles/wasm-micro-runtime/meson.build +++ b/vendor/packagefiles/wasm-micro-runtime/meson.build @@ -72,6 +72,7 @@ wamr_inc = include_directories( 'core/shared/platform/include', 'core/shared/platform/common/libc-util', 'core/shared/platform/common/memory', + 'core/shared/platform/common/posix', ) if wamr_platform == 'windows' @@ -90,7 +91,6 @@ if wamr_platform == 'windows' 'core/shared/platform/common/memory/mremap.c', ) else - wamr_inc += include_directories('core/shared/platform/common/posix') wamr_sources = files( 'core/shared/platform/' + wamr_platform + '/platform_init.c', 'core/shared/platform/common/libc-util/libc_errno.c', -- 2.51.2