mirror of https://github.com/boa-dev/boa.git
Haled Odat
1 year ago
5 changed files with 279 additions and 48 deletions
@ -1,29 +1,59 @@ |
|||||||
use std::{fs::OpenOptions, io::Write}; |
use std::{ |
||||||
|
fs::{File, OpenOptions}, |
||||||
|
io::{Read, Write}, |
||||||
|
}; |
||||||
|
|
||||||
use boa_engine::{ |
use boa_engine::{ |
||||||
object::ObjectInitializer, snapshot::SnapshotSerializer, Context, JsNativeError, JsObject, |
object::ObjectInitializer, |
||||||
JsResult, JsValue, NativeFunction, |
snapshot::{Snapshot, SnapshotSerializer}, |
||||||
|
Context, JsNativeError, JsObject, JsResult, JsValue, NativeFunction, |
||||||
}; |
}; |
||||||
|
|
||||||
const SNAPSHOT_PATH: &str = "./snapshot.bin"; |
const SNAPSHOT_PATH: &str = "./snapshot.bin"; |
||||||
|
|
||||||
|
fn get_file_as_byte_vec(filename: &str) -> Vec<u8> { |
||||||
|
let mut f = File::open(&filename).expect("no file found"); |
||||||
|
let metadata = std::fs::metadata(&filename).expect("unable to read metadata"); |
||||||
|
let mut buffer = vec![0; metadata.len() as usize]; |
||||||
|
f.read(&mut buffer).expect("buffer overflow"); |
||||||
|
|
||||||
|
buffer |
||||||
|
} |
||||||
|
|
||||||
fn create(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> { |
fn create(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> { |
||||||
let Ok(mut file) = OpenOptions::new().write(true).create(true).open(SNAPSHOT_PATH) else { |
let Ok(mut file) = OpenOptions::new().write(true).create(true).open(SNAPSHOT_PATH) else { |
||||||
return Err(JsNativeError::error().with_message("could not create snapshot.bin file").into()); |
return Err(JsNativeError::error().with_message("could not create snapshot.bin file").into()); |
||||||
}; |
}; |
||||||
|
|
||||||
let mut serializer = SnapshotSerializer::new(); |
let serializer = SnapshotSerializer::new(); |
||||||
|
|
||||||
serializer.serialize(context).unwrap(); |
let snapshot = serializer.serialize(context).unwrap(); |
||||||
|
|
||||||
file.write_all(serializer.bytes()).unwrap(); |
file.write_all(snapshot.bytes()).unwrap(); |
||||||
file.flush().unwrap(); |
file.flush().unwrap(); |
||||||
|
|
||||||
Ok(JsValue::undefined()) |
Ok(JsValue::undefined()) |
||||||
} |
} |
||||||
|
|
||||||
|
fn load(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> { |
||||||
|
// let Ok(mut file) = OpenOptions::new().read(true).open(SNAPSHOT_PATH) else {
|
||||||
|
// return Err(JsNativeError::error().with_message("could not open snapshot.bin file").into());
|
||||||
|
// };
|
||||||
|
|
||||||
|
let bytes = get_file_as_byte_vec(SNAPSHOT_PATH); |
||||||
|
|
||||||
|
let snapshot = Snapshot::new(bytes); |
||||||
|
|
||||||
|
let deser_context = snapshot.deserialize().unwrap(); |
||||||
|
|
||||||
|
*context = deser_context; |
||||||
|
|
||||||
|
Ok(JsValue::undefined()) |
||||||
|
} |
||||||
|
|
||||||
pub(super) fn create_object(context: &mut Context<'_>) -> JsObject { |
pub(super) fn create_object(context: &mut Context<'_>) -> JsObject { |
||||||
ObjectInitializer::new(context) |
ObjectInitializer::new(context) |
||||||
.function(NativeFunction::from_fn_ptr(create), "create", 0) |
.function(NativeFunction::from_fn_ptr(create), "create", 0) |
||||||
|
.function(NativeFunction::from_fn_ptr(load), "load", 1) |
||||||
.build() |
.build() |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue