Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Fix EditText and Graphic linkage #18770

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions core/src/avm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
use std::rc::Rc;

use crate::avm2::class::{AllocatorFn, CustomConstructorFn};
use crate::avm2::error::make_error_1107;
use crate::avm2::error::{make_error_1014, make_error_1107, type_error, Error1014Type};
use crate::avm2::globals::{
init_builtin_system_classes, init_native_system_classes, SystemClassDefs, SystemClasses,
};
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::scope::ScopeChain;
use crate::avm2::script::{Script, TranslationUnit};
use crate::character::Character;
use crate::context::UpdateContext;
use crate::display_object::{DisplayObject, DisplayObjectWeak, TDisplayObject};
use crate::display_object::{DisplayObject, DisplayObjectWeak, MovieClip, TDisplayObject};
use crate::string::{AvmString, StringContext};
use crate::tag_utils::SwfMovie;
use crate::PlayerRuntime;
Expand Down Expand Up @@ -579,6 +580,70 @@ impl<'gc> Avm2<'gc> {
Ok(())
}

pub fn lookup_class_for_character(
activation: &mut Activation<'_, 'gc>,
movie_clip: MovieClip<'gc>,
domain: Domain<'gc>,
name: QName<'gc>,
id: u16,
) -> Result<ClassObject<'gc>, Error<'gc>> {
let movie = movie_clip.movie().clone();

let class_object = domain
.get_defined_value(activation, name)?
.as_object()
.and_then(|o| o.as_class_object())
.ok_or_else(|| {
make_error_1014(
activation,
Error1014Type::ReferenceError,
name.to_qualified_name(activation.gc()),
)
})?;

let class = class_object.inner_class_definition();

let library = activation.context.library.library_for_movie_mut(movie);
let character = library.character_by_id(id);

if let Some(character) = character {
if matches!(
character,
Character::EditText(_)
| Character::Graphic(_)
| Character::MovieClip(_)
| Character::Avm2Button(_)
) {
// The class must extend DisplayObject to ensure that events
// can properly be dispatched to them
if !class.has_class_in_chain(activation.avm2().class_defs().display_object) {
return Err(Error::AvmError(type_error(
activation,
&format!("Error #2022: Class {}$ must inherit from DisplayObject to link to a symbol.", name.to_qualified_name(activation.gc())),
2022,
)?));
}
}
} else if movie_clip.avm2_class().is_none() {
// If this ID doesn't correspond to any character, and the MC that
Lord-McSweeney marked this conversation as resolved.
Show resolved Hide resolved
// we're processing doesn't have an AVM2 class set, then this
// ClassObject is going to be the class of the MC. Ensure it
// subclasses Sprite.
if !class.has_class_in_chain(activation.avm2().class_defs().sprite) {
return Err(Error::AvmError(type_error(
activation,
&format!(
"Error #2023: Class {}$ must inherit from Sprite to link to the root.",
name.to_qualified_name(activation.gc())
),
2023,
)?));
}
}

Ok(class_object)
}

/// Load an ABC file embedded in a `DoAbc` or `DoAbc2` tag.
pub fn do_abc(
context: &mut UpdateContext<'gc>,
Expand Down
14 changes: 11 additions & 3 deletions core/src/avm2/class.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! AVM2 classes

use crate::avm2::activation::Activation;
use crate::avm2::error::make_error_1014;
use crate::avm2::error::{make_error_1014, Error1014Type};
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{scriptobject_allocator, ClassObject, Object};
use crate::avm2::script::TranslationUnit;
Expand Down Expand Up @@ -448,7 +448,11 @@ impl<'gc> Class<'gc> {
.domain()
.get_class(activation.context, &multiname)
.ok_or_else(|| {
make_error_1014(activation, multiname.to_qualified_name(activation.gc()))
make_error_1014(
activation,
Error1014Type::VerifyError,
multiname.to_qualified_name(activation.gc()),
)
})?,
)
};
Expand All @@ -468,7 +472,11 @@ impl<'gc> Class<'gc> {
.domain()
.get_class(activation.context, &multiname)
.ok_or_else(|| {
make_error_1014(activation, multiname.to_qualified_name(activation.gc()))
make_error_1014(
activation,
Error1014Type::VerifyError,
multiname.to_qualified_name(activation.gc()),
)
})?,
);
}
Expand Down
24 changes: 14 additions & 10 deletions core/src/avm2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,23 @@ pub fn make_error_1010<'gc>(
}
}

pub enum Error1014Type {
ReferenceError,
VerifyError,
}

#[inline(never)]
#[cold]
pub fn make_error_1014<'gc>(
activation: &mut Activation<'_, 'gc>,
kind: Error1014Type,
class_name: AvmString<'gc>,
) -> Error<'gc> {
let err = verify_error(
activation,
&format!("Error #1014: Class {} could not be found.", class_name),
1014,
);

