@@ -5,7 +5,7 @@ import * as ts from "typescript";
55import { CompilerOptions } from "./CompilerOptions" ;
66import { DecoratorKind } from "./Decorator" ;
77import { TSTLErrors } from "./Errors" ;
8- import { TSHelper as tsHelper } from "./TSHelper" ;
8+ import { ContextType , TSHelper as tsHelper } from "./TSHelper" ;
99
1010/* tslint:disable */
1111const packageJSON = require ( "../package.json" ) ;
@@ -1190,25 +1190,26 @@ export abstract class LuaTranspiler {
11901190 ? `({ ${ result } })` : result ;
11911191 }
11921192
1193- const sig = this . checker . getResolvedSignature ( node ) ;
1193+ const signature = this . checker . getResolvedSignature ( node ) ;
11941194
11951195 // Handle super calls properly
11961196 if ( node . expression . kind === ts . SyntaxKind . SuperKeyword ) {
1197- params = this . transpileArguments ( node . arguments , sig ,
1197+ params = this . transpileArguments ( node . arguments , signature ,
11981198 ts . createNode ( ts . SyntaxKind . ThisKeyword ) as ts . Expression ) ;
11991199 const className = this . classStack [ this . classStack . length - 1 ] ;
12001200 return `${ className } .__base.constructor(${ params } )` ;
12011201 }
12021202
1203- const type = this . checker . getTypeAtLocation ( node . expression ) ;
12041203 callPath = this . transpileExpression ( node . expression ) ;
1205- if ( tsHelper . isFunctionWithContext ( type , this . checker )
1204+ const signatureDeclaration = signature . getDeclaration ( ) ;
1205+ if ( signatureDeclaration
1206+ && tsHelper . getDeclarationContextType ( signatureDeclaration , this . checker ) === ContextType . NonVoid
12061207 && ! ts . isPropertyAccessExpression ( node . expression )
12071208 && ! ts . isElementAccessExpression ( node . expression ) ) {
12081209 const context = this . isStrict ? ts . createNull ( ) : ts . createIdentifier ( "_G" ) ;
1209- params = this . transpileArguments ( node . arguments , sig , context ) ;
1210+ params = this . transpileArguments ( node . arguments , signature , context ) ;
12101211 } else {
1211- params = this . transpileArguments ( node . arguments , sig ) ;
1212+ params = this . transpileArguments ( node . arguments , signature ) ;
12121213 }
12131214 return isTupleReturn && ! isTupleReturnForward && ! isInDestructingAssignment && returnValueIsUsed
12141215 ? `({ ${ callPath } (${ params } ) })` : `${ callPath } (${ params } )` ;
@@ -1251,12 +1252,12 @@ export abstract class LuaTranspiler {
12511252 return this . transpileFunctionCallExpression ( node ) ;
12521253 }
12531254
1254- const sig = this . checker . getResolvedSignature ( node ) ;
1255+ const signature = this . checker . getResolvedSignature ( node ) ;
12551256
12561257 // Get the type of the function
12571258 if ( node . expression . expression . kind === ts . SyntaxKind . SuperKeyword ) {
12581259 // Super calls take the format of super.call(self,...)
1259- params = this . transpileArguments ( node . arguments , sig ,
1260+ params = this . transpileArguments ( node . arguments , signature ,
12601261 ts . createNode ( ts . SyntaxKind . ThisKeyword ) as ts . Expression ) ;
12611262 return `${ this . transpileExpression ( node . expression ) } (${ params } )` ;
12621263 } else {
@@ -1266,13 +1267,15 @@ export abstract class LuaTranspiler {
12661267 return `tostring(${ this . transpileExpression ( node . expression . expression ) } )` ;
12671268 } else if ( name === "hasOwnProperty" ) {
12681269 const expr = this . transpileExpression ( node . expression . expression ) ;
1269- params = this . transpileArguments ( node . arguments , sig ) ;
1270+ params = this . transpileArguments ( node . arguments , signature ) ;
12701271 return `(rawget(${ expr } , ${ params } )~=nil)` ;
12711272 } else {
1272- const type = this . checker . getTypeAtLocation ( node . expression ) ;
1273- const op = tsHelper . isFunctionWithContext ( type , this . checker ) ? ":" : "." ;
1273+ const signatureDeclaration = signature . getDeclaration ( ) ;
1274+ const op = ! signatureDeclaration
1275+ || tsHelper . getDeclarationContextType ( signatureDeclaration , this . checker ) !== ContextType . Void
1276+ ? ":" : "." ;
12741277 callPath = `${ this . transpileExpression ( node . expression . expression ) } ${ op } ${ name } ` ;
1275- params = this . transpileArguments ( node . arguments , sig ) ;
1278+ params = this . transpileArguments ( node . arguments , signature ) ;
12761279 return `${ callPath } (${ params } )` ;
12771280 }
12781281 }
@@ -1394,7 +1397,7 @@ export abstract class LuaTranspiler {
13941397 public transpileFunctionCallExpression ( node : ts . CallExpression ) : string {
13951398 const expression = node . expression as ts . PropertyAccessExpression ;
13961399 const callerType = this . checker . getTypeAtLocation ( expression . expression ) ;
1397- if ( ! tsHelper . isFunctionWithContext ( callerType , this . checker ) ) {
1400+ if ( tsHelper . getFunctionContextType ( callerType , this . checker ) === ContextType . Void ) {
13981401 throw TSTLErrors . UnsupportedMethodConversion ( node ) ;
13991402 }
14001403 const params = this . transpileArguments ( node . arguments ) ;
@@ -1435,10 +1438,12 @@ export abstract class LuaTranspiler {
14351438 fromTypeCache . add ( toType ) ;
14361439
14371440 // Check function assignments
1438- const fromHasContext = tsHelper . isFunctionWithContext ( fromType , this . checker ) ;
1439- const toHasContext = tsHelper . isFunctionWithContext ( toType , this . checker ) ;
1440- if ( fromHasContext !== toHasContext ) {
1441- if ( fromHasContext ) {
1441+ const fromContext = tsHelper . getFunctionContextType ( fromType , this . checker ) ;
1442+ const toContext = tsHelper . getFunctionContextType ( toType , this . checker ) ;
1443+ if ( fromContext === ContextType . Mixed || toContext === ContextType . Mixed ) {
1444+ throw TSTLErrors . UnsupportedOverloadAssignment ( node , toName ) ;
1445+ } else if ( fromContext !== toContext ) {
1446+ if ( toContext === ContextType . Void ) {
14421447 throw TSTLErrors . UnsupportedFunctionConversion ( node , toName ) ;
14431448 } else {
14441449 throw TSTLErrors . UnsupportedMethodConversion ( node , toName ) ;
@@ -1719,7 +1724,7 @@ export abstract class LuaTranspiler {
17191724 const methodName = this . transpileIdentifier ( node . name ) ;
17201725
17211726 const type = this . checker . getTypeAtLocation ( node ) ;
1722- const context = tsHelper . isFunctionWithContext ( type , this . checker ) ? "self" : null ;
1727+ const context = tsHelper . getFunctionContextType ( type , this . checker ) !== ContextType . Void ? "self" : null ;
17231728 const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters , context ) ;
17241729
17251730 let prefix = this . accessPrefix ( node ) ;
@@ -1805,7 +1810,7 @@ export abstract class LuaTranspiler {
18051810 }
18061811
18071812 const type = this . checker . getTypeAtLocation ( node ) ;
1808- const context = tsHelper . isFunctionWithContext ( type , this . checker ) ? "self" : null ;
1813+ const context = tsHelper . getFunctionContextType ( type , this . checker ) !== ContextType . Void ? "self" : null ;
18091814 const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters , context ) ;
18101815
18111816 // Build function header
@@ -2078,7 +2083,7 @@ export abstract class LuaTranspiler {
20782083
20792084 public transpileFunctionExpression ( node : ts . FunctionLikeDeclaration , context : string | null ) : string {
20802085 const type = this . checker . getTypeAtLocation ( node ) ;
2081- const hasContext = tsHelper . isFunctionWithContext ( type , this . checker ) ;
2086+ const hasContext = tsHelper . getFunctionContextType ( type , this . checker ) !== ContextType . Void ;
20822087 // Build parameter string
20832088 const [ paramNames , spreadIdentifier ] = this . transpileParameters ( node . parameters , hasContext ? context : null ) ;
20842089 let result = `function(${ paramNames . join ( "," ) } )\n` ;
0 commit comments