tunsafeaddr.nim 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. output: '''12
  3. 4'''
  4. """
  5. {.emit: """
  6. NI sum(NI* a, NI len) {
  7. NI i, result = 0;
  8. for (i = 0; i < len; ++i) result += a[i];
  9. return result;
  10. }
  11. """.}
  12. proc sum(a: ptr int; len: int): int {.importc, nodecl.}
  13. proc main =
  14. let foo = [8, 3, 1]
  15. echo sum(unsafeAddr foo[0], foo.len)
  16. # bug #3736
  17. proc p(x: seq[int]) = discard x[0].unsafeAddr # works
  18. proc q(x: seq[SomeInteger]) = discard x[0].unsafeAddr # doesn't work
  19. p(@[1])
  20. q(@[1])
  21. main()
  22. # bug #9403
  23. type
  24. MyObj = ref object
  25. len: int
  26. val: UncheckedArray[uint64]
  27. proc spot(x: MyObj): int64 =
  28. result = cast[UncheckedArray[int64]](x.val)[0]
  29. proc newMyObj(len: int): MyObj =
  30. unsafeNew(result, sizeof(result[]) + len * sizeof(uint64))
  31. result.len = len
  32. result.val[0] = 4u64
  33. result.val[1] = 8u64
  34. echo spot(newMyObj(2))