Skip to content

Commit

Permalink
Object decl arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyfa committed Apr 1, 2024
1 parent da68821 commit 0356312
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/cscompiler/components/CSCompiler_Expr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class CSCompiler_Expr extends CSCompiler_Base {
def: CSNew(
"haxe.lang.DynamicObject",
[],
[] // TODO fields
compileObjectDeclArgs(fields)
)
})
}
Expand Down Expand Up @@ -476,6 +476,38 @@ class CSCompiler_Expr extends CSCompiler_Base {
);
}

/**
Generate arguments given to `new haxe.lang.DynamicObject()`.
Instead of transpiling to string keys, we encode as int hashes.
**/
function compileObjectDeclArgs(fields:Array<{name:String, expr:TypedExpr}>):Array<CSExpr> {

var hashExprs:Array<CSExpr> = [];
var valueExprs:Array<CSExpr> = [];

for (field in fields) {
final hash = compiler.nameToHash(field.name);

hashExprs.push({
type: CSInst('int', []),
def: CSConst(CSInt(hash))
});

valueExprs.push(
csStatementToExpr(_compileExpression(field.expr))
);
}

return [{
type: CSInst('int[]', []),
def: CSArrayDecl(hashExprs)
}, {
type: CSInst('object[]', []),
def: CSArrayDecl(valueExprs)
}];

}

/**
Generate an expression given a `Unop` and typed expression (from `TypedExprDef.TUnop`).
**/
Expand Down

0 comments on commit 0356312

Please sign in to comment.