Skip to content

Commit ad43841

Browse files
Lord-McSweeneyLord-McSweeney
Lord-McSweeney
authored andcommitted
avm2: Fix constructing Object with arguments passed to constructor
1 parent 5f93e47 commit ad43841

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

core/src/avm2/globals/object.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,37 @@ use crate::avm2::activation::Activation;
44
use crate::avm2::class::{Class, ClassAttributes};
55
use crate::avm2::error;
66
use crate::avm2::method::{Method, NativeMethodImpl, ParamConfig};
7-
use crate::avm2::object::{FunctionObject, Object, TObject};
7+
use crate::avm2::object::{FunctionObject, Object, ScriptObject, TObject};
88
use crate::avm2::traits::Trait;
99
use crate::avm2::value::Value;
1010
use crate::avm2::{Error, Multiname, QName};
1111
use crate::string::AvmString;
1212

13-
/// Implements `Object`'s instance initializer.
14-
pub fn instance_init<'gc>(
13+
/// Implements `Object`'s instance initializer. This method is unreachable because
14+
/// `Object` has a custom constructor (`object_constructor`).
15+
fn instance_init<'gc>(
1516
_activation: &mut Activation<'_, 'gc>,
1617
_this: Value<'gc>,
1718
_args: &[Value<'gc>],
1819
) -> Result<Value<'gc>, Error<'gc>> {
1920
Ok(Value::Undefined)
2021
}
2122

23+
/// Implements `Object`'s custom constructor.
24+
fn object_constructor<'gc>(
25+
activation: &mut Activation<'_, 'gc>,
26+
args: &[Value<'gc>],
27+
) -> Result<Value<'gc>, Error<'gc>> {
28+
if let Some(arg) = args.get(0) {
29+
if !matches!(arg, Value::Undefined | Value::Null) {
30+
return Ok(*arg);
31+
}
32+
}
33+
34+
let constructed_object = ScriptObject::new_object(activation);
35+
Ok(constructed_object.into())
36+
}
37+
2238
fn class_call<'gc>(
2339
activation: &mut Activation<'_, 'gc>,
2440
_this: Value<'gc>,
@@ -299,6 +315,8 @@ pub fn create_i_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
299315
Method::from_builtin(class_call, "<Object call handler>", gc_context),
300316
);
301317

318+
object_i_class.set_custom_constructor(gc_context, object_constructor);
319+
302320
// Fixed traits (in AS3 namespace)
303321
let as3_instance_methods: Vec<(&str, NativeMethodImpl, _, _)> = vec![
304322
// These signatures are weird, but they match the describeTypeJSON output

0 commit comments

Comments
 (0)