@@ -130,6 +130,7 @@ import {
130130 LiteralExpression ,
131131 LiteralKind ,
132132 NewExpression ,
133+ ObjectLiteralExpression ,
133134 ParenthesizedExpression ,
134135 PropertyAccessExpression ,
135136 TernaryExpression ,
@@ -6144,7 +6145,10 @@ export class Compiler extends DiagnosticEmitter {
61446145 assert ( ! implicitNegate ) ;
61456146 return this . compileStaticString ( ( < StringLiteralExpression > expression ) . value ) ;
61466147 }
6147- // case LiteralKind.OBJECT:
6148+ case LiteralKind . OBJECT : {
6149+ assert ( ! implicitNegate ) ;
6150+ return this . compileObjectLiteral ( < ObjectLiteralExpression > expression , contextualType ) ;
6151+ }
61486152 // case LiteralKind.REGEXP:
61496153 }
61506154 this . error (
@@ -6392,6 +6396,88 @@ export class Compiler extends DiagnosticEmitter {
63926396 }
63936397 }
63946398
6399+ compileObjectLiteral ( expression : ObjectLiteralExpression , contextualType : Type ) : ExpressionRef {
6400+ var module = this . module ;
6401+
6402+ // contextual type must be a class
6403+ var classReference = contextualType . classReference ;
6404+ if ( ! classReference || classReference . is ( CommonFlags . ABSTRACT ) ) {
6405+ this . error (
6406+ DiagnosticCode . Type_0_is_not_assignable_to_type_1 ,
6407+ expression . range , "<object>" , contextualType . toString ( )
6408+ ) ;
6409+ return module . createUnreachable ( ) ;
6410+ }
6411+
6412+ // if present, check that the constructor is compatible with object literals
6413+ var ctor = classReference . constructorInstance ;
6414+ if ( ctor ) {
6415+ if ( ctor . signature . requiredParameters ) {
6416+ this . error (
6417+ DiagnosticCode . Constructor_of_class_0_must_not_require_any_arguments ,
6418+ expression . range , classReference . toString ( )
6419+ ) ;
6420+ return module . createUnreachable ( ) ;
6421+ }
6422+ if ( ctor . is ( CommonFlags . PRIVATE ) ) {
6423+ this . error (
6424+ DiagnosticCode . Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration ,
6425+ expression . range , classReference . toString ( )
6426+ ) ;
6427+ return module . createUnreachable ( ) ;
6428+ }
6429+ if ( ctor . is ( CommonFlags . PROTECTED ) ) {
6430+ this . error (
6431+ DiagnosticCode . Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration ,
6432+ expression . range , classReference . toString ( )
6433+ ) ;
6434+ return module . createUnreachable ( ) ;
6435+ }
6436+ }
6437+
6438+ // check and compile field values
6439+ var names = expression . names ;
6440+ var numNames = names . length ;
6441+ var values = expression . values ;
6442+ var members = classReference . members ;
6443+ var hasErrors = false ;
6444+ var exprs = new Array < ExpressionRef > ( numNames + 2 ) ;
6445+ var tempLocal = this . currentFunction . getTempLocal ( this . options . usizeType ) ;
6446+ assert ( numNames == values . length ) ;
6447+ for ( let i = 0 , k = numNames ; i < k ; ++ i ) {
6448+ let member = members ? members . get ( names [ i ] . text ) : null ;
6449+ if ( ! member || member . kind != ElementKind . FIELD ) {
6450+ this . error (
6451+ DiagnosticCode . Property_0_does_not_exist_on_type_1 ,
6452+ names [ i ] . range , names [ i ] . text , classReference . toString ( )
6453+ ) ;
6454+ hasErrors = true ;
6455+ continue ;
6456+ }
6457+ let type = ( < Field > member ) . type ;
6458+ exprs [ i + 1 ] = this . module . createStore ( // TODO: handle setters as well
6459+ type . byteSize ,
6460+ this . module . createGetLocal ( tempLocal . index , this . options . nativeSizeType ) ,
6461+ this . compileExpression ( values [ i ] , ( < Field > member ) . type , ConversionKind . IMPLICIT , WrapMode . NONE ) ,
6462+ type . toNativeType ( ) ,
6463+ ( < Field > member ) . memoryOffset
6464+ ) ;
6465+ }
6466+ this . currentType = classReference . type . nonNullableType ;
6467+ if ( hasErrors ) return module . createUnreachable ( ) ;
6468+
6469+ // allocate a new instance first and assign 'this' to the temp. local
6470+ exprs [ 0 ] = module . createSetLocal (
6471+ tempLocal . index ,
6472+ compileBuiltinAllocate ( this , classReference , expression )
6473+ ) ;
6474+
6475+ // once all field values have been set, return 'this'
6476+ exprs [ exprs . length - 1 ] = module . createGetLocal ( tempLocal . index , this . options . nativeSizeType ) ;
6477+
6478+ return module . createBlock ( null , exprs , this . options . nativeSizeType ) ;
6479+ }
6480+
63956481 compileNewExpression ( expression : NewExpression , contextualType : Type ) : ExpressionRef {
63966482 var module = this . module ;
63976483 var options = this . options ;
0 commit comments