Skip to content

Commit

Permalink
avm2: Switch ScriptObjectData.slots from RefLock<Vec<Value>> to `…
Browse files Browse the repository at this point in the history
…Vec<Lock<Value>>`
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Aug 31, 2024
1 parent 52e8534 commit 7214440
Show file tree
Hide file tree
Showing 25 changed files with 21 additions and 67 deletions.
1 change: 0 additions & 1 deletion core/src/avm2/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ pub fn load_player_globals<'gc>(
// operates on the global object until it gets its true vtable, so this should
// be fine.
let globals = ScriptObject::custom_object(mc, global_classdef, None, global_classdef.vtable());
globals.install_instance_slots(mc);

let scope = ScopeChain::new(domain);
let gs = scope.chain(mc, &[Scope::new(globals)]);
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,6 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
base.install_bound_method(mc, disp_id, function)
}

#[no_dynamic]
fn install_instance_slots(&self, mc: &Mutation<'gc>) {
self.base().install_instance_slots(mc);
}

/// Call the object.
fn call(
self,
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/array_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ impl<'gc> ArrayObject<'gc> {
},
))
.into();
instance.install_instance_slots(activation.context.gc_context);

class.call_native_init(instance.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/bitmapdata_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl<'gc> BitmapDataObject<'gc> {
.into();

bitmap_data.init_object2(activation.context.gc_context, instance);
instance.install_instance_slots(activation.context.gc_context);

// We call the custom BitmapData class with width and height...
// but, it always seems to be 1 in Flash Player when constructed from timeline?
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/bytearray_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ impl<'gc> ByteArrayObject<'gc> {
},
))
.into();
instance.install_instance_slots(activation.context.gc_context);

class.call_native_init(instance.into(), &[], activation)?;

Expand Down
3 changes: 0 additions & 3 deletions core/src/avm2/object/class_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ impl<'gc> ClassObject<'gc> {
);

self.set_vtable(activation.context.gc_context, class_vtable);
self.base().install_instance_slots(activation.gc());

self.run_class_initializer(activation)?;

Expand Down Expand Up @@ -802,8 +801,6 @@ impl<'gc> TObject<'gc> for ClassObject<'gc> {

let instance = instance_allocator(self, activation)?;

instance.install_instance_slots(activation.context.gc_context);

self.call_init(instance.into(), arguments, activation)?;

Ok(instance)
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/context3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl<'gc> Context3DObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

class.call_native_init(this.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/date_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl<'gc> DateObject<'gc> {
},
))
.into();
instance.install_instance_slots(activation.context.gc_context);

class.call_native_init(instance.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/domain_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl<'gc> DomainObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

// Note - we do *not* call the normal constructor, since that
// creates a new domain using the system domain as a parent.
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/event_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ impl<'gc> EventObject<'gc> {
},
));

// not needed, as base Event has no instance slots.
// yes, this is flimsy. Could call this if install_instance_slots only took gc_context.
// event_object.install_instance_slots(activation);

event_object.into()
}

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/index_buffer_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl<'gc> IndexBuffer3DObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

class.call_native_init(this.into(), &[], activation)?;

Expand Down
2 changes: 0 additions & 2 deletions core/src/avm2/object/loaderinfo_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ impl<'gc> LoaderInfoObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

class.call_native_init(this.into(), &[], activation)?;

Expand Down Expand Up @@ -214,7 +213,6 @@ impl<'gc> LoaderInfoObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

