From f8f4247b15840a5be38dee183ae65e12b8620b4e Mon Sep 17 00:00:00 2001 From: Peefy Date: Wed, 18 Sep 2024 11:43:16 +0800 Subject: [PATCH] feat: add kcl version api for the wasm host (#1656) * feat: add kcl version api for the wasm host Signed-off-by: peefy * ci: bump CI condition to pr on main and push on main Signed-off-by: peefy --------- Signed-off-by: peefy --- .../workflows/build-test-centos7-amd64.yaml | 10 +++++-- .github/workflows/build-test-macos-arm64.yml | 8 +++++- .github/workflows/build-test-ubuntu-arm64.yml | 10 +++++-- .github/workflows/macos_test.yaml | 8 +++++- .github/workflows/mingw_test.yaml | 8 +++++- .github/workflows/ubuntu_test.yaml | 8 +++++- .github/workflows/wasm_test.yaml | 8 +++++- .github/workflows/windows_test.yaml | 8 +++++- kclvm/src/lib.rs | 28 +++++++++++-------- 9 files changed, 73 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build-test-centos7-amd64.yaml b/.github/workflows/build-test-centos7-amd64.yaml index 1809b37ad..d5b1a48d1 100644 --- a/.github/workflows/build-test-centos7-amd64.yaml +++ b/.github/workflows/build-test-centos7-amd64.yaml @@ -1,7 +1,11 @@ name: Build and Test on centos7 amd64 - -on: ["push", "pull_request"] - +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test-centos7: name: Build and Test on centos7 amd64 diff --git a/.github/workflows/build-test-macos-arm64.yml b/.github/workflows/build-test-macos-arm64.yml index 1234f4aa4..ffd553369 100644 --- a/.github/workflows/build-test-macos-arm64.yml +++ b/.github/workflows/build-test-macos-arm64.yml @@ -1,5 +1,11 @@ name: Build and Test on MacOS ARCH64 -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: # Ref: https://github.com/actions/runner-images/tree/main/images/macos diff --git a/.github/workflows/build-test-ubuntu-arm64.yml b/.github/workflows/build-test-ubuntu-arm64.yml index b7c50a392..89dec5af8 100644 --- a/.github/workflows/build-test-ubuntu-arm64.yml +++ b/.github/workflows/build-test-ubuntu-arm64.yml @@ -1,7 +1,11 @@ name: Build and Test on Linux ARCH64 - -on: ["push", "pull_request"] - +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test-arm64: name: Build and Test on Linux ARM64 diff --git a/.github/workflows/macos_test.yaml b/.github/workflows/macos_test.yaml index 6d7096d46..4b3e5f96e 100644 --- a/.github/workflows/macos_test.yaml +++ b/.github/workflows/macos_test.yaml @@ -1,5 +1,11 @@ name: build-and-test-macos -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: # Ref: https://github.com/actions/runner-images/tree/main/images/macos diff --git a/.github/workflows/mingw_test.yaml b/.github/workflows/mingw_test.yaml index 2a79b4ccb..5ad74bc4c 100644 --- a/.github/workflows/mingw_test.yaml +++ b/.github/workflows/mingw_test.yaml @@ -1,5 +1,11 @@ name: build-and-test-windows-mingw -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: name: build and test on windows mingw diff --git a/.github/workflows/ubuntu_test.yaml b/.github/workflows/ubuntu_test.yaml index 25558a2bd..61efd2205 100644 --- a/.github/workflows/ubuntu_test.yaml +++ b/.github/workflows/ubuntu_test.yaml @@ -1,5 +1,11 @@ name: build-and-test-ubuntu -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: # Ref: https://github.com/actions/runner-images/tree/main/images/linux diff --git a/.github/workflows/wasm_test.yaml b/.github/workflows/wasm_test.yaml index 73ac96638..497f66fb4 100644 --- a/.github/workflows/wasm_test.yaml +++ b/.github/workflows/wasm_test.yaml @@ -1,5 +1,11 @@ name: build-and-test-wasm -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: # Ref: https://github.com/actions/runner-images/tree/main/images/linux diff --git a/.github/workflows/windows_test.yaml b/.github/workflows/windows_test.yaml index 71f3cc616..a3574ef56 100644 --- a/.github/workflows/windows_test.yaml +++ b/.github/workflows/windows_test.yaml @@ -1,5 +1,11 @@ name: build-and-test-windows -on: ["push", "pull_request"] +on: + pull_request: + branches: + - main + push: + branches: + - main jobs: build-and-test: name: build and test on windows diff --git a/kclvm/src/lib.rs b/kclvm/src/lib.rs index cedf41e6a..1f6a74a61 100644 --- a/kclvm/src/lib.rs +++ b/kclvm/src/lib.rs @@ -160,6 +160,23 @@ pub unsafe extern "C" fn kcl_fmt(src_ptr: *const c_char) -> *const c_char { } } +fn intern_fmt(src: &str) -> Result { + let api = API::default(); + let args = &FormatCodeArgs { + source: src.to_string(), + }; + match api.format_code(args) { + Ok(result) => String::from_utf8(result.formatted).map_err(|err| err.to_string()), + Err(err) => Err(err.to_string()), + } +} + +/// Exposes a normal kcl version function to the WASM host. +#[no_mangle] +pub unsafe extern "C" fn kcl_version() -> *const c_char { + CString::new(kclvm_version::VERSION).unwrap().into_raw() +} + /// Exposes a normal kcl runtime error function to the WASM host. #[no_mangle] pub unsafe extern "C" fn kcl_runtime_err(buffer: *mut u8, length: usize) -> isize { @@ -178,17 +195,6 @@ pub unsafe extern "C" fn kcl_runtime_err(buffer: *mut u8, length: usize) -> isiz }) } -fn intern_fmt(src: &str) -> Result { - let api = API::default(); - let args = &FormatCodeArgs { - source: src.to_string(), - }; - match api.format_code(args) { - Ok(result) => String::from_utf8(result.formatted).map_err(|err| err.to_string()), - Err(err) => Err(err.to_string()), - } -} - /// Exposes an allocation function to the WASM host. /// /// _This implementation is copied from wasm-bindgen_