Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: all runtime func args and kwargs #1636

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions kclvm/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ anyhow = "1"
blake3 = "1.5.4"
encoding = "0.2.33"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
hostname = "0.4.0"
dns-lookup = "2.0.4"

[[bin]]
name = "gen-api-spec"
path = "scripts/gen-api-spec.rs"
27 changes: 15 additions & 12 deletions kclvm/runtime/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub extern "C" fn kclvm_json_encode(
let ctx = mut_ptr_as_ref(ctx);
let kwargs = ptr_as_ref(kwargs);

if let Some(arg0) = args.arg_i(0) {
if let Some(arg0) = get_call_arg(args, kwargs, 0, Some("data")) {
let s = ValueRef::str(
arg0.to_json_string_with_options(&kwargs_to_opts(kwargs))
arg0.to_json_string_with_options(&args_to_opts(args, kwargs, 1))
.as_ref(),
);
return s.into_raw(ctx);
Expand All @@ -30,12 +30,13 @@ pub extern "C" fn kclvm_json_encode(
pub extern "C" fn kclvm_json_decode(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *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(arg0) = args.arg_i(0) {
if let Some(arg0) = get_call_arg(args, kwargs, 0, Some("value")) {
match ValueRef::from_json(ctx, arg0.as_str().as_ref()) {
Ok(x) => return x.into_raw(ctx),
Err(err) => panic!("{}", err),
Expand All @@ -49,12 +50,13 @@ pub extern "C" fn kclvm_json_decode(
pub extern "C" fn kclvm_json_validate(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *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(arg0) = args.arg_i(0) {
if let Some(arg0) = get_call_arg(args, kwargs, 0, Some("value")) {
match ValueRef::from_json(ctx, arg0.as_str().as_ref()) {
Ok(_) => return kclvm_value_True(ctx),
Err(_) => return kclvm_value_False(ctx),
Expand All @@ -77,7 +79,7 @@ pub extern "C" fn kclvm_json_dump_to_file(
match (data, filename) {
(Some(data), Some(filename)) => {
let filename = filename.as_str();
let json = data.to_json_string_with_options(&kwargs_to_opts(kwargs));
let json = data.to_json_string_with_options(&args_to_opts(args, kwargs, 2));
std::fs::write(&filename, json)
.unwrap_or_else(|e| panic!("Unable to write file '{}': {}", filename, e));
kclvm_value_Undefined(ctx)
Expand All @@ -88,18 +90,19 @@ pub extern "C" fn kclvm_json_dump_to_file(
}
}

fn kwargs_to_opts(kwargs: &ValueRef) -> JsonEncodeOptions {
fn args_to_opts(args: &ValueRef, kwargs: &ValueRef, index: usize) -> JsonEncodeOptions {
let mut opts = JsonEncodeOptions::default();
if let Some(sort_keys) = kwargs.kwarg_bool("sort_keys", None) {
if let Some(sort_keys) = get_call_arg_bool(args, kwargs, index, Some("sort_keys")) {
opts.sort_keys = sort_keys;
}
if let Some(indent) = kwargs.kwarg_int("indent", None) {
if let Some(indent) = get_call_arg_int(args, kwargs, index + 1, Some("indent")) {
opts.indent = indent;
}
if let Some(ignore_private) = kwargs.kwarg_bool("ignore_private", None) {
if let Some(ignore_private) = get_call_arg_bool(args, kwargs, index + 2, Some("ignore_private"))
{
opts.ignore_private = ignore_private;
}
if let Some(ignore_none) = kwargs.kwarg_bool("ignore_none", None) {
if let Some(ignore_none) = get_call_arg_bool(args, kwargs, index + 3, Some("ignore_none")) {
opts.ignore_none = ignore_none;
}
opts
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub extern "C" fn kclvm_manifests_yaml_stream(
None => YamlEncodeOptions::default(),
};

if let Some(value) = args.arg_i(0) {
if let Some(value) = get_call_arg(args, kwargs, 0, Some("values")) {
self::yaml::encode_yaml_stream_to_manifests(ctx, &value, opts);
} else {
panic!("yaml_stream() missing 1 required positional argument: 'values'");
Expand Down
Loading
Loading