Making plugin for a custom framework


#1

Hi guy’s, I am trying to make some plugin code, but still have no luck in that (
So, could you please help me ?!
Introduction:
I’ve a custom framework (yeah, some legacy stuff, a lot ! ), but it is simple, and at same time it has some ambiguous code, that prevents tern to make its work

There is a function:

mivi.Class.extend( { instanceMethod1: function () {...}, instanceMethod2: function () {...} }, { staticMethod1: function () {...}, staticMethod2: function () {...} } ).define("MyNewClass");
and here:
extend method make a new class that was inherited from a class that extend method was invoked on,
and define method present on each class, since they all inherited from some base class, that has that method, and its work to place this class into namespace mivi.Class so that will be mivi.Class.MyNewClass

So how I can achieve this in plugin, I don’t understand how to place some object into a constructors prototype…

Any ideas ??


#2

Hi, at this moment I end with this code:

	infer.registerFunction( 'miviClassDefine', function ( _self, args, argNodes ) {
		var cx = infer.cx()
			, aval = args[ 1 ] ? args[ 1 ] : _self
			, avalType = null
			, scope = null
			, type = null
			, constr = null
			, proto = null;
		if ( args[ 0 ] && argNodes[ 0 ].type == 'Literal' ) {
			if ( scope = cx.topScope.hasProp( 'mivi' ) ) {
				if ( scope.props.Class ) {
					if ( scope.props.Class.defProp ) {
						type = scope.props.Class
					} else {
						type = scope.props.Class.getType()
					}
					if ( !type ) {
						type = new infer.Obj()
					}
					if (aval instanceof infer.AVal) {
						avalType = aval.getType();
					} else if (aval.hasProp) {
						avalType = aval;
					} else {
						return new infer.AVal();
					}
					if (!avalType) {
						return new infer.AVal();
					}
/* constructor property is that current framework agnostic and means that there is defined function to call on 'new' keyword  */
					if (!avalType.hasProp("constructor")) {
						constr = new infer.Fn( argNodes[ 0 ].value + '_constructor', new infer.AVal(), [], [], new infer.AVal() );
					} else {
						constr = avalType.getProp("constructor").getFunctionType();
					}
					proto = constr.getProp("prototype");
					aval.propagate(proto);
					type.defProp( argNodes[ 0 ].value, aval.originNode ).addType( constr )
					scope.props.Class.addType( type )
				}
			}
		}
		return _self;
	} )

	infer.registerFunction( 'miviExtend', function ( _self, args, argNodes ) {
		if ( args.length && args[ 0 ].defProp ) {
			var aval
			for ( var i in _self.props ) {
				aval = args[ 0 ].defProp( i )
				_self.props[ i ].propagate( aval )
			}
			return args[ 0 ]
		}
		return new infer.AVal()
	} )

tern.registerPlugin( 'mivi', function ( server, options ) {
   server.addDefs( defs )
} )
// I have this define
var defs = {
  '!name': 'mivi'
  , 'mivi': {
    'Class': {
      'Class': {
        'extend': {
          '!type': 'fn(protoProps: ?, staticProps: ?) -> !custom:miviExtend'
              , '!doc': 'Extend object as inheritance'
          }
        , 'define': {
           '!type': 'fn(name: string, object: ?) -> !custom:miviClassDefine'
               , '!doc': 'Define class in mivi.Class namespace'
          }
       }
    }
}