diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 248b177..9387ad4 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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 diff --git a/fs/internal/ffi/fs_wasm.mbt b/fs/internal/ffi/fs_wasm.mbt index bb035f5..4acb26e 100644 --- a/fs/internal/ffi/fs_wasm.mbt +++ b/fs/internal/ffi/fs_wasm.mbt @@ -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) diff --git a/sys/internal/ffi/ffi.mbti b/sys/internal/ffi/ffi.mbti new file mode 100644 index 0000000..7263e3b --- /dev/null +++ b/sys/internal/ffi/ffi.mbti @@ -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 + diff --git a/sys/internal/ffi/moon.pkg.json b/sys/internal/ffi/moon.pkg.json new file mode 100644 index 0000000..70abf47 --- /dev/null +++ b/sys/internal/ffi/moon.pkg.json @@ -0,0 +1,10 @@ +{ + "import": [ + "moonbitlang/x/internal/ffi" + ], + "targets": { + "sys_wasm.mbt": ["wasm", "wasm-gc"], + "sys_js.mbt": ["js"], + "sys_native.mbt": ["native"] + } +} \ No newline at end of file diff --git a/sys/internal/ffi/sys_js.mbt b/sys/internal/ffi/sys_js.mbt new file mode 100644 index 0000000..5f29be3 --- /dev/null +++ b/sys/internal/ffi/sys_js.mbt @@ -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]; + #| } diff --git a/sys/internal/ffi/sys_native.mbt b/sys/internal/ffi/sys_native.mbt new file mode 100644 index 0000000..d9fe2d6 --- /dev/null +++ b/sys/internal/ffi/sys_native.mbt @@ -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() +} diff --git a/sys/internal/ffi/sys_wasm.mbt b/sys/internal/ffi/sys_wasm.mbt new file mode 100644 index 0000000..0fe76b8 --- /dev/null +++ b/sys/internal/ffi/sys_wasm.mbt @@ -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" diff --git a/sys/moon.pkg.json b/sys/moon.pkg.json new file mode 100644 index 0000000..6fa5364 --- /dev/null +++ b/sys/moon.pkg.json @@ -0,0 +1,8 @@ +{ + "import": [ + "moonbitlang/x/sys/internal/ffi" + ], + "targets": { + "sys_test.mbt": ["not", "native"] + } +} diff --git a/sys/sys.mbt b/sys/sys.mbt new file mode 100644 index 0000000..d0e97f9 --- /dev/null +++ b/sys/sys.mbt @@ -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) +} diff --git a/sys/sys.mbti b/sys/sys.mbti new file mode 100644 index 0000000..b4e817d --- /dev/null +++ b/sys/sys.mbti @@ -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 + diff --git a/sys/sys_test.mbt b/sys/sys_test.mbt new file mode 100644 index 0000000..7ebf66c --- /dev/null +++ b/sys/sys_test.mbt @@ -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") +}