Skip to content

Commit

Permalink
feat: support @sys.set_env_var(k, v)
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Dec 9, 2024
1 parent cc337c2 commit ad9422e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ on:
- main
pull_request:

env:
# just for testing env var
MOON_TEST: "yes"

jobs:
build:
strategy:
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moonbitlang/x",
"version": "0.4.13",
"version": "0.4.14",
"readme": "README.md",
"repository": "https://github.com/moonbitlang/x",
"license": "Apache-2.0",
Expand Down
2 changes: 2 additions & 0 deletions sys/internal/ffi/ffi.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ fn get_cli_args() -> Array[String]

fn get_env_vars() -> Map[String, String]

fn set_env_var(String, String) -> Unit

// Types and methods

// Type aliases
Expand Down
11 changes: 11 additions & 0 deletions sys/internal/ffi/sys_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ extern "js" fn get_env_vars_internal() -> Array[String] =
#| return result;
#| }

///|
pub fn set_env_var(key : String, value : String) -> Unit {
set_env_var_internal(key, value)
}

///|
extern "js" fn set_env_var_internal(key : String, value : String) -> Unit =
#| function(key, value) {
#| process.env[key] = value;
#| }

///|
pub extern "js" fn exit(code : Int) -> Unit =
#| function(code) {
Expand Down
6 changes: 6 additions & 0 deletions sys/internal/ffi/sys_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ pub fn get_env_vars() -> Map[String, String] {
panic()
}

///|
pub fn set_env_var(_key : String, _value : String) -> Unit {
// not implement yet
panic()
}

///|
pub extern "native" fn exit(code : Int) -> Unit = "exit"
15 changes: 14 additions & 1 deletion sys/internal/ffi/sys_wasm.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
///|
typealias XExternStringArray = @ffi.XExternStringArray

///|
typealias XExternString = @ffi.XExternString

///|
pub fn get_cli_args() -> Array[String] {
let args = get_cli_args_ffi()
Expand All @@ -36,7 +39,17 @@ pub fn get_env_vars() -> Map[String, String] {
}

///|
fn get_env_vars_ffi() -> XExternStringArray = "__moonbit_fs_unstable" "env_get_vars"
fn get_env_vars_ffi() -> XExternStringArray = "__moonbit_fs_unstable" "get_env_vars"

///|
pub fn set_env_var(key : String, value : String) -> Unit {
let key = @ffi.string_to_extern(key)
let value = @ffi.string_to_extern(value)
set_env_var_ffi(key, value)
}

///|
fn set_env_var_ffi(key : XExternString, value : XExternString) = "__moonbit_fs_unstable" "set_env_var"

///|
pub fn exit(code : Int) = "__moonbit_sys_unstable" "exit"
5 changes: 5 additions & 0 deletions sys/sys.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub fn get_env_vars() -> Map[String, String] {
@ffi.get_env_vars()
}

///|
pub fn set_env_var(key : String, value : String) -> Unit {
@ffi.set_env_var(key, value)
}

///|
pub fn exit(code : Int) -> Unit {
@ffi.exit(code)
Expand Down
2 changes: 2 additions & 0 deletions sys/sys.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ fn get_cli_args() -> Array[String]

fn get_env_vars() -> Map[String, String]

fn set_env_var(String, String) -> Unit

// Types and methods

// Type aliases
Expand Down
13 changes: 11 additions & 2 deletions sys/sys_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@
// limitations under the License.

test "get_env_vars" {
let res = @sys.get_env_vars()["MOON_TEST"]
let (k, v) = ("THIS_IS_A_TEST_FOR_ENV_VAR", "IT_WORKS")
let old_env = @sys.get_env_vars()[k]
inspect!(
old_env,
content=
#|None
,
)
@sys.set_env_var(k, v)
let res = @sys.get_env_vars()[k]
inspect!(
res,
content=
#|Some("yes")
#|Some("IT_WORKS")
,
)
}

0 comments on commit ad9422e

Please sign in to comment.