let message = &format!("Error #1014: Class {} could not be found.", class_name);
let err = match kind {
Error1014Type::ReferenceError => reference_error(activation, message, 1014),
Error1014Type::VerifyError => verify_error(activation, message, 1014),
};
match err {
Ok(err) => Error::AvmError(err),
Err(err) => err,
Expand Down Expand Up @@ -505,11 +510,10 @@ pub fn make_error_2004<'gc>(
kind: Error2004Type,
) -> Error<'gc> {
let message = "Error #2004: One of the parameters is invalid.";
let code = 2004;
let err = match kind {
Error2004Type::Error => error(activation, message, code),
Error2004Type::ArgumentError => argument_error(activation, message, code),
Error2004Type::TypeError => type_error(activation, message, code),
Error2004Type::Error => error(activation, message, 2004),
Error2004Type::ArgumentError => argument_error(activation, message, 2004),
Error2004Type::TypeError => type_error(activation, message, 2004),
};
match err {
Ok(err) => Error::AvmError(err),
Expand Down
8 changes: 3 additions & 5 deletions core/src/avm2/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use crate::avm2::activation::Activation;
use crate::avm2::error::make_error_2007;
use crate::avm2::globals::slots::*;
use crate::avm2::object::{Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::Multiname;
use crate::display_object::TDisplayObject;
use crate::string::AvmString;
use fnv::FnvHashMap;
Expand Down Expand Up @@ -383,9 +383,8 @@ fn dispatch_event_to_target<'gc>(
event.as_event().unwrap().event_type(),
);

let internal_ns = activation.avm2().namespaces.flash_events_internal;
let dispatch_list = dispatcher
.get_property(&Multiname::new(internal_ns, "_dispatchList"), activation)?
.get_slot(FLASH_EVENTS_EVENT_DISPATCHER__DISPATCH_LIST_SLOT)
.as_object();

if dispatch_list.is_none() {
Expand Down Expand Up @@ -447,9 +446,8 @@ pub fn dispatch_event<'gc>(
event: Object<'gc>,
simulate_dispatch: bool,
) -> Result<bool, Error<'gc>> {
let internal_ns = activation.avm2().namespaces.flash_events_internal;
let target = this
.get_property(&Multiname::new(internal_ns, "_target"), activation)?
.get_slot(FLASH_EVENTS_EVENT_DISPATCHER__TARGET_SLOT)
.as_object()
.unwrap_or(this);

Expand Down
8 changes: 6 additions & 2 deletions core/src/avm2/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ pub struct SystemClassDefs<'gc> {
pub graphicssolidfill: Class<'gc>,
pub graphicsshaderfill: Class<'gc>,
pub graphicsstroke: Class<'gc>,

pub cubetexture: Class<'gc>,
pub rectangletexture: Class<'gc>,
pub display_object: Class<'gc>,
pub sprite: Class<'gc>,
}

