Extend custom attributes with support for functions/methods

Issue #84 new
Anders Melander created an issue

Custom attributes are currently only supported at the class level:

type
  TestAttribute = class(TCustomAttribute);

  [Test]
  TMyClass = class
  end;

I request that the custom attribute feature be extended to also support functions/methods and properties.

Background

We're using DWScript as a key technology in enabling our application to serve as a platform. The platform is based on an add-in architecture complete with development environment, license management, content protection and an "app store/marketplace". Add-ins are implemented in script by the end user, 3rd parties or ourselves.

Since end users can install and execute scripts authored by 3rd parties and the source code of these scripts can be unreadable to the user (they can be encrypted in order to protect IP) we need to provide the user some sort of protection against malicious or poorly written code. We're doing this through a permission system in which the manifest of an add-in declares what permissions it need. This list of permissions required by the add-in are presented to the user at install time at which time they can then decide if they want to allow the add-in these permissions (install) or not (do not install). Run-time use of permissions not already granted at install time either cause the permission request to fail immediately or cause a permission request prompt.

Use case

Together with issue #85 this enhancement will enable me to implement a permission system on top of custom attribute meta data declared at design time.

Let's say I have two script functions declared at design time in a TdwsUnit:

function ReadData: string;
procedure WriteData: string;

I would like to declare that the ReadData function requires the DATA_READ permission and the WriteData function requires the DATA_WRITE permission. With custom attributes I could do it like so:

// All this is TdwsUnit declarations
type
   SystemPermissionAttribute = class(TCustomAttribute)
   private
     FPermission: string;
   public
     constructor Create(const Permission: string); begin FPermission := Permission; end;
     property Permission: string read FPermission;
   end;

const DATA_READ = 'DATA.READ';
const DATA_WRITE = 'DATA.WRITE';

[SystemPermission(DATA_READ)]
function ReadData: string;
[SystemPermission(DATA_WRITE)]
procedure WriteData: string;

When a script has been compiled I can examine the symbol dictionary to determine at least a subset of the permissions required by the script.

Comments (2)

  1. Eric Grange repo owner

    Basic capability is there for attaching attributes to any symbol, what's missing to implement this would be a suite of unit tests (in the same vein of those found in Test/FunctionsRTTI and Test/AttributesFail) to handle all the syntax issues associated to having attributes "anywhere" in the code (including orphan attributes because in wrong locations).

    Besides the number of tests required to ensure a good coverage, for compatibility with Delphi, unless EMBT improved their docs, some "reverse engineering" may be required to figure out the edge cases.

  2. Francisco Armando DueƱas Rodriguez

    Hi Mr Eric. I really need this feature to work as also the support for generics, in the case of this attributes, what do you need what I can do to help you.

  3. Log in to comment