1
0
Fork 0
easejs/test
Mike Gerwitz d84b86b21b
Added `proxy' keyword support
The concept of proxy methods will become an important, core concept in ease.js
that will provide strong benefits for creating decorators and proxies, removing
boilerplate code and providing useful metadata to the system. Consider the
following example:

  Class( 'Foo',
  {
      // ...

      'public performOperation': function( bar )
      {
          this._doSomethingWith( bar );
          return this;
      },
  } );

  Class( 'FooDecorator',
  {
      'private _foo': null,

      // ...

      'public performOperation': function( bar )
      {
          return this._foo.performOperation( bar );
      },
  } );

In the above example, `FooDecorator` is a decorator for `Foo`. Assume that the
`getValueOf()` method is undecorated and simply needs to be proxied to its
component --- an instance of `Foo`. (It is not uncommon that a decorator, proxy,
or related class will alter certain functionality while leaving much of it
unchanged.) In order to do so, we can use this generic, boilerplate code

  return this.obj.func.apply( this.obj, arguments );

which would need to be repeated again and again for *each method that needs to
be proxied*. We also have another problem --- `Foo.getValueOf()` returns
*itself*, which `FooDecorator` *also* returns.  This breaks encapsulation, so we
instead need to return ourself:

  'public performOperation': function( bar )
  {
      this._foo.performOperation( bar );
      return this;
  },

Our boilerplate code then becomes:

  var ret = this.obj.func.apply( this.obj, arguments );
  return ( ret === this.obj )
      ? this
      : ret;

Alternatively, we could use the `proxy' keyword:

  Class( 'FooDecorator2',
  {
      'private _foo': null,

      // ...

      'public proxy performOperation': '_foo',
  } );

`FooDecorator2.getValueOf()` and `FooDecorator.getValueOf()` both perform the
exact same task --- proxy the entire call to another object and return its
result, unless the result is the component, in which case the decorator itself
is returned.

Proxies, as of this commit, accomplish the following:
  - All arguments are forwarded to the destination
  - The return value is forwarded to the caller
    - If the destination returns a reference to itself, it will be replaced with
      a reference to the caller's context (`this`).
  - If the call is expected to fail, either because the destination is not an
    object or because the requested method is not a function, a useful error
    will be immediately thrown (rather than the potentially cryptic one that
    would otherwise result, requiring analysis of the stack trace).

N.B. As of this commit, static proxies do not yet function properly.
2012-05-03 09:49:22 -04:00
..
Interface Copyright year update 2011-12-23 00:09:11 -05:00
MemberBuilder Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
MemberBuilderValidator Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
perf Copyright year update 2011-12-23 00:09:11 -05:00
FallbackMemberBuilderTest.js Copyright year update 2011-12-23 00:09:11 -05:00
FallbackVisibilityObjectFactoryTest.js Copyright year update 2011-12-23 00:09:11 -05:00
Makefile [#25] Added suite runner 2011-11-03 21:23:40 -04:00
MethodWrapperFactoryTest.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
MethodWrappersTest.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
VersionTest.js Resolved version test error caused by verset commit 2012-04-06 00:21:05 -04:00
VisibilityObjectFactoryFactoryTest.js Copyright year update 2011-12-23 00:09:11 -05:00
VisibilityObjectFactoryTest.js Copyright year update 2011-12-23 00:09:11 -05:00
common.js Copyright year update 2011-12-23 00:09:11 -05:00
inc-testcase.js Copyright year update 2011-12-23 00:09:11 -05:00
runner.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-abstract.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
test-class-constructor.js [Fix #37] constructor property now properly set on instances 2012-01-19 23:21:04 -05:00
test-class-extend.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-gettersetter.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-implement.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-name.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-parent.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class-visibility.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class_builder-const.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
test-class_builder-final.js Copyright year update 2011-12-23 00:09:11 -05:00
test-class_builder-member-restrictions.js Added constructor property to reserved members list 2012-01-17 23:36:01 -05:00
test-class_builder-static.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
test-class_builder-visibility.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
test-combine-pre-es5.js Copyright year update 2011-12-23 00:09:11 -05:00
test-combine.js Copyright year update 2011-12-23 00:09:11 -05:00
test-index.js Created version module to provide additional version information 2011-12-23 18:31:11 -05:00
test-interface-name.js Copyright year update 2011-12-23 00:09:11 -05:00
test-prop_parser-parse-keywords.js Copyright year update 2011-12-23 00:09:11 -05:00
test-rmtrail Copyright year update 2011-12-23 00:09:11 -05:00
test-util-abstract.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-clone.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-copy.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-define-secure-prop.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-get-property-descriptor.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-prop-parse-keywords.js Copyright year update 2011-12-23 00:09:11 -05:00
test-util-prop-parse.js Added `proxy' keyword support 2012-05-03 09:49:22 -04:00
test-warn-exception.js Copyright year update 2011-12-23 00:09:11 -05:00
test-warn-handlers.js Copyright year update 2011-12-23 00:09:11 -05:00
test-warn-impl.js Copyright year update 2011-12-23 00:09:11 -05:00