Skip to content

Commit

Permalink
avm1/2 use plain RefCell for TextFormat objects
Browse files Browse the repository at this point in the history
`TextFormat` only contains static data and doesn't need write barriers
  • Loading branch information
moulins authored and torokati44 committed Aug 26, 2023
1 parent c27728b commit ecb5036
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 71 deletions.
8 changes: 4 additions & 4 deletions core/src/avm1/globals/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::display_object::{AutoSizeMode, EditText, TDisplayObject, TextSelectio
use crate::font::round_down_to_pixel;
use crate::html::TextFormat;
use crate::string::{AvmString, WStr};
use gc_arena::GcCell;
use gc_arena::Gc;
use swf::Color;

macro_rules! tf_method {
Expand Down Expand Up @@ -137,7 +137,7 @@ fn new_text_format<'gc>(
let object = ScriptObject::new(activation.context.gc_context, Some(proto));
object.set_native(
activation.context.gc_context,
NativeObject::TextFormat(GcCell::new(activation.context.gc_context, text_format)),
NativeObject::TextFormat(Gc::new(activation.context.gc_context, text_format.into())),
);
object
}
Expand All @@ -158,7 +158,7 @@ fn set_new_text_format<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
if let [Value::Object(text_format), ..] = args {
if let NativeObject::TextFormat(text_format) = text_format.native() {
text_field.set_new_text_format(text_format.read().clone(), &mut activation.context);
text_field.set_new_text_format(text_format.borrow().clone(), &mut activation.context);
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ fn set_text_format<'gc>(
text_field.set_text_format(
begin_index,
end_index,
text_format.read().clone(),
text_format.borrow().clone(),
&mut activation.context,
);
}
Expand Down
28 changes: 12 additions & 16 deletions core/src/avm1/globals/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::display_object::{AutoSizeMode, EditText, TDisplayObject};
use crate::ecma_conversions::round_to_even;
use crate::html::TextFormat;
use crate::string::{AvmString, WStr};
use gc_arena::GcCell;
use gc_arena::Gc;

macro_rules! getter {
($name:ident) => {
|activation, this, _args| {
if let NativeObject::TextFormat(text_format) = this.native() {
return Ok($name(activation, &text_format.read()));
return Ok($name(activation, &text_format.borrow()));
}
Ok(Value::Undefined)
}
Expand All @@ -27,11 +27,7 @@ macro_rules! setter {
|activation, this, args| {
if let NativeObject::TextFormat(text_format) = this.native() {
let value = args.get(0).unwrap_or(&Value::Undefined);
$name(
activation,
&mut text_format.write(activation.context.gc_context),
value,
)?;
$name(activation, &mut text_format.borrow_mut(), value)?;
}
Ok(Value::Undefined)
}
Expand All @@ -42,7 +38,7 @@ macro_rules! method {
($name:ident) => {
|activation, this, args| {
if let NativeObject::TextFormat(text_format) = this.native() {
return $name(activation, &text_format.read(), args);
return $name(activation, &text_format.borrow(), args);
}
Ok(Value::Undefined)
}
Expand Down Expand Up @@ -74,7 +70,7 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {

fn font<'gc>(activation: &mut Activation<'_, 'gc>, text_format: &TextFormat) -> Value<'gc> {
text_format.font.as_ref().map_or(Value::Null, |font| {
AvmString::new(activation.context.gc_context, font.clone()).into()
AvmString::new(activation.gc(), font.clone()).into()
})
}

Expand Down Expand Up @@ -130,7 +126,7 @@ fn set_color<'gc>(

fn url<'gc>(activation: &mut Activation<'_, 'gc>, text_format: &TextFormat) -> Value<'gc> {
text_format.url.as_ref().map_or(Value::Null, |url| {
AvmString::new(activation.context.gc_context, url.clone()).into()
AvmString::new(activation.gc(), url.clone()).into()
})
}

Expand All @@ -148,7 +144,7 @@ fn set_url<'gc>(

fn target<'gc>(activation: &mut Activation<'_, 'gc>, text_format: &TextFormat) -> Value<'gc> {
text_format.target.as_ref().map_or(Value::Null, |target| {
AvmString::new(activation.context.gc_context, target.clone()).into()
AvmString::new(activation.gc(), target.clone()).into()
})
}

Expand Down Expand Up @@ -366,7 +362,7 @@ fn tab_stops<'gc>(activation: &mut Activation<'_, 'gc>, text_format: &TextFormat
.as_ref()
.map_or(Value::Null, |tab_stops| {
ArrayObject::new(
activation.context.gc_context,
activation.gc(),
activation.context.avm1.prototypes().array,
tab_stops.iter().map(|&x| x.into()),
)
Expand Down Expand Up @@ -500,7 +496,7 @@ fn get_text_extent<'gc>(
temp_edittext.set_new_text_format(text_format.clone(), &mut activation.context);
temp_edittext.set_text(&text, &mut activation.context);

let result = ScriptObject::new(activation.context.gc_context, None);
let result = ScriptObject::new(activation.gc(), None);
let metrics = temp_edittext
.layout_metrics(None)
.expect("All text boxes should have at least one line at all times");
Expand Down Expand Up @@ -608,8 +604,8 @@ pub fn constructor<'gc>(
args.get(12).unwrap_or(&Value::Undefined),
)?;
this.set_native(
activation.context.gc_context,
NativeObject::TextFormat(GcCell::new(activation.context.gc_context, text_format)),
activation.gc(),
NativeObject::TextFormat(Gc::new(activation.gc(), text_format.into())),
);
Ok(this.into())
}
Expand All @@ -620,7 +616,7 @@ pub fn create_proto<'gc>(
proto: Object<'gc>,
fn_proto: Object<'gc>,
) -> Object<'gc> {
let object = ScriptObject::new(context.gc_context, Some(proto));
let object = ScriptObject::new(context.gc(), Some(proto));
define_properties_on(PROTO_DECLS, context, object, fn_proto);
object.into()
}
4 changes: 2 additions & 2 deletions core/src/avm1/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::string::AvmString;
use crate::xml::XmlNode;
use gc_arena::{Collect, Gc, GcCell, Mutation};
use ruffle_macros::enum_trait_object;
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::fmt::Debug;

pub mod array_object;
Expand All @@ -55,7 +55,7 @@ pub enum NativeObject<'gc> {
GradientGlowFilter(GradientFilter<'gc>),
ColorTransform(GcCell<'gc, ColorTransformObject>),
Transform(TransformObject<'gc>),
TextFormat(GcCell<'gc, TextFormat>),
TextFormat(Gc<'gc, RefCell<TextFormat>>),
NetStream(NetStream<'gc>),
BitmapData(BitmapDataWrapper<'gc>),
Xml(Xml<'gc>),
Expand Down
46 changes: 23 additions & 23 deletions core/src/avm2/globals/flash/text/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn set_align<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
let value = match value {
Value::Undefined | Value::Null => {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn set_block_indent<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.block_indent = match value {
Value::Undefined | Value::Null => None,
Expand All @@ -107,11 +107,11 @@ pub fn get_bold<'gc>(
}

pub fn set_bold<'gc>(
activation: &mut Activation<'_, 'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.bold = match value {
Value::Undefined | Value::Null => None,
Expand All @@ -138,11 +138,11 @@ pub fn get_bullet<'gc>(
}

pub fn set_bullet<'gc>(
activation: &mut Activation<'_, 'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.bullet = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn set_color<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.color = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn set_font<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.font = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -252,7 +252,7 @@ pub fn set_indent<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.indent = match value {
Value::Undefined | Value::Null => None,
Expand All @@ -279,11 +279,11 @@ pub fn get_italic<'gc>(
}

pub fn set_italic<'gc>(
activation: &mut Activation<'_, 'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.italic = match value {
Value::Undefined | Value::Null => None,
Expand All @@ -310,11 +310,11 @@ pub fn get_kerning<'gc>(
}

pub fn set_kerning<'gc>(
activation: &mut Activation<'_, 'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.kerning = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -345,7 +345,7 @@ pub fn set_leading<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.leading = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -376,7 +376,7 @@ pub fn set_left_margin<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.left_margin = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -407,7 +407,7 @@ pub fn set_letter_spacing<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.letter_spacing = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -438,7 +438,7 @@ pub fn set_right_margin<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.right_margin = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -469,7 +469,7 @@ pub fn set_size<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.size = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -503,7 +503,7 @@ pub fn set_tab_stops<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.tab_stops = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -547,7 +547,7 @@ pub fn set_target<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.target = match value {
Value::Undefined | Value::Null => None,
Expand All @@ -574,11 +574,11 @@ pub fn get_underline<'gc>(
}

pub fn set_underline<'gc>(
activation: &mut Activation<'_, 'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.underline = match value {
Value::Undefined | Value::Null => None,
Expand Down Expand Up @@ -608,7 +608,7 @@ pub fn set_url<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(mut text_format) = this.as_text_format_mut(activation.context.gc_context) {
if let Some(mut text_format) = this.as_text_format_mut() {
let value = args.get(0).unwrap_or(&Value::Undefined);
text_format.url = match value {
Value::Undefined | Value::Null => None,
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
}

/// Unwrap this object as a mutable text format.
fn as_text_format_mut(&self, _mc: &Mutation<'gc>) -> Option<RefMut<TextFormat>> {
fn as_text_format_mut(&self) -> Option<RefMut<TextFormat>> {
None
}

Expand Down Expand Up @@ -1404,7 +1404,7 @@ impl<'gc> Object<'gc> {
Self::DateObject(o) => WeakObject::DateObject(DateObjectWeak(Gc::downgrade(o.0))),
Self::DictionaryObject(o) => WeakObject::DictionaryObject(DictionaryObjectWeak(GcCell::downgrade(o.0))),
Self::QNameObject(o) => WeakObject::QNameObject(QNameObjectWeak(GcCell::downgrade(o.0))),
Self::TextFormatObject(o) => WeakObject::TextFormatObject(TextFormatObjectWeak(GcCell::downgrade(o.0))),
Self::TextFormatObject(o) => WeakObject::TextFormatObject(TextFormatObjectWeak(Gc::downgrade(o.0))),
Self::ProxyObject(o) => WeakObject::ProxyObject(ProxyObjectWeak(GcCell::downgrade(o.0))),
Self::ErrorObject(o) => WeakObject::ErrorObject(ErrorObjectWeak(GcCell::downgrade(o.0))),
Self::Stage3DObject(o) => WeakObject::Stage3DObject(Stage3DObjectWeak(Gc::downgrade(o.0))),
Expand Down
Loading

0 comments on commit ecb5036

Please sign in to comment.