Skip to content

Commit

Permalink
fix: runtime singleton values and add evaluator multithread exec tests (
Browse files Browse the repository at this point in the history
#1458)

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Jun 29, 2024
1 parent 4907018 commit 8d766a1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: evaluator/src/tests.rs
expression: "format!(\"{}\", evaluator.run().unwrap().1)"
---
v: null
x: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: evaluator/src/tests.rs
expression: "format!(\"{}\", evaluator.run().unwrap().1)"
---
x: 2
62 changes: 62 additions & 0 deletions kclvm/evaluator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ evaluator_snapshot! {import_stmt_1, r#"import math
import math
b = 2
"#}
evaluator_snapshot! {import_stmt_2, r#"
import regex
v = option("foo")
x = regex.match("foo", "^\\w+$")
"#}
evaluator_snapshot! {import_stmt_3, r#"import math
x = math.log(10)
"#}

evaluator_snapshot! {quant_expr_0, r#"b = all a in [1, 2, 3] {
a > 0
Expand Down Expand Up @@ -368,3 +378,55 @@ fn test_if_stmt_setters() {
let var_setters = scopes.get(MAIN_PKG).unwrap().setters.get("_a").unwrap();
assert_eq!(var_setters.len(), 3);
}

use std::sync::Arc;
use std::thread;

const MULTI_THREAD_SOURCE: &str = r#"
import regex
foo = option("foo")
bar = option("bar")
x = regex.match("", "")
"#;

#[test]
fn test_multithread_exec() {
let threads = 10;
multithread_check(threads, |thread| {
println!("run: {}", thread);
for _ in 0..1000 {
run_code(MULTI_THREAD_SOURCE);
}
println!("done: {}", thread);
});
}

fn multithread_check(threads: i32, check: impl Fn(i32) + Send + Sync + 'static) {
let check_shared = Arc::new(check);
let mut handles = vec![];
for thread in 0..threads {
let check_shared = Arc::clone(&check_shared);
let handle = thread::spawn(move || {
check_shared(thread);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}

fn run_code(source: &str) -> (String, String) {
let p = load_packages(&LoadPackageOptions {
paths: vec!["test.k".to_string()],
load_opts: Some(LoadProgramOptions {
k_code_list: vec![source.to_string()],
..Default::default()
}),
load_builtin: false,
..Default::default()
})
.unwrap();
let evaluator = Evaluator::new(&p.program);
evaluator.run().unwrap()
}
46 changes: 4 additions & 42 deletions kclvm/runtime/src/value/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ pub unsafe extern "C" fn kclvm_context_set_import_names(
// values: new
// ----------------------------------------------------------------------------

// singleton

#[allow(non_camel_case_types, non_upper_case_globals)]
static mut kclvm_value_Undefined_obj: usize = 0;

#[allow(non_camel_case_types, non_upper_case_globals)]
static mut kclvm_value_None_obj: usize = 0;

#[allow(non_camel_case_types, non_upper_case_globals)]
static mut kclvm_value_Bool_true_obj: usize = 0;

#[allow(non_camel_case_types, non_upper_case_globals)]
static mut kclvm_value_Bool_false_obj: usize = 0;

// Undefine/None

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_value_Undefined(ctx: *mut kclvm_context_t) -> *mut kclvm_value_ref_t {
Expand Down Expand Up @@ -120,18 +104,10 @@ pub extern "C" fn kclvm_value_Bool(
v: kclvm_bool_t,
) -> *mut kclvm_value_ref_t {
let ctx = mut_ptr_as_ref(ctx);
unsafe {
if v != 0 {
if kclvm_value_Bool_true_obj == 0 {
kclvm_value_Bool_true_obj = new_mut_ptr(ctx, ValueRef::bool(true)) as usize;
}
kclvm_value_Bool_true_obj as *mut kclvm_value_ref_t
} else {
if kclvm_value_Bool_false_obj == 0 {
kclvm_value_Bool_false_obj = new_mut_ptr(ctx, ValueRef::bool(false)) as usize;
}
kclvm_value_Bool_false_obj as *mut kclvm_value_ref_t
}
if v != 0 {
ValueRef::bool(true).into_raw(ctx)
} else {
ValueRef::bool(false).into_raw(ctx)
}
}

Expand Down Expand Up @@ -676,20 +652,6 @@ pub unsafe extern "C" fn kclvm_value_delete(p: *mut kclvm_value_ref_t) {
if p.is_null() {
return;
}
unsafe {
if p as usize == kclvm_value_Undefined_obj {
return;
}
if p as usize == kclvm_value_None_obj {
return;
}
if p as usize == kclvm_value_Bool_true_obj {
return;
}
if p as usize == kclvm_value_Bool_false_obj {
return;
}
}
let val = ptr_as_ref(p);
val.from_raw();
free_mut_ptr(p);
Expand Down

0 comments on commit 8d766a1

Please sign in to comment.