Skip to content

Commit c321158

Browse files
authored
Merge pull request #29 from kcl-lang/refactor-parser-api
refactor: parser api
2 parents 33e2dc3 + fba3537 commit c321158

File tree

14 files changed

+640
-618
lines changed

14 files changed

+640
-618
lines changed

java/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ doc = false
1111
jni = "0.21.1"
1212
anyhow = "1"
1313
kcl-lang = {path = "../"}
14+
once_cell = "1.19.0"
15+
lazy_static = "1.4.0"

java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>com.kcl</groupId>
8-
<artifactId>kcl-java</artifactId>
8+
<artifactId>kcl-java-lib</artifactId>
99
<version>0.1.0</version>
1010

1111
<properties>

java/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
extern crate anyhow;
22
extern crate jni;
33
extern crate kcl_lang;
4+
extern crate lazy_static;
5+
extern crate once_cell;
46

57
use anyhow::Result;
68
use jni::objects::{JByteArray, JClass, JObject};
79
use jni::sys::jbyteArray;
810
use jni::JNIEnv;
11+
use lazy_static::lazy_static;
12+
use once_cell::sync::OnceCell;
13+
use std::sync::Mutex;
14+
15+
lazy_static! {
16+
static ref API_INSTANCE: Mutex<OnceCell<kcl_lang::API>> = Mutex::new(OnceCell::new());
17+
}
918

1019
#[no_mangle]
1120
pub extern "system" fn Java_com_kcl_api_API_callNative(
@@ -21,11 +30,12 @@ pub extern "system" fn Java_com_kcl_api_API_callNative(
2130
}
2231

2332
fn intern_call_native(env: &mut JNIEnv, name: JByteArray, args: JByteArray) -> Result<jbyteArray> {
24-
let api = kcl_lang::API::new()?;
33+
let binding = API_INSTANCE.lock().unwrap();
34+
let api = binding.get_or_init(|| kcl_lang::API::new().expect("Failed to create API instance"));
2535
let name = env.convert_byte_array(name)?;
2636
let args = env.convert_byte_array(args)?;
2737
let result = api.call_native(&name, &args)?;
28-
let j_byte_array = env.byte_array_from_slice(result)?;
38+
let j_byte_array = env.byte_array_from_slice(&result)?;
2939
Ok(j_byte_array.into_raw())
3040
}
3141

java/src/main/java/com/kcl/api/API.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public API() {
3838
* }</pre>
3939
*
4040
* @param args the arguments specifying the file paths to be parsed.
41-
* @return the result of parsing the program and parse errors, including the AST in JSON format.
41+
* @return the result of parsing the program and parse errors, including the AST
42+
* in JSON format.
4243
* @throws Exception if an error occurs during the remote procedure call.
4344
*/
4445
@Override

python/kcl_py/api/gpyrpc_pb2.py

Lines changed: 0 additions & 610 deletions
This file was deleted.
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .gpyrpc_pb2 import *
1+
from .spec_pb2 import *
22
from .service import API
33

44
__all__ = ["API"]

python/kcl_py/api/service.py renamed to python/kcl_py_lib/api/service.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import ctypes
22
import tempfile
33
import threading
4-
from kcl_py.bootstrap import (lib_full_name, install_kclvm)
5-
from .gpyrpc_pb2 import *
4+
from kcl_py_lib.bootstrap import (lib_full_name, install_kclvm)
5+
from .spec_pb2 import *
66
from ctypes import c_char_p, c_void_p
77
import google.protobuf.json_format as json_format
88
from google.protobuf import message as _message
@@ -26,6 +26,12 @@ class API:
2626
def __init__(self):
2727
self.caller = Caller()
2828

29+
def parse_file(self, args: ParseFile_Args) -> ParseFile_Result:
30+
return self.caller.call("KclvmService.ParseFile", args)
31+
32+
def parse_program(self, args: ParseProgram_Args) -> ParseProgram_Result:
33+
return self.caller.call("KclvmService.ParseProgram", args)
34+
2935
def exec_program(self, args: ExecProgram_Args) -> ExecProgram_Result:
3036
return self.caller.call("KclvmService.ExecProgram", args)
3137

@@ -103,6 +109,10 @@ def call(self, name: str, args):
103109
def create_method_req_message(self, method: str) -> _message.Message:
104110
if method in ["Ping", "KclvmService.Ping"]:
105111
return Ping_Args()
112+
if method in ["ParseFile", "KclvmService.ParseFile"]:
113+
return ParseFile_Args()
114+
if method in ["ParseProgram", "KclvmService.ParseProgram"]:
115+
return ParseProgram_Args()
106116
if method in ["ExecProgram", "KclvmService.ExecProgram"]:
107117
return ExecProgram_Args()
108118
if method in ["ResetPlugin", "KclvmService.ResetPlugin"]:
@@ -130,6 +140,10 @@ def create_method_req_message(self, method: str) -> _message.Message:
130140
def create_method_resp_message(self, method: str) -> _message.Message:
131141
if method in ["Ping", "KclvmService.Ping"]:
132142
return Ping_Result()
143+
if method in ["ParseFile", "KclvmService.ParseFile"]:
144+
return ParseFile_Result()
145+
if method in ["ParseProgram", "KclvmService.ParseProgram"]:
146+
return ParseProgram_Result()
133147
if method in ["ExecProgram", "KclvmService.ExecProgram"]:
134148
return ExecProgram_Result()
135149
if method in ["ResetPlugin", "KclvmService.ResetPlugin"]:

python/kcl_py_lib/api/spec_pb2.py

Lines changed: 598 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

0 commit comments

Comments
 (0)