Skip to main content

ri/java/classes/
core.rs

1//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
2//!
3//! This file is part of Ri.
4//! The Ri project belongs to the Dunimd Team.
5//!
6//! Licensed under the Apache License, Version 2.0 (the "License");
7//! You may not use this file except in compliance with the License.
8//! You may obtain a copy of the License at
9//!
10//!     http://www.apache.org/licenses/LICENSE-2.0
11//!
12//! Unless required by applicable law or agreed to in writing, software
13//! distributed under the License is distributed on an "AS IS" BASIS,
14//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15//! See the License for the specific language governing permissions and
16//! limitations under the License.
17
18//! # Core Module JNI Bindings
19//!
20//! JNI bindings for Ri core classes.
21
22use jni::JNIEnv;
23use jni::objects::{JClass, JString};
24use jni::sys::{jlong, jboolean, jstring};
25use crate::core::{RiAppBuilder, RiAppRuntime, RiError};
26use crate::java::exception::{throw_ri_error, check_not_null};
27use crate::config::RiConfig;
28
29// =============================================================================
30// RiAppBuilder JNI Bindings
31// =============================================================================
32
33#[no_mangle]
34pub extern "system" fn Java_com_dunimd_ri_RiAppBuilder_new0(
35    _env: JNIEnv,
36    _class: JClass,
37) -> jlong {
38    let builder = Box::new(RiAppBuilder::new());
39    Box::into_raw(builder) as jlong
40}
41
42#[no_mangle]
43pub extern "system" fn Java_com_dunimd_ri_RiAppBuilder_withConfig(
44    mut env: JNIEnv,
45    _class: JClass,
46    ptr: jlong,
47    config_path: JString,
48) -> jlong {
49    if !check_not_null(&mut env, ptr, "RiAppBuilder") {
50        return 0;
51    }
52    
53    let builder = unsafe { Box::from_raw(ptr as *mut RiAppBuilder) };
54    let path: String = env.get_string(&config_path)
55        .expect("Failed to get config path")
56        .into();
57    
58    match builder.with_config(&path) {
59        Ok(new_builder) => {
60            let boxed = Box::new(new_builder);
61            Box::into_raw(boxed) as jlong
62        }
63        Err(e) => {
64            throw_ri_error(&mut env, &e.to_string());
65            0
66        }
67    }
68}
69
70#[no_mangle]
71pub extern "system" fn Java_com_dunimd_ri_RiAppBuilder_build(
72    mut env: JNIEnv,
73    _class: JClass,
74    ptr: jlong,
75) -> jlong {
76    if !check_not_null(&mut env, ptr, "RiAppBuilder") {
77        return 0;
78    }
79    
80    let builder = unsafe { Box::from_raw(ptr as *mut RiAppBuilder) };
81    
82    match builder.build() {
83        Ok(runtime) => {
84            let boxed = Box::new(runtime);
85            Box::into_raw(boxed) as jlong
86        }
87        Err(e) => {
88            throw_ri_error(&mut env, &e.to_string());
89            0
90        }
91    }
92}
93
94#[no_mangle]
95pub extern "system" fn Java_com_dunimd_ri_RiAppBuilder_free0(
96    _env: JNIEnv,
97    _class: JClass,
98    ptr: jlong,
99) {
100    if ptr != 0 {
101        unsafe {
102            let _ = Box::from_raw(ptr as *mut RiAppBuilder);
103        }
104    }
105}
106
107// =============================================================================
108// RiAppRuntime JNI Bindings
109// =============================================================================
110
111#[no_mangle]
112pub extern "system" fn Java_com_dunimd_ri_RiAppRuntime_free0(
113    _env: JNIEnv,
114    _class: JClass,
115    ptr: jlong,
116) {
117    if ptr != 0 {
118        unsafe {
119            let _ = Box::from_raw(ptr as *mut RiAppRuntime);
120        }
121    }
122}
123
124#[no_mangle]
125pub extern "system" fn Java_com_dunimd_ri_RiAppRuntime_isRunning(
126    mut env: JNIEnv,
127    _class: JClass,
128    ptr: jlong,
129) -> jboolean {
130    if !check_not_null(&mut env, ptr, "RiAppRuntime") {
131        return 0;
132    }
133    
134    let _runtime = unsafe { &*(ptr as *const RiAppRuntime) };
135    0
136}
137
138#[no_mangle]
139pub extern "system" fn Java_com_dunimd_ri_RiAppRuntime_shutdown(
140    mut env: JNIEnv,
141    _class: JClass,
142    ptr: jlong,
143) {
144    if !check_not_null(&mut env, ptr, "RiAppRuntime") {
145        return;
146    }
147    
148    let _runtime = unsafe { &*(ptr as *const RiAppRuntime) };
149}
150
151// =============================================================================
152// RiConfig JNI Bindings
153// =============================================================================
154
155#[no_mangle]
156pub extern "system" fn Java_com_dunimd_ri_RiConfig_new0(
157    _env: JNIEnv,
158    _class: JClass,
159) -> jlong {
160    let config = Box::new(RiConfig::default());
161    Box::into_raw(config) as jlong
162}
163
164#[no_mangle]
165pub extern "system" fn Java_com_dunimd_ri_RiConfig_free0(
166    _env: JNIEnv,
167    _class: JClass,
168    ptr: jlong,
169) {
170    if ptr != 0 {
171        unsafe {
172            let _ = Box::from_raw(ptr as *mut RiConfig);
173        }
174    }
175}
176
177#[no_mangle]
178pub extern "system" fn Java_com_dunimd_ri_RiConfig_get(
179    mut env: JNIEnv,
180    _class: JClass,
181    ptr: jlong,
182    key: JString,
183) -> jstring {
184    if !check_not_null(&mut env, ptr, "RiConfig") {
185        return std::ptr::null_mut();
186    }
187    
188    let config = unsafe { &*(ptr as *const RiConfig) };
189    let key_str: String = env.get_string(&key)
190        .expect("Failed to get key")
191        .into();
192    
193    match config.get(&key_str) {
194        Some(value) => {
195            env.new_string(value)
196                .expect("Failed to create Java string")
197                .into_raw()
198        }
199        None => std::ptr::null_mut(),
200    }
201}
202
203// =============================================================================
204// RiError JNI Bindings
205// =============================================================================
206
207#[no_mangle]
208pub extern "system" fn Java_com_dunimd_ri_RiError_getMessage(
209    mut env: JNIEnv,
210    _class: JClass,
211    ptr: jlong,
212) -> jstring {
213    if !check_not_null(&mut env, ptr, "RiError") {
214        return std::ptr::null_mut();
215    }
216    
217    let error = unsafe { &*(ptr as *const RiError) };
218    env.new_string(error.to_string())
219        .expect("Failed to create Java string")
220        .into_raw()
221}
222
223#[no_mangle]
224pub extern "system" fn Java_com_dunimd_ri_RiError_free0(
225    _env: JNIEnv,
226    _class: JClass,
227    ptr: jlong,
228) {
229    if ptr != 0 {
230        unsafe {
231            let _ = Box::from_raw(ptr as *mut RiError);
232        }
233    }
234}