Skip to content

Commit 0f35057

Browse files
committed
[Native] Linking and initialization improvements
1 parent 494ff87 commit 0f35057

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

source/targets/genC.hexa

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,14 @@ class GenCxx {
653653
}
654654

655655
if let expr = expr {
656-
out.push(printFunctionReturnType(funcType))
656+
let returns = printFunctionReturnType(funcType)
657+
out.push(returns)
657658
out.push(' ' + name)
658659
out.push(printFunctionArguments(args, funcType, variadic))
659660

660661
let _currentReturnType = currentReturnType
661662
currentReturnType = getFunctionReturnType(funcType)
662-
out.push(' ' + printBlock(expr) + ';\n\n')
663+
out.push(' ' + printFunctionBody(expr, returns) + ';\n\n')
663664
currentReturnType = _currentReturnType
664665
}
665666
}}
@@ -733,8 +734,8 @@ class GenCxx {
733734

734735
switch type {
735736
// TODO same for const
736-
case ClassInstance(type, generics): {
737-
if type.name == 'ArrayByValue' {
737+
case ClassInstance(classType, generics): {
738+
if classType.name == 'ArrayByValue' {
738739
var arraySize = ''
739740
if let count = generics[1] {
740741
arraySize = '[' + Type.stringify(count) + ']'
@@ -768,6 +769,11 @@ class GenCxx {
768769
}
769770
globalVariables.push(';\n')
770771

772+
defaults = false
773+
} else if classType.name == 'ByValue' {
774+
globalVariables.push(stringifyType(type))
775+
globalVariables.push(' ' + name + '_')
776+
globalVariables.push(';\n')
771777
defaults = false
772778
}
773779
}
@@ -776,7 +782,20 @@ class GenCxx {
776782
if defaults {
777783
//types.push('void* ' + name + '_;\n')
778784
globalVariables.push(stringifyType(type))
779-
globalVariables.push(' ' + name + '_;\n')
785+
globalVariables.push(' ' + name + '_') // TODO respect `@rename`
786+
787+
if let link = link, let linkName = link.linkName {
788+
if exports and extraUnderscore and linkName.startsWith('_') {
789+
globalVariables.push(' HEXA_LINK("_')
790+
} else {
791+
globalVariables.push(' HEXA_LINK("')
792+
}
793+
794+
globalVariables.push(linkName)
795+
globalVariables.push('")')
796+
}
797+
798+
globalVariables.push(';\n')
780799
if expr != null {
781800
let value String = printExpression(expr)
782801

@@ -1047,6 +1066,24 @@ class GenCxx {
10471066
}
10481067
}
10491068

1069+
// Add extra `return 0` to avoid stack corruption
1070+
fun printFunctionBody(s: Statement, returns: String): String {
1071+
if returns != 'void', returns.endsWith('*') or [
1072+
'float','int',
1073+
'uint8_t','uint16_t','uint32_t','uint64_t',
1074+
'int8_t','int16_t','int32_t','int64_t'
1075+
].includes(returns) {
1076+
switch s {
1077+
case Block(el):
1078+
let block = el.clone()
1079+
block.push(Statement.Return(Expression.Int(0)))
1080+
return printBlock(Statement.Block(block))
1081+
}
1082+
}
1083+
1084+
return printBlock(s)
1085+
}
1086+
10501087
var metaBlock: [String] = []
10511088
var tempId = 0
10521089

@@ -1634,12 +1671,18 @@ class GenCxx {
16341671
return 'String_$fromBool_(' + string + ')'
16351672
}
16361673

1637-
if fromValue == project.typer.typeString { switch to {
1638-
case ClassInstance(type): if (type.name == 'ConstCharPointer') {
1639-
console.log(type)
1640-
return '(' + string + ')->bytes_'
1674+
if fromValue == project.typer.typeString {
1675+
switch to {
1676+
case ClassInstance(type):
1677+
if type.name == 'ConstCharPointer' {
1678+
return '(' + string + ')->bytes_'
1679+
}
1680+
1681+
if type.name == 'ConstArrayPointer' {
1682+
return '(' + string + ')->bytes_'
1683+
}
16411684
}
1642-
}}
1685+
}
16431686

16441687
switch fromValue {
16451688
case Nullable(_):

0 commit comments

Comments
 (0)