Hello Robin and nekolist,
Thankyou Robin for your reply, It made me realize that I'd missed something:
neko.Lib.haxeToNeko( )
In the ndll that I had made I had this little snippet of code:
if( !val_is_string( directory ) ){
return val_null;
}
Which returned immediately, but I tried to add a line because I
started to suspect a thing:
if( !val_is_string( directory ) ){
failure("Wanna have a string!");
return val_null;
}
Of course the call did not block, the read function was never called
because my C function checked for Neko strings but never saw any
because it was called with haXe strings. Kinda a runtime error, this
is why I prefer static/strongly typed languages over dynamic ones ;)
But I still does not know how to expose host functions to the VM, It
would be fun to know how to do that. Probably I have already crossed
the solution, but forgot to convert between haXe and neko kinds and
therefore missed it.
Anyhow, right now it does not matter much to me if I can use ndll's
for all of the C functionality. I'll probably make a write-up of my
experiences for future/other neko users.
Thanks again!
Med vänliga hälsningar / best regards
→ Frank M. Eriksson ( http://knarf.se/ )
Office / Kontorsnr :(+46)18 - 490 24 42
2011/1/19 Robin Palotai <[email protected]>:
> 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)