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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use lazy_static::lazy_static; // 1.4.0
use std::sync::{
    Mutex,
    MutexGuard
};

pub mod interfaces;
pub mod client;
pub use client::{
    new_vm_api_client,
    ChainTester,
    get_vm_api_client,
    close_vm_api_client,
    set_apply,
    get_globals,
    GetTableRowsPrams,
};

pub mod server;


pub struct DebuggerConfig {
    pub debugger_server_address: String,
    pub debugger_server_port: u16,
    pub vm_api_server_address: String,
    pub vm_api_server_port: u16,
    pub apply_request_server_address: String,
    pub apply_request_server_port: u16,
}

impl DebuggerConfig {
    fn new() -> Self {
        Self { 
            debugger_server_address: "127.0.0.1".into(), 
            debugger_server_port: 9090, 
            vm_api_server_address: "127.0.0.1".into(), 
            vm_api_server_port: 9092,
            apply_request_server_address: "127.0.0.1".into(), 
            apply_request_server_port: 9091,
        }
    }
}

lazy_static! {
    static ref DEBUGGER_CONFIG: Mutex<DebuggerConfig> = Mutex::new(DebuggerConfig::new());
}

pub fn get_debugger_config() -> MutexGuard<'static, DebuggerConfig> {
    return DEBUGGER_CONFIG.lock().unwrap()
}

extern "Rust" {
	pub fn __eosio_generate_abi() -> String;
}

// pub fn generate_abi_file(package_name: &str) {
// 	let abi = unsafe {
// 		__eosio_generate_abi()
// 	};

//     // let package_name = env!("CARGO_PKG_NAME");
//     let abi_file = format!("./target/{}.abi", package_name);
// 	match std::fs::write(std::path::Path::new(&abi_file), abi) {
//         Ok(()) => {

//         }
//         Err(err) => {
//             panic!("{}", err);
//         }
//     }
// }

lazy_static! {
    static ref BUILD_CONTRACT_MUTEX: Mutex<std::collections::HashMap<String, String>> = Mutex::new(std::collections::HashMap::new());
}

pub fn build_contract(package_name: &str, project_dir: &str) {
    println!("++++++building {package_name} at {project_dir}");
    let mut build_contract = BUILD_CONTRACT_MUTEX.lock().unwrap();
    if build_contract.get(package_name).is_some() {
        return;
    }

    build_contract.insert(package_name.into(), project_dir.into());

    // let mut cargo_path: String = which::which("cargo").unwrap().to_str().unwrap().into();
    // if cargo_path.contains(".rustup") {
    //     let pos = cargo_path.find(".rustup").unwrap();
    //     cargo_path = format!("{}/{}", &cargo_path[0..pos], ".cargo/bin/cargo");
    //     println!("++++++++++cargo path:{}", cargo_path);
    // }
    std::env::set_var("RUSTFLAGS", "-C link-arg=-zstack-size=8192  -Clinker-plugin-lto");
    let mut cmd = std::process::Command::new("cargo");
    cmd
        .args([
            "+nightly",
            "build",
            "--target=wasm32-wasi",
            &format!("--target-dir={project_dir}/target"),
            "-Zbuild-std",
            "--no-default-features",
            "--release",
            "-Zbuild-std-features=panic_immediate_abort"
            ]
        );

    let mut child = cmd
        // capture the stdout to return from this function as bytes
        .stdout(std::process::Stdio::null())
        .spawn()
        .expect("command failed to start");
    let output = child.wait().unwrap();
    if !output.success() {
        panic!("build failed");
    }

    let in_wasm_file = format!("{project_dir}/target/wasm32-wasi/release/{}.wasm", package_name);
    let out_wasm_file = format!("{project_dir}/target/{}.wasm", package_name);
    let wasm = std::fs::read(in_wasm_file).unwrap();
    std::fs::write(out_wasm_file, wasm).unwrap();

    // generate_abi_file(package_name);
}