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
// Copyright 2022 The Goscript Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//! This crate is part of the Goscript project. Please refer to <https://goscript.dev> for more information.
//!
//! # Feature
//! - `async`: Channel and goroutine support
//! - `btree_map`: Make it use BTreeMap instead of HashMap
//! - `instruction_pos`: Add instruction position to bytecode for debugging
//! - `serde_borsh`: Serde support for bytecode using Borsh

mod instruction;
#[macro_use]
mod metadata;
#[cfg(feature = "async")]
mod channel;
mod objects;
#[macro_use]
mod dispatcher;
mod bytecode;
mod ffi;
mod stack;
mod value;
mod vm;

pub mod gc;
pub mod types {
    pub use crate::value::*;
}

pub mod parser {
    pub use go_parser::*;
}

pub use {
    ffi::*,
    go_parser::{Map, MapIter},
    go_pmacro::{ffi_impl, Ffi, UnsafePtr},
    value::Bytecode,
    vm::run,
    vm::PanicData,
};

pub struct CallStackDisplay<'a> {
    panic_data: &'a PanicData,
    bc: &'a Bytecode,
}

impl<'a> CallStackDisplay<'a> {
    pub fn new(panic_data: &'a PanicData, bc: &'a Bytecode) -> CallStackDisplay<'a> {
        Self { panic_data, bc }
    }
}

impl<'a> std::fmt::Display for CallStackDisplay<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (fkey, pc) in self.panic_data.call_stack.iter() {
            let func = &self.bc.objects.functions[*fkey];
            if let Some(p) = func.pos[*pc as usize] {
                if let Some(fs) = &self.bc.file_set {
                    writeln!(
                        f,
                        "{}",
                        fs.position(p as usize)
                            .unwrap_or(go_parser::FilePos::null())
                    )?;
                } else {
                    writeln!(f, "fileset not available, pos:{}", p)?;
                }
            } else {
                f.write_str("<no debug info available for current frame>\n")?;
            };
        }
        Ok(())
    }
}