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 Sep 17, 2023
1 parent faebea9 commit 13b7d75
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
11 changes: 10 additions & 1 deletion core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ for (let i = 0; i < 10; i++) {
op_shutdown: shutdown,
} = ensureFastOps();

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

function createHostObject() {
const o = ObjectCreate(null);
o[hostObjectBrand] = hostObjectBrand;
return o;
}

// Extra Deno.core.* exports
const core = ObjectAssign(globalThis.Deno.core, {
asyncStub,
Expand Down Expand Up @@ -842,7 +850,8 @@ for (let i = 0; i < 10; i++) {
source,
specifier,
) => ops.op_eval_context(source, specifier),
createHostObject: () => ops.op_create_host_object(),
createHostObject,
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 @@ -57,7 +57,6 @@ crate::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
26 changes: 17 additions & 9 deletions core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,6 @@ pub fn op_queue_microtask(
scope.enqueue_microtask(cb);
}

#[op2(core)]
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(core)]
pub fn op_encode<'a>(
scope: &mut v8::HandleScope<'a>,
Expand Down Expand Up @@ -254,6 +245,23 @@ impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> {
}
}

fn has_custom_host_object(
&mut self,
_scope: &mut v8::HandleScope,
) -> 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 13b7d75

Please sign in to comment.