-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick and dirty Haxe WebIDL example.
- Loading branch information
0 parents
commit 0397c23
Showing
13 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/out | ||
*.obj | ||
*.o | ||
*.hl | ||
*.js | ||
*.bc | ||
*.so | ||
*.tmp | ||
libpoint.cpp | ||
sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": "4.1.3", | ||
"resolveLibs": "scoped" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
#include <stdlib.h> | ||
|
||
class IntTuple { | ||
|
||
public: | ||
|
||
int *raw; | ||
|
||
IntTuple(int x = 0, int y = 0) { | ||
|
||
raw = (int *) malloc(sizeof(int) * 2); | ||
raw[0] = x; | ||
raw[1] = y; | ||
|
||
} | ||
|
||
~IntTuple() { | ||
|
||
free(raw); | ||
|
||
} | ||
|
||
int getX() { | ||
|
||
return raw[0]; | ||
|
||
} | ||
|
||
int getY() { | ||
|
||
return raw[1]; | ||
|
||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
all: hl js | ||
|
||
libpoint.cpp: | ||
haxe -lib webidl --macro "SampleModule.buildLibCpp()" | ||
|
||
libpoint.js: libpoint.cpp | ||
haxe -lib webidl --macro "SampleModule.buildLibJS()" | ||
|
||
js: libpoint.js | ||
haxe -js sample.js -lib webidl -main Sample -dce full | ||
|
||
# ---- HL PART | ||
|
||
ifndef HLPATH | ||
HLPATH = /path/to/hl | ||
endif | ||
|
||
libpoint.hdll: libpoint.cpp | ||
$(CC) -o libpoint.hdll -shared -fPIC -Wall -O3 -I . -I $(HLPATH) libpoint.cpp point.cpp -lstdc++ -lhl | ||
|
||
libpoint_win: libpoint.cpp | ||
cl /olibpoint.hdll /LD /EHsc /I $(HLPATH) /DYNAMICBASE libhl.lib libpoint.cpp point.cpp | ||
|
||
hl: libpoint.hdll | ||
haxe sample.hxml | ||
|
||
linux-shared: libpoint.cpp | ||
haxe -hl out/main.c -lib webidl -main Sample | ||
$(CC) -o libpoint.so -shared -fPIC -Wall -O3 -I . -I $(HLPATH) libpoint.cpp point.cpp -lstdc++ -lhl | ||
$(CC) -O3 -o sample -std=c11 -I out out/main.c -lhl -L"`pwd`" -lpoint | ||
|
||
run-linux-shared: | ||
LD_LIBRARY_PATH=`pwd`:${LD_LIBRARY_PATH} ./sample | ||
|
||
check-linux-shared: | ||
LD_LIBRARY_PATH=`pwd`:${LD_LIBRARY_PATH} valgrind ./sample | ||
|
||
.PHONY: libpoint.cpp libpoint.js | ||
|
||
.SUFFIXES : .cpp .o | ||
|
||
.cpp.o: | ||
$CC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
Example | ||
======= | ||
|
||
Quick and dirty example showing a way to | ||
create a Haxe WebIDL `NativePtr` from C++ | ||
to use in a place where an already created | ||
`NativePtr` is expected. | ||
|
||
.. note:: Please, note that I didn't test this with Emscripten, | ||
I've never used that and seems not trivial to | ||
install in my system — looks like any build | ||
will fail with a Python error. | ||
|
||
Setup | ||
----- | ||
|
||
``` | ||
lix download | ||
``` | ||
|
||
Build for Haxe/C++ — Linux | ||
-------------------------- | ||
|
||
``` | ||
make linux-shared | ||
``` | ||
|
||
Run — Linux, shared library | ||
--------------------------- | ||
|
||
``` | ||
make run-linux-shared | ||
``` | ||
|
||
Should print | ||
|
||
``` | ||
Sample.hx:21: Result = 11,13 len=17.0293863659264 | ||
Sample.hx:23: -1 | ||
Sample.hx:24: 1 | ||
Sample.hx:26: 11 | ||
Sample.hx:27: 13 | ||
``` | ||
|
||
Check memory with valgrind | ||
-------------------------- | ||
|
||
``` | ||
make check-linux-shared | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import haxe.ds.Vector; | ||
|
||
import SampleModule.IntTuple; | ||
import SampleModule.Point; | ||
import SampleModule.Init as SampleModuleInit; | ||
|
||
import hl.types.ArrayBase; | ||
|
||
class Sample { | ||
|
||
public static function main() { | ||
SampleModuleInit.init(startApp); | ||
} | ||
|
||
public static function startApp() { | ||
var p1 = new Point(); | ||
p1.x = 4; | ||
p1.y = 5; | ||
var p2 = new Point(7,8); | ||
var p = p1.op_add(p2); | ||
trace('Result = ${p.x},${p.y} len=${p.length()}'); | ||
final tuple = new IntTuple(-1, 1); | ||
trace(tuple.getX()); | ||
trace(tuple.getY()); | ||
p.mutateTuple(tuple.raw); | ||
trace(tuple.getX()); | ||
trace(tuple.getY()); | ||
tuple.delete(); | ||
p1.delete(); | ||
p2.delete(); | ||
p.delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#if !macro | ||
private typedef Import = haxe.macro.MacroType<[SampleModule.build()]>; | ||
#else | ||
|
||
class SampleModule { | ||
|
||
static var config : webidl.Options = { | ||
idlFile : "point.idl", | ||
nativeLib : "libpoint", | ||
includeCode : "#include \"point.h\"", | ||
autoGC : false, | ||
}; | ||
|
||
public static function build() { | ||
return webidl.Module.build(config); | ||
} | ||
|
||
public static function buildLibCpp() { | ||
webidl.Generate.generateCpp(config); | ||
} | ||
|
||
public static function buildLibJS() { | ||
var sourceFiles = ["point.cpp"]; | ||
webidl.Generate.generateJs(config, sourceFiles); | ||
} | ||
|
||
} | ||
|
||
#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-D webidl=0.0.0 | ||
# @install: lix --silent download "gh://github.com/Sanva/webidl#a57a4c633ad221068245f84a87648e1ff6bca7c0" into webidl/0.0.0/github/a57a4c633ad221068245f84a87648e1ff6bca7c0 | ||
-cp ${HAXE_LIBCACHE}/webidl/0.0.0/github/a57a4c633ad221068245f84a87648e1ff6bca7c0/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <math.h> | ||
#include "point.h" | ||
|
||
double Point::length() { | ||
return sqrt(x * x + y * y); | ||
} | ||
|
||
void Point::mutateTuple(int vector[]) { | ||
vector[0] = x; | ||
vector[1] = y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
#include "IntTuple.h" | ||
|
||
class Point { | ||
public: | ||
int x; | ||
int y; | ||
Point() { | ||
x = 0; | ||
y = 0; | ||
} | ||
Point(const Point &p) { | ||
x = p.x; | ||
y = p.y; | ||
} | ||
Point(int x, int y) { | ||
this->x = x; | ||
this->y = y; | ||
} | ||
Point operator +( const Point &p ) { | ||
return Point(x + p.x, y + p.y); | ||
} | ||
double length(); | ||
void mutateTuple(int vector[]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
interface Point { | ||
attribute long x; | ||
attribute long y; | ||
void Point(); | ||
void Point( long x, long y ); | ||
[Operator="*",Ref] Point op_add( [Const,Ref] Point p ); | ||
double length(); | ||
void mutateTuple(int[] vector); | ||
}; | ||
|
||
interface IntTuple { | ||
attribute int[] raw; | ||
void IntTuple(int x, int y); | ||
int getX(); | ||
int getY(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script src="libpoint.js"></script> | ||
<script src="sample.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-hl sample.hl | ||
-lib webidl | ||
-main Sample |