Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Quick and dirty Haxe WebIDL example.
  • Loading branch information
Sanva committed Oct 25, 2020
0 parents commit 0397c23
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
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
4 changes: 4 additions & 0 deletions .haxerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "4.1.3",
"resolveLibs": "scoped"
}
36 changes: 36 additions & 0 deletions IntTuple.h
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];

}

};
43 changes: 43 additions & 0 deletions Makefile
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
51 changes: 51 additions & 0 deletions README.rst
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
```
33 changes: 33 additions & 0 deletions Sample.hx
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();
}
}
29 changes: 29 additions & 0 deletions SampleModule.hx
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
3 changes: 3 additions & 0 deletions haxe_libraries/webidl.hxml
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/
11 changes: 11 additions & 0 deletions point.cpp
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;
}
25 changes: 25 additions & 0 deletions point.h
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[]);
};
17 changes: 17 additions & 0 deletions point.idl
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();
};
2 changes: 2 additions & 0 deletions sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="libpoint.js"></script>
<script src="sample.js"></script>
3 changes: 3 additions & 0 deletions sample.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-hl sample.hl
-lib webidl
-main Sample

0 comments on commit 0397c23

Please sign in to comment.