Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KMP] Fix issue: memory leak found in Base58.decode in iOS #4031

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions codegen/lib/kotlin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ def self.js_parameters(params)
def self.calling_parameters_ios(params)
names = params.map do |param|
name = fix_name(param.name)
if param.type.name == :data
"#{name}Data#{convert_calling_type_ios(param.type)}"
elsif param.type.name == :string
"#{name}String#{convert_calling_type_ios(param.type)}"
else
"#{name}#{convert_calling_type_ios(param.type)}"
end
end
names.join(', ')
end
Expand All @@ -55,8 +61,6 @@ def self.fix_name(name)
case name
when ''
"value"
when 'val'
"value"
when 'return'
'`return`'
else
Expand All @@ -65,19 +69,12 @@ def self.fix_name(name)
end

def self.convert_calling_type_ios(t)
case t.name
when :data
"#{if t.is_nullable then '?' else '' end}.toTwData()"
when :string
"#{if t.is_nullable then '?' else '' end}.toTwString()"
if t.is_enum
"#{if t.is_nullable then '?' else '' end}.nativeValue"
elsif t.is_class
"#{if t.is_nullable then '?' else '' end}.pointer"
else
if t.is_enum
"#{if t.is_nullable then '?' else '' end}.nativeValue"
elsif t.is_class
"#{if t.is_nullable then '?' else '' end}.pointer"
else
''
end
''
end
end

Expand Down
50 changes: 39 additions & 11 deletions codegen/lib/templates/kotlin/ios_class.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cnames.structs.TW<%= entity.name %>
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.toCValues

<% constructors = entity.static_methods.select { |method| method.name.start_with?('Create') } -%>
<% methods = entity.methods.select { |method| not method.name.start_with?('Delete') } -%>
Expand All @@ -14,12 +15,9 @@ actual class <%= entity.name %> constructor(

<% if constructor.return_type.is_nullable -%>
@Throws(IllegalArgumentException::class)
actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this(
TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>) ?: throw IllegalArgumentException()
<% else -%>
actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this(
TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>)!!
<% end -%>
actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this(
wrapperTW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.arguments(constructor.parameters) %>)
)
<% end -%>
<%# Property declarations -%>
Expand All @@ -31,12 +29,38 @@ actual class <%= entity.name %> constructor(
<%# Method declarations -%>
<% methods.each do |method| -%>

actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> =
<%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(pointer#{', ' if not method.parameters.one?}#{KotlinHelper.calling_parameters_ios(method.parameters.drop(1))})") %>
actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> {
<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%>
val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(pointer#{', ' if not method.parameters.one?}#{KotlinHelper.calling_parameters_ios(method.parameters.drop(1))})") %>
<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%>
return result
}
<% end -%>
<% if entity.static_properties.any? || static_methods.any? -%>
<% if entity.static_properties.any? || static_methods.any? || constructors.any? -%>

<%= if entity.static_properties.any? || static_methods.any? then "actual" else "private" end %> companion object {
<%# Constructor wrappers -%>
<% if constructors.any? -%>
<% constructors.each do |constructor| -%>

actual companion object {
<% if constructor.return_type.is_nullable -%>
@Throws(IllegalArgumentException::class)
<% end -%>
private fun wrapperTW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.parameters(constructor.parameters) %>): CPointer<TW<%= entity.name %>> {
<%= render('kotlin/ios_parameter_access.erb', { method: constructor }) -%>
val result = TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>)
<%= render('kotlin/ios_parameter_release.erb', { method: constructor }) -%>
<% if constructor.return_type.is_nullable -%>
if (result == null) {
throw IllegalArgumentException()
}
return result
<% else -%>
return result!!
<% end -%>
}
<% end -%>
<% end -%>
<%# Static property declarations -%>
<% entity.static_properties.each do |property| -%>

Expand All @@ -46,8 +70,12 @@ actual class <%= entity.name %> constructor(
<%# Static method declarations -%>
<% static_methods.each do |method| -%>

actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> =
<%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %>
actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> {
<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%>
val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %>
<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%>
return result
}
<% end -%>
}
<% end -%>
Expand Down
8 changes: 6 additions & 2 deletions codegen/lib/templates/kotlin/ios_enum.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ actual enum class <%= entity.name %>(
<%- entity.methods.each do |method| -%>
<%- next if method.name.start_with?('Delete') -%>

actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> =
TW<%= entity.name %><%= method.name %>(value<%= ', ' if not method.parameters.one? %><%= KotlinHelper.calling_parameters_ios(method.parameters.drop(1)) %>)<%= KotlinHelper.convert_calling_return_type_ios(method.return_type) %>
actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> {
<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%>
val result = TW<%= entity.name %><%= method.name %>(value<%= ', ' if not method.parameters.one? %><%= KotlinHelper.calling_parameters_ios(method.parameters.drop(1)) %>)<%= KotlinHelper.convert_calling_return_type_ios(method.return_type) %>
<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%>
return result
}
<%- end -%>
<%# Value -%>
<% if entity.cases.any? { |e| !e.value.nil? } -%>
Expand Down
26 changes: 26 additions & 0 deletions codegen/lib/templates/kotlin/ios_parameter_access.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%
method = locals[:method]

method.parameters.each do |param| -%>
<% if param.type.name == :data -%>
<% if param.type.is_nullable -%>
val <%= param.name %>Data = if (<%= param.name %> == null) {
null
} else {
TWDataCreateWithBytes(<%= param.name %>.toUByteArray().toCValues(), <%= param.name %>.size.toULong())
}
<% else -%>
val <%= param.name %>Data = TWDataCreateWithBytes(<%= param.name %>.toUByteArray().toCValues(), <%= param.name %>.size.toULong())
<% end -%>
<% elsif param.type.name == :string -%>
<% if param.type.is_nullable -%>
val <%= param.name %>String = if (<%= param.name %> == null) {
null
} else {
TWStringCreateWithUTF8Bytes(<%= param.name %>)
}
<% else -%>
val <%= param.name %>String = TWStringCreateWithUTF8Bytes(<%= param.name %>)
<% end -%>
<% end -%>
<% end -%>
22 changes: 22 additions & 0 deletions codegen/lib/templates/kotlin/ios_parameter_release.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%
method = locals[:method]

method.parameters.each do |param| -%>
<% if param.type.name == :data -%>
<% if param.type.is_nullable -%>
if (<%= param.name %>Data != null) {
TWDataDelete(<%= param.name %>Data)
}
<% else -%>
TWDataDelete(<%= param.name %>Data)
<% end -%>
<% elsif param.type.name == :string -%>
<% if param.type.is_nullable -%>
if (<%= param.name %>String != null) {
TWStringDelete(<%= param.name %>String)
}
<% else -%>
TWStringDelete(<%= param.name %>String)
<% end -%>
<% end -%>
<% end -%>
10 changes: 8 additions & 2 deletions codegen/lib/templates/kotlin/ios_struct.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<%= render('kotlin/package.erb') %>

import kotlinx.cinterop.toCValues

actual object <%= entity.name %> {
<%# Static property declarations -%>
<% entity.static_properties.each do |property| -%>
Expand All @@ -10,7 +12,11 @@ actual object <%= entity.name %> {
<% entity.static_methods.each do |method| -%>
<% next if method.name.start_with?('Create') -%>

actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> =
<%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %>
actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> {
<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%>
val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %>
<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%>
return result
}
<% end -%>
}
Loading