Skip to content

Commit 0ef0e37

Browse files
committed
avm1: clean-up global definitions
- Adds a new `DeclContext` with convenience methods for defining classes - Use the same function names for each global (`create_class` for classes, `create` for objects)
1 parent b03501f commit 0ef0e37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+902
-1436
lines changed

core/src/avm1/globals.rs

Lines changed: 174 additions & 369 deletions
Large diffs are not rendered by default.

core/src/avm1/globals/accessibility.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
33
use crate::avm1::activation::Activation;
44
use crate::avm1::error::Error;
5-
use crate::avm1::property_decl::{define_properties_on, Declaration};
5+
use crate::avm1::property_decl::{DeclContext, Declaration};
66
use crate::avm1::{Object, Value};
77
use crate::avm1_stub;
8-
use crate::string::StringContext;
98

109
const OBJECT_DECLS: &[Declaration] = declare_properties! {
1110
"isActive" => method(is_active; DONT_DELETE | READ_ONLY);
1211
"sendEvent" => method(send_event; DONT_DELETE | READ_ONLY);
1312
"updateProperties" => method(update_properties; DONT_DELETE | READ_ONLY);
1413
};
1514

15+
pub fn create<'gc>(context: &mut DeclContext<'_, 'gc>) -> Object<'gc> {
16+
let accessibility = Object::new(context.strings, Some(context.object_proto));
17+
context.define_properties_on(accessibility, OBJECT_DECLS);
18+
accessibility
19+
}
20+
1621
pub fn is_active<'gc>(
1722
activation: &mut Activation<'_, 'gc>,
1823
_this: Object<'gc>,
@@ -39,13 +44,3 @@ pub fn update_properties<'gc>(
3944
avm1_stub!(activation, "Accessibility", "updateProperties");
4045
Ok(Value::Undefined)
4146
}
42-
43-
pub fn create_accessibility_object<'gc>(
44-
context: &mut StringContext<'gc>,
45-
proto: Object<'gc>,
46-
fn_proto: Object<'gc>,
47-
) -> Object<'gc> {
48-
let accessibility = Object::new(context, Some(proto));
49-
define_properties_on(OBJECT_DECLS, context, accessibility, fn_proto);
50-
accessibility
51-
}

core/src/avm1/globals/array.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use crate::avm1::activation::Activation;
44
use crate::avm1::clamp::Clamp;
55
use crate::avm1::error::Error;
6-
use crate::avm1::function::FunctionObject;
7-
use crate::avm1::property_decl::{define_properties_on, Declaration};
6+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
87
use crate::avm1::{Attribute, NativeObject, Object, Value};
98
use crate::ecma_conversions::f64_to_wrapping_i32;
109
use crate::string::{AvmString, StringContext};
@@ -62,6 +61,22 @@ const OBJECT_DECLS: &[Declaration] = declare_properties! {
6261
"NUMERIC" => int(SortOptions::NUMERIC.bits());
6362
};
6463

64+
pub fn create_class<'gc>(
65+
context: &mut DeclContext<'_, 'gc>,
66+
super_proto: Object<'gc>,
67+
) -> SystemClass<'gc> {
68+
let proto = ArrayBuilder::new_with_proto(context.strings, super_proto).with([]);
69+
let class = context.native_class_with_proto(constructor, Some(array), proto);
70+
context.define_properties_on(proto, PROTO_DECLS);
71+
72+
// TODO: These were added in Flash Player 7, but are available even to SWFv6 and lower
73+
// when run in Flash Player 7. Make these conditional if we add a parameter to control
74+
// target Flash Player version.
75+
context.define_properties_on(class.constr, OBJECT_DECLS);
76+
77+
class
78+
}
79+
6580
/// Intermediate builder for constructing `ArrayObject`,
6681
/// used to work around borrow-checker issues.
6782
pub struct ArrayBuilder<'gc> {
@@ -121,23 +136,8 @@ impl<'gc> ArrayBuilder<'gc> {
121136
}
122137
}
123138

