1#[macro_use]
2extern crate lazy_static;
3
4mod builtins;
5mod isolate_state;
6mod js_loading;
7mod module;
8mod script;
9
10pub(crate) use isolate_state::IsolateState;
11
12pub fn init(v8_flags: Option<Vec<String>>) {
13 if let Some(mut v8_flags) = v8_flags {
14 v8_flags.push("jstime".to_owned());
15 v8_flags.rotate_right(1);
16
17 v8::V8::set_flags_from_command_line(v8_flags);
18 }
19
20 let platform = v8::new_default_platform(0, false).make_shared();
21 v8::V8::initialize_platform(platform);
22 v8::V8::initialize();
23}
24
25#[derive(Default)]
27pub struct Options {
28 }
31
32impl Options {
33 pub fn new(_snapshot: Option<&'static [u8]>) -> Options {
34 Options {
35 }
38 }
39}
40
41#[allow(clippy::all)]
43pub struct JSTime {
44 isolate: Option<v8::OwnedIsolate>,
45 }
47
48impl JSTime {
49 pub fn new(options: Options) -> JSTime {
51 let create_params =
52 v8::Isolate::create_params().external_references(&**builtins::EXTERNAL_REFERENCES);
53 let isolate = v8::Isolate::new(create_params);
57 JSTime::create(options, isolate)
58 }
59
60 fn create(_options: Options, mut isolate: v8::OwnedIsolate) -> JSTime {
95 let global_context = {
96 let scope = &mut v8::HandleScope::new(&mut isolate);
97 let context = v8::Context::new(scope);
98 v8::Global::new(scope, context)
99 };
100
101 isolate.set_slot(IsolateState::new(global_context));
102
103 if true {
105 let context = IsolateState::get(&mut isolate).borrow().context();
106 let scope = &mut v8::HandleScope::with_context(&mut isolate, context);
107 builtins::Builtins::create(scope);
108 }
109
110 JSTime {
111 isolate: Some(isolate),
112 }
114 }
115
116 fn isolate(&mut self) -> &mut v8::Isolate {
117 match self.isolate.as_mut() {
118 Some(i) => i,
119 None => unsafe {
120 std::hint::unreachable_unchecked();
121 },
122 }
123 }
124
125 pub fn import(&mut self, filename: &str) -> Result<(), String> {
127 let context = IsolateState::get(self.isolate()).borrow().context();
128 let scope = &mut v8::HandleScope::with_context(self.isolate(), context);
129 let loader = module::Loader::new();
130
131 let mut cwd = std::env::current_dir().unwrap();
132 cwd.push("jstime");
133 let cwd = cwd.into_os_string().into_string().unwrap();
134 match loader.import(scope, &cwd, filename) {
135 Ok(_) => Ok(()),
136 Err(e) => Err(e.to_string(scope).unwrap().to_rust_string_lossy(scope)),
137 }
138 }
139
140 pub fn run_script(&mut self, source: &str, filename: &str) -> Result<String, String> {
142 let context = IsolateState::get(self.isolate()).borrow().context();
143 let scope = &mut v8::HandleScope::with_context(self.isolate(), context);
144 match script::run(scope, source, filename) {
145 Ok(v) => Ok(v.to_string(scope).unwrap().to_rust_string_lossy(scope)),
146 Err(e) => Err(e.to_string(scope).unwrap().to_rust_string_lossy(scope)),
147 }
148 }
149}
150
151impl Drop for JSTime {
152 fn drop(&mut self) {
153 }
159}