Blame view

tests/Test1.nim 993 Bytes
01dbfa89   Göran Krampe   Test code with co...
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
  # This import adds macros to generate FFI Smalltalk code
  import squeaknim
  
  # Just compile with "nim c Test1.nim", copy resulting lib and Test1.st
  # to a Squeak directory, file in Test1.st and then it should be fine
  # to call foo, see class side of Test1 
  
  type
    MyFloat = float32
    Vector3* = object
      x*, y*, z*: MyFloat
  
  # This sets the name of the FFI module (and class) and a Smalltalk prefix
  # to avoid collisions for generated classes, like Vector3 which turns into
  # URVector3 in Smalltalk
  setModulename "Test1", "UR"
  
  # Generate the Smalltalk class URVector3 representing Vector3
  wrapObject(Vector3)
  
  # Generate declarations for Smalltalk code 
  writeExternalLibrary()
  
  # Here follows Nim procedures that will be callable via FFI
  # Note that exportSt pragma to export.
  
  proc foo*(a, b: Vector3, c: int): cstring {.exportSt: "bar".} =
    # Just a silly test proc
    result = "x plus y plus c " & $(a.x + b.y + c.float)
  
  # Write the Smalltalk code to file
  writeSmallTalkCode("Test1.st")