124-
pub fn create_array_object<'gc>(
125-
context: &mut StringContext<'gc>,
126-
array_proto: Object<'gc>,
127-
fn_proto: Object<'gc>,
128-
) -> Object<'gc> {
129-
let array =
130-
FunctionObject::constructor(context, constructor, Some(array), fn_proto, array_proto);
131-
132-
// TODO: These were added in Flash Player 7, but are available even to SWFv6 and lower
133-
// when run in Flash Player 7. Make these conditional if we add a parameter to control
134-
// target Flash Player version.
135-
define_properties_on(OBJECT_DECLS, context, array, fn_proto);
136-
array
137-
}
138-
139139
/// Implements `Array` constructor
140-
pub fn constructor<'gc>(
140+
fn constructor<'gc>(
141141
activation: &mut Activation<'_, 'gc>,
142142
this: Object<'gc>,
143143
args: &[Value<'gc>],
@@ -792,13 +792,3 @@ fn qsort<'gc>(
792792

793793
Ok(())
794794
}
795-
796-
pub fn create_proto<'gc>(
797-
context: &mut StringContext<'gc>,
798-
proto: Object<'gc>,
799-
fn_proto: Object<'gc>,
800-
) -> Object<'gc> {
801-
let array = ArrayBuilder::new_with_proto(context, proto).with([]);
802-
define_properties_on(PROTO_DECLS, context, array, fn_proto);
803-
array
804-
}

core/src/avm1/globals/as_broadcaster.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use crate::avm1::error::Error;
44
use crate::avm1::function::ExecutionReason;
5-
use crate::avm1::function::FunctionObject;
65
use crate::avm1::property::Attribute;
7-
use crate::avm1::property_decl::Declaration;
6+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
87
use crate::avm1::{Activation, ArrayBuilder, Object, Value};
98
use crate::string::{AvmString, StringContext};
109
use gc_arena::Collect;
@@ -17,19 +16,17 @@ const OBJECT_DECLS: &[Declaration] = declare_properties! {
1716
"broadcastMessage" => function(broadcast_message; DONT_ENUM | DONT_DELETE);
1817
};
1918

20-
pub fn create<'gc>(
21-
context: &mut StringContext<'gc>,
22-
proto: Object<'gc>,
23-
fn_proto: Object<'gc>,
24-
) -> (BroadcasterFunctions<'gc>, Object<'gc>) {
25-
let as_broadcaster_proto = Object::new(context, Some(proto));
19+
pub fn create_class<'gc>(
20+
context: &mut DeclContext<'_, 'gc>,
21+
super_proto: Object<'gc>,
22+
) -> (BroadcasterFunctions<'gc>, SystemClass<'gc>) {
2623
// Despite the documentation says that there is no constructor function for the `AsBroadcaster`
2724
// class, Flash accepts expressions like `new AsBroadcaster()`, and a newly-created object is
2825
// returned in such cases.
29-
let as_broadcaster = FunctionObject::empty(context, fn_proto, as_broadcaster_proto);
26+
let class = context.empty_class(super_proto);
3027

3128
let mut define_as_object = |index: usize| -> Object<'gc> {
32-
match OBJECT_DECLS[index].define_on(context, as_broadcaster, fn_proto) {
29+
match OBJECT_DECLS[index].define_on(context.strings, class.constr, context.fn_proto) {
3330
Value::Object(o) => o,
3431
_ => panic!("expected object for broadcaster function"),
3532
}
@@ -42,7 +39,7 @@ pub fn create<'gc>(
4239
remove_listener: define_as_object(2),
4340
broadcast_message: define_as_object(3),
4441
},
45-
as_broadcaster,
42+
class,
4643
)
4744
}
4845

core/src/avm1/globals/bevel_filter.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! flash.filters.BevelFilter object
22
3-
use crate::avm1::function::FunctionObject;
43
use crate::avm1::object::NativeObject;
5-
use crate::avm1::property_decl::{define_properties_on, Declaration};
4+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
65
use crate::avm1::{Activation, Error, Object, Value};
7-
use crate::string::StringContext;
86
use gc_arena::{Collect, Gc, Mutation};
97
use ruffle_macros::istr;
108
use std::cell::Cell;
@@ -405,6 +403,15 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
405403
"type" => property(bevel_filter_method!(23), bevel_filter_method!(24); VERSION_8);
406404
};
407405

