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

Kybra 12 9 2022 #1

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod resource {

#[pyfunction]
fn getrlimit(resource: i32, vm: &VirtualMachine) -> PyResult<Limits> {
if resource < 0 || resource >= RLIM_NLIMITS as _ {
if resource < 0 || resource >= RLIM_NLIMITS as i32 {
return Err(vm.new_value_error("invalid resource specified".to_owned()));
}
let rlimit = unsafe {
Expand All @@ -164,7 +164,7 @@ mod resource {

#[pyfunction]
fn setrlimit(resource: i32, limits: Limits, vm: &VirtualMachine) -> PyResult<()> {
if resource < 0 || resource >= RLIM_NLIMITS as _ {
if resource < 0 || resource >= RLIM_NLIMITS as i32 {
return Err(vm.new_value_error("invalid resource specified".to_owned()));
}
let res = unsafe {
Expand Down
9 changes: 5 additions & 4 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ num-traits = "0.2.14"
num-integer = "0.1.44"
num-rational = "0.4.0"
rand = "0.8.5"
getrandom = { version = "0.2.6", features = ["js"] }
getrandom = { version = "0.2.6", features = ["custom"] }
log = "0.4.16"
serde = { version = "1.0.136", features = ["derive"] }
caseless = "0.2.1"
chrono = { version = "0.4.19", features = ["wasmbind"] }
chrono = { version = "=0.4.19" }
itertools = "0.10.3"
hex = "0.4.3"
hexf-parse = "0.2.1"
Expand Down Expand Up @@ -71,6 +71,7 @@ flate2 = "1.0.23"
once_cell = "1.10.0"
memoffset = "0.6.5"
optional = "0.5.0"
ic-cdk = "0.6.8"

# RustPython crates implementing functionality based on CPython
sre-engine = "0.4.1"
Expand Down Expand Up @@ -126,8 +127,8 @@ features = [
"impl-default", "vcruntime", "ifdef", "netioapi", "memoryapi",
]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.80"
# [target.'cfg(target_arch = "wasm32")'.dependencies]
# wasm-bindgen = "0.2.80"

[build-dependencies]
itertools = "0.10.3"
Expand Down
35 changes: 23 additions & 12 deletions vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ mod time {

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
type Date;
#[wasm_bindgen(static_method_of = Date)]
fn now() -> f64;
}
// Date.now returns unix time in milliseconds, we want it in seconds
Ok(Date::now() / 1000.0)
// use wasm_bindgen::prelude::*;
// #[wasm_bindgen]
// extern "C" {
// type Date;
// #[wasm_bindgen(static_method_of = Date)]
// fn now() -> f64;
// }
// // Date.now returns unix time in milliseconds, we want it in seconds
// Ok(Date::now() / 1000.0)

Ok(ic_cdk::api::time() as f64 / 1_000_000_000 as f64)
}

#[pyfunction]
Expand Down Expand Up @@ -144,14 +146,20 @@ mod time {
fn naive_or_local(self, vm: &VirtualMachine) -> PyResult<NaiveDateTime> {
Ok(match self {
OptionalArg::Present(secs) => pyobj_to_naive_date_time(secs, vm)?,
OptionalArg::Missing => chrono::offset::Local::now().naive_local(),
OptionalArg::Missing => chrono::NaiveDateTime::from_timestamp(
(ic_cdk::api::time() / 1_000_000_000) as i64,
0,
),
})
}

fn naive_or_utc(self, vm: &VirtualMachine) -> PyResult<NaiveDateTime> {
Ok(match self {
OptionalArg::Present(secs) => pyobj_to_naive_date_time(secs, vm)?,
OptionalArg::Missing => chrono::offset::Utc::now().naive_utc(),
OptionalArg::Missing => chrono::NaiveDateTime::from_timestamp(
(ic_cdk::api::time() / 1_000_000_000) as i64,
0,
),
})
}
}
Expand All @@ -160,7 +168,10 @@ mod time {
fn naive_or_local(self, vm: &VirtualMachine) -> PyResult<NaiveDateTime> {
Ok(match self {
OptionalArg::Present(t) => t.to_date_time(vm)?,
OptionalArg::Missing => chrono::offset::Local::now().naive_local(),
OptionalArg::Missing => chrono::NaiveDateTime::from_timestamp(
(ic_cdk::api::time() / 1_000_000_000) as i64,
0,
),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::sync::atomic::Ordering;
/// });
/// ```
pub struct Interpreter {
vm: VirtualMachine,
pub vm: VirtualMachine,
}

impl Interpreter {
Expand Down
21 changes: 11 additions & 10 deletions vm/src/vm/vm_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ impl VirtualMachine {
}
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
{
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn error(s: &str);
}
let mut s = String::new();
self.write_exception(&mut s, &exc).unwrap();
error(&s);
panic!("{}; exception backtrace above", msg)
// use wasm_bindgen::prelude::*;
// #[wasm_bindgen]
// extern "C" {
// #[wasm_bindgen(js_namespace = console)]
// fn error(s: &str);
// }
// let mut s = String::new();
// self.write_exception(&mut s, &exc).unwrap();
// error(&s);
// panic!("{}; exception backtrace above", msg)
panic!("{}", msg)
}
}

Expand Down