Blame view

testlib/testlib.nim 927 Bytes
70473583   Göran Krampe   First sample and ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  # We exercise some trivial types and how they map.
  #
  #   SqueakFFI      Nim
  #   =========      ======
  #   long       =>  int
  #   char*      =>  cstring        
  
  import math
  
  # A single proc, returns an int. Since we are on 32 bits
  # an int is 4 bytes (same size as pointer) and in Squeak FFI
  # this is a long. The exportc pragma ensures that the exported
  # name for this proc is exactly "hello" and not mangled.
  proc hello*(): int {.exportc.} =
    42
  
  # Trivial, a Nim string can be sent as a cstring because they are
  # automatically 0-terminated.
  proc foo*(): cstring {.exportc.} =
    "hey"
  
  # Not a problem taking int arguments, they are "long" in Squeak FFI.
  proc add*(x, y:int): int {.exportc.} =
    x + y
  
  # Just return the length of a cstring
  proc length*(x: cstring): int {.exportc.} =
    len(x)
  
  # Here we convert cstrings to Nim strings, concatenate and return.
  proc concat*(x, y: cstring): cstring {.exportc.} =
    $x & $y