406+
pub fn create_class<'gc>(
407+
context: &mut DeclContext<'_, 'gc>,
408+
super_proto: Object<'gc>,
409+
) -> SystemClass<'gc> {
410+
let class = context.native_class(bevel_filter_method!(0), None, super_proto);
411+
context.define_properties_on(class.proto, PROTO_DECLS);
412+
class
413+
}
414+
408415
fn method<'gc>(
409416
activation: &mut Activation<'_, 'gc>,
410417
this: Object<'gc>,
@@ -520,21 +527,3 @@ fn method<'gc>(
520527
_ => Value::Undefined,
521528
})
522529
}
523-
524-
pub fn create_proto<'gc>(
525-
context: &mut StringContext<'gc>,
526-
proto: Object<'gc>,
527-
fn_proto: Object<'gc>,
528-
) -> Object<'gc> {
529-
let bevel_filter_proto = Object::new(context, Some(proto));
530-
define_properties_on(PROTO_DECLS, context, bevel_filter_proto, fn_proto);
531-
bevel_filter_proto
532-
}
533-
534-
pub fn create_constructor<'gc>(
535-
context: &mut StringContext<'gc>,
536-
proto: Object<'gc>,
537-
fn_proto: Object<'gc>,
538-
) -> Object<'gc> {
539-
FunctionObject::constructor(context, bevel_filter_method!(0), None, fn_proto, proto)
540-
}

core/src/avm1/globals/bitmap_data.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
//! flash.display.BitmapData object
22
33
use super::matrix::object_to_matrix;
4-
use crate::avm1::function::FunctionObject;
54
use crate::avm1::globals::bitmap_filter;
65
use crate::avm1::globals::color_transform::ColorTransformObject;
76
use crate::avm1::object::NativeObject;
8-
use crate::avm1::property_decl::{define_properties_on, Declaration};
7+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
98
use crate::avm1::{Activation, Attribute, Error, Object, Value};
109
use crate::bitmap::bitmap_data::BitmapData;
1110
use crate::bitmap::bitmap_data::{BitmapDataDrawError, IBitmapDrawable};
1211
use crate::bitmap::bitmap_data::{ChannelOptions, ThresholdOperation};
1312
use crate::bitmap::{is_size_valid, operations};
1413
use crate::character::Character;
1514
use crate::display_object::DisplayObject;
16-
use crate::string::StringContext;
1715
use crate::swf::BlendMode;
1816
use crate::{avm1_stub, avm_error};
1917
use ruffle_macros::istr;
@@ -54,6 +52,16 @@ const OBJECT_DECLS: &[Declaration] = declare_properties! {
5452
"loadBitmap" => method(load_bitmap);
5553
};
5654

55+
pub fn create_class<'gc>(
56+
context: &mut DeclContext<'_, 'gc>,
57+
super_proto: Object<'gc>,
58+
) -> SystemClass<'gc> {
59+
let class = context.native_class(constructor, None, super_proto);
60+
context.define_properties_on(class.proto, PROTO_DECLS);
61+
context.define_properties_on(class.constr, OBJECT_DECLS);
62+
class
63+
}
64+
5765
fn new_bitmap_data<'gc>(
5866
proto: Option<Value<'gc>>,
5967
bitmap_data: BitmapData<'gc>,
@@ -1561,16 +1569,3 @@ fn load_bitmap<'gc>(
15611569
)
15621570
.into())
15631571
}
1564-
1565-
pub fn create_constructor<'gc>(
1566-
context: &mut StringContext<'gc>,
1567-
proto: Object<'gc>,
1568-
fn_proto: Object<'gc>,
1569-
) -> Object<'gc> {
1570-
define_properties_on(PROTO_DECLS, context, proto, fn_proto);
1571-
1572-
let bitmap_data_constructor =
1573-
FunctionObject::constructor(context, constructor, None, fn_proto, proto);
1574-
define_properties_on(OBJECT_DECLS, context, bitmap_data_constructor, fn_proto);
1575-
bitmap_data_constructor
1576-
}

