Skip to content

Commit

Permalink
feat: Pure JS custom host objects
Browse files Browse the repository at this point in the history
Avoids substantial slowdown caused by host objects with embedder fields.

* Depends on: denoland/rusty_v8#1322
* Depends on: denoland/rusty_v8#1324
* Deno bug: denoland/deno#12067
  • Loading branch information
lrowe committed Nov 28, 2023
1 parent 208bedf commit 3e4e100
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@
return { fileName, lineNumber, columnNumber };
}

const hostObjectBrand = SymbolFor("Deno.core.hostObject");

// Extra Deno.core.* exports
const core = ObjectAssign(globalThis.Deno.core, {
asyncStub,
Expand Down Expand Up @@ -794,7 +796,7 @@
source,
specifier,
) => ops.op_eval_context(source, specifier),
createHostObject: () => ops.op_create_host_object(),
hostObjectBrand,
encode: (text) => ops.op_encode(text),
decode: (buffer) => ops.op_decode(buffer),
serialize: (
Expand Down
1 change: 0 additions & 1 deletion core/ops_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ deno_core::extension!(
ops_builtin_v8::op_set_has_tick_scheduled,
ops_builtin_v8::op_eval_context,
ops_builtin_v8::op_queue_microtask,
ops_builtin_v8::op_create_host_object,
ops_builtin_v8::op_encode,
ops_builtin_v8::op_decode,
ops_builtin_v8::op_serialize,
Expand Down
23 changes: 14 additions & 9 deletions core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,6 @@ pub fn op_eval_context<'a>(
}
}

#[op2]
pub fn op_create_host_object<'a>(
scope: &mut v8::HandleScope<'a>,
) -> v8::Local<'a, v8::Object> {
let template = v8::ObjectTemplate::new(scope);
template.set_internal_field_count(1);
template.new_instance(scope).unwrap()
}

#[op2]
pub fn op_encode<'a>(
scope: &mut v8::HandleScope<'a>,
Expand Down Expand Up @@ -269,6 +260,20 @@ impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> {
}
}

fn has_custom_host_object(&mut self, _isolate: &mut v8::Isolate) -> bool {
true
}

fn is_host_object<'s>(
&mut self,
scope: &mut v8::HandleScope<'s>,
object: v8::Local<'s, v8::Object>,
) -> Option<bool> {
let key = v8::String::new(scope, "Deno.core.hostObject").unwrap();
let symbol = v8::Symbol::for_key(scope, key);
object.has_own_property(scope, symbol.into())
}

fn write_host_object<'s>(
&mut self,
scope: &mut v8::HandleScope<'s>,
Expand Down

0 comments on commit 3e4e100

Please sign in to comment.