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: remove runtime unsound codes #1012

Merged
merged 1 commit into from
Jan 29, 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
2 changes: 1 addition & 1 deletion kclvm/runtime/src/_kcl_run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.
#![allow(clippy::missing_safety_doc)]

use std::os::raw::c_char;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

// Auto generated, DONOT EDIT!!!

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

// Auto generated, DONOT EDIT!!!

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

// Auto generated by <make gen-api-spec> command, DONOT EDIT!!!

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/_kclvm_main_win.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

extern void* kclvm_main(void* ctx);

Expand Down
54 changes: 0 additions & 54 deletions kclvm/runtime/src/api/err_type.rs

This file was deleted.

16 changes: 16 additions & 0 deletions kclvm/runtime/src/api/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Copyright The KCL Authors. All rights reserved.

#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
pub enum RuntimeErrorType {
EvaluationError = 1,
RecursiveLoad = 2,
FloatOverflow = 3,
FloatUnderflow = 4,
IntOverflow = 5,
TypeError = 6,
AssertionError = 7,
Deprecated = 8,
DeprecatedWarning = 9,
SchemaCheckFailure = 10,
}
20 changes: 5 additions & 15 deletions kclvm/runtime/src/api/kclvm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

#[allow(non_camel_case_types)]
type kclvm_value_ref_t = crate::ValueRef;
Expand Down Expand Up @@ -37,8 +37,9 @@ pub struct KclError {
}