core/src/avm1/globals/bitmap_filter.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ use crate::avm1::globals::drop_shadow_filter::DropShadowFilter;
1111
use crate::avm1::globals::glow_filter::GlowFilter;
1212
use crate::avm1::globals::gradient_filter::GradientFilter;
1313
use crate::avm1::object::NativeObject;
14-
use crate::avm1::property_decl::{define_properties_on, Declaration};
14+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
1515
use crate::avm1::{Attribute, Object, Value};
1616
use crate::context::UpdateContext;
17-
use crate::string::StringContext;
1817
use ruffle_macros::istr;
1918
use ruffle_render::filters::Filter;
2019

2120
const PROTO_DECLS: &[Declaration] = declare_properties! {
2221
"clone" => method(clone);
2322
};
2423

24+
pub fn create_class<'gc>(
25+
context: &mut DeclContext<'_, 'gc>,
26+
super_proto: Object<'gc>,
27+
) -> SystemClass<'gc> {
28+
let class = context.empty_class(super_proto);
29+
context.define_properties_on(class.proto, PROTO_DECLS);
30+
class
31+
}
32+
2533
pub fn clone<'gc>(
2634
activation: &mut Activation<'_, 'gc>,
2735
this: Object<'gc>,
@@ -164,13 +172,3 @@ pub fn create_instance<'gc>(
164172
result.set_native(activation.gc(), native);
165173
result
166174
}
167-
168-
pub fn create_proto<'gc>(
169-
context: &mut StringContext<'gc>,
170-
proto: Object<'gc>,
171-
fn_proto: Object<'gc>,
172-
) -> Object<'gc> {
173-
let object = Object::new(context, Some(proto));
174-
define_properties_on(PROTO_DECLS, context, object, fn_proto);
175-
object
176-
}

core/src/avm1/globals/blur_filter.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! flash.filters.BlurFilter object
2-
3-
use crate::avm1::function::FunctionObject;
42
use crate::avm1::object::NativeObject;
5-
use crate::avm1::property_decl::{define_properties_on, Declaration};
3+
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
64
use crate::avm1::{Activation, Error, Object, Value};
7-
use crate::string::StringContext;
85
use gc_arena::{Collect, Gc, Mutation};
96
use std::cell::Cell;
107
use swf::{BlurFilterFlags, Fixed16};
@@ -134,6 +131,15 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
134131
"quality" => property(blur_filter_method!(5), blur_filter_method!(6); VERSION_8);
135132
};
136133

134+
pub fn create_class<'gc>(
135+
context: &mut DeclContext<'_, 'gc>,
136+
super_proto: Object<'gc>,
137+
) -> SystemClass<'gc> {
138+
let class = context.native_class(blur_filter_method!(0), None, super_proto);
139+
context.define_properties_on(class.proto, PROTO_DECLS);
140+
class
141+
}
142+
137143
fn method<'gc>(
138144
activation: &mut Activation<'_, 'gc>,
139145
this: Object<'gc>,
@@ -178,21 +184,3 @@ fn method<'gc>(
178184
_ => Value::Undefined,
179185
})
180186
}
181-
182-
pub fn create_proto<'gc>(
183-
context: &mut StringContext<'gc>,
184-
proto: Object<'gc>,
185-
fn_proto: Object<'gc>,
186-
) -> Object<'gc> {
187-
let blur_filter_proto = Object::new(context, Some(proto));
188-
define_properties_on(PROTO_DECLS, context, blur_filter_proto, fn_proto);
189-
blur_filter_proto
190-
}
191-
192-
pub fn create_constructor<'gc>(
193-
context: &mut StringContext<'gc>,
194-
proto: Object<'gc>,
195-
fn_proto: Object<'gc>,
196-
) -> Object<'gc> {
197-
FunctionObject::constructor(context, blur_filter_method!(0), None, fn_proto, proto)
198-
}

0 commit comments

Comments
 (0)