Skip to content

Commit

Permalink
feat: impl read_env func
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed May 16, 2024
1 parent 9e3fc86 commit 8e5e592
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 65 deletions.
6 changes: 3 additions & 3 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ impl KclvmServiceImpl {
/// assert_eq!(result.type_errors.len(), 0);
/// assert_eq!(result.symbols.len(), 12);
/// assert_eq!(result.scopes.len(), 3);
/// assert_eq!(result.node_symbol_map.len(), 176);
/// assert_eq!(result.symbol_node_map.len(), 176);
/// assert_eq!(result.fully_qualified_name_map.len(), 185);
/// assert_eq!(result.node_symbol_map.len(), 177);
/// assert_eq!(result.symbol_node_map.len(), 177);
/// assert_eq!(result.fully_qualified_name_map.len(), 186);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -291,6 +284,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down
48 changes: 24 additions & 24 deletions kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -180,13 +180,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -383,6 +376,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -415,20 +415,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 165,
index: 166,
generation: 0,
},
kind: Value,
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -625,6 +618,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -664,13 +664,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -867,6 +860,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -899,7 +899,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 166,
index: 167,
generation: 0,
},
kind: Value,
Expand Down
52 changes: 26 additions & 26 deletions kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -240,7 +240,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 165,
index: 166,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -337,7 +337,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -382,13 +382,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -585,6 +578,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -617,20 +617,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 166,
index: 167,
generation: 0,
},
kind: Value,
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -827,6 +820,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -866,13 +866,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 135,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 136,
Expand Down Expand Up @@ -1069,6 +1062,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 164,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -1101,7 +1101,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 167,
index: 168,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 164,
index: 165,
generation: 0,
},
kind: Value,
Expand Down
Binary file modified kclvm/runtime/src/_kclvm.bc
Binary file not shown.
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ kclvm_value_ref_t* kclvm_file_mv(kclvm_context_t* ctx, kclvm_value_ref_t* args,

kclvm_value_ref_t* kclvm_file_read(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_file_read_env(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_file_size(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_file_workdir(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ declare %kclvm_value_ref_t* @kclvm_file_mv(%kclvm_context_t* %ctx, %kclvm_value_

declare %kclvm_value_ref_t* @kclvm_file_read(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_file_read_env(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_file_size(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_file_workdir(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub enum ApiFunc {
kclvm_file_modpath,
kclvm_file_mv,
kclvm_file_read,
kclvm_file_read_env,
kclvm_file_size,
kclvm_file_workdir,
kclvm_file_write,
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_file_modpath" => crate::kclvm_file_modpath as *const () as u64,
"kclvm_file_mv" => crate::kclvm_file_mv as *const () as u64,
"kclvm_file_read" => crate::kclvm_file_read as *const () as u64,
"kclvm_file_read_env" => crate::kclvm_file_read_env as *const () as u64,
"kclvm_file_size" => crate::kclvm_file_size as *const () as u64,
"kclvm_file_workdir" => crate::kclvm_file_workdir as *const () as u64,
"kclvm_file_write" => crate::kclvm_file_write as *const () as u64,
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_file_append(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_append(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

// api-spec: kclvm_file_read_env
// api-spec(c): kclvm_value_ref_t* kclvm_file_read_env(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_read_env(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

// api-spec: kclvm_template_execute
// api-spec(c): kclvm_value_ref_t* kclvm_template_execute(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_template_execute(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
21 changes: 21 additions & 0 deletions kclvm/runtime/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,24 @@ pub extern "C" fn kclvm_file_append(
panic!("append() requires 'filepath' argument");
}
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_read_env(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);

if let Some(key) = get_call_arg_str(args, kwargs, 0, Some("key")) {
match std::env::var(key) {
Ok(v) => ValueRef::str(&v).into_raw(ctx),
Err(_) => ValueRef::undefined().into_raw(ctx),
}
} else {
panic!("read_env() requires 'key' argument");
}
}
Loading

0 comments on commit 8e5e592

Please sign in to comment.