Skip to content

Commit 9dd9c52

Browse files
authored
cwa: implement time_now spec (#15)
* internal/abi/cwa: support the time_now call * cwa: use time_now call, test it
1 parent 675ac7c commit 9dd9c52

File tree

9 files changed

+212
-0
lines changed

9 files changed

+212
-0
lines changed

cwa/libcwa/Cargo.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cwa/libcwa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ repository = "https://github.com/Xe/olin"
99
homepage = "https://github.com/Xe/olin"
1010

1111
[dependencies]
12+
chrono = "0.4"

cwa/libcwa/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub mod sys {
2424
pub fn resource_write(id: i32, data_ptr: *const u8, data_len: usize) -> i32;
2525
pub fn resource_close(id: i32);
2626
pub fn resource_flush(id: i32) -> i32;
27+
28+
pub fn time_now() -> i64;
2729
}
2830
}
2931

@@ -267,3 +269,17 @@ impl Write for Resource {
267269
.unwrap_err());
268270
}
269271
}
272+
273+
pub mod time {
274+
extern crate chrono;
275+
276+
use time::chrono::TimeZone;
277+
278+
pub fn now() -> chrono::DateTime<chrono::Utc> {
279+
return chrono::Utc.timestamp(ts(), 0);
280+
}
281+
282+
pub fn ts() -> i64 {
283+
unsafe { ::sys::time_now() }
284+
}
285+
}

cwa/tests/Cargo.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cwa/tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ authors = ["Christine Dodrill <[email protected]>"]
66
[dependencies]
77
libcwa = { path = "../libcwa" }
88
httparse = "1.3.2"
9+
chrono = "0.4"

cwa/tests/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate libcwa;
88
use libcwa::*;
99

1010
mod http;
11+
mod time;
1112

1213
#[no_mangle]
1314
pub extern "C" fn cwa_main() -> i32 {
@@ -29,6 +30,17 @@ pub extern "C" fn cwa_main() -> i32 {
2930
Err(e) => result = e as i32,
3031
}
3132

33+
if result != 0 {
34+
return result;
35+
}
36+
37+
log::info("time test");
38+
let result: i32;
39+
match time::test() {
40+
Ok(()) => result = 0,
41+
Err(e) => result = e as i32,
42+
}
43+
3244
result
3345
}
3446

cwa/tests/src/time.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extern crate chrono;
2+
extern crate libcwa;
3+
4+
use time::chrono::TimeZone;
5+
use std::io;
6+
use libcwa::*;
7+
8+
pub fn test() -> Result<(), i32> {
9+
let now: i64 = time::ts();
10+
let dt = chrono::Utc.timestamp(now, 0);
11+
12+
log::info(&format!("ts: {}, dt: {}", now, dt.to_rfc3339()));
13+
14+
let now = time::now();
15+
log::info(&format!("time::now(): {}", now.to_rfc3339()));
16+
17+
log::info("time test passed");
18+
Ok(())
19+
}

internal/abi/cwa/core.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ func (p *Process) ResolveFunc(module, field string) exec.FunctionImport {
222222

223223
return 0
224224
}
225+
case "time_now":
226+
return func(vm *exec.VirtualMachine) int64 {
227+
p.SetVM(vm)
228+
229+
return p.timeNow()
230+
}
225231
default:
226232
log.Panicf("unknown import %s::%s", module, field)
227233
}

internal/abi/cwa/time.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cwa
2+
3+
import "time"
4+
5+
func (p *Process) timeNow() int64 {
6+
return time.Now().UTC().Unix()
7+
}

0 commit comments

Comments
 (0)