Skip to content

Commit 288b40e

Browse files
committed
fixup: add c addon variant
1 parent 0c167a9 commit 288b40e

File tree

7 files changed

+151
-2
lines changed

7 files changed

+151
-2
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/async-rewriter3/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
dist
33
pkg
44
target
5+
build

packages/async-rewriter3/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[lib]
7-
crate-type = ["cdylib", "rlib"]
7+
crate-type = ["cdylib", "rlib", "staticlib"]
88

99
[[bin]]
1010
name = "async-rewriter3"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <node_api.h>
2+
#include <stdlib.h>
3+
4+
extern uint8_t async_rewrite_c(
5+
const uint8_t* input,
6+
uintptr_t input_len,
7+
uint8_t** output,
8+
uintptr_t* output_len,
9+
uint8_t debug_level);
10+
extern void async_rewrite_free_result(
11+
uint8_t* output);
12+
13+
static napi_value async_rewrite_napi(napi_env env, napi_callback_info info) {
14+
napi_status status;
15+
napi_value argv[2];
16+
size_t argc = sizeof(argv) / sizeof(argv[0]);
17+
18+
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
19+
if (status != napi_ok) {
20+
return NULL;
21+
}
22+
if (argc < 2) {
23+
napi_throw_error(env, NULL, "Wrong number of arguments");
24+
return NULL;
25+
}
26+
27+
size_t bufsize = 0;
28+
status = napi_get_value_string_utf8(env, argv[0], NULL, 0, &bufsize);
29+
if (status != napi_ok) {
30+
return NULL;
31+
}
32+
uint8_t* input = (uint8_t*)malloc(bufsize + 1);
33+
if (input == NULL) {
34+
napi_throw_error(env, NULL, "Memory allocation failed");
35+
return NULL;
36+
}
37+
status = napi_get_value_string_utf8(env, argv[0], (char*)input, bufsize + 1, &bufsize);
38+
if (status != napi_ok) {
39+
free(input);
40+
return NULL;
41+
}
42+
input[bufsize] = '\0';
43+
uint32_t debug_level = 0;
44+
status = napi_get_value_uint32(env, argv[1], &debug_level);
45+
if (status != napi_ok) {
46+
free(input);
47+
return NULL;
48+
}
49+
50+
uint8_t* output = NULL;
51+
uintptr_t output_len = 0;
52+
uint8_t result = async_rewrite_c(input, bufsize, &output, &output_len, debug_level);
53+
free(input);
54+
55+
if (result != 0) {
56+
napi_throw_error(env, NULL, "Error in async_rewrite_c");
57+
return NULL;
58+
}
59+
60+
napi_value result_value;
61+
status = napi_create_string_utf8(env, (const char*)output, output_len, &result_value);
62+
async_rewrite_free_result(output);
63+
if (status != napi_ok) {
64+
return NULL;
65+
}
66+
67+
return result_value;
68+
}
69+
70+
NAPI_MODULE_INIT() {
71+
napi_value exported_function;
72+
napi_status status;
73+
status = napi_create_function(env,
74+
"asyncRewrite",
75+
NAPI_AUTO_LENGTH,
76+
async_rewrite_napi,
77+
NULL,
78+
&exported_function);
79+
if (status != napi_ok) {
80+
return NULL;
81+
}
82+
status = napi_set_named_property(env,
83+
exports,
84+
"asyncRewrite",
85+
exported_function);
86+
if (status != napi_ok) {
87+
return NULL;
88+
}
89+
return exports;
90+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
'targets': [{
3+
'target_name': 'async_rewriter3',
4+
'sources': [ 'addon/binding.c' ],
5+
'libraries': [
6+
'<(PRODUCT_DIR)/../../target/release/libasync_rewriter3.a'
7+
],
8+
'xcode_settings': {
9+
'MACOSX_DEPLOYMENT_TARGET': '12.0',
10+
}
11+
}]
12+
}

packages/async-rewriter3/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"node": ">=14.15.1"
1818
},
1919
"scripts": {
20+
"prebuild-addon": "cargo build --release",
21+
"build-addon": "node-gyp rebuild",
22+
"install": "npm run build-addon",
2023
"compile": "npm run webpack-build",
2124
"prewebpack": "rm -rf dist pkg",
2225
"webpack": "webpack",
@@ -29,5 +32,6 @@
2932
"depcheck": "^1.4.3",
3033
"wasm-pack": "^0.13.1",
3134
"webpack-merge": "^5.8.0"
32-
}
35+
},
36+
"gypfile": true
3337
}

packages/async-rewriter3/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use oxc_parser::{ParseOptions, Parser};
1010
use oxc_semantic::{AstNode, Semantic, SemanticBuilder};
1111
use oxc_span::GetSpan;
1212
use oxc_span::{SourceType, Span};
13+
use core::slice;
1314
use std::{borrow::Cow, cmp::Ordering, collections::VecDeque};
1415
use wasm_bindgen::prelude::*;
1516

@@ -607,3 +608,43 @@ pub fn async_rewrite(input: &str, debug_level: DebugLevel) -> Result<String, Str
607608

608609
Ok(result)
609610
}
611+
612+
#[no_mangle]
613+
pub extern "C" fn async_rewrite_c(
614+
input: *const u8,
615+
input_len: usize,
616+
output: *mut *mut i8,
617+
output_len: *mut usize,
618+
debug_level: u8,
619+
) -> u8 {
620+
let input = unsafe { String::from_utf8_lossy(slice::from_raw_parts(input, input_len)) };
621+
622+
let result = async_rewrite(input.as_ref(), match debug_level {
623+
0 => DebugLevel::None,
624+
1 => DebugLevel::TypesOnly,
625+
2 => DebugLevel::Verbose,
626+
_ => return 1,
627+
});
628+
match result {
629+
Ok(output_str) => {
630+
let output_cstr = std::ffi::CString::new(output_str).unwrap();
631+
unsafe {
632+
*output_len = output_cstr.as_bytes().len();
633+
*output = output_cstr.into_raw();
634+
}
635+
0
636+
}
637+
Err(_) => 1,
638+
}
639+
}
640+
641+
#[no_mangle]
642+
pub extern "C" fn async_rewrite_free_result(
643+
result: *mut i8,
644+
) -> () {
645+
if !result.is_null() {
646+
unsafe {
647+
drop(std::ffi::CString::from_raw(result));
648+
}
649+
}
650+
}

0 commit comments

Comments
 (0)