Skip to content

Commit

Permalink
Bool support ?
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBebbington committed Sep 20, 2013
1 parent 4dfbe78 commit a17b7ee
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 52 deletions.
9 changes: 2 additions & 7 deletions build.hxml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
-main samples.sdl.Test
-neko samples/sdl/test.n
-lib ffi
-dce full
--next
-main samples.fastmath.FastMath
-x samples/fastmath/run
-neko samples/fastmath/run.n
-cmd neko samples/fastmath/run.n 30 2 6
-lib ffi
-dce full
-debug
--next
-D display
-xml haxedoc.xml
Expand Down
2 changes: 2 additions & 0 deletions ffi/Pointer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern class Pointer {
}
#else
abstract Pointer(Dynamic) {
public inline function new(v:Dynamic)
this = v;
@:to public inline function getString() {
return ffi_get_str(this);
}
Expand Down
23 changes: 17 additions & 6 deletions ffi/lib/EasyLibrary.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import haxe.macro.*;
extern class EasyLibrary {
/** The underlying library **/
public var lib(default, null):Library;
/** The name of the library **/
public var name(default, null):String;
/** Loads the library with the name/path or throws an error **/
public function new(name:String):Void;
}
#else
@:autoBuild(ffi.lib.EasyLibrary.Builder.build()) class EasyLibrary {
public var lib(default, null):Library;
public var name(default, null):String;
public function new(name:String) {
this.lib = Library.load(name);
this.name = name;
this.lib = try Library.load(name) catch(e:Dynamic) null;
if(lib == null)
throw 'Could not load library "$name"';
}
public inline function toString():String
return name;
}
#end
#if macro
Expand All @@ -37,7 +43,6 @@ class Builder {
var ofc:Function = cast f.kind.getParameters()[0];
switch(f.kind) {
case FFun(fc):
var pr = new haxe.macro.Printer();
var ef = Reflect.copy(f);
ef.kind = FieldType.FFun(Reflect.copy(ofc));
var nfc:Function = ef.kind.getParameters()[0];
Expand All @@ -47,20 +52,25 @@ class Builder {
var of = Reflect.copy(f);
of.name = '_sym_${f.name}';
of.kind = FieldType.FVar(macro:ffi.Function);
inits.push(macro $i{of.name} = lib.getSymbol($v{f.name}));
inits.push(macro $i{of.name} = lib[$v{f.name}]);
nfs.push(of);
var cif = Reflect.copy(ef);
cif.name = '_cif_${f.name}';
cif.kind = FieldType.FVar(macro:ffi.Cif);
inits.push(macro $i{cif.name} = new ffi.Cif());
nfs.push(cif);
inits.push(macro $i{cif.name}.prep(${{expr: EArrayDecl([for(a in ofc.args) toFFIType(a.type)]), pos: Context.currentPos()}}, ${toFFIType(f.kind.getParameters()[0].ret)}));
var fexpr = macro $i{cif.name}.call($i{of.name}, ${{pos: cif.pos, expr: EArrayDecl([for(a in nfc.args) macro $i{a.name}])}});
var fexpr = macro try $i{cif.name}.call($i{of.name}, ${{pos: cif.pos, expr: EArrayDecl([for(a in nfc.args) macro $i{a.name}])}}) catch(e:Dynamic) throw e+" in "+$v{f.name};
f.kind = FieldType.FFun({
ret: nfc.ret,
params: [],
args: nfc.args,
expr: (ComplexTypeTools.toString(nfc.ret) == "Void") ? fexpr : macro return $fexpr
expr: switch(nfc.ret) {
case macro:Void: fexpr;
case macro:String: macro return Pointer.toString($fexpr);
case macro:Bool: macro $fexpr > 0;
default: macro return $fexpr;
}
});
nfs.push(f);
default:
Expand All @@ -84,7 +94,7 @@ class Builder {
}
public function toHaxeType(c:ComplexType):ComplexType {
return switch(c) {
case macro:Int64, macro:haxe.Int64, macro:UInt64, macro:haxe.UInt64: macro:haxe.Int64;
case macro:Int64, macro:haxe.Int64, macro:UInt64, macro:haxe.UInt64, macro:Long, macro:ULong: macro:haxe.Int64;
case TPath({pack: [], params: [], name: name}) if(name.indexOf("Int") != -1): macro:Int;
case macro:Float, macro:Single: macro:Float;
case TPath({pack: [], params: _, name: "Pointer"}): macro:ffi.Pointer;
Expand All @@ -98,6 +108,7 @@ class Builder {
case macro:Single: macro ffi.Type.DOUBLE;
case TPath({pack: [], params: _, name: "Pointer"}):
macro ffi.Type.POINTER;
case macro:Bool: macro ffi.Type.UINT8;
case TPath({pack: [], params: [], name: name}):
var ename = name.toUpperCase();
if(StringTools.startsWith(ename, "INT"))
Expand Down
8 changes: 4 additions & 4 deletions ffi/lib/Library.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ extern class Library {
/** Loads the library matching the path or returns null if it could not be loaded **/
public static function load(path:String):Null<Library>;
/** Loads a symbol from the library **/
public function getSymbol(func:String):ffi.Function;
/** Closes the library. MUST be called after using it to avoid memory leaks **/
public function close():Void;
public function get(func:String):ffi.Function;
}
#else
using sys.FileSystem;
import sys.io.*;
abstract Library(Dynamic) {
public static inline function load(path:String):Library
return ffi_load_library(path);
public inline function getSymbol(func:String):ffi.Function
@:arrayAccess public inline function get(func:String):ffi.Function
return ffi_load_symbol(this, func);
public inline function close():Void
ffi_close_library(this);
Expand Down
12 changes: 6 additions & 6 deletions haxedoc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,10 @@
</class>
<class path="ffi.Util" params="" file="./ffi/Util.hx" extern="1"><haxe_doc>Some random utilities used around the library - do not use from outside the library</haxe_doc></class>
<class path="ffi.lib.EasyLibrary" params="" file="./ffi/lib/EasyLibrary.hx" extern="1">
<name public="1" set="null">
<c path="String"/>
<haxe_doc>The name of the library</haxe_doc>
</name>
<lib public="1" set="null">
<c path="ffi.lib.Library"/>
<haxe_doc>The underlying library</haxe_doc>
Expand All @@ -1459,17 +1463,13 @@
</f>
<haxe_doc>Loads the library matching the path or returns null if it could not be loaded</haxe_doc>
</load>
<close public="1" set="method">
<f a=""><x path="Void"/></f>
<haxe_doc>Closes the library. MUST be called after using it to avoid memory leaks</haxe_doc>
</close>
<getSymbol public="1" set="method">
<get public="1" set="method">
<f a="func">
<c path="String"/>
<c path="ffi.Function"/>
</f>
<haxe_doc>Loads a symbol from the library</haxe_doc>
</getSymbol>
</get>
<haxe_doc>A runtime libary</haxe_doc>
</class>
<class path="haxe.EnumTools" params="" file="/usr/lib/haxe/std/haxe/EnumTools.hx" extern="1">
Expand Down
6 changes: 4 additions & 2 deletions samples/fastmath/FastMath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class FastMath extends ffi.lib.EasyLibrary {
public function ceil(f:Float):Float;
static function main() {
var m = new FastMath();
for(i in 1...100)
trace('${m.sqrt(i)} squared = $i');
for(a in Sys.args()) {
var i = Std.parseFloat(a);
Sys.println('square root of $i = ${m.sqrt(i)}');
}
}
}
27 changes: 0 additions & 27 deletions samples/sdl/Test.hx

This file was deleted.

0 comments on commit a17b7ee

Please sign in to comment.