Skip to content

Commit

Permalink
avm1/avm2: Add AvmString::from_static_wstr and use it
Browse files Browse the repository at this point in the history
Replaces the old `From<&'static WStr>` impl on AvmString
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Jan 19, 2025
1 parent dc5ca25 commit f3c0a78
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/src/avm1/globals/bevel_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ fn method<'gc>(
}
GET_TYPE => {
let type_: &WStr = this.type_().into();
AvmString::from(type_).into()
AvmString::from_static_wstr(activation.gc(), type_).into()
}
SET_TYPE => {
this.set_type(activation, args.get(0))?;
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/globals/displacement_map_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fn method<'gc>(
}
GET_MODE => {
let mode: &WStr = this.mode().into();
AvmString::from(mode).into()
AvmString::from_static_wstr(activation.gc(), mode).into()
}
SET_MODE => {
this.set_mode(activation, args.get(0))?;
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/globals/gradient_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ fn method<'gc>(
}
GET_TYPE => {
let type_: &WStr = this.type_().into();
AvmString::from(type_).into()
AvmString::from_static_wstr(activation.gc(), type_).into()
}
SET_TYPE => {
this.set_type(activation, args.get(0))?;
Expand Down
8 changes: 6 additions & 2 deletions core/src/avm2/globals/flash/display/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::avm2::globals::flash::display::display_object::initialize_for_allocat
use crate::avm2::object::{BitmapDataObject, ClassObject, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::string::AvmString;
use ruffle_render::bitmap::PixelSnapping;
use ruffle_wstr::WStr;

Expand Down Expand Up @@ -156,15 +157,18 @@ pub fn set_bitmap_data<'gc>(

/// Stub `Bitmap.pixelSnapping`'s getter
pub fn get_pixel_snapping<'gc>(
_activation: &mut Activation<'_, 'gc>,
activation: &mut Activation<'_, 'gc>,
this: Value<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let this = this.as_object().unwrap();

if let Some(bitmap) = this.as_display_object().and_then(|dobj| dobj.as_bitmap()) {
let value: &WStr = bitmap.pixel_snapping().into();
return Ok(Value::String(value.into()));
return Ok(Value::String(AvmString::from_static_wstr(
activation.gc(),
value,
)));
}
Ok(Value::Undefined)
}
Expand Down
17 changes: 8 additions & 9 deletions core/src/string/avm_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ impl<'gc> AvmString<'gc> {
}
}

pub fn from_static_wstr(gc_context: &Mutation<'gc>, string: &'static WStr) -> Self {
let repr = AvmStringRepr::from_raw_static(string, false);

Self {
source: Source::Managed(Gc::new(gc_context, repr)),
}
}

pub fn substring(mc: &Mutation<'gc>, string: AvmString<'gc>, start: usize, end: usize) -> Self {
match string.source {
Source::Managed(repr) => {
Expand Down Expand Up @@ -175,15 +183,6 @@ impl From<&'static str> for AvmString<'_> {
}
}

impl From<&'static WStr> for AvmString<'_> {
#[inline]
fn from(str: &'static WStr) -> Self {
Self {
source: Source::Static(str),
}
}
}

impl Deref for AvmString<'_> {
type Target = WStr;
#[inline]
Expand Down

0 comments on commit f3c0a78

Please sign in to comment.