Переглянути джерело

Nim bindings: remove to_Range and clean up type mappings

- to_Range() converter removed, to easy to f*ck up and end
  up with corrupted memory
- size_t mapping changed from Nim's uint to int (because Nim's
  sizeof returns int
- change Range.pointer to Range.addr (looks better since it is
  usually used with Nim's unsafeAddr)
Andre Weissflog 3 роки тому
батько
коміт
49ab1f46c1
1 змінених файлів з 11 додано та 7 видалено
  1. 11 7
      bindgen/gen_nim.py

+ 11 - 7
bindgen/gen_nim.py

@@ -42,7 +42,7 @@ overrides = {
     'SG_BUFFERTYPE_VERTEXBUFFER':   'SG_BUFFERTYPE_VERTEX_BUFFER',
     'SG_BUFFERTYPE_INDEXBUFFER':    'SG_BUFFERTYPE_INDEX_BUFFER',
     'SG_ACTION_DONTCARE':           'SG_ACTION_DONT_CARE',
-    'ptr':                          'pointer', # range ptr
+    'ptr':                          'addr', # range ptr
 }
 
 enumPrefixOverrides = {
@@ -90,7 +90,7 @@ prim_types = {
     'double':       'float64',
     'uintptr_t':    'uint',
     'intptr_t':     'int',
-    'size_t':       'uint',
+    'size_t':       'int',  # not a bug, Nim's sizeof() returns int
 }
 
 prim_defaults = {
@@ -565,11 +565,6 @@ def gen_extra(inp):
         l('else:')
         l('  error("unsupported platform")')
         l('')
-    if inp['prefix'] in ['sg_', 'sdtx_', 'sshape_']:
-        l('# helper function to convert "anything" into a Range')
-        l('converter to_Range*[T](source: T): Range =')
-        l('  Range(pointer: source.unsafeAddr, size: source.sizeof.uint)')
-        l('')
     if inp['prefix'] in ['sg_']:
         l('## Convert a 4-element tuple of numbers to a gfx.Color')
         l('converter toColor*[R:SomeNumber,G:SomeNumber,B:SomeNumber,A:SomeNumber](rgba: tuple [r:R,g:G,b:B,a:A]):Color =')
@@ -579,6 +574,15 @@ def gen_extra(inp):
         l('converter toColor*[R:SomeNumber,G:SomeNumber,B:SomeNumber](rgba: tuple [r:R,g:G,b:B]):Color =')
         l('  Color(r:rgba.r.float32, g:rgba.g.float32, b:rgba.b.float32, a:1.float32)')
         l('')
+    # NOTE: this simplistic to_Range() converter has various issues, some of them dangerous:
+    #   - doesn't work as expected for slice types
+    #   - it's very easy to create a range that points to invalid memory
+    #     (so far observed for stack-allocated structs <= 16 bytes)
+    #if inp['prefix'] in ['sg_', 'sdtx_', 'sshape_']:
+    #    l('# helper function to convert "anything" into a Range')
+    #    l('converter to_Range*[T](source: T): Range =')
+    #    l('  Range(addr: source.unsafeAddr, size: source.sizeof.uint)')
+    #    l('')
     c_source_path = '/'.join(c_source_paths[inp['prefix']].split('/')[3:])
     l('{.passc:"-DSOKOL_NIM_IMPL".}')
     l(f'{{.compile:"{c_source_path}".}}')