Skip to content

Commit

Permalink
feat: support get cli args & env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Nov 12, 2024
1 parent a966a58 commit 5420b79
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
moonrun --version
- name: moon test
env:
# just for testing env var
MOON_TEST: "yes"
run: |
moon test --target all --serial --release
moon test --target all --serial
Expand Down
3 changes: 2 additions & 1 deletion fs/internal/ffi/fs_wasm.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
// limitations under the License.

typealias XExternString = @ffi.XExternString

typealias XExternByteArray = @ffi.XExternByteArray
typealias XExternStringArray = @ffi.XExternStringArray

typealias XExternStringArray = @ffi.XExternStringArray

pub fn read_file_to_string(path : String) -> String {
let path = @ffi.string_to_extern(path)
Expand Down
13 changes: 13 additions & 0 deletions sys/internal/ffi/ffi.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package moonbitlang/x/sys/internal/ffi

// Values
fn get_cli_args() -> Array[String]

fn get_env_var(String) -> String

// Types and methods

// Type aliases

// Traits

10 changes: 10 additions & 0 deletions sys/internal/ffi/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"import": [
"moonbitlang/x/internal/ffi"
],
"targets": {
"sys_wasm.mbt": ["wasm", "wasm-gc"],
"sys_js.mbt": ["js"],
"sys_native.mbt": ["native"]
}
}
31 changes: 31 additions & 0 deletions sys/internal/ffi/sys_js.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed 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.

pub fn get_cli_args() -> Array[String] {
get_cli_args_internal()
}

extern "js" fn get_cli_args_internal() -> Array[String] =
#| function() {
#| return process.argv.slice(2);
#| }

pub fn get_env_var(s : String) -> String {
get_env_var_internal(s)
}

extern "js" fn get_env_var_internal(s : String) -> String =
#| function(s) {
#| return process.env[s];
#| }
23 changes: 23 additions & 0 deletions sys/internal/ffi/sys_native.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed 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.

pub fn get_cli_args() -> Array[String] {
// not implement yet
panic()
}

pub fn get_env_var(_s : String) -> String {
// not implement yet
panic()
}
32 changes: 32 additions & 0 deletions sys/internal/ffi/sys_wasm.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed 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.

typealias XExternString = @ffi.XExternString

typealias XExternStringArray = @ffi.XExternStringArray

pub fn get_cli_args() -> Array[String] {
let args = get_cli_args_ffi()
@ffi.string_array_from_extern(args)
}

fn get_cli_args_ffi() -> XExternStringArray = "__moonbit_fs_unstable" "args_get"

pub fn get_env_var(s : String) -> String {
let s = @ffi.string_to_extern(s)
let res = get_env_var_ffi(s)
@ffi.string_from_extern(res)
}

fn get_env_var_ffi(s : XExternString) -> XExternString = "__moonbit_fs_unstable" "env_get_var"
8 changes: 8 additions & 0 deletions sys/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"import": [
"moonbitlang/x/sys/internal/ffi"
],
"targets": {
"sys_test.mbt": ["not", "native"]
}
}
21 changes: 21 additions & 0 deletions sys/sys.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed 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.

pub fn get_cli_args() -> Array[String] {
@ffi.get_cli_args()
}

pub fn get_env_var(s : String) -> String {
@ffi.get_env_var(s)
}
13 changes: 13 additions & 0 deletions sys/sys.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package moonbitlang/x/sys

// Values
fn get_cli_args() -> Array[String]

fn get_env_var(String) -> String

// Types and methods

// Type aliases

// Traits

18 changes: 18 additions & 0 deletions sys/sys_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed 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.

test "get_env_var" {
let res = @sys.get_env_var("MOON_TEST")
inspect!(res, content="yes")
}

0 comments on commit 5420b79

Please sign in to comment.