summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 78d1822412760e1d6a58130b6ace9855dc8b91da (plain)
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
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;

use boa_interner::Interner;
use boa_parser::Parser;
use boa_parser::Source;
use ion::IonValue;
use runtime::Runtime;

mod ion;
mod runtime;

#[derive(Debug)]
pub enum RelJsError {
    IoError(io::Error),
    JsParseError(boa_parser::Error),
    BytecodeEOF,
}

impl From<io::Error> for RelJsError {
    fn from(value: io::Error) -> Self {
        RelJsError::IoError(value)
    }
}

impl From<boa_parser::Error> for RelJsError {
    fn from(value: boa_parser::Error) -> Self {
        RelJsError::JsParseError(value)
    }
}



fn main() -> Result<(), RelJsError> {
    //let src = Source::from_filepath(Path::new("script.js"))?;
    //let mut parser = Parser::new(src);
    //let mut interner = Interner::new();
    //let script = parser.parse_script(&mut interner)?;

    let mut bin = File::open("demo/test-1.bin")?;
    let mut data = Vec::new();
    bin.read_to_end(&mut data)?;

    let mut runtime = Runtime::new();
    let fnref = runtime.load_bytecode(data);

    let mut args = Vec::with_capacity(3);
    args.push(IonValue::new_null());
    args.push(IonValue::new_null());
    args.push(IonValue::new_null());

    let result = runtime.invoke(fnref, args);

    for (i, v) in result.iter().enumerate() {
        println!("Result {i}:");
        for b in v.bytes() {
            print!("{b:02x} ");
        }
        println!("\n");
    }

    Ok(())
}