@@ -8,11 +8,7 @@ import {
88 getIdentifierExportScope ,
99 hasDefaultExportModifier ,
1010} from "../../utils/export" ;
11- import {
12- createExportsIdentifier ,
13- createLocalOrExportedOrGlobalDeclaration ,
14- createSelfIdentifier ,
15- } from "../../utils/lua-ast" ;
11+ import { createExportsIdentifier , createLocalOrExportedOrGlobalDeclaration } from "../../utils/lua-ast" ;
1612import { importLuaLibFeature , LuaLibFeature , transformLuaLibFunction } from "../../utils/lualib" ;
1713import { hasMemberInClassOrAncestor } from "./members/accessors" ;
1814import { getExtendedTypeNode , isStaticNode } from "./utils" ;
@@ -27,20 +23,19 @@ export function createClassSetup(
2723) : lua . Statement [ ] {
2824 const result : lua . Statement [ ] = [ ] ;
2925
30- // [____exports.]className = {}
31- const classTable : lua . Expression = lua . createTableExpression ( ) ;
32-
33- const isDefaultExport = hasDefaultExportModifier ( statement ) ;
26+ // __TS__Class()
27+ const classInitializer = transformLuaLibFunction ( context , LuaLibFeature . Class , statement ) ;
3428
35- const defaultExportLeftHandSide = isDefaultExport
29+ const defaultExportLeftHandSide = hasDefaultExportModifier ( statement )
3630 ? lua . createTableIndexExpression ( createExportsIdentifier ( ) , createDefaultExportStringLiteral ( statement ) )
3731 : undefined ;
3832
39- const classVar = defaultExportLeftHandSide
40- ? [ lua . createAssignmentStatement ( defaultExportLeftHandSide , classTable , statement ) ]
41- : createLocalOrExportedOrGlobalDeclaration ( context , className , classTable , statement ) ;
42-
43- result . push ( ...classVar ) ;
33+ // [____exports.]className = __TS__Class()
34+ if ( defaultExportLeftHandSide ) {
35+ result . push ( lua . createAssignmentStatement ( defaultExportLeftHandSide , classInitializer , statement ) ) ;
36+ } else {
37+ result . push ( ...createLocalOrExportedOrGlobalDeclaration ( context , className , classInitializer , statement ) ) ;
38+ }
4439
4540 if ( defaultExportLeftHandSide ) {
4641 // local localClassName = ____exports.default
@@ -79,14 +74,6 @@ export function createClassSetup(
7974 importLuaLibFeature ( context , LuaLibFeature . ClassIndex ) ;
8075 }
8176
82- // localClassName.__index = localClassName
83- const classIndex = lua . createTableIndexExpression (
84- lua . cloneIdentifier ( localClassName ) ,
85- lua . createStringLiteral ( "__index" )
86- ) ;
87- const assignClassIndex = lua . createAssignmentStatement ( classIndex , lua . cloneIdentifier ( localClassName ) , statement ) ;
88- result . push ( assignClassIndex ) ;
89-
9077 // localClassName.____setters = {}
9178 if ( statement . members . some ( m => ts . isSetAccessor ( m ) && isStaticNode ( m ) ) ) {
9279 const classSetters = lua . createTableIndexExpression (
@@ -99,12 +86,9 @@ export function createClassSetup(
9986 importLuaLibFeature ( context , LuaLibFeature . ClassNewIndex ) ;
10087 }
10188
102- // localClassName.prototype = {}
89+ // localClassName.prototype
10390 const createClassPrototype = ( ) =>
10491 lua . createTableIndexExpression ( lua . cloneIdentifier ( localClassName ) , lua . createStringLiteral ( "prototype" ) ) ;
105- const classPrototypeTable = lua . createTableExpression ( ) ;
106- const assignClassPrototype = lua . createAssignmentStatement ( createClassPrototype ( ) , classPrototypeTable , statement ) ;
107- result . push ( assignClassPrototype ) ;
10892
10993 // localClassName.prototype.____getters = {}
11094 if ( statement . members . some ( m => ts . isGetAccessor ( m ) && ! isStaticNode ( m ) ) ) {
@@ -120,23 +104,15 @@ export function createClassSetup(
120104 result . push ( assignClassPrototypeGetters ) ;
121105 }
122106
123- const classPrototypeIndex = lua . createTableIndexExpression (
124- createClassPrototype ( ) ,
125- lua . createStringLiteral ( "__index" )
126- ) ;
127107 if ( hasMemberInClassOrAncestor ( context , statement , m => ts . isGetAccessor ( m ) && ! isStaticNode ( m ) ) ) {
128108 // localClassName.prototype.__index = __TS__Index(localClassName.prototype)
129- const assignClassPrototypeIndex = lua . createAssignmentStatement (
130- classPrototypeIndex ,
131- transformLuaLibFunction ( context , LuaLibFeature . Index , undefined , createClassPrototype ( ) ) ,
132- statement
109+ const classPrototypeIndex = lua . createTableIndexExpression (
110+ createClassPrototype ( ) ,
111+ lua . createStringLiteral ( "__index" )
133112 ) ;
134- result . push ( assignClassPrototypeIndex ) ;
135- } else {
136- // localClassName.prototype.__index = localClassName.prototype
137113 const assignClassPrototypeIndex = lua . createAssignmentStatement (
138114 classPrototypeIndex ,
139- createClassPrototype ( ) ,
115+ transformLuaLibFunction ( context , LuaLibFeature . Index , undefined , createClassPrototype ( ) ) ,
140116 statement
141117 ) ;
142118 result . push ( assignClassPrototypeIndex ) ;
@@ -170,18 +146,6 @@ export function createClassSetup(
170146 result . push ( assignClassPrototypeIndex ) ;
171147 }
172148
173- // localClassName.prototype.constructor = localClassName
174- const classPrototypeConstructor = lua . createTableIndexExpression (
175- createClassPrototype ( ) ,
176- lua . createStringLiteral ( "constructor" )
177- ) ;
178- const assignClassPrototypeConstructor = lua . createAssignmentStatement (
179- classPrototypeConstructor ,
180- lua . cloneIdentifier ( localClassName ) ,
181- statement
182- ) ;
183- result . push ( assignClassPrototypeConstructor ) ;
184-
185149 const hasStaticGetters = hasMemberInClassOrAncestor (
186150 context ,
187151 statement ,
@@ -303,46 +267,5 @@ export function createClassSetup(
303267 result . push ( setClassMetatable ) ;
304268 }
305269
306- const newFuncStatements : lua . Statement [ ] = [ ] ;
307-
308- // local self = setmetatable({}, localClassName.prototype)
309- const assignSelf = lua . createVariableDeclarationStatement (
310- createSelfIdentifier ( ) ,
311- lua . createCallExpression ( lua . createIdentifier ( "setmetatable" ) , [
312- lua . createTableExpression ( ) ,
313- createClassPrototype ( ) ,
314- ] ) ,
315- statement
316- ) ;
317- newFuncStatements . push ( assignSelf ) ;
318-
319- // self:____constructor(...)
320- const callConstructor = lua . createExpressionStatement (
321- lua . createMethodCallExpression ( createSelfIdentifier ( ) , lua . createIdentifier ( "____constructor" ) , [
322- lua . createDotsLiteral ( ) ,
323- ] ) ,
324- statement
325- ) ;
326- newFuncStatements . push ( callConstructor ) ;
327-
328- // return self
329- const returnSelf = lua . createReturnStatement ( [ createSelfIdentifier ( ) ] , statement ) ;
330- newFuncStatements . push ( returnSelf ) ;
331-
332- // function localClassName.new(construct, ...) ... end
333- // or function export.localClassName.new(construct, ...) ... end
334- const newFunc = lua . createAssignmentStatement (
335- lua . createTableIndexExpression ( lua . cloneIdentifier ( localClassName ) , lua . createStringLiteral ( "new" ) ) ,
336- lua . createFunctionExpression (
337- lua . createBlock ( newFuncStatements ) ,
338- undefined ,
339- lua . createDotsLiteral ( ) ,
340- undefined ,
341- lua . FunctionExpressionFlags . Declaration
342- ) ,
343- statement
344- ) ;
345- result . push ( newFunc ) ;
346-
347270 return result ;
348271}
0 commit comments