mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 04:01:11 +08:00
fix: 修复 array 元素溢出时分配内存错误问题
This commit is contained in:
@@ -36,19 +36,26 @@ impl JsObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_array(shape_id: ShapeId, proto: JsValue, n_elements: usize) -> Self {
|
pub fn new_array(
|
||||||
assert!(
|
shape_id: ShapeId,
|
||||||
n_elements <= 4,
|
proto: JsValue,
|
||||||
"arrays with >4 inline elements not yet supported"
|
n_elements: usize,
|
||||||
);
|
bump: &bumpalo::Bump,
|
||||||
Self {
|
) -> Self {
|
||||||
|
let mut obj = Self {
|
||||||
header: (shape_id & 0x00FF_FFFF) | (1 << 30) | (1 << 29),
|
header: (shape_id & 0x00FF_FFFF) | (1 << 30) | (1 << 29),
|
||||||
inline: [JsValue::undefined(); 4],
|
inline: [JsValue::undefined(); 4],
|
||||||
overflow: std::ptr::null_mut(),
|
overflow: std::ptr::null_mut(),
|
||||||
proto,
|
proto,
|
||||||
generation: 1,
|
generation: 1,
|
||||||
_pad: 0,
|
_pad: 0,
|
||||||
|
};
|
||||||
|
let count = (n_elements as u8).min(31);
|
||||||
|
obj.set_prop_count(count);
|
||||||
|
if n_elements > 4 {
|
||||||
|
obj.alloc_overflow(bump, n_elements);
|
||||||
}
|
}
|
||||||
|
obj
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shape_id(&self) -> ShapeId {
|
pub fn shape_id(&self) -> ShapeId {
|
||||||
@@ -255,7 +262,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_array_flags() {
|
fn new_array_flags() {
|
||||||
let obj = JsObject::new_array(5, JsValue::null(), 3);
|
let bump = bumpalo::Bump::new();
|
||||||
|
let obj = JsObject::new_array(5, JsValue::null(), 3, &bump);
|
||||||
assert!(obj.is_array());
|
assert!(obj.is_array());
|
||||||
assert_eq!(obj.shape_id(), 5);
|
assert_eq!(obj.shape_id(), 5);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -544,10 +544,12 @@ impl Vm {
|
|||||||
OpCode::NEW_ARRAY => {
|
OpCode::NEW_ARRAY => {
|
||||||
let proto_ptr = &*self.object_prototype as *const JsObject as *mut JsObject;
|
let proto_ptr = &*self.object_prototype as *const JsObject as *mut JsObject;
|
||||||
let n = opcode::imm16(instr) as usize;
|
let n = opcode::imm16(instr) as usize;
|
||||||
|
let bump = self.epoch.bump();
|
||||||
let obj = self.epoch.alloc(JsObject::new_array(
|
let obj = self.epoch.alloc(JsObject::new_array(
|
||||||
EMPTY_SHAPE_ID,
|
EMPTY_SHAPE_ID,
|
||||||
JsValue::from_js_object(proto_ptr),
|
JsValue::from_js_object(proto_ptr),
|
||||||
n,
|
n,
|
||||||
|
bump,
|
||||||
));
|
));
|
||||||
self.regs[rd] = JsValue::object(obj as *mut u8);
|
self.regs[rd] = JsValue::object(obj as *mut u8);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user