#[allow(non_camel_case_types)]
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Default)]
pub enum Type {
#[default]
any_type,
bool_type,
bool_lit_type(bool),
Expand All @@ -55,12 +56,6 @@ pub enum Type {
func_type(FuncType),
}

impl Default for Type {
fn default() -> Self {
Type::any_type
}
}

#[derive(PartialEq, Clone, Default, Debug)]
pub struct ListType {
pub elem_type: Box<Type>,
Expand Down Expand Up @@ -461,19 +456,14 @@ pub enum Kind {
Func = 18,
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
#[derive(Clone, PartialEq, Eq, Debug, Hash, Default)]
pub enum ConfigEntryOperationKind {
#[default]
Union = 0,
Override = 1,
Insert = 2,
}

impl Default for ConfigEntryOperationKind {
fn default() -> Self {
ConfigEntryOperationKind::Union
}
}

impl ConfigEntryOperationKind {
pub fn from_i32(v: i32) -> Self {
match v {
Expand Down
6 changes: 3 additions & 3 deletions kclvm/runtime/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

pub mod kclvm;
pub use self::kclvm::*;

pub mod utils;
pub use self::utils::*;

pub mod err_type;
pub use self::err_type::*;
pub mod error;
pub use self::error::*;
9 changes: 7 additions & 2 deletions kclvm/runtime/src/api/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2021 The KCL Authors. All rights reserved.
#![allow(clippy::missing_safety_doc)]
//! Copyright The KCL Authors. All rights reserved.

use std::os::raw::c_char;

use crate::{Context, ValueRef};

/// New a mutable raw pointer.
/// Safety: The caller must ensure that `ctx` lives longer than the returned pointer
/// and that the pointer is properly deallocated by calling `free_mut_ptr`.
pub fn new_mut_ptr(ctx: &mut Context, x: ValueRef) -> *mut ValueRef {
let ptr = Box::into_raw(Box::new(x));
// Store the object pointer address to
Expand All @@ -15,6 +16,7 @@ pub fn new_mut_ptr(ctx: &mut Context, x: ValueRef) -> *mut ValueRef {
}

/// Free a mutable raw pointer.
/// Safety: The caller must ensure `p` is a valid pointer obtained from `new_mut_ptr`.
pub(crate) fn free_mut_ptr<T>(p: *mut T) {
if !p.is_null() {
unsafe {
Expand All @@ -24,19 +26,22 @@ pub(crate) fn free_mut_ptr<T>(p: *mut T) {
}

/// Convert a const raw pointer to a immutable borrow.
/// Safety: The caller must ensure that `p` is valid for the lifetime `'a`.
pub(crate) fn ptr_as_ref<'a, T>(p: *const T) -> &'a T {
assert!(!p.is_null());
unsafe { &*p }
}

/// Convert a mutable raw pointer to a mutable borrow.
/// Safety: The caller must ensure that `p` is valid for the lifetime `'a`.
pub(crate) fn mut_ptr_as_ref<'a, T>(p: *mut T) -> &'a mut T {
assert!(!p.is_null());

unsafe { &mut *p }
}

/// Convert a C str pointer to a Rust &str.
/// Safety: The caller must ensure that `s` is a valid null-terminated C string.
pub(crate) fn c2str<'a>(s: *const c_char) -> &'a str {
let s = unsafe { std::ffi::CStr::from_ptr(s) }.to_str().unwrap();
s
Expand Down
6 changes: 3 additions & 3 deletions kclvm/runtime/src/base64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

extern crate base64;
use base64::{decode, encode};
Expand All @@ -21,7 +21,7 @@ pub extern "C" fn kclvm_base64_encode(
return ValueRef::str(s.as_str()).into_raw(ctx);
}
_ => {
ctx.set_err_type(&ErrType::TypeError_Runtime_TYPE);
ctx.set_err_type(&RuntimeErrorType::TypeError);

panic!("a bytes-like object is required, not '{}'", p.as_str());
}
Expand All @@ -44,7 +44,7 @@ pub extern "C" fn kclvm_base64_decode(
return ValueRef::str(std::str::from_utf8(&de_str).unwrap()).into_raw(ctx);
}
_ => {
ctx.set_err_type(&ErrType::TypeError_Runtime_TYPE);
ctx.set_err_type(&RuntimeErrorType::TypeError);

panic!(
"argument should be a bytes-like object or ASCII string, not '{}'",
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

use crate::*;

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/context/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.
#![allow(clippy::missing_safety_doc)]

use crate::*;
Expand Down
6 changes: 3 additions & 3 deletions kclvm/runtime/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

pub mod api;
pub use api::*;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl PanicInfo {
Err(_) => PanicInfo {
__kcl_PanicInfo__: true,
message: s.to_string(),
err_type_code: crate::ErrType::CompileError_TYPE as i32,
err_type_code: crate::RuntimeErrorType::EvaluationError as i32,
..Default::default()
},
}
Expand Down Expand Up @@ -162,7 +162,7 @@ impl crate::Context {
}
}

pub fn set_err_type(&mut self, err_type: &crate::ErrType) {
pub fn set_err_type(&mut self, err_type: &crate::RuntimeErrorType) {
self.panic_info.__kcl_PanicInfo__ = true;
self.panic_info.err_type_code = *err_type as i32;
}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

extern crate md5;
extern crate sha1;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/datetime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

extern crate chrono;

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/json/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

use crate::*;

Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

use kclvm_runtime_internal_macros::runtime_fn;

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 @@ -51,7 +51,7 @@ type kclvm_value_ref_t = ValueRef;
/// TODO: more options on the function `yaml_stream`.
#[no_mangle]
#[runtime_fn]
pub unsafe extern "C" fn kclvm_manifests_yaml_stream(
pub extern "C" fn kclvm_manifests_yaml_stream(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
Expand Down
16 changes: 4 additions & 12 deletions kclvm/runtime/src/manifests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ fn test_kclvm_manifests_yaml_stream() {
ConfigEntryOperationKind::Override,
-1,
);
unsafe {
kclvm_manifests_yaml_stream(&mut ctx, &args, &kwargs);
}
kclvm_manifests_yaml_stream(&mut ctx, &args, &kwargs);
assert_eq!(
Some(yaml_str.to_string()),
ctx.buffer.custom_manifests_output
Expand All @@ -97,9 +95,7 @@ fn test_kclvm_manifests_yaml_stream_invalid() {
let mut ctx = Context::new();
let args = ValueRef::list(None).into_raw(&mut ctx);
let kwargs = ValueRef::dict(None).into_raw(&mut ctx);
unsafe {
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
}
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
},
);
assert_panic(
Expand All @@ -109,9 +105,7 @@ fn test_kclvm_manifests_yaml_stream_invalid() {
let args = ValueRef::list(None).into_raw(&mut ctx);
let kwargs = ValueRef::dict(Some(&[("opts", &ValueRef::str("invalid_kwarg"))]))
.into_raw(&mut ctx);
unsafe {
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
}
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
},
);
assert_panic(
Expand All @@ -120,9 +114,7 @@ fn test_kclvm_manifests_yaml_stream_invalid() {
let mut ctx = Context::new();
let args = ValueRef::list(None).into_raw(&mut ctx);
let kwargs = ValueRef::dict(Some(&[("opts", &ValueRef::none())])).into_raw(&mut ctx);
unsafe {
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
}
kclvm_manifests_yaml_stream(ctx.into_raw(), args, kwargs);
},
);
std::panic::set_hook(prev_hook);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

extern crate num_integer;

Expand Down
17 changes: 7 additions & 10 deletions kclvm/runtime/src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
Expand Down Expand Up @@ -171,17 +171,14 @@ pub extern "C" fn kclvm_net_is_IP(
let args = ptr_as_ref(args);

if let Some(ip) = args.arg_i_str(0, None) {
if let Ok(..) = Ipv4Addr::from_str(ip.as_ref()) {
return kclvm_value_True(ctx);
}
if let Ok(..) = Ipv6Addr::from_str(ip.as_ref()) {
return kclvm_value_True(ctx);
if Ipv4Addr::from_str(ip.as_ref()).is_ok() || Ipv6Addr::from_str(ip.as_ref()).is_ok() {
kclvm_value_True(ctx)
} else {
kclvm_value_False(ctx)
}

return kclvm_value_False(ctx);
} else {
panic!("is_IP() missing 1 required positional argument: 'ip'");
}

panic!("is_IP() missing 1 required positional argument: 'ip'");
}

// is_loopback_IP(ip: str) -> bool
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/regex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
//! Copyright The KCL Authors. All rights reserved.

extern crate fancy_regex;

Expand Down
Loading
Loading