Skip to main content

risingwave_jni_core/
macros.rs

1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Utils
16/// A macro that splits the input by comma and calls the callback `$macro` with the split result.
17/// The each part of the split result is within a bracket, and the callback `$macro` can have its
18/// first parameter as `{$({$($args:tt)+})*}` to match the split result.
19///
20/// ```
21/// macro_rules! call_split_by_comma {
22///     ({$({$($args:tt)+})*}) => {
23///         [$(
24///            stringify! {$($args)+}
25///         ),*]
26///     };
27///     ($($input:tt)*) => {{
28///         risingwave_jni_core::split_by_comma! {
29///             {$($input)*},
30///             call_split_by_comma
31///         }
32///     }};
33/// }
34///
35/// let expected_result = [
36///     "hello",
37///     "my friend",
38/// ];
39///
40/// assert_eq!(expected_result, {call_split_by_comma!(hello, my friend)});
41/// ```
42#[macro_export]
43macro_rules! split_by_comma {
44    // Entry of macro. Put input as the first parameter and initialize empty
45    // second and third parameter.
46    (
47        {$($input:tt)*},
48        $macro:path $(,$args:tt)*
49    ) => {
50        $crate::split_by_comma! {
51            {$($input)*},
52            {}, // previous splits
53            {}, // current split
54            $macro $(,$args)*
55        }
56    };
57    // When the first token is comma, move the tokens of current split to the
58    // list of previous splits with a bracket wrapped, and then clear the current
59    // split.
60    (
61        {
62            ,
63            $($rest:tt)*
64        },
65        {
66            $(
67                {$($prev_split:tt)*}
68            )*
69        },
70        {
71            $($current_split:tt)*
72        },
73        $macro:path $(,$args:tt)*
74    ) => {
75        $crate::split_by_comma! {
76            {
77                $($rest)*
78            },
79            {
80                $(
81                    {$($prev_split)*}
82                )*
83                {
84                    $($current_split)*
85                }
86            },
87            {},
88            $macro $(,$args)*
89        }
90    };
91    // Do the same as the previous match when we just reach the end of input,
92    // with the content of last split not added to the list of previous split yet.
93    (
94        {},
95        {
96            $(
97                {$($prev_split:tt)*}
98            )*
99        },
100        {
101            $($current_split:tt)+
102        },
103        $macro:path $(,$args:tt)*
104    ) => {
105        $crate::split_by_comma! {
106            {},
107            {
108                $(
109                    {$($prev_split)*}
110                )*
111                {
112                    $($current_split)+
113                }
114            },
115            {},
116            $macro $(,$args)*
117        }
118    };
119    // For a token that is not comma, add to the list of current staging split.
120    (
121        {
122            $first:tt
123            $($rest:tt)*
124        },
125        {
126            $(
127                {$($prev_split:tt)*}
128            )*
129        },
130        {
131            $($current_split:tt)*
132        },
133        $macro:path $(,$args:tt)*
134    ) => {
135        $crate::split_by_comma! {
136            {
137                $($rest)*
138            },
139            {
140                $(
141                    {$($prev_split)*}
142                )*
143            },
144            {
145                $($current_split)* $first
146            },
147            $macro $(,$args)*
148        }
149    };
150    // When all split result are added to the list, call the callback `$macro` with the result.
151    (
152        {},
153        {
154            $(
155                {$($prev_split:tt)*}
156            )*
157        },
158        {},
159        $macro:path $(,$args:tt)*
160    ) => {
161        $macro! {
162            {
163                $(
164                    {$($prev_split)*}
165                )*
166            }
167            $(,$args)*
168        }
169    };
170}
171// End of utils part
172
173/// Generate the dot separated java class name to the slash separated name.
174///
175/// ```
176/// assert_eq!(
177///     "java/lang/String",
178///     risingwave_jni_core::gen_class_name!(java.lang.String)
179/// );
180/// assert_eq!(
181///     "java/lang/String",
182///     risingwave_jni_core::gen_class_name!(String)
183/// );
184/// ```
185#[macro_export]
186macro_rules! gen_class_name {
187    // A single part class name will be prefixed with `java.lang.`
188    ($single_part_class:ident $($param_name:ident)?) => {
189        $crate::gen_class_name! { @inner java.lang.$single_part_class }
190    };
191    ($($class:ident).+ $($param_name:ident)?) => {
192        $crate::gen_class_name! { @inner $($class).+ }
193    };
194    (@inner $last:ident) => {
195        stringify! {$last}
196    };
197    (@inner $first:ident . $($rest:ident).+) => {
198        concat! {stringify! {$first}, "/", $crate::gen_class_name! {@inner $($rest).+} }
199    }
200}
201
202/// Generate the type signature of a single type
203/// ```
204/// use risingwave_jni_core::gen_jni_type_sig;
205/// assert_eq!("Z", gen_jni_type_sig!(boolean));
206/// assert_eq!("B", gen_jni_type_sig!(byte));
207/// assert_eq!("C", gen_jni_type_sig!(char));
208/// assert_eq!("S", gen_jni_type_sig!(short));
209/// assert_eq!("I", gen_jni_type_sig!(int));
210/// assert_eq!("J", gen_jni_type_sig!(long));
211/// assert_eq!("F", gen_jni_type_sig!(float));
212/// assert_eq!("D", gen_jni_type_sig!(double));
213/// assert_eq!("V", gen_jni_type_sig!(void));
214/// assert_eq!("Ljava/lang/Class;", gen_jni_type_sig!(Class<?>));
215/// assert_eq!("Ljava/lang/String;", gen_jni_type_sig!(String));
216/// assert_eq!("[B", gen_jni_type_sig!(byte[]));
217/// ```
218#[macro_export]
219macro_rules! gen_jni_type_sig {
220    (boolean $($param_name:ident)?) => {
221        "Z"
222    };
223    (byte $($param_name:ident)?) => {
224        "B"
225    };
226    (char $($param_name:ident)?) => {
227        "C"
228    };
229    (short $($param_name:ident)?) => {
230        "S"
231    };
232    (int $($param_name:ident)?) => {
233        "I"
234    };
235    (long $($param_name:ident)?) => {
236        "J"
237    };
238    (float $($param_name:ident)?) => {
239        "F"
240    };
241    (double $($param_name:ident)?) => {
242        "D"
243    };
244    (void) => {
245        "V"
246    };
247    (Class $(< ? >)? $($param_name:ident)?) => {
248        $crate::gen_jni_type_sig! { java.lang.Class }
249    };
250    ($($class_part:ident).+ $($param_name:ident)?) => {
251        concat! {"L", $crate::gen_class_name! {$($class_part).+}, ";"}
252    };
253    ($($class_part:ident).+ [] $($param_name:ident)?) => {
254        concat! { "[", $crate::gen_jni_type_sig! {$($class_part).+}}
255    };
256    ($($invalid:tt)*) => {
257        compile_error!(concat!("unsupported type `", stringify!($($invalid)*), "`"))
258    };
259}
260
261/// Cast a `JValueGen` to a concrete type by the given type
262///
263/// ```
264/// use jni::objects::JValue;
265/// use jni::sys::JNI_TRUE;
266/// use risingwave_jni_core::cast_jvalue;
267/// assert_eq!(cast_jvalue!({boolean}, JValue::Bool(JNI_TRUE)), true);
268/// assert_eq!(cast_jvalue!({byte}, JValue::Byte(10 as i8)), 10);
269/// assert_eq!(cast_jvalue!({char}, JValue::Char('c' as u16)), 'c' as u16);
270/// assert_eq!(cast_jvalue!({double}, JValue::Double(3.14)), 3.14);
271/// assert_eq!(cast_jvalue!({float}, JValue::Float(3.14)), 3.14);
272/// assert_eq!(cast_jvalue!({int}, JValue::Int(10)), 10);
273/// assert_eq!(cast_jvalue!({long}, JValue::Long(10)), 10);
274/// assert_eq!(cast_jvalue!({short}, JValue::Short(10)), 10);
275/// cast_jvalue!({void}, JValue::Void);
276/// let null = jni::objects::JObject::null();
277/// let _: &jni::objects::JObject<'_> = cast_jvalue!({String}, JValue::Object(&null));
278/// let _: jni::objects::JByteArray<'_> = cast_jvalue!({byte[]}, jni::objects::JValueOwned::Object(null));
279/// ```
280#[macro_export]
281macro_rules! cast_jvalue {
282    ({ boolean }, $value:expr) => {{ $value.z().expect("should be bool") }};
283    ({ byte }, $value:expr) => {{ $value.b().expect("should be byte") }};
284    ({ char }, $value:expr) => {{ $value.c().expect("should be char") }};
285    ({ double }, $value:expr) => {{ $value.d().expect("should be double") }};
286    ({ float }, $value:expr) => {{ $value.f().expect("should be float") }};
287    ({ int }, $value:expr) => {{ $value.i().expect("should be int") }};
288    ({ long }, $value:expr) => {{ $value.j().expect("should be long") }};
289    ({ short }, $value:expr) => {{ $value.s().expect("should be short") }};
290    ({ void }, $value:expr) => {{ $value.v().expect("should be void") }};
291    ({ byte[] }, $value:expr) => {{
292        let obj = $value.l().expect("should be object");
293        unsafe { jni::objects::JByteArray::from_raw(obj.into_raw()) }
294    }};
295    ({ $($class:tt)+ }, $value:expr) => {{ $value.l().expect("should be object") }};
296}
297
298/// Cast a `JValueGen` to a concrete type by the given type
299///
300/// ```
301/// use jni::sys::JNI_TRUE;
302/// use risingwave_jni_core::{cast_jvalue, to_jvalue};
303/// assert_eq!(
304///     cast_jvalue!({ boolean }, to_jvalue!({ boolean }, JNI_TRUE)),
305///     true
306/// );
307/// assert_eq!(cast_jvalue!({ byte }, to_jvalue!({ byte }, 10)), 10);
308/// assert_eq!(
309///     cast_jvalue!({ char }, to_jvalue!({ char }, 'c')),
310///     'c' as u16
311/// );
312/// assert_eq!(cast_jvalue!({ double }, to_jvalue!({ double }, 3.14)), 3.14);
313/// assert_eq!(cast_jvalue!({ float }, to_jvalue!({ float }, 3.14)), 3.14);
314/// assert_eq!(cast_jvalue!({ int }, to_jvalue!({ int }, 10)), 10);
315/// assert_eq!(cast_jvalue!({ long }, to_jvalue!({ long }, 10)), 10);
316/// assert_eq!(cast_jvalue!({ short }, to_jvalue!({ short }, 10)), 10);
317/// let obj = jni::objects::JObject::null();
318/// cast_jvalue!({ String }, to_jvalue!({ String }, &obj));
319/// ```
320#[macro_export]
321macro_rules! to_jvalue {
322    ({ boolean $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Bool($value as _) }};
323    ({ byte $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Byte($value as _) }};
324    ({ char $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Char($value as _) }};
325    ({ double $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Double($value as _) }};
326    ({ float $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Float($value as _) }};
327    ({ int $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Int($value as _) }};
328    ({ long $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Long($value as _) }};
329    ({ short $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Short($value as _) }};
330    ({ void }, $value:expr) => {{
331        compile_error! {concat! {"unlike to pass void value: ", stringify! {$value} }}
332    }};
333    ({ $($class:ident)+ $([])? $($param_name:ident)? }, $value:expr) => {{ jni::objects::JValue::Object($value as _) }};
334}
335
336/// Generate the jni signature of a given function
337/// ```
338/// use risingwave_jni_core::gen_jni_sig;
339/// assert_eq!(gen_jni_sig!(boolean f(int, short, byte[])), "(IS[B)Z");
340/// assert_eq!(
341///     gen_jni_sig!(boolean f(int, short, byte[], java.lang.String)),
342///     "(IS[BLjava/lang/String;)Z"
343/// );
344/// assert_eq!(
345///     gen_jni_sig!(boolean f(int, java.lang.String)),
346///     "(ILjava/lang/String;)Z"
347/// );
348/// assert_eq!(gen_jni_sig!(public static native int defaultVnodeCount()), "()I");
349/// assert_eq!(
350///     gen_jni_sig!(long hummockIteratorNew(byte[] readPlan)),
351///     "([B)J"
352/// );
353/// assert_eq!(gen_jni_sig!(long hummockIteratorNext(long pointer)), "(J)J");
354/// assert_eq!(
355///     gen_jni_sig!(void hummockIteratorClose(long pointer)),
356///     "(J)V"
357/// );
358/// assert_eq!(gen_jni_sig!(byte[] rowGetKey(long pointer)), "(J)[B");
359/// assert_eq!(
360///     gen_jni_sig!(java.sql.Timestamp rowGetTimestampValue(long pointer, int index)),
361///     "(JI)Ljava/sql/Timestamp;"
362/// );
363/// assert_eq!(
364///     gen_jni_sig!(String rowGetStringValue(long pointer, int index)),
365///     "(JI)Ljava/lang/String;"
366/// );
367/// assert_eq!(
368///     gen_jni_sig!(static native Object rowGetArrayValue(long pointer, int index, Class clazz)),
369///     "(JILjava/lang/Class;)Ljava/lang/Object;"
370/// );
371/// ```
372#[macro_export]
373macro_rules! gen_jni_sig {
374    // handle the result of `split_by_comma`
375    ({$({$($args:tt)+})*}, {return {$($ret:tt)*}}) => {{
376        concat! {
377            "(", $($crate::gen_jni_type_sig!{ $($args)+ },)* ")",
378            $crate::gen_jni_type_sig! {$($ret)+}
379        }
380    }};
381    ({$($ret:tt)*}, {$($args:tt)*}) => {{
382        $crate::split_by_comma! {
383            {$($args)*},
384            $crate::gen_jni_sig,
385            {return {$($ret)*}}
386        }
387    }};
388    // handle the result of `split_extract_plain_native_methods`
389    ({{$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}}}) => {{
390        $crate::gen_jni_sig! {
391            {$($ret)*}, {$($args)*}
392        }
393    }};
394    ($($input:tt)*) => {{
395        $crate::split_extract_plain_native_methods! {{$($input)*;}, $crate::gen_jni_sig}
396    }}
397}
398
399#[macro_export]
400macro_rules! for_all_plain_native_methods {
401    ($macro:path $(,$args:tt)*) => {
402        $macro! {
403            {
404                public static native void tracingSlf4jEvent(String threadName, String name, int level, String message, String stackTrace);
405
406                public static native boolean tracingSlf4jEventEnabled(int level);
407
408                public static native int defaultVnodeCount();
409
410                public static native boolean validateCdcSourceColumnType(
411                    int cdcTableType,
412                    String upstreamTypeName,
413                    int rwTypeName,
414                    long charMaxLength,
415                    boolean isUnsigned,
416                    String postgresUdtName);
417
418                static native long iteratorNewStreamChunk(long pointer);
419
420                static native boolean iteratorNext(long pointer);
421
422                public static native void initObjectStoreForTest(String stateStoreUrl, String dataDirectory);
423
424                public static native void putObject(String objectName, byte[] data);
425
426                public static native String getObjectStoreType();
427
428                public static native void deleteObjects(String dir);
429
430                public static native byte[] getObject(String objectName);
431
432                public static native String[] listObject(String dir);
433
434                static native void iteratorClose(long pointer);
435
436                static native long newStreamChunkFromPayload(byte[] streamChunkPayload);
437
438                static native long newStreamChunkFromPretty(String str);
439
440                static native void streamChunkClose(long pointer);
441
442                static native byte[] iteratorGetKey(long pointer);
443
444                static native int iteratorGetOp(long pointer);
445
446                static native boolean iteratorIsNull(long pointer, int index);
447
448                static native short iteratorGetInt16Value(long pointer, int index);
449
450                static native int iteratorGetInt32Value(long pointer, int index);
451
452                static native long iteratorGetInt64Value(long pointer, int index);
453
454                static native float iteratorGetFloatValue(long pointer, int index);
455
456                static native double iteratorGetDoubleValue(long pointer, int index);
457
458                static native boolean iteratorGetBooleanValue(long pointer, int index);
459
460                static native String iteratorGetStringValue(long pointer, int index);
461
462                static native java.time.LocalDateTime iteratorGetTimestampValue(long pointer, int index);
463
464                static native java.time.OffsetDateTime iteratorGetTimestamptzValue(long pointer, int index);
465
466                static native java.math.BigDecimal iteratorGetDecimalValue(long pointer, int index);
467
468                static native java.time.LocalTime iteratorGetTimeValue(long pointer, int index);
469
470                static native java.time.LocalDate iteratorGetDateValue(long pointer, int index);
471
472                static native String iteratorGetIntervalValue(long pointer, int index);
473
474                static native String iteratorGetJsonbValue(long pointer, int index);
475
476                static native byte[] iteratorGetByteaValue(long pointer, int index);
477
478                // TODO: object or object array?
479                static native Object iteratorGetArrayValue(long pointer, int index, Class<?> clazz);
480
481                public static native boolean sendCdcSourceMsgToChannel(long channelPtr, byte[] msg);
482
483                public static native boolean sendCdcSourceErrorToChannel(long channelPtr, String errorMsg);
484
485                public static native void cdcSourceSenderClose(long channelPtr);
486
487                public static native boolean sendOracleSnapshotResponseToChannel(long channelPtr, byte[] msg);
488
489                public static native com.risingwave.java.binding.JniSinkWriterStreamRequest
490                    recvSinkWriterRequestFromChannel(long channelPtr);
491
492                public static native boolean sendSinkWriterResponseToChannel(long channelPtr, byte[] msg);
493
494                public static native boolean sendSinkWriterErrorToChannel(long channelPtr, String msg);
495
496                public static native byte[] recvSinkCoordinatorRequestFromChannel(long channelPtr);
497
498                public static native boolean sendSinkCoordinatorResponseToChannel(long channelPtr, byte[] msg);
499            }
500            $(,$args)*
501        }
502    };
503}
504
505/// Given the plain text of a list native methods, split the methods by semicolon (;), extract
506/// the return type, argument list and name of the methods and pass the result to the callback
507/// `$macro` with the extracted result as the first parameter. The result can be matched with
508/// pattern `{$({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*}`
509///
510/// ```
511/// macro_rules! call_split_extract_plain_native_methods {
512///     ({$({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*}) => {
513///         [$(
514///             (stringify! {$func_name}, stringify!{$($ret)*}, stringify!{($($args)*)})
515///         ),*]
516///     };
517///     ($($input:tt)*) => {{
518///         risingwave_jni_core::split_extract_plain_native_methods! {
519///             {$($input)*},
520///             call_split_extract_plain_native_methods
521///         }
522///     }}
523/// }
524/// assert_eq!([
525///     ("f", "int", "(int param1, boolean param2)"),
526///     ("f2", "boolean[]", "()"),
527///     ("f3", "java.lang.String", "(byte[] param)")
528/// ], call_split_extract_plain_native_methods!(
529///     int f(int param1, boolean param2);
530///     boolean[] f2();
531///     java.lang.String f3(byte[] param);
532/// ))
533/// ```
534#[macro_export]
535macro_rules! split_extract_plain_native_methods {
536    (
537        {$($input:tt)*},
538        $macro:path
539        $(,$extra_args:tt)*
540    ) => {{
541        $crate::split_extract_plain_native_methods! {
542            {$($input)*},
543            {},
544            $macro
545            $(,$extra_args)*
546        }
547    }};
548    (
549        {
550            $(public)? static native $($first:tt)*
551        },
552        {
553            $($second:tt)*
554        },
555        $macro:path
556        $(,$extra_args:tt)*
557    ) => {
558        $crate::split_extract_plain_native_methods! {
559            {
560                $($first)*
561            },
562            {
563                $($second)*
564            },
565            $macro
566            $(,$extra_args)*
567        }
568    };
569    (
570        {
571            $($ret:tt).+ $func_name:ident($($args:tt)*); $($rest:tt)*
572        },
573        {
574            $({$prev_func_name:ident, {$($prev_ret:tt)*}, {$($prev_args:tt)*}})*
575        },
576        $macro:path
577        $(,$extra_args:tt)*
578    ) => {
579        $crate::split_extract_plain_native_methods! {
580            {$($rest)*},
581            {
582                $({$prev_func_name, {$($prev_ret)*}, {$($prev_args)*}})*
583                {$func_name, {$($ret).+}, {$($args)*}}
584            },
585            $macro
586            $(,$extra_args)*
587        }
588    };
589    (
590        {
591            $($ret:tt).+ [] $func_name:ident($($args:tt)*); $($rest:tt)*
592        },
593        {
594            $({$prev_func_name:ident, {$($prev_ret:tt)*}, {$($prev_args:tt)*}})*
595        },
596        $macro:path
597        $(,$extra_args:tt)*
598    ) => {
599        $crate::split_extract_plain_native_methods! {
600            {$($rest)*},
601            {
602                $({$prev_func_name, {$($prev_ret)*}, {$($prev_args)*}})*
603                {$func_name, {$($ret).+ []}, {$($args)*}}
604            },
605            $macro
606            $(,$extra_args)*
607        }
608    };
609    (
610        {},
611        {
612            $({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*
613        },
614        $macro:path
615        $(,$extra_args:tt)*
616    ) => {
617        $macro! {
618            {
619                $({$func_name, {$($ret)*}, {$($args)*}})*
620            }
621            $(,$extra_args)*
622        }
623    };
624    ($($invalid:tt)*) => {
625        compile_error!(concat!("unable to split extract `", stringify!($($invalid)*), "`"))
626    };
627}
628
629/// Pass the information of all native methods to the callback `$macro`. The input can be matched
630/// with pattern `{$({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*}`
631#[macro_export]
632macro_rules! for_all_native_methods {
633    ($macro:path $(,$args:tt)*) => {{
634        $crate::for_all_plain_native_methods! {
635            $crate::for_all_native_methods,
636            $macro
637            $(,$args)*
638        }
639    }};
640    (
641        {
642            $({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*
643        },
644        $macro:path
645        $(,$extra_args:tt)*
646    ) => {{
647        $macro! {
648            {$({$func_name, {$($ret)*}, {$($args)*}})*}
649            $(,$extra_args)*
650        }
651    }};
652    (
653        {$($input:tt)*},
654        $macro:path
655        $(,$extra_args:tt)*
656    ) => {{
657        $crate::split_extract_plain_native_methods! {
658            {$($input)*},
659            $crate::for_all_native_methods,
660            $macro
661            $(,$extra_args)*
662        }
663    }};
664}
665
666/// Convert the argument value list when invoking a method to a list of `JValue` by the argument type list in the signature
667/// ```
668/// use risingwave_jni_core::convert_args_list;
669/// use jni::objects::{JObject, JValue};
670/// use jni::sys::JNI_TRUE;
671/// let list: [JValue<'static, 'static>; 3] = convert_args_list!(
672///     {boolean first, int second, byte third},
673///     {true, 10, 20}
674/// );
675/// match &list[0] {
676///     JValue::Bool(b) => assert_eq!(*b, JNI_TRUE),
677///     value => unreachable!("wrong value: {:?}", value),
678/// }
679/// match &list[1] {
680///     JValue::Int(v) => assert_eq!(*v, 10),
681///     value => unreachable!("wrong value: {:?}", value),
682/// }
683/// match &list[2] {
684///     JValue::Byte(v) => assert_eq!(*v, 20),
685///     value => unreachable!("wrong value: {:?}", value),
686/// }
687/// ```
688#[macro_export]
689macro_rules! convert_args_list {
690    (
691        {
692            {$($first_args:tt)+}
693            $({$($args:tt)+})*
694        },
695        {
696            {$first_value:expr}
697            $({$value:expr})*
698        },
699        {
700            $({$converted:expr})*
701        }) => {
702        $crate::convert_args_list! {
703            {$({$($args)+})*},
704            {$({$value})*},
705            {
706                $({$converted})*
707                {
708                    $crate::to_jvalue! {
709                        {$($first_args)+},
710                        {$first_value}
711                    }
712                }
713            }
714        }
715    };
716    ({$($args:tt)+}, {}, {$({$converted:expr})*}) => {
717        compile_error! {concat!{"trailing argument not passed: ", stringify!{$($args)+}}}
718    };
719    ({}, {$($value:tt)+}, {$({$converted:expr})*}) => {
720        compile_error! {concat!{"trailing extra value passed: ", stringify!{$($value)+}}}
721    };
722    ({}, {}, {$({$converted:expr})*}) => {
723        [$(
724            $converted
725        ),*]
726    };
727    ({$($args:tt)*}, {$($value:expr),*}) => {{
728        $crate::split_by_comma! {
729            {$($args)*},
730            $crate::convert_args_list,
731            {$({$value})*},
732            {}
733        }
734    }};
735    ($($invalid:tt)*) => {
736        compile_error!(concat!("failed to convert `", stringify!($($invalid)*), "`"))
737    };
738}
739
740#[macro_export]
741macro_rules! call_static_method {
742    (
743        {{$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}}},
744        {$($class:ident).+},
745        {$env:expr} $(, $method_args:expr)*
746    ) => {{
747        $crate::call_static_method! {
748            $env,
749            $crate::gen_class_name!($($class).+),
750            stringify! {$func_name},
751            {{$($ret)*}, {$($args)*}}
752            $(, $method_args)*
753        }
754    }};
755    ($env:expr, {$($class:ident).+}, {$($method:tt)*} $(, $args:expr)*) => {{
756        $crate::split_extract_plain_native_methods! {
757            {$($method)*;},
758            $crate::call_static_method,
759            {$($class).+},
760            {$env} $(, $args)*
761        }
762    }};
763    (
764        $env:expr,
765        $class_name:expr,
766        $func_name:expr,
767        {{$($ret:tt)*}, {$($args:tt)*}}
768        $(, $method_args:expr)*
769    ) => {{
770        $env.call_static_method(
771            $class_name,
772            $func_name,
773            $crate::gen_jni_sig! { {$($ret)+}, {$($args)*}},
774            &{
775                $crate::convert_args_list! {
776                    {$($args)*},
777                    {$($method_args),*}
778                }
779            },
780        ).map(|jvalue| {
781            $crate::cast_jvalue! {
782                {$($ret)*},
783                jvalue
784            }
785        })
786    }};
787}
788
789#[macro_export]
790macro_rules! call_method {
791    (
792        {{$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}}},
793        $env:expr, $obj:expr $(, $method_args:expr)*
794    ) => {{
795        $env.call_method(
796            $obj,
797            stringify!{$func_name},
798            $crate::gen_jni_sig! { {$($ret)+}, {$($args)*}},
799            &{
800                $crate::convert_args_list! {
801                    {$($args)*},
802                    {$($method_args),*}
803                }
804            },
805        ).map(|jvalue| {
806            $crate::cast_jvalue! {
807                {$($ret)*},
808                jvalue
809            }
810        })
811    }};
812    ($env:expr, $obj:expr, {$($method:tt)*} $(, $args:expr)*) => {{
813        $crate::split_extract_plain_native_methods! {
814            {$($method)*;},
815            $crate::call_method,
816            $env, $obj $(, $args)*
817        }
818    }};
819}
820
821#[macro_export]
822macro_rules! gen_native_method_entry {
823    (
824        $class_prefix:ident, $func_name:ident, {$($ret:tt)+}, {$($args:tt)*}
825    ) => {{
826        {
827            let fn_ptr = $crate::paste! {[<$class_prefix $func_name> ]} as *mut c_void;
828            let sig = $crate::gen_jni_sig! { {$($ret)+}, {$($args)*}};
829            jni::NativeMethod {
830                name: jni::strings::JNIString::from(stringify! {$func_name}),
831                sig: jni::strings::JNIString::from(sig),
832                fn_ptr,
833            }
834        }
835    }};
836}
837
838#[cfg(test)]
839mod tests {
840    use std::fmt::Formatter;
841
842    #[test]
843    fn test_for_all_gen() {
844        macro_rules! gen_array {
845            (test) => {{
846                for_all_native_methods! {
847                    {
848                        public static native int defaultVnodeCount();
849                        static native long hummockIteratorNew(byte[] readPlan);
850                        public static native byte[] rowGetKey(long pointer);
851                    },
852                    gen_array
853                }
854            }};
855            (all) => {{
856                for_all_native_methods! {
857                    gen_array
858                }
859            }};
860            ({$({ $func_name:ident, {$($ret:tt)+}, {$($args:tt)*} })*}) => {{
861                [
862                    $(
863                        (stringify! {$func_name}, gen_jni_sig! { {$($ret)+}, {$($args)*}}),
864                    )*
865                ]
866            }};
867        }
868        let sig: [(_, _); 3] = gen_array!(test);
869        assert_eq!(
870            sig,
871            [
872                ("defaultVnodeCount", "()I"),
873                ("hummockIteratorNew", "([B)J"),
874                ("rowGetKey", "(J)[B")
875            ]
876        );
877
878        let sig = gen_array!(all);
879        assert!(!sig.is_empty());
880    }
881
882    #[test]
883    fn test_all_native_methods() {
884        // This test shows the signature of all native methods
885        let expected = expect_test::expect![[r#"
886            [
887                tracingSlf4jEvent                        (Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V,
888                tracingSlf4jEventEnabled                 (I)Z,
889                defaultVnodeCount                        ()I,
890                validateCdcSourceColumnType              (ILjava/lang/String;IJZLjava/lang/String;)Z,
891                iteratorNewStreamChunk                   (J)J,
892                iteratorNext                             (J)Z,
893                initObjectStoreForTest                   (Ljava/lang/String;Ljava/lang/String;)V,
894                putObject                                (Ljava/lang/String;[B)V,
895                getObjectStoreType                       ()Ljava/lang/String;,
896                deleteObjects                            (Ljava/lang/String;)V,
897                getObject                                (Ljava/lang/String;)[B,
898                listObject                               (Ljava/lang/String;)[Ljava/lang/String;,
899                iteratorClose                            (J)V,
900                newStreamChunkFromPayload                ([B)J,
901                newStreamChunkFromPretty                 (Ljava/lang/String;)J,
902                streamChunkClose                         (J)V,
903                iteratorGetKey                           (J)[B,
904                iteratorGetOp                            (J)I,
905                iteratorIsNull                           (JI)Z,
906                iteratorGetInt16Value                    (JI)S,
907                iteratorGetInt32Value                    (JI)I,
908                iteratorGetInt64Value                    (JI)J,
909                iteratorGetFloatValue                    (JI)F,
910                iteratorGetDoubleValue                   (JI)D,
911                iteratorGetBooleanValue                  (JI)Z,
912                iteratorGetStringValue                   (JI)Ljava/lang/String;,
913                iteratorGetTimestampValue                (JI)Ljava/time/LocalDateTime;,
914                iteratorGetTimestamptzValue              (JI)Ljava/time/OffsetDateTime;,
915                iteratorGetDecimalValue                  (JI)Ljava/math/BigDecimal;,
916                iteratorGetTimeValue                     (JI)Ljava/time/LocalTime;,
917                iteratorGetDateValue                     (JI)Ljava/time/LocalDate;,
918                iteratorGetIntervalValue                 (JI)Ljava/lang/String;,
919                iteratorGetJsonbValue                    (JI)Ljava/lang/String;,
920                iteratorGetByteaValue                    (JI)[B,
921                iteratorGetArrayValue                    (JILjava/lang/Class;)Ljava/lang/Object;,
922                sendCdcSourceMsgToChannel                (J[B)Z,
923                sendCdcSourceErrorToChannel              (JLjava/lang/String;)Z,
924                cdcSourceSenderClose                     (J)V,
925                sendOracleSnapshotResponseToChannel      (J[B)Z,
926                recvSinkWriterRequestFromChannel         (J)Lcom/risingwave/java/binding/JniSinkWriterStreamRequest;,
927                sendSinkWriterResponseToChannel          (J[B)Z,
928                sendSinkWriterErrorToChannel             (JLjava/lang/String;)Z,
929                recvSinkCoordinatorRequestFromChannel    (J)[B,
930                sendSinkCoordinatorResponseToChannel     (J[B)Z,
931            ]
932        "#]];
933
934        struct MethodInfo {
935            name: &'static str,
936            sig: &'static str,
937        }
938
939        impl std::fmt::Debug for MethodInfo {
940            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
941                write!(f, "{:40} {}", self.name, self.sig)
942            }
943        }
944
945        macro_rules! gen_all_native_method_info {
946            () => {{
947                $crate::for_all_native_methods! {
948                    gen_all_native_method_info
949                }
950            }};
951            ({$({$func_name:ident, {$($ret:tt)*}, {$($args:tt)*}})*}) => {
952                [$(
953                    (
954                        MethodInfo {
955                            name: stringify! {$func_name},
956                            sig: $crate::gen_jni_sig! {
957                                {$($ret)*}, {$($args)*}
958                            },
959                        }
960                    )
961                ),*]
962            }
963        }
964        let info = gen_all_native_method_info!();
965        expected.assert_debug_eq(&info);
966    }
967}