Skip to content

Commit

Permalink
feat(test262): Implement the detachArrayBuffer test harness function
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Jan 3, 2025
1 parent 71f28ae commit 722696d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
46 changes: 45 additions & 1 deletion nova_cli/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use nova_vm::ecmascript::{
builtins::{create_builtin_function, ArgumentsList, Behaviour, BuiltinFunctionArgs},
builtins::{
array_buffer, create_builtin_function, ArgumentsList, Behaviour, BuiltinFunctionArgs,
},
execution::{agent::ExceptionType, Agent, JsResult},
types::{InternalMethods, IntoValue, Object, PropertyDescriptor, PropertyKey, String, Value},
};
Expand All @@ -22,6 +24,7 @@ pub fn initialize_global_object(agent: &mut Agent, global: Object, mut gc: GcSco
}
Ok(Value::Undefined)
}

// 'readTextFile' function
fn read_text_file(
agent: &mut Agent,
Expand All @@ -48,6 +51,25 @@ pub fn initialize_global_object(agent: &mut Agent, global: Object, mut gc: GcSco
.map_err(|e| agent.throw_exception(ExceptionType::Error, e.to_string(), gc.nogc()))?;
Ok(String::from_string(agent, file, gc.nogc()).into_value())
}

// `detachArrayBuffer` function
fn detach_array_buffer(
agent: &mut Agent,
_this: Value,
args: ArgumentsList,
gc: GcScope<'_, '_>,
) -> JsResult<Value> {
let Value::ArrayBuffer(array_buffer) = args.get(0) else {
return Err(agent.throw_exception_with_static_message(
ExceptionType::Error,
"Cannot detach non ArrayBuffer argument",
gc.nogc(),
));
};
array_buffer::detach_array_buffer(agent, array_buffer, None);
Ok(Value::Undefined)
}

let function = create_builtin_function(
agent,
Behaviour::Regular(print),
Expand Down Expand Up @@ -91,6 +113,28 @@ pub fn initialize_global_object(agent: &mut Agent, global: Object, mut gc: GcSco
gc.reborrow(),
)
.unwrap();

let function = create_builtin_function(
agent,
Behaviour::Regular(detach_array_buffer),
BuiltinFunctionArgs::new(1, "detachArrayBuffer", agent.current_realm_id()),
gc.nogc(),
);
let property_key = PropertyKey::from_static_str(agent, "detachArrayBuffer", gc.nogc()).unbind();
global
.internal_define_own_property(
agent,
property_key,
PropertyDescriptor {
value: Some(function.into_value()),
writable: Some(true),
enumerable: Some(false),
configurable: Some(true),
..Default::default()
},
gc.reborrow(),
)
.unwrap();
}

/// Exit the program with parse errors.
Expand Down
1 change: 1 addition & 0 deletions nova_vm/src/ecmascript/builtins/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
},
};

pub use abstract_operations::detach_array_buffer;
pub(crate) use abstract_operations::{
allocate_array_buffer, array_buffer_byte_length, clone_array_buffer, get_value_from_buffer,
is_detached_buffer, is_fixed_length_array_buffer, set_value_in_buffer, Ordering,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ pub(crate) fn is_detached_buffer(agent: &Agent, array_buffer: ArrayBuffer) -> bo
/// The abstract operation DetachArrayBuffer takes argument *arrayBuffer* (an
/// ArrayBuffer) and optional argument *key* (anything) and returns either a
/// normal completion containing UNUSED or a throw completion.
pub(crate) fn detach_array_buffer(
array_buffer: ArrayBuffer,
agent: &mut Agent,
_key: Option<DetachKey>,
) {
pub fn detach_array_buffer(agent: &mut Agent, array_buffer: ArrayBuffer, _key: Option<DetachKey>) {
// 1. Assert: IsSharedArrayBuffer(arrayBuffer) is false.
// TODO: SharedArrayBuffer that we can even take here.
// 2. If key is not present, set key to undefined.
Expand Down
3 changes: 2 additions & 1 deletion tests/nova-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
// ES2020).

globalThis.$262 = {
global: globalThis
global: globalThis,
detachArrayBuffer: detachArrayBuffer,
};

0 comments on commit 722696d

Please sign in to comment.