Skip to content

Commit

Permalink
[ISSUE apache#4612] prepare shenyu-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
hailang committed Jul 10, 2023
1 parent 1cd6444 commit 43fc5bb
Show file tree
Hide file tree
Showing 20 changed files with 1,309 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/build-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: build wasm

on: [push]

jobs:
linux:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Build with rust
run: |
cd ${{ github.workspace }}/shenyu-wasm/shenyu-wasm-build
/home/runner/.cargo/bin/cargo clean
/home/runner/.cargo/bin/cargo build --release
- uses: actions/upload-artifact@v2
with:
name: lib
path: shenyu-wasm/shenyu-wasm-build/target/release/lib*
if-no-files-found: error

mac:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Set up rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Build with rust
run: |
cd ${{ github.workspace }}/shenyu-wasm/shenyu-wasm-build
/Users/runner/.cargo/bin/cargo clean
/Users/runner/.cargo/bin/cargo build --release
- uses: actions/upload-artifact@v2
with:
name: lib
path: shenyu-wasm/shenyu-wasm-build/target/release/lib*
if-no-files-found: error

windows:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Set up rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Build with rust
run: |
cd ${{ github.workspace }}/shenyu-wasm/shenyu-wasm-build
C://Users//runneradmin//.cargo//bin//cargo.exe clean
C://Users//runneradmin//.cargo//bin//cargo.exe build --release
- uses: actions/upload-artifact@v2
with:
name: lib
path: shenyu-wasm/shenyu-wasm-build/target/release/*.dll
if-no-files-found: error
- uses: actions/upload-artifact@v2
with:
name: lib
path: shenyu-wasm/shenyu-wasm-build/target/release/*.lib
if-no-files-found: error
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Thumbs.db
# agent build ignore
/agent/

/**/Cargo.lock
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>shenyu-sdk</module>
<module>shenyu-discovery</module>
<module>shenyu-kubernetes-controller</module>
<module>shenyu-wasm</module>
</modules>

<licenses>
Expand Down
32 changes: 32 additions & 0 deletions shenyu-wasm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu</artifactId>
<version>2.6.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shenyu-wasm</artifactId>
<packaging>pom</packaging>
<modules>
<module>shenyu-wasm-runtime</module>
</modules>
</project>
15 changes: 15 additions & 0 deletions shenyu-wasm/shenyu-wasm-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
publish = false
name = "shenyu-wasm-build"
version = "0.1.0"
authors = ["zhangzicheng@apache.org"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasmer = { git = "https://github.com/wasmerio/wasmer", rev = "1.0.0" }
wasmer-runtime = { git = "https://github.com/wasmerio/wasmer", rev = "1.0.0" }
wasmer-runtime-core = { git = "https://github.com/wasmerio/wasmer", rev = "1.0.0" }
jni = "0.16"
44 changes: 44 additions & 0 deletions shenyu-wasm/shenyu-wasm-build/src/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub use jni::errors::Error;
use jni::{errors::ErrorKind, JNIEnv};
use std::thread;

pub fn runtime_error(message: String) -> Error {
Error::from_kind(ErrorKind::Msg(message))
}

#[derive(Debug)]
pub enum JOption<T> {
Some(T),
None,
}

impl<T> JOption<T> {
pub fn unwrap_or(self, default: T) -> T {
match self {
JOption::Some(result) => result,
JOption::None => default,
}
}
}

pub fn joption_or_throw<T>(env: &JNIEnv, result: thread::Result<Result<T, Error>>) -> JOption<T> {
match result {
Ok(result) => match result {
Ok(result) => JOption::Some(result),
Err(error) => {
if !env.exception_check().unwrap() {
env.throw_new("java/lang/RuntimeException", &error.to_string())
.expect("Cannot throw an `java/lang/RuntimeException` exception.");
}

JOption::None
}
},
Err(ref error) => {
env.throw_new("java/lang/RuntimeException", format!("{:?}", error))
.expect("Cannot throw an `java/lang/RuntimeException` exception.");

JOption::None
}
}
}
Loading

0 comments on commit 43fc5bb

Please sign in to comment.