class.call_native_init(this.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/namespace_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ impl<'gc> NamespaceObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

class.call_native_init(this.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/primitive_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ impl<'gc> PrimitiveObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

//We explicitly DO NOT CALL the native initializers of primitives here.
//If we did so, then those primitive initializers' method types would
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/program_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl<'gc> Program3DObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

class.call_native_init(this.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/qname_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl<'gc> QNameObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

Ok(this)
}
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/regexp_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl<'gc> RegExpObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.context.gc_context);

class.call_native_init(this.into(), &[], activation)?;

Expand Down
52 changes: 21 additions & 31 deletions core/src/avm2/object/script_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::avm2::vtable::VTable;
use crate::avm2::Multiname;
use crate::avm2::{Error, QName};
use crate::string::AvmString;
use gc_arena::barrier::unlock;
use gc_arena::barrier::{unlock, Write};
use gc_arena::{
lock::{Lock, RefLock},
Collect, Gc, GcWeak, Mutation,
Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct ScriptObjectData<'gc> {
values: RefLock<DynamicMap<DynamicKey<'gc>, Value<'gc>>>,

/// Slots stored on this object.
slots: RefLock<Vec<Value<'gc>>>,
slots: Vec<Lock<Value<'gc>>>,

/// Methods stored on this object.
bound_methods: RefLock<Vec<Option<FunctionObject<'gc>>>>,
Expand Down Expand Up @@ -133,8 +133,6 @@ impl<'gc> ScriptObject<'gc> {
),
));

base.install_instance_slots(mc);

ScriptObject(base.0).into()
}
}
Expand All @@ -160,9 +158,18 @@ impl<'gc> ScriptObjectData<'gc> {
proto: Option<Object<'gc>>,
vtable: VTable<'gc>,
) -> Self {
let default_slots = vtable.default_slots();
let mut slots = vec![Lock::new(Value::Undefined); default_slots.len()];

for (i, value) in default_slots.iter().enumerate() {
if let Some(value) = value {
slots[i] = Lock::new(*value);
}
}

ScriptObjectData {
values: RefLock::new(Default::default()),
slots: RefLock::new(Vec::new()),
slots,
bound_methods: RefLock::new(Vec::new()),
proto: Lock::new(proto),
instance_class,
Expand All @@ -187,14 +194,6 @@ impl<'gc> ScriptObjectWrapper<'gc> {
unlock!(Gc::write(mc, self.0), ScriptObjectData, values).borrow_mut()
}

fn slots(&self) -> Ref<Vec<Value<'gc>>> {
self.0.slots.borrow()
}

fn slots_mut(&self, mc: &Mutation<'gc>) -> RefMut<Vec<Value<'gc>>> {
unlock!(Gc::write(mc, self.0), ScriptObjectData, slots).borrow_mut()
}

fn bound_methods(&self) -> Ref<Vec<Option<FunctionObject<'gc>>>> {
self.0.bound_methods.borrow()
}
Expand Down Expand Up @@ -318,9 +317,11 @@ impl<'gc> ScriptObjectWrapper<'gc> {
}

pub fn get_slot(&self, id: u32) -> Result<Value<'gc>, Error<'gc>> {
self.slots()
self.0
.slots
.get(id as usize)
.cloned()
.map(|s| s.get())
.ok_or_else(|| format!("Slot index {id} out of bounds!").into())
}

Expand All @@ -331,29 +332,18 @@ impl<'gc> ScriptObjectWrapper<'gc> {
value: Value<'gc>,
mc: &Mutation<'gc>,
) -> Result<(), Error<'gc>> {
if let Some(slot) = self.slots_mut(mc).get_mut(id as usize) {
*slot = value;
if let Some(slot) = self.0.slots.get(id as usize) {
Gc::write(mc, self.0);
// SAFETY: We just triggered a write barrier on the Gc.
let slot_write = unsafe { Write::assume(slot) };
slot_write.unlock().set(value);

Ok(())
} else {
Err(format!("Slot index {id} out of bounds!").into())
}
}

pub fn install_instance_slots(&self, mc: &Mutation<'gc>) {
use std::ops::Deref;
let vtable = self.vtable();
let default_slots = vtable.default_slots();
let mut slots = self.slots_mut(mc);

for value in default_slots.deref() {
if let Some(value) = value {
slots.push(*value);
} else {
slots.push(Value::Undefined)
}
}
}

/// Retrieve a bound method from the method table.
pub fn get_bound_method(&self, id: u32) -> Option<FunctionObject<'gc>> {
self.bound_methods().get(id as usize).and_then(|v| *v)
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/soundchannel_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl<'gc> SoundChannelObject<'gc> {
position: Cell::new(0.0),
},
));
sound_object.install_instance_slots(activation.context.gc_context);

class.call_native_init(Value::Object(sound_object.into()), &[], activation)?;

Expand Down
2 changes: 0 additions & 2 deletions core/src/avm2/object/stage_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl<'gc> StageObject<'gc> {
display_object,
},
));
instance.install_instance_slots(activation.context.gc_context);

Ok(instance)
}
Expand Down Expand Up @@ -105,7 +104,6 @@ impl<'gc> StageObject<'gc> {
display_object,
},
));
this.install_instance_slots(activation.context.gc_context);

// note: for Graphics, there's no need to call init.

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/textformat_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ impl<'gc> TextFormatObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

Ok(this)
}
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/texture_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl<'gc> TextureObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

class.call_native_init(this.into(), &[], activation)?;

Expand Down
2 changes: 0 additions & 2 deletions core/src/avm2/object/vector_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ impl<'gc> VectorObject<'gc> {
))
.into();

object.install_instance_slots(activation.context.gc_context);

Ok(object)
}
}
Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/object/vertex_buffer_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl<'gc> VertexBuffer3DObject<'gc> {
},
))
.into();
this.install_instance_slots(activation.gc());

class.call_native_init(this.into(), &[], activation)?;

Expand Down
1 change: 0 additions & 1 deletion core/src/avm2/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ impl<'gc> Script<'gc> {
object_class.proto(), // Just use Object's prototype
global_obj_vtable,
);
global_object.install_instance_slots(mc);

self.0.write(mc).globals = Some(global_object);

Expand Down

0 comments on commit 7214440

Please sign in to comment.