RealyUniqueName / haxe

Haxe - The Cross-Platform Toolkit

Home Page:http://haxe.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

External storage for class metadata and reflexion

njuneau opened this issue · comments

Hello,

This is a suggestion. I'm no compiler expert, so fire at will if this is a bad one!

The current Haxe compiler stores class metadata in a static field called "meta". Metadata is then accessed using a function with the same name. While it does not cause any direct problem (the risk of colliding with this very specific name is low, I guess), I wonder if this would not be better stored elsewhere in order to reduce clutter in the generated class file.

Maybe something along the lines of the Java world (hides from people throwing rocks) like having an explicit class definition somewhere in which to store any metadata or compiler-related information could be a nice thing? It would reduce the risks of name collision and any information unobtainable by using the standard PHP reflection API could be stored there.

For example: given a class "org.acme.Test", there could be a generated class "haxe.rtti.generated.org.acme.Test" containing all the information. The developer itself would not see this generated class - only the compiler and internal Haxe's RTTI API would have access to it.

Someone would have to double check here, but I do not think that PHP has standardized annotations yet. There was an RFC for annotations here ( https://wiki.php.net/rfc/annotations ). And some frameworks already provide a solution by using comments ( http://symfony.com/doc/current/validation.html / https://wiki.typo3.org/Dependency_Injection ).

I am very far from being a fan of the comment approach since comments are, by definition, not supposed to alter the execution of the program. Is there anything being pushed forward by PHP regarding metadata?

I'm going to move class data out from class declaration to the bottom of the file. So the final look of the generated file will be like this:

namespace some;

class MyClass {
  <...>
}

Boot.getClass('\\some\\MyClass').setMeta(<...>);

Meta and reflection data is stored in php7.Boot upon class loading.
This is how meta and reflection is generated:

<?php
namespace unit\issues\_Issue2042;

use \php7\Boot;
use \php7\_Boot\HxAnon;

interface WithMeta
{
    <...>
}

Boot::registerClass(WithMeta::class, 'unit.issues._Issue2042.WithMeta');
Boot::registerSetters('unit\\issues\\_Issue2042\\WithMeta', [
    'accFunc' => true
]);
Boot::registerGetters('unit\\issues\\_Issue2042\\WithMeta', [
    'accFunc' => true,
    'getterOnly' => true
]);
Boot::registerMeta(WithMeta::class, new HxAnon([
    "obj" => new HxAnon([
        "someMeta" => \Array_hx::wrap([1]),
        "otherMeta" => \Array_hx::wrap([2]),
    ]),
    "fields" => new HxAnon([
        "testing" => new HxAnon([
            "varMeta" => \Array_hx::wrap([3]),
        ]),
    ]),
]));