[oden] Hot-reload script files

This commit is contained in:
John Doty 2023-08-19 16:54:50 -07:00
parent 642ced45f8
commit a850c3cc58
7 changed files with 241 additions and 9 deletions

View file

@ -8,8 +8,9 @@ edition = "2021"
[dependencies]
anyhow = "1"
bitflags = "1"
thiserror = "1"
oden-js-sys = {path = "../oden-js-sys"}
thiserror = "1"
[dev-dependencies]
assert_matches = "1.5.0"

View file

@ -396,6 +396,23 @@ impl ContextRef {
pub fn process_all_jobs(&self) -> Result<()> {
self.get_runtime().process_all_jobs()
}
/// Deserialize a value from bytes generated by `ValueRef::serialize()`.
///
/// NOTE: The serialized value is only good for this exact version of
/// QuickJS- do *not* expect to be able to save it to disk and
/// re-load it. This is for more ephemeral usage: passing values
/// between threads, etc.
pub fn deserialize(&self, data: &[u8]) -> Result<Value> {
self.check_exception(unsafe {
sys::JS_ReadObject(
self.ctx,
data.as_ptr(),
data.len(),
sys::JS_READ_OBJ_REFERENCE as i32,
)
})
}
}
#[derive(Debug)]

View file

@ -337,6 +337,42 @@ impl ValueRef {
}
}
}
/// Serialize this value into a byte array. Consume the byte array with
/// `ContextRef::deserialize()`.
///
/// NOTE: The serialized value is only good for this exact version of
/// QuickJS- do *not* expect to be able to save it to disk and
/// re-load it. This is for more ephemeral usage: passing values
/// between threads, etc.
///
/// NOTE: In theory if we wanted to just serialize a message we could
/// avoid a memory copy; we copy here to avoid keeping the runtime
/// alive, which we would have to do in order to free the buffer
/// correctly. If we were willing to keep the runtime alive...
///
/// TODO: We do not have support for SharedArrayBuffers here, which would
/// let us use this function to pass large buffers between Runtime
/// instances, like on different threads, without copying. What a
/// pity.
pub fn serialize(&self, ctx: &ContextRef) -> Result<Vec<u8>> {
unsafe {
let mut size = 0;
let data = sys::JS_WriteObject(
ctx.ctx,
&mut size,
self.val,
sys::JS_WRITE_OBJ_REFERENCE as i32,
);
if data.is_null() {
Err(ctx.exception_error())
} else {
let result = std::slice::from_raw_parts(data, size).to_vec();
sys::js_free(ctx.ctx, data as *mut std::ffi::c_void);
Ok(result)
}
}
}
}
impl fmt::Debug for ValueRef {