Skip to content

Commit

Permalink
avm2: Initialize global scope object earlier and remove `set_instance…
Browse files Browse the repository at this point in the history
…_class`
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Jul 1, 2024
1 parent d88abd7 commit c227ff9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
9 changes: 5 additions & 4 deletions core/src/avm2/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,16 @@ pub fn load_player_globals<'gc>(
// Function is more of a "normal" class than the other two, so we can create it normally.
let fn_classdef = function::create_class(activation, object_i_class, class_i_class);

// Register the classes in the domain, now
// Do the same for the global class
let global_classdef = global_scope::create_class(activation, object_i_class, class_i_class);

// Register the classes in the domain, now (except for the global class)
domain.export_class(object_i_class.name(), object_i_class, mc);
domain.export_class(class_i_class.name(), class_i_class, mc);
domain.export_class(fn_classdef.name(), fn_classdef, mc);

// Initialize the script
let globals = ScriptObject::custom_object(mc, object_i_class, None, None);
let globals = ScriptObject::custom_object(mc, global_classdef, None, None);
let script = Script::empty_script(mc, globals, domain);

let gs = ScopeChain::new(domain).chain(mc, &[Scope::new(globals)]);
Expand Down Expand Up @@ -565,11 +568,9 @@ pub fn load_player_globals<'gc>(
fn_class.link_prototype(activation, fn_proto)?;

// Construct the global class.
let global_classdef = global_scope::create_class(activation);
let global_class = ClassObject::from_class(activation, global_classdef, Some(object_class))?;

globals.set_proto(mc, global_class.prototype());
globals.set_instance_class(mc, global_classdef);
globals.set_vtable(mc, global_class.instance_vtable());

activation.context.avm2.toplevel_global_object = Some(globals);
Expand Down
10 changes: 7 additions & 3 deletions core/src/avm2/globals/global_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ pub fn class_init<'gc>(
}

/// Construct `global`'s class.
pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
pub fn create_class<'gc>(
activation: &mut Activation<'_, 'gc>,
object_classdef: Class<'gc>,
class_classdef: Class<'gc>,
) -> Class<'gc> {
let mc = activation.context.gc_context;
let class = Class::new(
QName::new(activation.avm2().public_namespace_base_version, "global"),
Some(activation.avm2().classes().object.inner_class_definition()),
Some(object_classdef),
Method::from_builtin(instance_init, "<global instance initializer>", mc),
Method::from_builtin(class_init, "<global class initializer>", mc),
activation.avm2().classes().class.inner_class_definition(),
class_classdef,
mc,
);

Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,6 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
self.instance_class().name().to_qualified_name(mc)
}

fn set_instance_class(&self, mc: &Mutation<'gc>, instance_class: Class<'gc>) {
let mut base = self.base_mut(mc);
base.set_instance_class(instance_class);
}

// Sets a different vtable for object, without changing instance_of.
fn set_vtable(&self, mc: &Mutation<'gc>, vtable: VTable<'gc>) {
let mut base = self.base_mut(mc);
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object/script_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,6 @@ impl<'gc> ScriptObjectData<'gc> {
self.instance_class().is_sealed()
}

/// Set the class object for this object.
pub fn set_instance_class(&mut self, instance_class: Class<'gc>) {
self.instance_class = instance_class;
}

pub fn set_vtable(&mut self, vtable: VTable<'gc>) {
self.vtable = Some(vtable);
}
Expand Down
7 changes: 6 additions & 1 deletion core/src/avm2/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,13 @@ impl<'gc> TranslationUnit<'gc> {
drop(read);

let object_class = activation.avm2().classes().object;
let class_classdef = activation.avm2().classes().class.inner_class_definition();

let global_classdef = global_scope::create_class(activation);
let global_classdef = global_scope::create_class(
activation,
object_class.inner_class_definition(),
class_classdef,
);

let global_class =
ClassObject::from_class(activation, global_classdef, Some(object_class))?;
Expand Down

0 comments on commit c227ff9

Please sign in to comment.