Skip to content

Instantly share code, notes, and snippets.

@rinfz
Created November 24, 2018 16:43
Show Gist options
  • Save rinfz/c8a00f357340cbd82c3e1c02fbe1eba5 to your computer and use it in GitHub Desktop.
Save rinfz/c8a00f357340cbd82c3e1c02fbe1eba5 to your computer and use it in GitHub Desktop.
calling D from perl 6 (windows)
use NativeCall;
sub routine(int64) returns Str is native('mydll') { * }
my $start = now;
my $foo = routine(100_000);
say $foo;
say now - $start, " seconds";
module mydll;
import std.stdio;
extern(C)
export immutable(char)* routine(int n) {
import std.bigint : BigInt;
import std.format : format;
import std.range : iota;
import std.string : toStringz;
BigInt prev = "1";
BigInt curr = "1";
foreach(int x; n.iota) {
prev = curr;
curr = prev + curr;
}
return "%d".format(curr).toStringz;
}
version(Windows)
extern(Windows) bool DllMain(void* hInstance, uint ulReason, void*) {
import std.c.windows.windows;
import core.sys.windows.dll;
switch (ulReason)
{
default: assert(0);
case DLL_PROCESS_ATTACH:
dll_process_attach( hInstance, true );
break;
case DLL_PROCESS_DETACH:
dll_process_detach( hInstance, true );
break;
case DLL_THREAD_ATTACH:
dll_thread_attach( true, true );
break;
case DLL_THREAD_DETACH:
dll_thread_detach( true, true );
break;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment