Skip to content

Commit ff38aab

Browse files
committed
feat: add run function to MoonBitVM and update tests to use it
1 parent 19b458a commit ff38aab

16 files changed

Lines changed: 46 additions & 51 deletions

benchmark_test.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test "benchmark_variable_access" (b : @bench.T) {
1616
let vm = MoonBitVM::new(log=false)
1717

1818
// 设置变量
19-
vm.eval("let x = 42; let y = 100; let z = x + y") |> ignore
19+
vm.run("let x = 42; let y = 100; let z = x + y")
2020
b.bench(fn() { b.keep(vm.eval("x + y + z")) })
2121
}
2222

moon.pkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ options(
1010
"exports": [
1111
"create",
1212
"eval",
13+
"run",
1314
"eval_result_to_string",
1415
"code_to_ast",
1516
"add_extern_fn",

pkg.generated.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub(all) struct MoonBitVM {
3939
pub fn MoonBitVM::create(log? : Bool) -> Self
4040
pub fn MoonBitVM::eval(Self, String, log? : Bool) -> EvalResult
4141
pub fn MoonBitVM::new(log? : Bool) -> Self
42+
pub fn MoonBitVM::run(Self, String, log? : Bool) -> Unit
4243

4344
// Type aliases
4445

test/arithmetic.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test "arithmetic_operators" {
1515
///|
1616
test "math" {
1717
let vm = MoonBitVM::new()
18-
ignore(vm.eval("import { \"moonbitlang/core/math\" }"))
18+
vm.run("import { \"moonbitlang/core/math\" }")
1919
inspect(vm.eval("@math.PI"), content="3.141592653589793")
2020
}
2121

test/async.mbt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
///|
66
test "async_run_executes_function_immediately" {
77
let vm = MoonBitVM::new()
8-
ignore(vm.eval("fn run_async(f : async () -> Unit noraise) = \"%async.run\""))
8+
vm.run("fn run_async(f : async () -> Unit noraise) = \"%async.run\"")
99
inspect(vm.eval("run_async(() => 7)"), content="7")
1010
}
1111

1212
///|
1313
test "async_suspend_success" {
1414
let vm = MoonBitVM::new()
15-
ignore(
16-
vm.eval(
17-
(
18-
#|async fn[T, E : Error] async_suspend(
19-
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
20-
#|) -> T raise E = "%async.suspend"
21-
),
15+
vm.run(
16+
(
17+
#|async fn[T, E : Error] async_suspend(
18+
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
19+
#|) -> T raise E = "%async.suspend"
2220
),
2321
)
2422
inspect(vm.eval("async_suspend((ok, _err) => ok(42))"), content="42")
@@ -27,13 +25,11 @@ test "async_suspend_success" {
2725
///|
2826
test "async_suspend_error_branch" {
2927
let vm = MoonBitVM::new()
30-
ignore(
31-
vm.eval(
32-
(
33-
#|async fn[T, E : Error] async_suspend(
34-
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
35-
#|) -> T raise E = "%async.suspend"
36-
),
28+
vm.run(
29+
(
30+
#|async fn[T, E : Error] async_suspend(
31+
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
32+
#|) -> T raise E = "%async.suspend"
3733
),
3834
)
3935
inspect(
@@ -54,13 +50,11 @@ test "async_suspend_error_branch" {
5450
///|
5551
test "async_suspend_callback_not_resolved" {
5652
let vm = MoonBitVM::new()
57-
ignore(
58-
vm.eval(
59-
(
60-
#|async fn[T, E : Error] async_suspend(
61-
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
62-
#|) -> T raise E = "%async.suspend"
63-
),
53+
vm.run(
54+
(
55+
#|async fn[T, E : Error] async_suspend(
56+
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
57+
#|) -> T raise E = "%async.suspend"
6458
),
6559
)
6660
assert_eq(
@@ -78,13 +72,11 @@ test "async_suspend_callback_not_resolved" {
7872
///|
7973
test "async_suspend_first_callback_wins" {
8074
let vm = MoonBitVM::new()
81-
ignore(
82-
vm.eval(
83-
(
84-
#|async fn[T, E : Error] async_suspend(
85-
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
86-
#|) -> T raise E = "%async.suspend"
87-
),
75+
vm.run(
76+
(
77+
#|async fn[T, E : Error] async_suspend(
78+
#| cb : ((T) -> Unit, (E) -> Unit) -> Unit,
79+
#|) -> T raise E = "%async.suspend"
8880
),
8981
)
9082
inspect(

test/attributes.mbt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
///|
22
test "alias" {
33
let vm = MoonBitVM::new()
4-
vm.eval(
4+
vm.run(
55
(
66
#|#alias(number_one)
77
#|fn one() -> Int {
88
#| 1
99
#|}
1010
),
1111
)
12-
|> ignore
1312
inspect(vm.eval("one()"), content="1")
1413
inspect(vm.eval("number_one()"), content="1")
1514
}

test/constructors.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
///|
77
test "constructor_single_argument_any_match" {
88
let vm = MoonBitVM::new()
9-
ignore(vm.eval("import { \"moonbitlang/core/list\" }"))
9+
vm.run("import { \"moonbitlang/core/list\" }")
1010
assert_eq(
1111
vm.eval("let l = @list.from_array([1, 2]); l").to_string(),
1212
"More(1, tail=More(2, tail=Empty))",

test/control_flow.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ test "raise_expressions" {
215215
///|
216216
test "loop_expressions" {
217217
let vm = MoonBitVM::new()
218-
ignore(vm.eval("import { \"moonbitlang/core/list\" }"))
218+
vm.run("import { \"moonbitlang/core/list\" }")
219219
// Test loop expressions
220220
inspect(
221221
vm.eval(

test/json.mbt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test "json_value" {
1111
///|
1212
test "json_array" {
1313
let vm = MoonBitVM::new()
14-
vm.eval("let json_array : Json = [1, 2, 3]") |> ignore
14+
vm.run("let json_array : Json = [1, 2, 3]")
1515
inspect(vm.eval("json_array"), content="[1,2,3]")
1616
assert_eq(
1717
vm.eval("typeof(json_array)").to_string(),
@@ -22,8 +22,7 @@ test "json_array" {
2222
///|
2323
test "json_type" {
2424
let vm = MoonBitVM::new()
25-
vm.eval("let json : Json = { \"a\": [\"1\", \"2\"], \"b\": [\"3\", \"4\"] }")
26-
|> ignore
25+
vm.run("let json : Json = { \"a\": [\"1\", \"2\"], \"b\": [\"3\", \"4\"] }")
2726
assert_eq(
2827
vm.eval("typeof(json)").to_string(),
2928
"@moonbitlang/core/builtin.Json",

test/list.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///|
22
test "list_methods" {
33
let vm = MoonBitVM::new()
4-
ignore(vm.eval("import { \"moonbitlang/core/list\" }"))
4+
vm.run("import { \"moonbitlang/core/list\" }")
55
assert_eq(
66
vm.eval("let list = @list.from_array([1, 2, 3]); list").to_string(),
77
"More(1, tail=More(2, tail=More(3, tail=Empty)))",

0 commit comments

Comments
 (0)