/src/cocktail/classInstance/as3/ClassInstance.hx

http://github.com/silexlabs/Cocktail · Haxe · 70 lines · 25 code · 8 blank · 37 comment · 3 complexity · 291944232bad4440a458a3b6d5b8e1c9 MD5 · raw file

  1. /*
  2. This file is part of Silex - see http://projects.silexlabs.org/?/silex
  3. Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  7. */
  8. package cocktail.classInstance.as3;
  9. import haxe.Log;
  10. import cocktail.classInstance.abstract.AbstractClassInstance;
  11. /**
  12. * This is the implementation of the native instance proxy for Flash. It implemenents
  13. * a class instantiation method specific to the Flash runtime
  14. * @author Yannick DOMINGUEZ
  15. */
  16. class ClassInstance extends AbstractClassInstance
  17. {
  18. /**
  19. * class constructor
  20. */
  21. public function new(nativeInstanceClassName:String)
  22. {
  23. super(nativeInstanceClassName);
  24. //instantiate the native Flash class and store a reference to it
  25. var classReference:Class<Dynamic> = Type.resolveClass(nativeInstanceClassName);
  26. _nativeInstance = Type.createInstance(classReference, new Array());
  27. }
  28. //////////////////////////////////////////////////////////////////////////////////////////
  29. // Overriden proxy method to add Flash specific behaviour. Used to prevent
  30. // runtime error from being thrown in Flash
  31. //////////////////////////////////////////////////////////////////////////////////////////
  32. /**
  33. * check if a function exists on the class instance. Must rely on Type.getInstanceField to check if
  34. * the method exists, as Reflect.hasField only works for dynamic attributes and only return false for
  35. * class attributes/methods.
  36. *
  37. * @param functionName the name of the searched method
  38. * @return
  39. */
  40. override public function isFunction(functionName:String):Bool
  41. {
  42. //retrieve all the static native class fields
  43. var instanceFields:Array<String> = Type.getInstanceFields(Type.getClass(_nativeInstance));
  44. var numInstanceFields:Int = instanceFields.length;
  45. //loop in the fields to find a match for the method name
  46. for (i in 0...numInstanceFields)
  47. {
  48. if (instanceFields[i] == functionName)
  49. {
  50. //a field with the right name exists,
  51. //test if it is a function
  52. return super.isFunction(functionName);
  53. }
  54. }
  55. //the method does not exist
  56. return false;
  57. }
  58. }