How to create an Action Script 3 class used in two SWF files to allow the same class when one SWF dynamically loads the other?

Background

I am developing a high-module application in pure Action Script 3 (we use the Flex 4 SDK to automate our collections, but all our code should be able to compile directly in Flash CS4 Professional).

We have a “framework.swc” file that contains interface definitions that are shared between all our modules, we have a “mainmodule.swf” that loads our other modules, and then we have various .swf files for our other modules, We use the Loader class in combination with ApplicationDomain :: getDefinition () to dynamically load the classes [we use the new LoaderContext (false, ApplicationDomain.currentDomain) "].

Problem

All our modules implement the "AbstractModule" interface, which is defined in "framework.swc". However, when creating a dynamically loaded module (AbstractModule module) returns false. More importantly, if I call module.someMethod (someobject), where someobject implements the interface defined in "framework.swc", and when the module method expects an object of the same interface defined in "framework.swc", I get a time error run "TypeError: Error # 1034: Error of type Coercion: cannot convert _ to _.

It seems that "mainmodule.swf" and "loadedmodule.swf" (the module I downloaded for testing) internally use separate definitions for common interfaces in "framework.swc"

Question

"mainmodule.swf" "loadedmodule.swf" , ?

+3
4

Ok. , . , "AbstractX" ( "X" - ) -: "ImportX" "ExportX". ExportX , AbstractX Object, AbstractX, , AbstractX, / , - . ImportX , , AbstractX ( AbstractX AbstractX), Object AbstractX. ExportX, ImportX ImportY, ImportZ ..; ExportX ImportY, ImportZ .. , AbstractX, ImportX , Object. , :

public interface AbstractX
{
    // The export/import functions are mandatory 
    // for all such interfaces. They allow
    // for the wrappers to be correctly manipulated.
    function export() : Object;
    function original() : Object;

    // The interface functions vary from 
    // interface to interface. They can
    // be called something much more appropriate.
    function interfaceFunction1(param : AbstractY) : AbstractZ;
    function interfaceFunction2(param : AbstractA) : AbstractB;
}
// A class of type Import_ always implements Abstract_
public class ImportX implements AbstractX
{
    // The constructor for an Import_ Object
    // is always of type Object.
    public function ImportX(obj : Object) : void {
        _loadedobj = obj;
        _exportobj = obj.export();
    }

    // Every Import_ class must implement a similar "wrap" function:
    public static function wrap(obj : Object) : AbstractX {
        var result : AbstractX = null; 
        if ( obj != null ){
            if ( obj is AbstractX ){ // Don't wrap if convertible, directly.
                result = obj as AbstractX;
            }else if ( obj.original() is AbstractX ){ // Don't double wrap
                result = obj.original() as AbstractX;
            }else{
                // Needs to be wrapped.
                result = new ImportX(obj);
            }
         }
         return result;
     }

    public function export() : Object {
        return _exportobj;
    }

    public function original() : Object {
        return _loadedobj;
    }

    // For the interface functions, we delegate to _exportobj
    // and we wrap the return values, but not the parameters.
    public function interfaceFunction1(param : AbstractY) : AbstractZ {
        return AbstractZ.wrap(_exportobj.interfaceFunction1(param));
    }

    public function interfaceFunction2(param : AbstractA) : AbstractB {
        return AbstractB.wrap(_exportobj.interfaceFunction2(param));
    }

    private var _loadedobj : Object;
    private var _exportobj : Object;
}
// Although an Export_ object provides SIMILAR methods to type Abstract_,
// the signatures need to be changed so that only builtin/predefined types
// appear. Thus Export_ NEVER implements Abstract_.
public class ExportX
{
    // The constructor to Export_ always takes an object of type Abstract_
    public function ExportX(obj : AbstractX) : void {
        _obj = obj;
    }

    public function original() : Object {
        return _obj;
    }

    public function export() : Object {
        return this;
    }

    // For the interface functions, we delegate to _obj
    // and we wrap the parameters, not the return values.
    // Also note the change in signature.
    public function interfaceFunction1(param : Object) : Object {
        return _obj.interfaceFunction1(AbstractY.wrap(param));
    }

    public function interfaceFunction2(param : Object) : Object {
        return _obj.interfaceFunction2(AbstractA.wrap(param));
    }

    private var _obj : AbstractX = null;
}
// The definition of class X can occur in and be loaded by any module.
public class X implements AbstractX
{
    public function X( /* ... */ ) : void {
        //...
    }

    public function export() : Object {
        if ( ! _export ){
            _export = new ExportX(this);
        }
        return _export;
    }

    public function original() : Object {
        return this;
    }

    public function interfaceFunction1(param : AbstractY) : AbstractZ {
        // ...
    }

    public function interfaceFunction2(param : AbstractA) : AbstractB {
       // ...
    }

    private var _export : Object = null;
}
// Ok. So here is how you use this...
var classx   : Class = dynamicallyLoadClassFromModule("X","module.swf"); 
var untypedx : Object = new classx();
var typedx   : AbstractX = ImportX.wrap(untypedx);
// Use typedx ...
+1

-compiler.external-library-path ... , swc, , , , ... , CS4, ...

Greetz

back2dos

0

, Runtime (RSL). RSL . , CS4 . , " Flash CS4" Flex SDK / CS4 IDE.

, , SWF- .

0

I am not 100% sure if this is what you need, but the Gaia Framework implements a global API shared by many swfs to interact with each other. You can check this out and maybe get some ideas. Now I am faced with the same situation as yours, so I am checking alternatives ... this post will be very useful, thanks!

0
source

Source: https://habr.com/ru/post/1712338/


All Articles