Re: "First natively compiled language with hot code-reloading at runtime"

2019-04-30 Thread def_pri_pub
Hey, a while back I wrote an article on this subject: 
[https://16bpp.net/page/hot-loading-code-in-nim](https://16bpp.net/page/hot-loading-code-in-nim)

Neat work you've done though.


Geometry Library

2019-02-19 Thread def_pri_pub
I know there are packages out there such as `glm` (which I'm already using). 
But are there any good **pure Nim** packages/libraries for doing geometric 
computations? I'm looking to target JS.


Re: "Nim needs better documentation" - share your thoughts

2019-01-14 Thread def_pri_pub
I think the Leap Motion C API docs look quite nice:

[https://developer.leapmotion.com/documentation/v4/index.html](https://developer.leapmotion.com/documentation/v4/index.html)


SQL modules args/string santization

2019-01-12 Thread def_pri_pub
I've been playing around with the db_sqlite module a bit. Looking at the procs 
for exec() and getRow()/getAllRows() It can take an option args parameter. are 
these arguments automatically sanitized for me, or do I manually need to do it 
myself. I'd like to avoid a "Bobby Tables" incident in the app I'm writing.


Re: "Nim needs better documentation" - share your thoughts

2019-01-11 Thread def_pri_pub
Hi, it's been a while since I posted here in the community (or done much with 
Nim), but here at least is my $0.02:

I'm not a big fan right now of how inline documentation comments/strings work.

  1. This may be more of a personal preference, but I don't like putting the 
documentation right in the body of the function. It looks like this was 
influenced from Python. Java, C#, C/C++ doc systems (e.g. Doxygen), Rust, 
Swift, PHP; only to name a few of the major languages out there, put their 
doc-strings right above function, not in it.
  2. I would rather see a tool like Doxygen be used to generate the 
documentation for Nim. It's very mature, flexible, and generates easy to use 
docs. One other thing is that Doxygen generated docs typically are quite 
navigable. I find it a little hard to search through Nim's documentation a the 
moment. I'm not sure about the difficulty integrating the Nim language into 
Doxygen though




Re: Binding a JavsScript object that's not part of the DOM

2018-05-15 Thread def_pri_pub
Does it live update to when the style changes? That's one of the benefits 
CSSStyleDeclaration is that if you change the CSS for an object, you don't need 
to query for the style declaration again.


Binding a JavsScript object that's not part of the DOM

2018-05-13 Thread def_pri_pub
I'm writing an app right now and I need to figure out what the width & height 
of div, with it's applied CSS styling. Back in vanilla JS, there is the 
function 
[getComputedStyle()](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle)
 which will give me 
[CSSStyleDeclaration](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration)
 object. That object is not part of the DOM or the BOM, it's part of the CSSOM. 
I don't really know how to bind this. How would I go about this?


Re: Nim and hot loading - any experiences to share?

2017-10-01 Thread def_pri_pub
Sorry for the belated reply.

I'm a little confused at what you're trying to achieve here. Can you explain it 
in a few words what you want? It it something with trying to get dynamically 
allocated memory to place nice with hot-loaded code?


Re: Nim and hot loading - any experiences to share?

2017-09-24 Thread def_pri_pub
I wrote this article a while back:

[https://16bpp.net/page/hot-loading-code-in-nim](https://16bpp.net/page/hot-loading-code-in-nim)


Re: Using `string` vs `seq[uint8]` for non-text data

2017-09-21 Thread def_pri_pub
Sorry to be a bit of a pest, but I want to know if this is the thing I should 
be doing or not for my Nim code now?


Re: Using `string` vs `seq[uint8]` for non-text data

2017-09-19 Thread def_pri_pub
Okay, that sounds better to me. So for in my case with my stb_image wrapper, 
should I switch the API now to use seq[byte] or should I hold off on it for a 
little longer?


Using `string` vs `seq[uint8]` for non-text data

2017-09-17 Thread def_pri_pub
This stems from some discussion I've had on the stb_image bindings: 
[https://gitlab.com/define-private-public/stb_image-Nim/issues/4#note_40535183](https://gitlab.com/define-private-public/stb_image-Nim/issues/4#note_40535183)

I'd like some discussion on the use of the string datatype for non-text data. 
**I.e. using `string` as a buffer for binary data.**

It's always been my impression that if a language/API provides a string 
datatype, that strings should only be used for text data; e.g. Qt's QString and 
QByteArray objects. strings typically have the notion of being data that is 
mean for humans to read. Where an explicit byte array should be used for non 
text data (e.g. image data). Even though under the hood (most of the time) 
there is a char *.

In the issue ticket I linked, Eduardo Bart mentioned that int the Nim codebase 
that string is also used as a buffer (instead of a seq[uint8]), e.g. readFile() 
and writeFile(). Where even the data those procs could be returning binary and 
not text.

How do others here feel about this?


Re: Killing an AsyncHttpServer

2017-09-09 Thread def_pri_pub
Yeah, I have the killServer = true in a block somewhere.

Adding return after server.close() does work, but I need to mark server with 
{.threadsafe.}. It also causes an issue with the HTTP Request (on the browser 
side) doesn't get finished and just hangs. Interestingly enough this doesn't 
actually stop the application then and there. It's if I try to send a request 
after killServer = true that it will end the program with this output:


Traceback (most recent call last)
Server.nim(76)   Server
asyncdispatch.nim(1010)  waitFor
asyncdispatch.nim(1013)  poll
Error: unhandled exception: No handles or timers registered in dispatcher. 
[ValueError]



Re: Killing an AsyncHttpServer

2017-09-06 Thread def_pri_pub
It now compiles successfully, but it doesn't seem to be killing the server when 
I trip that killServer block. It will run over the line and continue on with 
execution.


Killing an AsyncHttpServer

2017-09-05 Thread def_pri_pub
So I've got some code like this:


import asyncDispatch
import asynchttpserver

# Create the server object and state data
var
  server = newAsyncHttpServer()
let port = 8000

# ...

proc handleRequest(req: Request) {.async.}=
  ## Handle an incomming connection
  var killServer = false
  
  # ...
  
  if killServer:
server.close()
  
  # Write a response
  var headers = newHttpHeaders()
  headers.add("Content-Type", "text/html")
  await req.respond(Http200, mkPage(pageViews, killServer), headers)


# Handle requests
echo("Listening for connections on http://localhost:; & $port & "/")
waitFor server.serve(Port(port), handleRequest)


If I try to call server.close() I keep on getting this error: 


Server.nim(74, 15) Error: type mismatch: got (AsyncHttpServer, Port, proc 
(req: Request): Future[system.void]{.locks: .})
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
  callback: proc (request: Request): Future[void]; address = ""): 
Future[void]


I've tried creating a wrapper async function around the close call, that didn't 
work. I tried using an event callback, that didn't work either. Both other 
produced the same compilation errors. What's going on here?


Re: Having a hard time getting raylib bindings to work

2017-08-10 Thread def_pri_pub
So this is a fun issue now...

I've gotten the bindings to work, but when you want to do something with 
Textures, they won't display on the screen. The raylib tracelog says everything 
is okay when it came to loading the assets. I'm tracking the issue here: 
[https://gitlab.com/define-private-public/raylib-Nim/issues/1](https://gitlab.com/define-private-public/raylib-Nim/issues/1)

I've tried:

  * dynamically loading raylib.so at runtime, (via the dynlib pragma)
  * statically linking raylib
  * statically compiling the raylib source into raylib.nim (via the compile 
pragma)



The only thing I can think of is that there is some issue with how c2Nim 
translated the Image data structure. I think it has to do with that void * 
pointer.


typedef struct Image {
void *data; // Image raw data
int width;  // Image base width
int height; // Image base height
int mipmaps;// Mipmap levels, 1 by default
int format; // Data format (TextureFormat type)
} Image;



type
  Image* = object
data*: pointer ##  Image raw data
width*: cint   ##  Image base width
height*: cint  ##  Image base height
mipmaps*: cint ##  Mipmap levels, 1 by default
format*: cint  ##  Data format (TextureFormat type)


Anyone got any ideas?


Re: Having a hard time getting raylib bindings to work

2017-07-25 Thread def_pri_pub
Taking a look [at the man page for 
dlopen(3)](http://man7.org/linux/man-pages/man3/dlopen.3.html), I think the 
easiest thing to do would to be to wrap the dlopen() call in the Nim code in 
something that sets and then resets the LD_LIBRARY_PATH environment variable.

Taking a look at the dlopen() imports in 
[dylib.nim](https://github.com/nim-lang/Nim/blob/75345ad1aacf8e2a4d43e4e89f16f6a641d6a9c3/lib/pure/dynlib.nim#L85)
 and 
[dyncalls.nim](https://github.com/nim-lang/Nim/blob/7e351fc7fa96b4d560c5a51118bab22abb590585/lib/system/dyncalls.nim#L67),
 we could use the os modules' getEnv() and putEnv() functions.

E.g.: 


from os import getEnv, putEnv

proc real_dlopen(path: cstring, mode: cint): LibHandle
  {.importc: "dlopen", header: "".}

proc dlopen(path: cstring, mode: cint): LibHandle=
  # Modify the library search path
  var
origLibPath = getEnv("LD_LIBRARY_PATH")
newLibPath = originalLibPath & ":/usr/local/lib"
  
  setEnv("LD_LIBRARY_PATH", newLibPath)
  result = real_dlopen(path, mode)
  setEnv("LD_LIBRARY_PATH", origLibPath)



I think that would do it. Looking also [at OS X's 
dlopen()](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/dlopen.3.html),
 it might be best to add in a block that will get the correct environment 
variable to get, set, and reset. Interestingly enough, OS X already searches 
/usr/local/lib by defualt. But I'm not sure about /usr/local/Cellar, where is 
where homebrew likes to put stuff.

I'll leave the details up to you, but I think this needs to be the case for 
Linux at least.


Re: Having a hard time getting raylib bindings to work

2017-07-24 Thread def_pri_pub
I consider it something that Nim should be concerned about. It's already caused 
me a fair bit of grief trying to get the bindings working on Linux and I'm sure 
anyone else will run into the same issue down the road. Keep in mind it's not 
my .so that it was failing to find, but an .so that mine depended on, which was 
in a well known install location.


Re: Having a hard time getting raylib bindings to work

2017-07-24 Thread def_pri_pub
I think I figured out why setting export LD_LIBRARY_PATH=/usr/local/lib was 
necessary to make it work. [The dlopen() 
function](http://man7.org/linux/man-pages/man3/dlopen.3.html) is used under the 
hood for the dynlib macro on Linux. dlopen() will not search /usr/local/lib by 
default. It needs to be manually specified...

Is this something that maybe should be fixed in the Nim source to also search 
/usr/local/lib for .so files on unix systems?


Re: Having a hard time getting raylib bindings to work

2017-07-23 Thread def_pri_pub
I use ubuntu (well, xubuntu... technically). I doubt it's a bug on their side. 
Someone else would have tripped over this way before I would have and fixed it 
too.


Re: Having a hard time getting raylib bindings to work

2017-07-22 Thread def_pri_pub
I've been able to get the Nim bindings to work on Linux using .so files!! There 
is still a bit of an odd quirk:

  1. GLFW 3.2.1 needs to be installed (with cmake -DBUILD_SHARED_LIBS=ON) from 
source (sudo make install). So those get put in /usr/local/lib
  2. raylib needs to be installed (with make SHARED_RAYLIB=YES) from source 
(sudo make install SHARED_RAYLIB=YES). Those get put in /usr/local/lib too.



So at this point, you can compile the core_basic_window.nim example I have in 
examples. But the problem is that if I try to run it, I get this:


ben@linux:examples$ ./core_basic_window
/usr/local/lib/libraylib.so: undefined symbol: glfwSetWindowIcon
could not load: libraylib.so


But when I did export LD_LIBRARY_PATH=/usr/local/lib. then it would run fine. 
What the hell is going on here? Isn't /usr/local/lib one of the default library 
search paths? That's where the .so files are put.


Re: Having a hard time getting raylib bindings to work

2017-07-21 Thread def_pri_pub
I found out some more information about the issue on Linux. If you read the 
link to my edited post at the top it explains a little more. tl;dr the 
raylib.so Linux .so is broken.


Having a hard time getting raylib bindings to work

2017-07-20 Thread def_pri_pub
I'm trying to get some Nim bindings for [this neat little game programming 
library called raylib](https://github.com/raysan5/raylib/). But well, I'm 
having some difficulties trying to do so.

After cleaning up raylib.h, I was able to use c2nim to auto-generate a .nim. I 
commented out a few things and added in the {.importc.} pragmas manually. You 
can [see it thus far 
here](https://gitlab.com/define-private-public/raylib-Nim/blob/dac2fdab777604053c470b4a41a6f9844148575b/src/raylib.nim).

If you want to help me out, you'll need to build your own copy of raylib, off 
of the develop branch, using the SHARED_RAYLIB=YES option at make. There are 
some extra instructions on their Wiki.

On Linux: I can't seem to get the library to load at runtime. It compiles fine. 
But when I try to run the core_basic_window in examples, I keep on getting 
could not load: libraylib.so messages. I tried compiling with the extra 
information and it only said that it couldn't find the .so file. I tried 
placing the .so in /usr/local/lib, and the current working directory (with 
setting LD_LIBRARY_PATH). Still nothing. Does anyone see what's wrong here?

On OS X: It's in a partial working state. For now, the .dylib needs to be in 
the same folder as the .nim you want to compile, as well as for to run the 
executable. There's a bit of a problem though. It's failing to load some of the 
functions from the .dylib, thus making the application crash if I don't have 
that {.deadCodeElim: on.} pragma at the top of src/raylib.nim. For example, if 
I remove that pragma, it will compile, but at runtime it will say could not 
import: LoadMeshEx. The proc id in Nim looks fine to me. Anyone know what's 
going wrong?

For Windows: I haven't tried yet. If someone else to try this out and see how 
they are going, I'd be very appreciative.


Re: Games made in Nim

2017-05-18 Thread def_pri_pub
I made a game called "Pucker Up," for the Linux Game Jam 2017. The original 
entry's source can be found here: 
[https://gitlab.com/define-private-public/LGJ2017](https://gitlab.com/define-private-public/LGJ2017)

I also was able to (with moderate ease) pull out the OpenGL & SDL2 stuff, and 
replace it with the necessary HTML5 things to port it to the browser using 
Nim's JavaScript target. You can play it here: 
[https://16bpp.net/games/pucker-up-lgj-2017](https://16bpp.net/games/pucker-up-lgj-2017)


Re: The Linux Game Jam 2017 - itch.io

2017-03-22 Thread def_pri_pub
I was looking into using that stuff to port what I did for the Game Jam to JS, 
but I was wondering about how to get SDL_Mixer compiled to JS.


Re: The Linux Game Jam 2017 - itch.io

2017-03-22 Thread def_pri_pub
Also, I can't remember who here runs the Hookrace.net blog, but they talked 
about how they got NimES their (Nim & SDL2 NES emulator) ported to JavaScript:

[https://hookrace.net/blog/porting-nes-go-nim](https://hookrace.net/blog/porting-nes-go-nim)/


Re: The Linux Game Jam 2017 - itch.io

2017-03-22 Thread def_pri_pub
Also, I can't remember who here runs the Hookrace.net blog, but they talked 
about how they got NimES their (Nim & SDL2 NES emulator) ported to JavaScript:

[https://hookrace.net/blog/porting-nes-go-nim](https://hookrace.net/blog/porting-nes-go-nim)/


Re: Mascot

2017-03-17 Thread def_pri_pub
> Honey badgers are admired precisely for their capacity and inclination to do 
> violence. A Nim honey badger should look meaner than most I'd think.

Should we have it ripping apart a Snake? Or gnawing down on a Gopher?


Re: Mascot

2017-03-17 Thread def_pri_pub
@jester, those designs do look a lot better IMO.


Re: Mascot

2017-03-17 Thread def_pri_pub
Aww man, and here I was hoping for some sort of cutesy anime girl for a mascot. 
Oh well : P (Just kidding)

I think having a mascot is a good idea, but I don't think that having him front 
and center everything would be good. Having the crown logo in more places would 
be better. I also think the mascot design is a bit overly complex right now and 
not that aesthetically pleasing to look at. Right now, the mascot kind of 
reminds me of 
[this](https://www.google.com/search?q=japanese+mascot+bear+with+melon+head=lnms=isch=X=2=0ahUKEwjjh4uq1d3SAhXBWCYKHUNGD6oQ_AUIBigB=1680=912).
 I think it would be good to make him look a bit more friendly, but [looking at 
other graphics of Honey 
Badgers](https://www.google.com/search?q=honey+badger+graphics=lnms=isch=X=0ahUKEwjhheSW1d3SAhWnyoMKHaG4BkEQ_AUIBigB=1680=912#tbm=isch=honey+badger+graphic&*)
 we might as go for that "bite and rip off your arm," feeling.


Re: Trouble with reading from stdin

2017-02-10 Thread def_pri_pub
Is it something related to stdin's default buffer size? Default is around 4096 
character IIRC.


Re: Trouble with reading from stdin

2017-02-09 Thread def_pri_pub
When I ran a debug build, I got this stack of errors:


Traceback (most recent call last)
random_art.nim(185)  random_art
equationParser.nim(51)   parseEquation
equationParser.nim(73)   buildEquation
ModExpr.nim(72)  buildMod
equationParser.nim(75)   buildEquation
WellExpr.nim(56) buildWell
equationParser.nim(79)   buildEquation
SinExpr.nim(84)  buildSin
equationParser.nim(79)   buildEquation
SinExpr.nim(84)  buildSin
equationParser.nim(75)   buildEquation
WellExpr.nim(56) buildWell
equationParser.nim(77)   buildEquation
TentExpr.nim(57) buildTent
equationParser.nim(73)   buildEquation
ModExpr.nim(73)  buildMod
equationParser.nim(77)   buildEquation
TentExpr.nim(57) buildTent
equationParser.nim(73)   buildEquation
ModExpr.nim(73)  buildMod
equationParser.nim(71)   buildEquation
ProductExpr.nim(59)  buildProduct
equationParser.nim(83)   buildEquation
MixExpr.nim(63)  buildMix
equationParser.nim(83)   buildEquation
MixExpr.nim(64)  buildMix
equationParser.nim(79)   buildEquation
SinExpr.nim(84)  buildSin
equationParser.nim(83)   buildEquation
MixExpr.nim(63)  buildMix
equationParser.nim(69)   buildEquation
SumExpr.nim(62)  buildSum
equationParser.nim(77)   buildEquation
TentExpr.nim(57) buildTent
equationParser.nim(69)   buildEquation
SumExpr.nim(61)  buildSum
equationParser.nim(67)   buildEquation
ConstExpr.nim(88)buildConst
strutils.nim parseFloat
Error: unhandled exception: invalid float: - [ValueError]


But I need to reiterate, it only happens when I copy and paste into a terminal 
using standard input. If it's piped in, there is no error what soever and 
renders fine.


C#/Java like interfaces for Nim

2017-01-21 Thread def_pri_pub
One of my favorite things from C#/Java are interfaces and I've been trying to 
work on writing some macros can generate an interface and allow you to 
implement it. So far I've played around with getting some sort of "toy 
interface," working as well as some of the macros too.

Here is my initial prototype code:


type
  # The interface
  IWorker* = object
obj*: ref RootObj
setup_proc*: proc(obj: ref RootObj, a, b: float)
run_proc*: proc(obj: ref RootObj)
getResult_proc*: proc(obj: ref RootObj): float
  
  
  # A base type
  MathOperation = object of RootObj
result: float
  
  Adder = ref object of MathOperation
first, second: float
  
  Subtracter = ref object of MathOperation
initial, subtraction: float

#  Multiplier = ref object of MathOperation
#value, multiplier: float
#
#  Divider = ref object of MathOperation
#numerator, divisor: float


#=== Procs for the IWorker ===#
proc setup(worker: IWorker; a, b: float) =
  worker.setup_proc(worker.obj, a, b)

proc run(worker: IWorker) =
  worker.run_proc(worker.obj)

proc getResult(worker: IWorker): float =
  return worker.getResult_proc(worker.obj)


#=== Procs for the Adder ===#
proc Adder_setup(obj: ref RootObj; a, b: float) =
  var this = cast[Adder](obj)
  this.first = a
  this.second = b

proc Adder_run(obj: ref RootObj) =
  var this = cast[Adder](obj)
  this.result = this.first + this.second

proc Adder_getResult(obj: ref RootObj): float =
  var this = cast[Adder](obj)
  return this.result


#=== Procs for the Subtractter ===#
proc Subtracter_setup(obj: ref RootObj; a, b: float) =
  var this = cast[Subtracter](obj)
  this.initial = a
  this.subtraction = b

proc Subtracter_run(obj: ref RootObj) =
  var this = cast[Subtracter](obj)
  this.result = this.initial - this.subtraction

proc Subtracter_getResult(obj: ref RootObj): float =
  var this = cast[Subtracter](obj)
  return this.result


var
  worker: IWorker
  
  # Slap together the interface
  add = IWorker(
setup_proc: Adder_setup,
run_proc: Adder_run,
getResult_proc: Adder_getResult
  )
  
  sub = IWorker(
setup_proc: Subtracter_setup,
run_proc: Subtracter_run,
getResult_proc: Subtracter_getResult
  )


# Assign an object (the objects in this case really don't have any special 
data)
add.obj = new(Adder)
sub.obj = new(Subtracter)


# Try out the workers
worker = add
worker.setup(10.0, 2.0)
worker.run
echo worker.getResult   # Should be 12in

worker = sub
worker.setup(10.0, 2.0)
worker.run
echo worker.getResult   # Should be 8



Right now I've got a macro called INTERFACE that acts like the one below and 
can produce the type object.


INTERFACE IWorker ACTS_ON MathOperation:
  proc setup(a, b: float)
  proc run()
  proc getResult(): float

# Produces this specifically:
#type
#  # The interface
#  IWorker = object
#obj: ref MathOperation
#setup_proc: proc(obj: ref MathOperation; a, b: float)
#run_proc: proc(obj: ref MathOperation)
#getResult_proc: proc(obj: ref MathOperation): float



But I've been doing some thinking and wondering if I should have the interface 
actually act on an object. This would get rid of the obj field and remove the 
insertion of the ref RootObj`s from the generated `type object interface. This 
way an interface isn't tied to working on one type of object, but instead an 
interface could be a "collection of functions (stored in an object) that can 
act on any types," instead of a "contract for operations on a specific type."

I'm wondering which road I should go down. The "contract," way would closely 
represent how interfaces work in C#/Java, but I feel that the "collection," 
idea would give more flexibility to Nim. What's your input?


Re: Problems making a library and importing it

2017-01-12 Thread def_pri_pub
You need to add the export marker right after the proc's id. Try this:


proc testEcho*() =
  echo "hello world"


You can also drop that : void part too.


Re: Nim Podcast

2017-01-09 Thread def_pri_pub
Sounds like a cool idea, but I'd be interested in reading a newsletter first.


Re: Seq with custom fields

2017-01-04 Thread def_pri_pub
You will need to add this proc for the add:


proc add*(es: var ExtSeq; x: int) {.inline.}=
  es.s.add(x)


The inline pragma isn't necessary, but it speeds things up a little.

For using the index tokens ([ and ]), you just need to override that proc too:


# Get
proc `[]`*(es: var ExtSeq; index: int) {.inline.}=
 return es.s[index]

# Set
proc `[]=`*(es: var ExtSeq; index: int; value: int) {.inline.}=
  es.s[index] = value



Re: Why do custom types need to be reference counted objects for dynamic dispatch to work.

2017-01-04 Thread def_pri_pub
Wow. Thanks for that post. I think this belongs in the docs somewhere or on a 
wiki.

I came across this issue when I was change the prototype of a base method but 
forgot to change one of the child objects. So for that specific child object it 
was using the base method. Would using the base pragma have the Nim compiler 
fail if the child prototypes didn't match the parent?


Why do custom types need to be reference counted objects for dynamic dispatch to work.

2017-01-04 Thread def_pri_pub
Let's have a code example. When I'm using ref object of ... This code:


type
  Animal = ref object of RootObj
name: string

method makeNoise(this: Animal) {.base.} =
  echo "..."


type
  Human = ref object of Animal
  Dog = ref object of Animal

method makeNoise(this: Human) =
  echo "Hi, I'm ", this.name

method makeNoise(this: Dog) =
  echo "*Bark!* [said ", this.name, "]"


let
  h = Human(name: "Kevin Bacon")
  d = Dog(name: "Fuzzy")

h.makeNoise()
d.makeNoise()

let a:Animal = Dog(name: "Fluffy")
a.makeNoise()


Produces this result: 


Hi, I'm Kevin Bacon
*Bark!* [said Fuzzy]
*Bark!* [said Fluffy]


But when we take away that ref keyword from the type lines, the output becomes 
this: 


Hi, I'm Kevin Bacon
*Bark!* [said Fuzzy]
...


This leads me to believe that my objects need to be reference counted to take 
advantage of dynamic dispatch. Is this true?


Re: Getting a segfault when testing my C wrapper on OS X

2016-12-29 Thread def_pri_pub
I put the source of the header in it's own .nim file so my users wouldn't have 
to add a \--cincludes: option at the compilation step.

I do find it odd that import had different behavior on Linux vs. OS X in 
regards to this, and it effected a return value.


Re: Getting a segfault when testing my C wrapper on OS X

2016-12-29 Thread def_pri_pub
Well, I got the unit tests run without any segfaults (on OS X).

On a whim, I decided to change the import stb_image_header line (in 
stb_image.nim) to use the include statement instead; it worked. Is this a bug I 
should report?


Re: Getting a segfault when testing my C wrapper on OS X

2016-12-27 Thread def_pri_pub
Alright, I tried that and it does echo out a pointer address (e.g. 0xFFff08b7e).

Where this is getting kind of weird for me is that I have this raytracer that 
I'm working on that uses stb_image.h as well and it able to load an image on OS 
X without throwing a seg fault. The code for the mini-wrapper is here: 
[https://gitlab.com/define-private-public/PeterShirley-RayTracing-Nim/blob/7130281eee79c76be8e0c6ecb6dc36c99ab5aa52/book2/stb_image.nim](https://gitlab.com/define-private-public/PeterShirley-RayTracing-Nim/blob/7130281eee79c76be8e0c6ecb6dc36c99ab5aa52/book2/stb_image.nim)

The only major difference is that the wrapper in the ray tracer doesn't have 
the header file embedded directly in a .nim file, though that shouldn't matter.

I am really perplexed by this.


Re: Getting a segfault when testing my C wrapper on OS X

2016-12-26 Thread def_pri_pub
It shouldn't be an issue with the test suite. I added this line right after the 
let data = stbi_load(... call


echo x
echo y
echo channels_in_file
echo data.repr


And it gave me this output:


2
2
4
Traceback (most recent call last)
tests.nim(56)tests
stb_image.nim(86)load
repr.nim(308)reprAny
repr.nim(252)reprAux
repr.nim(234)reprRef
repr.nim(271)reprAux
SIG
SEGV: Illegal storage access. (Attempt to read from nil?)


I find this very odd because the return parameters are fine, but the actual 
returned value is not.


Getting a segfault when testing my C wrapper on OS X

2016-12-22 Thread def_pri_pub
I'm working on my stb_image wrapper right now. It's almost ready for release. 
But to be a good little developer I want to test it on the three major 
platforms (Linux, OS X, and Windows) before saying it's v1.0.

I developed it on Linux and it passes all of the unit tests. But when I tried 
to have it run on OS X (sierra), I got this error:


[Suite] Unit Tests for stb_image wrapper
Traceback (most recent call last)
tests.nim(56)tests
stb_image.nim(86)load
SIGSEGV: Illegal storage access. (Attempt to read from nil?)


The line in question can be found here: 
[https://gitlab.com/define-private-public/stb_image-Nim/blob/bd4fc301069b068d7657b838ecf369be0573683a/stb_image.nim#L86](https://gitlab.com/define-private-public/stb_image-Nim/blob/bd4fc301069b068d7657b838ecf369be0573683a/stb_image.nim#L86)

I did a little investigating. The wrapped function stbi_load() is setting the 
width, height, and components variables like it should, but nothing is being 
passed into data upon the function's return. I got the same error when I tried 
to do this:


echo data[]


I really have no idea what is wrong.


Re: How to embed a header file into a .nim file

2016-12-17 Thread def_pri_pub
I went with yglukhov's solution (since it seemed to be the simplest to use), 
but It's really what I wanted to avoid doing.


Re: Return values question

2016-12-12 Thread def_pri_pub
Thanks for that post, it definitely gave me a lot of insight into what is going 
on in Nim. I'm working with a lot of pixel data and wanted to return it from a 
function as a seq[uint8]. I want to avoid it doing an necessary copy (on 
return) for a lot of data (which I assume is happening right now).


Return values question

2016-12-12 Thread def_pri_pub
How does Nim return values from a proc? Are basic datatypes (like int and 
float) returned by copy? What about more complex structures like sequences and 
strings?

For example, would this proc: 


proc fillWith(n, count: int): seq[int] =
  var s: seq[int] = @[]
  for i in countup(0, count - 1):
s.add(n)
  
  return s


return what was instantiated on line 2? Or would it copy over the values to new 
sequence and return that to the caller? What about custom objects?


Best way to represent pixel data in Nim

2016-12-05 Thread def_pri_pub
I want to work on a 
[stb_image](https://github.com/nothings/stb/blob/master/stb_image.h) wrapper. 
I'm trying to figure out the best (and most efficient) way of storing pixel 
data in Nim's data structures.

stb_image stores the pixel data in an unsigned char * type. I had a chat with 
the person who wrote the nimPNG package and he says that using Nim's string 
would be a good container. But when I was working with stb_image in Nim (for 
another project) I was using a seq[uint8] type ([thread link 
here](http://forum.nim-lang.org///forum.nim-lang.org/t/2638), [final 
mini-wrapper 
here](https://gitlab.com/define-private-public/PeterShirley-RayTracing-Nim/blob/77728b48a4a58a1420f1bbcef983c37284a71dcf/working/stb_image.nim).
 Which one would be better to work with?


Problems trying to wrap a single header file library

2016-11-28 Thread def_pri_pub
I'm working on porting over some ray tracing code from a C++ project. So far 
things have been fine, but they used this library called 
[stb_image.h](https://github.com/nothings/stb/blob/master/stb_image.h) to load 
in textures.

I thought I would try to create a wrapper for the single function that is used 
"stbi_load()"

So far I've got this (filename "stb_image.nim") 


# This file is a wrapper of a subset for the "stb_image.h" library that is
# needed for the raytracer

# Required before including the stb_image.h header
{.emit: """
#define STB_IMAGE_IMPLEMENTATION
""".}

# Internal function
proc stbi_load(filename: cstring; x, y, comp: var cint; req_comp: cint): 
cstring
  {.importc: "stbi_load", header: "stb_image.h".}


## External function (the Nim friendly version)
proc stbi_load*(filename: string; x, y, comp: var int; req_comp: int): 
seq[uint8] =
  var
width: cint
height: cint
comp2: cint
pixelData: seq[uint8]
  
  let data = stbi_load(filename.cstring, width, height, comp2, 
req_comp.cint)
  
  # Set the data
  x = width.int
  y = width.int
  comp = comp2.int
  
  newSeq(pixelData, x * y)
  # TODO set the pixelData
  
  return pixelData



I seem to be fine, until I compile and get this: 


CC: stb_image
Error: execution of an external compiler program 'gcc -c  -w  
-I/home/ben/bin/Nim/lib -o nimcache/stb_image.o nimcache/stb_image.c' failed 
with exit code: 1

nimcache/stb_image.c:10:23: fatal error: stb_image.h: No such file or 
directory
compilation terminated.


I have the stb_image.h file right alongside the nim one. What should I do here?

Also, do you think the return type cstring (for the internal function) is fine? 
stb_image() returns an unsigned char *. I figured casting it to a seq[uint8] 
should be fine.


Bitwise operation problems.

2016-11-27 Thread def_pri_pub
I'm trying to map my "quadruplet" object to an unsigned 32-bit number, that is 
supposed to represent an RGBA pixel. I'm able to convert my floating point 
quadruplet values to their respective R, G, B, and A components, but when I try 
to do an and on all of the components, I'm getting a 0. Here is my code:


proc map*(x, a, b, p, q: float): float {.inline.} =
  return (x - a) * (q - p) / (b - a) + p

#...

type
  # [-1, 1] -> [0x00, 0xFF]
  Quadruplet* = ref object of RootObj
w*: float
x*: float
y*: float
z*: float


# ...

# Returns a quadruplet as a nicely formatted RGBA pixel.  Remember:
# [-1, 1] -> [0x00, 0xFF].  Anything out of that range will be clamped
proc toRGBA*(q: Quadruplet): uint32 {.inline.} =
  let
r = q.x.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 24
g = q.y.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 16
b = q.z.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 8
a = q.w.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32
pixel = r and g and b and a
  
  echo ""
  echo r
  echo g
  echo b
  echo a
  echo pixel
  echo ""
  
  return pixel


Those echo statements are in there for debugging purposes right now.

So an example call would be: echo Quadruplet(w:0, x:1, y:1, z:1).toRGBA() # 50% 
white

And it's producing this on standard output 


4278190080
16711680
65280
127
0


All of those components look correct, except for when I combine them with the 
ands into the full pixel. Can someone explain what's wrong here and how to fix 
it? 


Re: Converting a nim string to a ptr GlChar

2016-11-25 Thread def_pri_pub
That second one "with force," is what did the trick. (That first one didn't 
compile).

Thanks!


Converting a nim string to a ptr GlChar

2016-11-19 Thread def_pri_pub
Right now I'm trying to get a simple OpenGL ES example working with Nimious' GL 
ES bindings. I'm hitting a bit of a wall when trying to get the proc 
glShaderSource() to accept my shader's source code (which is gotten through the 
readFile() proc, which outputs a nim string). Here is a link to that proc: 
[https://github.com/nimious/gles/blob/master/src/gles2/gl2.nim#L1545](https://github.com/nimious/gles/blob/master/src/gles2/gl2.nim#L1545)

In C, this usually is the way someone loads a shader source: 


const char* vertexShaderSrc =
"#version 400"
""
"in vec3 pos;"
""
"void main() {"
"  gl_Position = vec4(pos, 1.0);"
"}";

// ...

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, , NULL)



I'm fine up until that 3rd parameter to something that GLES' glShaderSource() 
proc will accept? It needs a ptr ptr GlChar. [GlChar is defined 
here](https://github.com/nimious/gles/blob/master/src/gles2/gl2.nim#L22)


Re: "Error: expression has no address" issue with an array type

2016-11-19 Thread def_pri_pub
Oh, did not know that. Thanks! I didn't find any mention of that in the Nim 
manual, it might be something to add here: 
[http://nim-lang.org/docs/manual.html#statements-and-expressions-the-addr-operator](http://forum.nim-lang.org///nim-lang.org/docs/manual.html#statements-and-expressions-the-addr-operator)


"Error: expression has no address" issue with an array type

2016-11-19 Thread def_pri_pub
Right now I'm trying to do a very tiny OpenGL in nim. I'm at the stage where I 
need to bind my buffer data. The compiler is giving me this error:


Error: expression has no address


Here is the C code I'm trying to port: 


float points = [
  0.0, 0.5, 0.0,
  0.5, -0.5, 0.0,
  -0.5, -0.5, 0.0
];

// ...

GLuint vbo = 0;
glGenBuffers(1, );
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);


And here is my nim code right now: 


let
  points = [
0.0, 0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, -0.5, 0.0
  ]
  numVertexBuffers = GlSizei(1)
  pointsBufferSize = GlSizeiptr(sizeof(points))

# ...

var
  vbo: GlUint

glGenBuffers(numVertexBuffers, vbo.addr)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, pointsBufferSize, points.addr, 
GL_STATIC_DRAW)  # It errors here on `points.addr`



Here is the definition for the glBufferData() proc: 
[https://github.com/nimious/gles/blob/master/src/gles2/gl2.nim#L494](https://github.com/nimious/gles/blob/master/src/gles2/gl2.nim#L494)
 I have no idea what's wrong here.


Re: How to iterate through a file using readLine()

2016-11-17 Thread def_pri_pub
No, that's for the proc that returns a boolean. I meant for the one that 
returns a TaintedString.


Re: How to iterate through a file using readLine()

2016-11-17 Thread def_pri_pub
Thanks. What would readLine() return though (using the proc I linked) if it 
reached EOF?


How to iterate through a file using readLine()

2016-11-17 Thread def_pri_pub
I know that this exists to cover what I want to do:


for line in lines filename:
  echo line


But how would I go about it using the [readLine() function 
here](http://forum.nim-lang.org///nim-lang.org/docs/system.html#readLine,File)?