mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
refactor: simplify deno_core's grab_global and ensure_objs (#16564)
- refactor: remove JsRuntime::ensure_objs - refactor: Replace JsRuntime::grab_global with JsRuntime::eval
This commit is contained in:
parent
0d52945d43
commit
3019c45f87
3 changed files with 24 additions and 51 deletions
|
@ -110,7 +110,7 @@ pub fn initialize_context<'s>(
|
||||||
// extensions may provide ops that aren't part of the snapshot.
|
// extensions may provide ops that aren't part of the snapshot.
|
||||||
if snapshot_options.loaded() {
|
if snapshot_options.loaded() {
|
||||||
// Grab the Deno.core.ops object & init it
|
// Grab the Deno.core.ops object & init it
|
||||||
let ops_obj = JsRuntime::grab_global::<v8::Object>(scope, "Deno.core.ops")
|
let ops_obj = JsRuntime::eval::<v8::Object>(scope, "Deno.core.ops")
|
||||||
.expect("Deno.core.ops to exist");
|
.expect("Deno.core.ops to exist");
|
||||||
initialize_ops(scope, ops_obj, op_ctxs, snapshot_options);
|
initialize_ops(scope, ops_obj, op_ctxs, snapshot_options);
|
||||||
if snapshot_options != SnapshotOptions::CreateFromExisting {
|
if snapshot_options != SnapshotOptions::CreateFromExisting {
|
||||||
|
@ -120,13 +120,22 @@ pub fn initialize_context<'s>(
|
||||||
}
|
}
|
||||||
|
|
||||||
// global.Deno = { core: { } };
|
// global.Deno = { core: { } };
|
||||||
let core_val = JsRuntime::ensure_objs(scope, global, "Deno.core").unwrap();
|
let deno_obj = v8::Object::new(scope);
|
||||||
|
let deno_str = v8::String::new(scope, "Deno").unwrap();
|
||||||
|
global.set(scope, deno_str.into(), deno_obj.into());
|
||||||
|
|
||||||
|
let core_obj = v8::Object::new(scope);
|
||||||
|
let core_str = v8::String::new(scope, "core").unwrap();
|
||||||
|
deno_obj.set(scope, core_str.into(), core_obj.into());
|
||||||
|
|
||||||
// Bind functions to Deno.core.*
|
// Bind functions to Deno.core.*
|
||||||
set_func(scope, core_val, "callConsole", call_console);
|
set_func(scope, core_obj, "callConsole", call_console);
|
||||||
|
|
||||||
// Bind functions to Deno.core.ops.*
|
// Bind functions to Deno.core.ops.*
|
||||||
let ops_obj = JsRuntime::ensure_objs(scope, global, "Deno.core.ops").unwrap();
|
let ops_obj = v8::Object::new(scope);
|
||||||
|
let ops_str = v8::String::new(scope, "ops").unwrap();
|
||||||
|
core_obj.set(scope, ops_str.into(), ops_obj.into());
|
||||||
|
|
||||||
if !snapshot_options.will_snapshot() {
|
if !snapshot_options.will_snapshot() {
|
||||||
initialize_async_ops_info(scope, ops_obj, op_ctxs);
|
initialize_async_ops_info(scope, ops_obj, op_ctxs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -745,64 +745,28 @@ impl JsRuntime {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Grab a Global handle to a v8 value returned by the expression
|
pub fn eval<'s, T>(
|
||||||
pub(crate) fn grab<'s, T>(
|
|
||||||
scope: &mut v8::HandleScope<'s>,
|
scope: &mut v8::HandleScope<'s>,
|
||||||
root: v8::Local<'s, v8::Value>,
|
code: &str,
|
||||||
path: &str,
|
|
||||||
) -> Option<v8::Local<'s, T>>
|
) -> Option<v8::Local<'s, T>>
|
||||||
where
|
where
|
||||||
v8::Local<'s, T>: TryFrom<v8::Local<'s, v8::Value>, Error = v8::DataError>,
|
v8::Local<'s, T>: TryFrom<v8::Local<'s, v8::Value>, Error = v8::DataError>,
|
||||||
{
|
{
|
||||||
path
|
let scope = &mut v8::EscapableHandleScope::new(scope);
|
||||||
.split('.')
|
let source = v8::String::new(scope, code).unwrap();
|
||||||
.fold(Some(root), |p, k| {
|
let script = v8::Script::compile(scope, source, None).unwrap();
|
||||||
let p = v8::Local::<v8::Object>::try_from(p?).ok()?;
|
let v = script.run(scope)?;
|
||||||
let k = v8::String::new(scope, k)?;
|
scope.escape(v).try_into().ok()
|
||||||
p.get(scope, k.into())
|
|
||||||
})?
|
|
||||||
.try_into()
|
|
||||||
.ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn grab_global<'s, T>(
|
|
||||||
scope: &mut v8::HandleScope<'s>,
|
|
||||||
path: &str,
|
|
||||||
) -> Option<v8::Local<'s, T>>
|
|
||||||
where
|
|
||||||
v8::Local<'s, T>: TryFrom<v8::Local<'s, v8::Value>, Error = v8::DataError>,
|
|
||||||
{
|
|
||||||
let context = scope.get_current_context();
|
|
||||||
let global = context.global(scope);
|
|
||||||
Self::grab(scope, global.into(), path)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn ensure_objs<'s>(
|
|
||||||
scope: &mut v8::HandleScope<'s>,
|
|
||||||
root: v8::Local<'s, v8::Object>,
|
|
||||||
path: &str,
|
|
||||||
) -> Option<v8::Local<'s, v8::Object>> {
|
|
||||||
path.split('.').fold(Some(root), |p, k| {
|
|
||||||
let k = v8::String::new(scope, k)?.into();
|
|
||||||
match p?.get(scope, k) {
|
|
||||||
Some(v) if !v.is_null_or_undefined() => v.try_into().ok(),
|
|
||||||
_ => {
|
|
||||||
let o = v8::Object::new(scope);
|
|
||||||
p?.set(scope, k, o.into());
|
|
||||||
Some(o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Grabs a reference to core.js' opresolve & syncOpsCache()
|
/// Grabs a reference to core.js' opresolve & syncOpsCache()
|
||||||
fn init_cbs(&mut self) {
|
fn init_cbs(&mut self) {
|
||||||
let scope = &mut self.handle_scope();
|
let scope = &mut self.handle_scope();
|
||||||
let recv_cb =
|
let recv_cb =
|
||||||
Self::grab_global::<v8::Function>(scope, "Deno.core.opresolve").unwrap();
|
Self::eval::<v8::Function>(scope, "Deno.core.opresolve").unwrap();
|
||||||
let recv_cb = v8::Global::new(scope, recv_cb);
|
let recv_cb = v8::Global::new(scope, recv_cb);
|
||||||
let build_custom_error_cb =
|
let build_custom_error_cb =
|
||||||
Self::grab_global::<v8::Function>(scope, "Deno.core.buildCustomError")
|
Self::eval::<v8::Function>(scope, "Deno.core.buildCustomError")
|
||||||
.expect("Deno.core.buildCustomError is undefined in the realm");
|
.expect("Deno.core.buildCustomError is undefined in the realm");
|
||||||
let build_custom_error_cb = v8::Global::new(scope, build_custom_error_cb);
|
let build_custom_error_cb = v8::Global::new(scope, build_custom_error_cb);
|
||||||
// Put global handles in state
|
// Put global handles in state
|
||||||
|
@ -854,7 +818,7 @@ impl JsRuntime {
|
||||||
// TODO(@AaronO): make ops stable across snapshots
|
// TODO(@AaronO): make ops stable across snapshots
|
||||||
{
|
{
|
||||||
let scope = &mut self.handle_scope();
|
let scope = &mut self.handle_scope();
|
||||||
let o = Self::grab_global::<v8::Object>(scope, "Deno.core.ops").unwrap();
|
let o = Self::eval::<v8::Object>(scope, "Deno.core.ops").unwrap();
|
||||||
let names = o.get_own_property_names(scope, Default::default()).unwrap();
|
let names = o.get_own_property_names(scope, Default::default()).unwrap();
|
||||||
for i in 0..names.length() {
|
for i in 0..names.length() {
|
||||||
let key = names.get_index(scope, i).unwrap();
|
let key = names.get_index(scope, i).unwrap();
|
||||||
|
|
|
@ -101,7 +101,7 @@ fn grab_cb(
|
||||||
scope: &mut v8::HandleScope,
|
scope: &mut v8::HandleScope,
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> v8::Global<v8::Function> {
|
) -> v8::Global<v8::Function> {
|
||||||
let cb = JsRuntime::grab_global::<v8::Function>(scope, path)
|
let cb = JsRuntime::eval::<v8::Function>(scope, path)
|
||||||
.unwrap_or_else(|| panic!("{} must be defined", path));
|
.unwrap_or_else(|| panic!("{} must be defined", path));
|
||||||
v8::Global::new(scope, cb)
|
v8::Global::new(scope, cb)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue