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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::{fmt, io};
use crate::{
    object::NonNullObject,
    prelude::*,
};

/// An instance of Ruby's `RubyVM::InstructionSequence` class.
///
/// **Note:** The binary data that comes from an instruction sequence is not
/// portable and should not be used in another version or architecture of Ruby.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct InstrSeq(NonNullObject);

impl AsRef<AnyObject> for InstrSeq {
    #[inline]
    fn as_ref(&self) -> &AnyObject { self.0.as_ref() }
}

impl From<InstrSeq> for AnyObject {
    #[inline]
    fn from(obj: InstrSeq) -> Self { obj.0.into() }
}

impl PartialEq<AnyObject> for InstrSeq {
    #[inline]
    fn eq(&self, obj: &AnyObject) -> bool {
        self.as_any_object() == obj
    }
}

unsafe impl Object for InstrSeq {
    #[inline]
    fn unique_id() -> Option<u128> {
        Some((!0) - 1)
    }

    #[inline]
    fn cast<A: Object>(obj: A) -> Option<Self> {
        if obj.class().inherits(Class::instr_seq()) {
            unsafe { Some(Self::cast_unchecked(obj)) }
        } else {
            None
        }
    }
}

impl fmt::Display for InstrSeq {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_any_object().fmt(f)
    }
}

impl InstrSeq {
    #[inline]
    fn _compile(args: &[AnyObject]) -> Result<Self> {
        Class::instr_seq().call_with("compile", args).map(|obj| unsafe {
            Self::cast_unchecked(obj)
        })
    }

    /// Compiles `script` into an instruction sequence.
    #[inline]
    pub fn compile(script: impl Into<String>) -> Result<Self> {
        Self::_compile(&[script.into().into()])
    }

    /// Compiles `script` with `options` into an instruction sequence.
    #[inline]
    pub fn compile_with(
        script: impl Into<String>,
        options: impl Into<Hash>,
    ) -> Result<Self> {
        Self::_compile(&[script.into().into(), options.into().into()])
    }

    #[inline]
    fn _compile_file(args: &[AnyObject]) -> Result<Self> {
        Class::instr_seq().call_with("compile_file", args).map(|obj| unsafe {
            Self::cast_unchecked(obj)
        })
    }

    /// Compiles the contents of a file at `path` into an instruction sequence.
    #[inline]
    pub fn compile_file(path: impl Into<String>) -> Result<Self> {
        Self::_compile_file(&[path.into().into()])
    }

    /// Compiles the contents of a file at `path` with `options into an
    /// instruction sequence.
    #[inline]
    pub fn compile_file_with(
        path: impl Into<String>,
        options: impl Into<Hash>,
    ) -> Result<Self> {
        Self::_compile_file(&[path.into().into(), options.into().into()])
    }

    /// Loads an instruction sequence from a binary formatted string created by
    /// [`to_binary`](#method.to_binary).
    ///
    /// # Safety
    ///
    /// This loader does not have a verifier, so loading broken/modified binary
    /// causes critical problems.
    ///
    /// # Examples
    ///
    /// This is equivalent to calling
    /// `RubyVM::InstructionSequence.load_from_binary`:
    ///
    /// ```
    /// # rosy::vm::init().unwrap();
    /// # rosy::protected(|| {
    /// use rosy::{vm::InstrSeq, String};
    ///
    /// let script = "'hi' * 3";
    ///
    /// let seq1 = InstrSeq::compile(script).expect("Invalid script");
    /// let seq2 = unsafe { InstrSeq::from_binary(seq1.to_binary()) };
    ///
    /// assert_eq!(String::from("hihihi"), seq2.eval().unwrap());
    /// # }).unwrap();
    /// ```
    #[inline]
    pub unsafe fn from_binary(binary: impl Into<String>) -> Self {
        Self::cast_unchecked(Class::instr_seq().call_with_unchecked(
            "load_from_binary",
            &[binary.into()]
        ))
    }

    /// Evaluates `self` and returns the result.
    ///
    /// # Examples
    ///
    /// This is equivalent to calling `eval` in a protected context:
    ///
    /// ```
    /// use rosy::{vm::InstrSeq, String};
    /// # rosy::vm::init().unwrap();
    ///
    /// let script = "'hi' * 3";
    /// let instr_seq = InstrSeq::compile(script).expect("Invalid script");
    ///
    /// let result = instr_seq.eval().unwrap();
    /// assert_eq!(String::from("hihihi"), result);
    /// ```
    #[inline]
    pub fn eval(self) -> Result<AnyObject> {
        self.call("eval")
    }

    /// Returns the serialized binary data.
    #[inline]
    pub fn to_binary(self) -> String {
        unsafe { String::cast_unchecked(self.call_unchecked("to_binary")) }
    }

    /// Writes the serialized binary data of `self` to `w`.
    ///
    /// This makes it easy to write the contents of `self` to a
    /// [`File`](https://doc.rust-lang.org/std/fs/struct.File.html) or any other
    /// common I/O type.
    #[inline]
    pub fn write_binary(self, mut w: impl io::Write) -> io::Result<()> {
        let binary = self.to_binary();
        let bytes = unsafe { binary.as_bytes() };
        w.write_all(bytes)
    }

    /// Returns a human-readable form of `self`.
    #[inline]
    pub fn disassemble(self) -> String {
        unsafe { String::cast_unchecked(self.call_unchecked("disasm")) }
    }

    /// Returns the file path of `self`, or `<compiled>` if it was compiled from
    /// a string.
    #[inline]
    pub fn path(self) -> String {
        unsafe { String::cast_unchecked(self.call_unchecked("path")) }
    }

    /// Returns the absolute path of `self` if it was compiled from a file.
    #[inline]
    pub fn absolute_path(self) -> Option<String> {
        unsafe {
            let path = self.call_unchecked("absolute_path");
            if path.is_nil() {
                None
            } else {
                Some(String::cast_unchecked(path))
            }
        }
    }
}