Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit bfc1100

Browse files
committed
attach additional pointer to decoder/token_rewriter, pass it to decode/token_rewrite function
1 parent fe60119 commit bfc1100

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

decoder.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ static void test_decoder_fields(void)
7676
const char *ALWAYS_ERR = "always_err";
7777

7878
d = lib_ruby_parser__test__always_ok_decoder();
79-
decoder_result = (d.f)(LIB_RUBY_PARSER_new_string_from_cstr("utf-8"), LIB_RUBY_PARSER_new_bytes_from_cstr("2 + 2", 5));
79+
decoder_result = (d.f)(d.state, LIB_RUBY_PARSER_new_string_from_cstr("utf-8"), LIB_RUBY_PARSER_new_bytes_from_cstr("2 + 2", 5));
8080
assert_eq(decoder_result.tag, LIB_RUBY_PARSER_DECODER_RESULT_OK);
8181
assert_byte_list(decoder_result.as.ok, ALWAYS_OK);
8282
LIB_RUBY_PARSER_drop_decoder_result(&decoder_result);
8383

8484
d = lib_ruby_parser__test__always_err_decoder();
85-
decoder_result = (d.f)(LIB_RUBY_PARSER_new_string_from_cstr("utf-8"), LIB_RUBY_PARSER_new_bytes_from_cstr("2 + 2", 5));
85+
decoder_result = (d.f)(d.state, LIB_RUBY_PARSER_new_string_from_cstr("utf-8"), LIB_RUBY_PARSER_new_bytes_from_cstr("2 + 2", 5));
8686
assert_eq(decoder_result.as.err.tag, LIB_RUBY_PARSER_INPUT_ERROR_DECODING_ERROR);
8787
assert_string_eq(decoder_result.as.err.as.unsupported_encoding, ALWAYS_ERR);
8888
LIB_RUBY_PARSER_drop_decoder_result(&decoder_result);

decoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ typedef struct LIB_RUBY_PARSER_DecoderResult
3838
void LIB_RUBY_PARSER_drop_decoder_result(LIB_RUBY_PARSER_DecoderResult *decoder_result);
3939

4040
struct LIB_RUBY_PARSER_Decoder;
41-
typedef LIB_RUBY_PARSER_DecoderResult (*LIB_RUBY_PARSER_Decoder_Function)(LIB_RUBY_PARSER_String, LIB_RUBY_PARSER_ByteList);
41+
typedef LIB_RUBY_PARSER_DecoderResult (*LIB_RUBY_PARSER_Decoder_Function)(void *state, LIB_RUBY_PARSER_String encoding, LIB_RUBY_PARSER_ByteList input);
4242
typedef struct LIB_RUBY_PARSER_Decoder
4343
{
4444
LIB_RUBY_PARSER_Decoder_Function f;
45+
void *state;
4546
} LIB_RUBY_PARSER_Decoder;
4647

4748
typedef struct LIB_RUBY_PARSER_MaybeDecoder

