Skip to content

Commit 5ea7a2b

Browse files
author
Your Name
committed
refactor: rename CreateMethodProperty to DefineMethodProperty and use it in VM opcodes
Address PR review feedback: - Rename create_method_property to define_method_property per ECMAScript 2026 spec (10.2.8) - Update return type to JsResult<()> since the spec returns 'unused' - Use define_method_property in DefineClassMethodByName, DefineClassStaticMethodByName, and DefineClassMethodByValue VM opcodes instead of manual PropertyDescriptor construction - Remove unrelated TypedArray wrapper methods (entries, keys, values, iterator, to_string) and associated tests from jstypedarray.rs - Revert pub(crate) visibility changes in builtin.rs - Move CopyDataProperties from jsobject.rs to operations.rs
1 parent ce8836a commit 5ea7a2b

File tree

4 files changed

+27
-154
lines changed

4 files changed

+27
-154
lines changed

core/engine/src/builtins/typed_array/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl BuiltinTypedArray {
624624
/// - [ECMAScript reference][spec]
625625
///
626626
/// [spec]: https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries
627-
pub(crate) fn entries(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
627+
fn entries(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
628628
// 1. Let O be the this value.
629629
// 2. Perform ? ValidateTypedArray(O, seq-cst).
630630
let (ta, _) = TypedArray::validate(this, Ordering::SeqCst)?;
@@ -2546,7 +2546,7 @@ impl BuiltinTypedArray {
25462546
/// - [ECMAScript reference][spec]
25472547
///
25482548
/// [spec]: https://tc39.es/ecma262/#sec-%typedarray%.prototype.values
2549-
pub(crate) fn values(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
2549+
fn values(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
25502550
// 1. Let O be the this value.
25512551
// 2. Perform ? ValidateTypedArray(O, seq-cst).
25522552
let (ta, _) = TypedArray::validate(this, Ordering::SeqCst)?;

core/engine/src/object/builtins/jstypedarray.rs

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -877,41 +877,6 @@ impl JsTypedArray {
877877
})
878878
}
879879

880-
/// Calls `TypedArray.prototype.entries()`.
881-
#[inline]
882-
pub fn entries(&self, context: &mut Context) -> JsResult<JsValue> {
883-
BuiltinTypedArray::entries(&self.inner.clone().into(), &[], context)
884-
}
885-
886-
/// Calls `TypedArray.prototype.keys()`.
887-
#[inline]
888-
pub fn keys(&self, context: &mut Context) -> JsResult<JsValue> {
889-
BuiltinTypedArray::keys(&self.inner.clone().into(), &[], context)
890-
}
891-
892-
/// Calls `TypedArray.prototype.values()`.
893-
#[inline]
894-
pub fn values(&self, context: &mut Context) -> JsResult<JsValue> {
895-
BuiltinTypedArray::values(&self.inner.clone().into(), &[], context)
896-
}
897-
898-
/// Calls `TypedArray.prototype[@@iterator]()`.
899-
#[inline]
900-
pub fn iterator(&self, context: &mut Context) -> JsResult<JsValue> {
901-
BuiltinTypedArray::values(&self.inner.clone().into(), &[], context)
902-
}
903-
904-
/// Calls `TypedArray.prototype.toString()`.
905-
#[inline]
906-
pub fn to_string(&self, context: &mut Context) -> JsResult<JsString> {
907-
// TypedArray.prototype.toString is the same as Array.prototype.toString
908-
let result = crate::builtins::Array::to_string(&self.inner.clone().into(), &[], context)?;
909-
result
910-
.as_string()
911-
.js_expect("Array.prototype.toString always returns string")
912-
.map_err(Into::into)
913-
}
914-
915880
/// It is a getter that returns the same string as the typed array constructor's name.
916881
/// It returns `Ok(JsValue::Undefined)` if the this value is not one of the typed array subclasses.
917882
///
@@ -1383,87 +1348,3 @@ fn typed_iterators_f32() {
13831348
let vec2 = array.iter(context).collect::<Vec<_>>();
13841349
assert_eq!(vec, vec2);
13851350
}
1386-
1387-
#[test]
1388-
fn typed_array_to_string() {
1389-
let context = &mut Context::default();
1390-
let vec = vec![1u8, 2, 3];
1391-
let array = JsUint8Array::from_iter(vec, context).unwrap();
1392-
assert_eq!(array.to_string(context).unwrap(), crate::js_string!("1,2,3"));
1393-
}
1394-
1395-
#[test]
1396-
fn typed_array_entries() {
1397-
let context = &mut Context::default();
1398-
let vec = vec![1u8, 2];
1399-
let array = JsUint8Array::from_iter(vec, context).unwrap();
1400-
let entries = array.entries(context).unwrap();
1401-
let mut entries_vec = Vec::new();
1402-
let next_str = crate::js_string!("next");
1403-
loop {
1404-
let next_fn = entries.as_object().unwrap().get(next_str.clone(), context).unwrap();
1405-
let result = next_fn.as_object().unwrap().call(&entries, &[], context).unwrap();
1406-
if result.as_object().unwrap().get(crate::js_string!("done"), context).unwrap().to_boolean() {
1407-
break;
1408-
}
1409-
entries_vec.push(result.as_object().unwrap().get(crate::js_string!("value"), context).unwrap());
1410-
}
1411-
assert_eq!(entries_vec.len(), 2);
1412-
}
1413-
1414-
#[test]
1415-
fn typed_array_keys() {
1416-
let context = &mut Context::default();
1417-
let vec = vec![1u8, 2];
1418-
let array = JsUint8Array::from_iter(vec, context).unwrap();
1419-
let keys = array.keys(context).unwrap();
1420-
let mut keys_vec = Vec::new();
1421-
let next_str = crate::js_string!("next");
1422-
loop {
1423-
let next_fn = keys.as_object().unwrap().get(next_str.clone(), context).unwrap();
1424-
let result = next_fn.as_object().unwrap().call(&keys, &[], context).unwrap();
1425-
if result.as_object().unwrap().get(crate::js_string!("done"), context).unwrap().to_boolean() {
1426-
break;
1427-
}
1428-
keys_vec.push(result.as_object().unwrap().get(crate::js_string!("value"), context).unwrap());
1429-
}
1430-
assert_eq!(keys_vec, vec![JsValue::new(0), JsValue::new(1)]);
1431-
}
1432-
1433-
#[test]
1434-
fn typed_array_values() {
1435-
let context = &mut Context::default();
1436-
let vec = vec![1u8, 2];
1437-
let array = JsUint8Array::from_iter(vec, context).unwrap();
1438-
let values = array.values(context).unwrap();
1439-
let mut values_vec = Vec::new();
1440-
let next_str = crate::js_string!("next");
1441-
loop {
1442-
let next_fn = values.as_object().unwrap().get(next_str.clone(), context).unwrap();
1443-
let result = next_fn.as_object().unwrap().call(&values, &[], context).unwrap();
1444-
if result.as_object().unwrap().get(crate::js_string!("done"), context).unwrap().to_boolean() {
1445-
break;
1446-
}
1447-
values_vec.push(result.as_object().unwrap().get(crate::js_string!("value"), context).unwrap());
1448-
}
1449-
assert_eq!(values_vec, vec![JsValue::new(1), JsValue::new(2)]);
1450-
}
1451-
1452-
#[test]
1453-
fn typed_array_iterator() {
1454-
let context = &mut Context::default();
1455-
let vec = vec![1u8, 2];
1456-
let array = JsUint8Array::from_iter(vec, context).unwrap();
1457-
let values = array.iterator(context).unwrap();
1458-
let mut values_vec = Vec::new();
1459-
let next_str = crate::js_string!("next");
1460-
loop {
1461-
let next_fn = values.as_object().unwrap().get(next_str.clone(), context).unwrap();
1462-
let result = next_fn.as_object().unwrap().call(&values, &[], context).unwrap();
1463-
if result.as_object().unwrap().get(crate::js_string!("done"), context).unwrap().to_boolean() {
1464-
break;
1465-
}
1466-
values_vec.push(result.as_object().unwrap().get(crate::js_string!("value"), context).unwrap());
1467-
}
1468-
assert_eq!(values_vec, vec![JsValue::new(1), JsValue::new(2)]);
1469-
}

core/engine/src/object/operations.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,33 +177,40 @@ impl JsObject {
177177
self.__define_own_property__(&key.into(), new_desc.into(), context)
178178
}
179179

180-
/// `7.3.5 CreateMethodProperty ( O, P, V )`
180+
/// `10.2.8 DefineMethodProperty ( homeObject, key, closure, enumerable )`
181+
///
182+
/// Defines a method property on an object with the specified attributes.
181183
///
182184
/// More information:
183185
/// - [ECMAScript reference][spec]
184186
///
185-
/// [spec]: https://tc39.es/ecma262/#sec-createmethodproperty
186-
pub(crate) fn create_method_property<K, V>(
187+
/// [spec]: https://tc39.es/ecma262/#sec-definemethodproperty
188+
pub(crate) fn define_method_property<K, V>(
187189
&self,
188190
key: K,
189191
value: V,
190192
context: &mut InternalMethodPropertyContext<'_>,
191-
) -> JsResult<bool>
193+
) -> JsResult<()>
192194
where
193195
K: Into<PropertyKey>,
194196
V: Into<JsValue>,
195197
{
196-
// 1. Assert: Type(O) is Object.
197-
// 2. Assert: IsPropertyKey(P) is true.
198-
// 3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
198+
// 1. Assert: homeObject is an ordinary, extensible object with no non-configurable properties.
199+
// 2. If key is a Private Name, then
200+
// a. Return PrivateElement { [[Key]]: key, [[Kind]]: method, [[Value]]: closure }.
201+
// 3. Else,
202+
// a. Let desc be the PropertyDescriptor { [[Value]]: closure, [[Writable]]: true, [[Enumerable]]: enumerable, [[Configurable]]: true }.
199203
let new_desc = PropertyDescriptor::builder()
200204
.value(value)
201205
.writable(true)
202206
.enumerable(false)
203207
.configurable(true);
204208

205-
// 4. Return ! O.[[DefineOwnProperty]](P, newDesc).
206-
self.__define_own_property__(&key.into(), new_desc.into(), context)
209+
// b. Perform ! DefinePropertyOrThrow(homeObject, key, desc).
210+
self.__define_own_property__(&key.into(), new_desc.into(), context)?;
211+
212+
// c. Return unused.
213+
Ok(())
207214
}
208215

209216
/// Create data property or throw

core/engine/src/vm/opcode/define/class/method.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,9 @@ impl DefineClassStaticMethodByName {
3939
.set_home_object(class.clone());
4040
}
4141

42-
class.__define_own_property__(
43-
&key,
44-
PropertyDescriptor::builder()
45-
.value(function.clone())
46-
.writable(true)
47-
.enumerable(false)
48-
.configurable(true)
49-
.build(),
42+
class.define_method_property(
43+
key,
44+
function.clone(),
5045
&mut InternalMethodPropertyContext::new(context),
5146
)?;
5247
Ok(())
@@ -92,14 +87,9 @@ impl DefineClassMethodByName {
9287
.set_home_object(class_proto.clone());
9388
}
9489

95-
class_proto.__define_own_property__(
96-
&key,
97-
PropertyDescriptor::builder()
98-
.value(function.clone())
99-
.writable(true)
100-
.enumerable(false)
101-
.configurable(true)
102-
.build(),
90+
class_proto.define_method_property(
91+
key,
92+
function.clone(),
10393
&mut InternalMethodPropertyContext::new(context),
10494
)?;
10595
Ok(())
@@ -194,14 +184,9 @@ impl DefineClassMethodByValue {
194184
.set_home_object(class_proto.clone());
195185
}
196186

197-
class_proto.__define_own_property__(
198-
&key,
199-
PropertyDescriptor::builder()
200-
.value(function.clone())
201-
.writable(true)
202-
.enumerable(false)
203-
.configurable(true)
204-
.build(),
187+
class_proto.define_method_property(
188+
key,
189+
function.clone(),
205190
&mut InternalMethodPropertyContext::new(context),
206191
)?;
207192
Ok(())

0 commit comments

Comments
 (0)