impl<'gc> SystemClasses<'gc> {
Expand Down Expand Up @@ -373,9 +374,10 @@ impl<'gc> SystemClassDefs<'gc> {
graphicssolidfill: object,
graphicsshaderfill: object,
graphicsstroke: object,

cubetexture: object,
rectangletexture: object,
display_object: object,
sprite: object,
}
}
}
Expand Down Expand Up @@ -975,6 +977,7 @@ pub fn init_native_system_classes(activation: &mut Activation<'_, '_>) {
[
("flash.display", "Bitmap", bitmap),
("flash.display", "BitmapData", bitmapdata),
("flash.display", "DisplayObject", display_object),
("flash.display", "IGraphicsData", igraphicsdata),
("flash.display", "GraphicsBitmapFill", graphicsbitmapfill),
("flash.display", "GraphicsEndFill", graphicsendfill),
Expand All @@ -991,6 +994,7 @@ pub fn init_native_system_classes(activation: &mut Activation<'_, '_>) {
),
("flash.display", "GraphicsSolidFill", graphicssolidfill),
("flash.display", "GraphicsStroke", graphicsstroke),
("flash.display", "Sprite", sprite),
("flash.display3D.textures", "CubeTexture", cubetexture),
(
"flash.display3D.textures",
Expand Down
28 changes: 2 additions & 26 deletions core/src/avm2/globals/flash/display/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,9 @@ pub fn shape_allocator<'gc>(
class: ClassObject<'gc>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Object<'gc>, Error<'gc>> {
let shape_cls = activation.avm2().classes().shape.inner_class_definition();
let display_object = Graphic::empty(activation.context).into();

let mut class_def = Some(class.inner_class_definition());
let orig_class = class;
while let Some(class) = class_def {
if class == shape_cls {
let display_object = Graphic::empty(activation.context).into();
return initialize_for_allocator(activation, display_object, orig_class);
}

if let Some((movie, symbol)) = activation
.context
.library
.avm2_class_registry()
.class_symbol(class)
{
let child = activation
.context
.library
.library_for_movie_mut(movie)
.instantiate_by_id(symbol, activation.context.gc_context)?;

return initialize_for_allocator(activation, child, orig_class);
}
class_def = class.super_class();
}
unreachable!("A Shape subclass should have Shape in superclass chain");
initialize_for_allocator(activation, display_object, class)
}

/// Implements `graphics`.
Expand Down
10 changes: 6 additions & 4 deletions core/src/avm2/globals/flash/events/EventDispatcher.as
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// This is a stub - the actual class is defined in `eventdispatcher.rs`
package flash.events {
public class EventDispatcher implements IEventDispatcher {
internal var _target:IEventDispatcher;
internal var _dispatchList:Object;
[Ruffle(InternalSlot)]
private var target:IEventDispatcher;

[Ruffle(InternalSlot)]
private var dispatchList:Object;

public function EventDispatcher(target:IEventDispatcher = null) {
this._target = target;
this.target = target;
}

public native function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void;
Expand Down
24 changes: 7 additions & 17 deletions core/src/avm2/globals/flash/events/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

use crate::avm2::activation::Activation;
use crate::avm2::events::{dispatch_event as dispatch_event_internal, parent_of};
use crate::avm2::globals::slots::*;
use crate::avm2::object::{DispatchObject, Object, TObject};
use crate::avm2::parameters::ParametersExt;
use crate::avm2::value::Value;
use crate::avm2::Multiname;
use crate::avm2::{Avm2, Error};

/// Get an object's dispatch list, lazily initializing it if necessary.
fn dispatch_list<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
) -> Result<Object<'gc>, Error<'gc>> {
let namespaces = activation.avm2().namespaces;

match this.get_property(
&Multiname::new(namespaces.flash_events_internal, "_dispatchList"),
activation,
)? {
match this.get_slot(FLASH_EVENTS_EVENT_DISPATCHER__DISPATCH_LIST_SLOT) {
Value::Object(o) => Ok(o),
_ => {
let dispatch_list = DispatchObject::empty_list(activation);
this.init_property(
&Multiname::new(namespaces.flash_events_internal, "_dispatchList"),
this.set_slot(
FLASH_EVENTS_EVENT_DISPATCHER__DISPATCH_LIST_SLOT,
dispatch_list.into(),
activation,
)?;
Expand Down Expand Up @@ -86,7 +81,7 @@ pub fn has_event_listener<'gc>(

let does_have = dispatch_list
.as_dispatch_mut(activation.context.gc_context)
.ok_or_else(|| Error::from("Internal properties should have what I put in them"))?
.expect("Internal properties should have what I put in them")
.has_event_listener(event_type)
.into();

Expand All @@ -99,24 +94,19 @@ pub fn will_trigger<'gc>(
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let namespaces = activation.avm2().namespaces;

let dispatch_list = dispatch_list(activation, this)?;
let event_type = args.get_string(activation, 0)?;

if dispatch_list
.as_dispatch_mut(activation.context.gc_context)
.ok_or_else(|| Error::from("Internal properties should have what I put in them"))?
.expect("Internal properties should have what I put in them")
.has_event_listener(event_type)
{
return Ok(true.into());
}

let target = this
.get_property(
&Multiname::new(namespaces.flash_events_internal, "_target"),
activation,
)?
.get_slot(FLASH_EVENTS_EVENT_DISPATCHER__TARGET_SLOT)
.as_object()
.unwrap_or(this);

Expand Down
13 changes: 5 additions & 8 deletions core/src/avm2/globals/flash/net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `flash.net` namespace

use crate::avm2::error::{make_error_2007, reference_error};
use crate::avm2::error::{make_error_1014, make_error_2007, Error1014Type};
use crate::avm2::object::TObject;
use crate::avm2::parameters::ParametersExt;
use crate::avm2::{Activation, Error, Object, Value};
Expand Down Expand Up @@ -125,13 +125,10 @@ pub fn get_class_by_alias<'gc>(
if let Some(class_object) = activation.avm2().get_class_by_alias(name) {
Ok(class_object.into())
} else {
// can't create error 1014 normally,
// as this is one place where it's a ReferenceError for some reason
let error = reference_error(
Err(make_error_1014(
activation,
&format!("Error #1014: Class {} could not be found.", name),
1014,
)?;
Err(Error::AvmError(error))
Error1014Type::ReferenceError,
name,
))
}
}
Loading