ruby-parser-c/src/decoder.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ pub extern "C" fn LIB_RUBY_PARSER_drop_decoder_result(decoder_result: *mut Decod
5050

5151
#[repr(C)]
5252
pub struct Decoder {
53-
pub f: extern "C" fn(encoding: StringBlob, input: ByteListBlob) -> DecoderResultBlob,
53+
pub f: extern "C" fn(
54+
state: *mut std::ffi::c_void,
55+
encoding: StringBlob,
56+
input: ByteListBlob,
57+
) -> DecoderResultBlob,
58+
pub state: *mut std::ffi::c_void,
5459
}
5560

5661
#[cfg(feature = "tests")]
5762
#[no_mangle]
5863
pub extern "C" fn lib_ruby_parser__test__always_ok_decoder() -> Decoder {
5964
#[no_mangle]
6065
pub extern "C" fn lib_ruby_parser__test__always_ok_decoder_fn(
66+
_state: *mut std::ffi::c_void,
6167
encoding: StringBlob,
6268
input: ByteListBlob,
6369
) -> DecoderResultBlob {
@@ -69,6 +75,7 @@ pub extern "C" fn lib_ruby_parser__test__always_ok_decoder() -> Decoder {
6975
}
7076
Decoder {
7177
f: lib_ruby_parser__test__always_ok_decoder_fn,
78+
state: std::ptr::null_mut(),
7279
}
7380
}
7481

@@ -77,6 +84,7 @@ pub extern "C" fn lib_ruby_parser__test__always_ok_decoder() -> Decoder {
7784
pub extern "C" fn lib_ruby_parser__test__always_err_decoder() -> Decoder {
7885
#[no_mangle]
7986
pub extern "C" fn lib_ruby_parser__test__always_err_decoder_fn(
87+
_state: *mut std::ffi::c_void,
8088
encoding: StringBlob,
8189
input: ByteListBlob,
8290
) -> DecoderResultBlob {
@@ -90,6 +98,7 @@ pub extern "C" fn lib_ruby_parser__test__always_err_decoder() -> Decoder {
9098
}
9199
Decoder {
92100
f: lib_ruby_parser__test__always_err_decoder_fn,
101+
state: std::ptr::null_mut(),
93102
}
94103
}
95104

ruby-parser-c/src/parser_options.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ impl From<CParserOptions> for lib_ruby_parser::ParserOptions {
2121

2222
let decoder = decoder.map(|decoder| {
2323
lib_ruby_parser::source::Decoder::new(Box::new(move |encoding, input| {
24-
(decoder.f)(encoding.into(), input.into()).into()
24+
let Decoder { f, state } = decoder;
25+
f(state, encoding.into(), input.into()).into()
2526
}))
2627
});
2728

2829
let token_rewriter = token_rewriter.map(|token_rewriter| {
2930
lib_ruby_parser::source::token_rewriter::TokenRewriter::new(Box::new(
3031
move |token, input| {
3132
let input: &'static [u8] = unsafe { std::mem::transmute(input) };
32-
(token_rewriter.f)(Box::leak(token), input.into())
33+
let TokenRewriter { f, state } = token_rewriter;
34+
f(state, Box::leak(token), input.into())
3335
},
3436
))
3537
});

ruby-parser-c/src/token_rewriter.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ pub extern "C" fn LIB_RUBY_PARSER_drop_token_rewriter_result(
5959

6060
#[repr(C)]
6161
pub struct TokenRewriter {
62-
pub f: extern "C" fn(token: *mut Token, input: SharedByteListBlob) -> TokenRewriterResult,
62+
pub f: extern "C" fn(
63+
*mut std::ffi::c_void,
64+
token: *mut Token,
65+
input: SharedByteListBlob,
66+
) -> TokenRewriterResult,
67+
pub state: *mut std::ffi::c_void,
6368
}
6469

6570
#[cfg(feature = "tests")]
6671
#[no_mangle]
6772
pub extern "C" fn lib_ruby_parser__test__always_keep_token_rewriter() -> TokenRewriter {
6873
#[no_mangle]
6974
pub extern "C" fn lib_ruby_parser__test__always_keep_token_rewriter_fn(
75+
_state: *mut std::ffi::c_void,
7076
token: *mut Token,
7177
_input: SharedByteListBlob,
7278
) -> TokenRewriterResult {
@@ -79,6 +85,7 @@ pub extern "C" fn lib_ruby_parser__test__always_keep_token_rewriter() -> TokenRe
7985
}
8086
TokenRewriter {
8187
f: lib_ruby_parser__test__always_keep_token_rewriter_fn,
88+
state: std::ptr::null_mut(),
8289
}
8390
}
8491

@@ -87,6 +94,7 @@ pub extern "C" fn lib_ruby_parser__test__always_keep_token_rewriter() -> TokenRe
8794
pub extern "C" fn lib_ruby_parser__test__always_rewrite_token_rewriter() -> TokenRewriter {
8895
#[no_mangle]
8996
pub extern "C" fn lib_ruby_parser__test__always_rewrite_token_rewriter_fn(
97+
_state: *mut std::ffi::c_void,
9098
token: *mut Token,
9199
_input: SharedByteListBlob,
92100
) -> TokenRewriterResult {
@@ -107,6 +115,7 @@ pub extern "C" fn lib_ruby_parser__test__always_rewrite_token_rewriter() -> Toke
107115
}
108116
TokenRewriter {
109117
f: lib_ruby_parser__test__always_rewrite_token_rewriter_fn,
118+
state: std::ptr::null_mut(),
110119
}
111120
}
112121

token_rewriter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ static void test_token_rewriter(void)
7676
token = (LIB_RUBY_PARSER_Token *)malloc(sizeof(LIB_RUBY_PARSER_Token));
7777
token->token_type = 396; // tPLUS
7878
token->token_value.raw = (LIB_RUBY_PARSER_ByteList){.ptr = NULL, .len = 0, .capacity = 0};
79-
result = (token_rewriter.f)(token, input);
79+
result = (token_rewriter.f)(token_rewriter.state, token, input);
8080
assert_token(*(result.rewritten_token), "tPLUS");
8181
LIB_RUBY_PARSER_drop_token_rewriter_result(&result);
8282

8383
token_rewriter = lib_ruby_parser__test__always_rewrite_token_rewriter();
8484
token = (LIB_RUBY_PARSER_Token *)malloc(sizeof(LIB_RUBY_PARSER_Token));
8585
token->token_type = 396; // tPLUS
8686
token->token_value.raw = (LIB_RUBY_PARSER_ByteList){.ptr = NULL, .len = 0, .capacity = 0};
87-
result = (token_rewriter.f)(token, input);
87+
result = (token_rewriter.f)(token_rewriter.state, token, input);
8888
assert_token(*(result.rewritten_token), "tMINUS");
8989
LIB_RUBY_PARSER_drop_token_rewriter_result(&result);
9090
}

token_rewriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ typedef struct LIB_RUBY_PARSER_TokenRewriterResult
3434
void LIB_RUBY_PARSER_drop_token_rewriter_result(LIB_RUBY_PARSER_TokenRewriterResult *);
3535

3636
struct LIB_RUBY_PARSER_TokenRewriter;
37-
typedef LIB_RUBY_PARSER_TokenRewriterResult (*LIB_RUBY_PARSER_TokenRewriter_Function)(LIB_RUBY_PARSER_Token *, LIB_RUBY_PARSER_SharedByteList);
37+
typedef LIB_RUBY_PARSER_TokenRewriterResult (*LIB_RUBY_PARSER_TokenRewriter_Function)(void *state, LIB_RUBY_PARSER_Token *token, LIB_RUBY_PARSER_SharedByteList input);
3838
typedef struct LIB_RUBY_PARSER_TokenRewriter
3939
{
4040
LIB_RUBY_PARSER_TokenRewriter_Function f;
41+
void *state;
4142
} LIB_RUBY_PARSER_TokenRewriter;
4243

4344
typedef struct LIB_RUBY_PARSER_MaybeTokenRewriter

0 commit comments

Comments
 (0)