Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pure JS custom host objects #207

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 24 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 @@ -208,6 +199,7 @@ struct SerializeDeserialize<'a> {
host_objects: Option<v8::Local<'a, v8::Array>>,
error_callback: Option<v8::Local<'a, v8::Function>>,
for_storage: bool,
host_object_brand: Option<v8::Global<v8::Symbol>>,
}

impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> {
Expand Down Expand Up @@ -269,6 +261,23 @@ 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> {
if let Some(symbol) = &self.host_object_brand {
let key = v8::Local::new(scope, symbol);
object.has_own_property(scope, key.into())
} else {
Some(false)
}
}

fn write_host_object<'s>(
&mut self,
scope: &mut v8::HandleScope<'s>,
Expand Down Expand Up @@ -393,10 +402,15 @@ pub fn op_serialize(
None => None,
};

let key = v8::String::new(scope, "Deno.core.hostObject").unwrap();
let symbol = v8::Symbol::for_key(scope, key);
let host_object_brand = Some(v8::Global::new(scope, symbol));

let serialize_deserialize = Box::new(SerializeDeserialize {
host_objects,
error_callback,
for_storage: options.for_storage,
host_object_brand,
});
let mut value_serializer =
v8::ValueSerializer::new(scope, serialize_deserialize);
Expand Down Expand Up @@ -475,6 +489,7 @@ pub fn op_deserialize<'a>(
host_objects,
error_callback: None,
for_storage: options.for_storage,
host_object_brand: None,
});
let mut value_deserializer =
v8::ValueDeserializer::new(scope, serialize_deserialize, &zero_copy);
Expand Down