This is a sample cpp file wrapping a single function, which works for me:

---------------
#include <string>
#include <string.h> // memcpy

#include <neko.h>

extern "C" value some_fun(value inarray1, value inarray2) {

   if (!val_is_string(inarray1) || !val_is_string(inarray2))
      return val_null;

   std::string result;
   some_operation(
         val_string(inarray1), val_strlen(inarray1),
         val_string(inarray2), val_strlen(inarray2),
         &result );

   value ret = alloc_empty_string(result.length());
   memcpy(val_string(ret), result.data(), result.length());

   return ret;
}

DEFINE_PRIM(some_fun, 2);
-----------------------

Then compile it to "some.ndll" (link in the necessary dependencies),
and get the method from Haxe using:

-----------------------
  var _somefun: Dynamic;

   var ldr = neko.vm.Loader.local();
   var epath =  neko.FileSystem.fullPath(
            neko.io.Path.directory(neko.Sys.executablePath() ));
   ldr.addPath(epath + "/");
   _somefun = ldr.loadPrimitive("some@some_fun", 2);


   public function some_fun(a: String, b: String): String {
      return neko.Lib.nekoToHaxe(_somefun(
               neko.Lib.haxeToNeko(a),
               neko.Lib.haxeToNeko(b) ));
   }
--------------------

As for the non-blocking call eating up the CPU, possibly you are
polling it in a very tight loop. Try to add some neko.Sys.sleep(0.02)
to sleep 20 msecs in the loop (or less if you prefer).

Hope it helps,
Robin

On Wed, Jan 19, 2011 at 1:30 AM, Frank Eriksson <[email protected]> wrote:
> Hello nekolist,
>
> I'm trying to write a program that has some functions that I would
> like to expose to the embedded VM, but I've had very little success of
> doing that. I had no problems with embedding the NekoVM, and call the
> main function of a haXe script.
>
> I've tried to do several things, using the DEFINEPRIM macro to define
> the function, tried to store the function as a field of the module and
> call it that way. But I'm out of luck.
>
> Basically right now I want to watch files using the iNotify API, first
> I tried to just make an ndll that exports a function that just watches
> a directory. But that did not go so well, by some strange fluke the c
> standard read function became non-blocking, so it did return even
> though there was no data from iNotify. Which means that the "idling"
> background program utilized one core to 100%, which is not good in the
> long run for a laptop (Especially not one in the HP Pavilion series).
>
> So therefore I tried to host the VM from inside my application, but I
> found the documentation a bit coarse in that section - and I'm not
> very used to C/C++ at all. Haven't been using them for a great time.
>
> I've read some of the mailinglist and found out about stuff like
> xcross, nme and other things. Seems to me like you mostly
> discusses/get questions about co-routines? Anyway I hope for that
> someone could show me a snippet that I can wrap my winter-tired head
> around that puts a host c function into the NekoVM so it is usable
> from haXe.
>
> Thanks in advance!
>
> Med vänliga hälsningar / best regards
> → Frank M. Eriksson ( http://knarf.se/ )
> Office / Kontorsnr :(+46)18 - 490 24 42
>
> --
> Neko : One VM to run them all
> (http://nekovm.org)
>

--
Neko : One VM to run them all
(http://nekovm.org)

Reply via email to