1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! See [http://docs.screeps.com/api/#Game.cpu]
//!
//! [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
use std::collections;

use serde::{Deserialize, Serialize};

use crate::{constants::ReturnCode, macros::*, traits::TryInto};

/// See [`v8_getheapstatistics`]
///
/// [`v8_getheapstatistics`]: https://nodejs.org/dist/latest-v8.x/docs/api/v8.html#v8_v8_getheapstatistics
#[derive(Default, Serialize, Deserialize)]
pub struct HeapStatistics {
    pub total_heap_size: u32,
    pub total_heap_size_executable: u32,
    pub total_physical_size: u32,
    pub used_heap_size: u32,
    pub heap_size_limit: u32,
    pub malloced_memory: u32,
    pub peak_malloced_memory: u32,
    pub does_zap_garbage: u32,
    pub externally_allocated_size: u32,
}

js_serializable!(HeapStatistics);
js_deserializable!(HeapStatistics);

/// See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn limit() -> f64 {
    js_unwrap!(Game.cpu.limit)
}

/// See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn tick_limit() -> f64 {
    js_unwrap!(Game.cpu.tickLimit)
}

/// See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn bucket() -> f64 {
    js_unwrap!(Game.cpu.bucket)
}

/// See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn shard_limits() -> collections::HashMap<String, f64> {
    js_unwrap!(Game.cpu.shardLimits)
}

/// See [http://docs.screeps.com/api/#Game.getHeapStatistics]
///
/// [http://docs.screeps.com/api/#Game.getHeapStatistics]: http://docs.screeps.com/api/#Game.getHeapStatistics
///
/// Returns object with all 0 values if heap statistics are not available.
pub fn get_heap_statistics() -> HeapStatistics {
    use stdweb::Value;

    let heap_stats: Value = js_unwrap!(Game.cpu.getHeapStatistics && Game.cpu.getHeapStatistics());

    match heap_stats {
        Value::Null | Value::Undefined | Value::Bool(false) => HeapStatistics::default(),
        other => other.try_into().expect(
            "expected Game.cpu.getHeapStatistics() to return an object with a known format",
        ),
    }
}

/// See [http://docs.screeps.com/api/#Game.getUsed]
///
/// [http://docs.screeps.com/api/#Game.getUsed]: http://docs.screeps.com/api/#Game.getUsed
pub fn get_used() -> f64 {
    js_unwrap!(Game.cpu.getUsed())
}

/// See [http://docs.screeps.com/api/#Game.setShardLimits]
///
/// [http://docs.screeps.com/api/#Game.setShardLimits]: http://docs.screeps.com/api/#Game.setShardLimits
pub fn set_shard_limits(limits: collections::HashMap<String, f64>) -> ReturnCode {
    js_unwrap!(Game.cpu.setShardLimits(@{limits}))
}

/// Reset your runtime environment and wipe all data in heap memory.
///
/// See [Game.cpu.halt()](https://docs.screeps.com/api/#Game.halt).
pub fn halt() {
    js! {
        Game.cpu.halt();
    }
}