Giter Club home page Giter Club logo

webidl's Introduction

Haxe WebIDL

This library allows to access any C++ library from Haxe/JS (using Emscripten or Web Assembly) and Haxe/HashLink by simply defining an idl file.

For a complete example, see the sample

Installing

git clone https://github.com/ncannasse/webidl.git # Clone the repo
haxelib dev webidl webidl # Set the webidl package install directory to the cloned repo

Usage

Given the following IDL file describing a C++ library:

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();
};

And the following Haxe code (strictly typed thanks to IDL definitions):

class Sample {

    public static function main() {
	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()}');
	p1.delete();
	p2.delete();
	p.delete();
    }
	
}

This compiles to the following Javascript:

// Generated by Haxe 4.0.0
(function () { "use strict";
var Sample = function() { };
Sample.main = function() {
    var this1 = _eb_Point_new0();
    var p1 = this1;
    _eb_Point_set_x(p1,4);
    _eb_Point_set_y(p1,5);
    var this2 = _eb_Point_new2(7,8);
    var p2 = this2;
    var p = _eb_Point_op_add1(p1,p2);
    console.log("Result = " + _eb_Point_get_x(p) + "," + _eb_Point_get_y(p) + " len=" + _eb_Point_length0(p));
    _eb_Point_delete(p1);
    _eb_Point_delete(p2);
    _eb_Point_delete(p);
};
Sample.main();
})();

Haxe webidl can be used for both Haxe/JS and Haxe/HashLink, the cpp bindings generated are the same for both platforms.

webidl's People

Contributors

akitsu-sanae avatar nadako avatar ncannasse avatar tong avatar zicklag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webidl's Issues

Question: Are static methods and namespaces supported?

Hi,

Are static methods and namespaces supported?

My example would look like: tdme.utils.Console.idl:

namespace tdme {
  namespace utils {
    interface Console {
      static void println();
    }
  }
}

Also is there support for some std containers and std::string?

Many thanx and

Best regards
Andreas

new try to compile sample on mac

New year, new try :)
but I still facing errors :/

With Haxe nightly build

make js
haxe -lib webidl --macro "SampleModule.buildLibCpp()"
haxe -lib webidl --macro "SampleModule.buildLibJS()"
> python /Users/user/Documents/GIT-REPOS/emsdk/emscripten/1.38.21/emcc.py -O2 -c point.cpp -o point.bc
> python /Users/user/Documents/GIT-REPOS/emsdk/emscripten/1.38.21/emcc.py -O2 -c libpoint.cpp -o libpoint.bc
> make -f Makefile.tmp
python "/Users/user/Documents/GIT-REPOS/emsdk/emscripten/1.38.21/emcc.py" point.bc libpoint.bc -O2 -s EXPORT_NAME="'libpoint'" --memory-init-file 0 -o libpoint.js
haxe -js sample.js -lib webidl -main Sample -dce full
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Duplicate access modifier inline
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Previously defined here
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Duplicate access modifier inline
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Previously defined here
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Duplicate access modifier inline
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Previously defined here
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Duplicate access modifier inline
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Previously defined here
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Duplicate access modifier inline
SampleModule.hx:2: characters 26-70 : Defined in this class
/Users/user/haxe/versions/80f42bd/std/haxe/macro/Expr.hx:826: characters 2-10 : Previously defined here
SampleModule.hx:2: characters 26-70 : Defined in this class
make: *** [js] Error 1

Haxe 3.4.7

make js
haxe -lib webidl --macro "SampleModule.buildLibCpp()"
/Users/user/haxe/haxelib/webidl/1,0,0/webidl/Parser.hx:82: characters 28-29 : Unexpected (
/Users/user/haxe/haxelib/webidl/1,0,0/webidl/Parser.hx:87: characters 28-29 : Unexpected (
/Users/user/haxe/haxelib/webidl/1,0,0/webidl/Parser.hx:90: characters 3-9 : Unexpected ensure
make: *** [libpoint.cpp] Error 1

Cannot compile the sample with haxe 4.2.5

I have this error

webidl/webidl/Module.hx:431: characters 14-20 : Warning : __js__ is deprecated, use js.Syntax.code instead
webidl/webidl/Module.hx:431: characters 22-75 : The `code` argument for js.Syntax.code must be a string constant

This is the problematic line
untyped __js__('${opts.nativeLib} = ${opts.nativeLib}().then(onReady)');

Make AutoGC Work for the JavaScript Target

Currently the AutoGC option doesn't do anything for the JavaScript target. The WebIDL binder that comes with Emscripten and is used for Ammo.js does do some kind of wrapping of the generated bindings that allows the pointers to be garbage collected by JavaScript. I don't understand exactly how this works, but I'll have to try to take the same concept and implement it here.

If anybody else understands it I could use some pointers. ๐Ÿ™‚

try to compile sample on mac

Hello,

I have try to compile the sample example, on Mac (10.12.6), without success.
With Haxe nightly build : a2058ad (2017-11-24 23:06)

Haxe/JS

MAC:sample user$ make js
haxe -lib webidl --macro "SampleModule.buildLibCpp()"
haxe -lib webidl --macro "SampleModule.buildLibJS()"
> python /Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/emcc.py -O2 -c point.cpp -o point.bc
> python /Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/emcc.py -O2 -c libpoint.cpp -o libpoint.bc
> make -f Makefile.tmp
python "/Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/emcc.py" point.bc libpoint.bc -O2 -s EXPORT_NAME="'libpoint'" --memory-init-file 0 -o libpoint.js
extract archive contents(/Users/user/.emscripten_cache/asmjs/libcxx_noexcept.a) failed with error: llvm-ar failed to extract file(s) ['/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/any_6dd8e4e1.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/bind_854a22e6.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/chrono_2aa73c48.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/condition_variable_7f188147.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/debug_ab332be3.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/exception_79e5900d.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/future_8fa8a270.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/hash_72828e53.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/ios_ac52e915.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/iostream_901f8d59.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/locale_1a06cfe0.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/memory_9c523531.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/mutex_da112174.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/new_5b5bcf2d.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/optional_ab2998f6.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/random_b07b8c86.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/regex_4fd6c5e5.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/shared_mutex_62a3e4d1.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/stdexcept_0b9fd5e0.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/string_a8ec1c3c.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/strstream_a72f5565.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/system_error_195de63a.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/thread_4405f6af.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/typeinfo_fa1dba93.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/utility_baafad1b.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/valarray_f119550e.cpp.o', '/private/var/folders/w4/h9j82c1933ggrt7fhtnl978nhk1650/T/emscripten_temp_Z9gcxh_archive_contents/variant_aac0e9c1.cpp.o'] from archive file /Users/user/.emscripten_cache/asmjs/libcxx_noexcept.a! Error:dyld: lazy symbol binding failed: Symbol not found: _futimens
  Referenced from: /Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/clang/e1.37.22_64bit/llvm-ar
  Expected in: /usr/lib/libSystem.B.dylib

dyld: Symbol not found: _futimens
  Referenced from: /Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/clang/e1.37.22_64bit/llvm-ar
  Expected in: /usr/lib/libSystem.B.dylib

0  llvm-ar                  0x0000000109191d88 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  llvm-ar                  0x0000000109192336 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fffbeca5b3a _sigtramp + 26
3  libsystem_platform.dylib 0x00000001105d8b4d _sigtramp + 1368600621
4  libsystem_platform.dylib 0x00000001105e3464 _sigtramp + 1368643908
5  libsystem_platform.dylib 0x00000001105be793 _sigtramp + 1368493171
6  libsystem_platform.dylib 0x00000001105be89e _sigtramp + 1368493438
7  libdyld.dylib            0x00007fffbea92282 dyld_stub_binder + 282
8  llvm-ar                  0x00000001093c8340 (anonymous namespace)::DarwinX86AsmBackend::getCompactUnwindRegNum(unsigned int) const::CU64BitRegs + 10650
9  llvm-ar                  0x0000000109059e6d performOperation(ArchiveOperation, llvm::object::Archive*, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> >, std::__1::vector<llvm::NewArchiveMember, std::__1::allocator<llvm::NewArchiveMember> >*) + 2013
10 llvm-ar                  0x00000001090590a2 performOperation(ArchiveOperation, std::__1::vector<llvm::NewArchiveMember, std::__1::allocator<llvm::NewArchiveMember> >*) + 930
11 llvm-ar                  0x0000000109056c99 main + 361
12 libdyld.dylib            0x00007fffbea96235 start + 1
Stack dump:
0.	Program arguments: /Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/clang/e1.37.22_64bit/llvm-ar xo /Users/user/.emscripten_cache/asmjs/libcxx_noexcept.a 

Traceback (most recent call last):
  File "/Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/emcc.py", line 2611, in <module>
    run()
  File "/Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/emcc.py", line 1443, in run
    final = shared.Building.link(linker_inputs, DEFAULT_FINAL, force_archive_contents=force_archive_contents, temp_files=misc_temp_files, just_calculate=just_calculate)
  File "/Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/tools/shared.py", line 1772, in link
    Building.read_link_inputs(filter(lambda x: not x.startswith('-'), files))
  File "/Users/user/Documents/GIT-REPOS/webidl/sample/emsdk-portable/emscripten/1.37.22/tools/shared.py", line 1698, in read_link_inputs
    raise Exception('llvm-ar failed on archive ' + archive_names[n] + '!')
Exception: llvm-ar failed on archive /Users/user/.emscripten_cache/asmjs/libcxx_noexcept.a!
make[1]: *** [all] Error 1
/Users/user/haxe/haxelib/webidl/1,0,0/webidl/Generate.hx:389: characters 18-23 : Uncaught exception Command 'make' has exit with error code 2
No stack information available, consider compiling with -D eval-stack
make: *** [libpoint.js] Error 1

Haxe/HashLink

MAC:sample user$ make hl
haxe -lib webidl --macro "SampleModule.buildLibCpp()"
cc -o libpoint.hdll -shared -Wall -O3 -I . -I /Users/user/Documents/GIT-REPOS/hashlink/src libpoint.cpp point.cpp -lstdc++ -lhl
libpoint.cpp:36:13: warning: unused function 'finalize_Point' [-Wunused-function]
static void finalize_Point( _ref(Point)* _this ) { free_ref(_this); }
            ^
1 warning generated.
ld: library not found for -lhl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libpoint.hdll] Error 1

[Question] What is the plan with regards to handling bindings for multiple c++ classes in the same project

@zicklag ... while I have you here maybe you can answer something for me? What is the plan with regards to handling bindings for multiple c++ classes in the same project. The idl file in the sample is called point.idl presumably because the only thing it handles presently is the Point class but if I wanted to add a class (different header and implementation file) would I just append it to the same .idl file? And if so I suspect I will have to handle it somehow in the makefile? Perhaps make is a bit light for a larger workload if that's the case? Perhaps the work presently being done by make should be handled by a webidl executable? I'm not sure what the long term goals are for this project but it looks like something that I would like to make more industrial use of than just a few samples...

Originally posted by @WolfieWerewolf in #13 (comment)

Add License

Hi, could you add a license for this project, please? I'm hoping to integrate components of it into Kha's build process, but I need the license terms.

BTW this lib is extremely useful, and will help a lot. ๐Ÿ‘

Question about pointers as params

I am trying to create a test function:

int test(int integers[], int count) {
    int res = 0;
    for (int i = 0; i < count; i ++) res += integers[i];
    return res;
}

and the IDL definition:

    float test(int[] integers, int count);

However, I am not at all sure how to call this function through Haxe. Is it possible at the moment?

image

Errors compiling using Clang

I am trying to compile bullet using webidl with Clang, but I'm getting these errors:

/Users/jefvel/project/libs/bullet/src/bullet.cpp:35:21: error: pasting formed 'u"delete() is not allowed on const value."', an invalid preprocessing token
        if( !r->finalize ) hl_error("delete() is not allowed on const value.");
                           ^
/Users/jefvel/project/libs/bullet/../../hashlink/src/hl.h:586:55: note: expanded from macro 'hl_error'
#define hl_error(msg, ...) hl_throw(hl_alloc_strbytes(USTR(msg), ## __VA_ARGS__))
                                                      ^
/Users/jefvel/project/libs/bullet/../../hashlink/src/hl.h:234:21: note: expanded from macro 'USTR'
#       define USTR(str)        u##str
                                 ^
/Users/jefvel/project/libs/bullet/src/bullet.cpp:35:21: error: use of undeclared identifier 'u'
/Users/jefvel/project/libs/bullet/../../hashlink/src/hl.h:586:55: note: expanded from macro 'hl_error'
#define hl_error(msg, ...) hl_throw(hl_alloc_strbytes(USTR(msg), ## __VA_ARGS__))
                                                      ^
/Users/jefvel/project/libs/bullet/../../hashlink/src/hl.h:234:20: note: expanded from macro 'USTR'
#       define USTR(str)        u##str
                                ^
/Users/jefvel/project/libs/bullet/../../hashlink/src/hl.h:234:20: note: expanded from macro 'u'
/Users/jefvel/project/libs/bullet/src/bullet.cpp:43:16: error: assigning to 'void *' from incompatible type 'void (*)(pref<btVector3> *)'
        r->finalize = finalize;
                      ^~~~~~~~
/Users/jefvel/project/libs/bullet/src/bullet.cpp:557:9: note: in instantiation of function template specialization '_alloc_ref<btVector3>' requested here
        return alloc_ref((new btVector3()),btVector3);
               ^
/Users/jefvel/project/libs/bullet/src/bullet.cpp:30:24: note: expanded from macro 'alloc_ref'
#define alloc_ref(r,t) _alloc_ref(r,finalize_##t)

...

(It continues with a lot more error: assigning to 'void *' from incompatible type ' X errors.)

I managed to circumvent this and successfully compile by using gcc and setting the flag -fpermissive, but I'm wondering if there could be some way to make it work in Clang directly.

Sample.hx:2: characters 8-20 : Module SampleModule does not define type SampleModule

before I try to build I've installed emcc .
haxe version Haxe Compiler 4.1.0-rc.1+4bf5cce05

1:

  haxelib git webidl https://github.com/ncannasse/webidl.git

2:

git clone https://github.com/ncannasse/webidl.git

3

cd webidl/sample

4

haxelib dev webidl . 

5

make js

root@cloud-q5l9gc-cyse:~/test/webidl/sample# make js
haxe -lib webidl --macro "SampleModule.buildLibCpp()"
haxe -lib webidl --macro "SampleModule.buildLibJS()"
> python /root/libs/emsdk/upstream/emscripten//emcc.py -O2 -c point.cpp -o point.bc
> python /root/libs/emsdk/upstream/emscripten//emcc.py -O2 -c libpoint.cpp -o libpoint.bc
> make -f Makefile.tmp
make[1]: Entering directory '/root/test/webidl/sample'
python "/root/libs/emsdk/upstream/emscripten//emcc.py" point.bc libpoint.bc -O2 -s EXPORT_NAME="'libpoint'" --memory-init-file 0 -o libpoint.js
make[1]: Leaving directory '/root/test/webidl/sample'
haxe -js sample.js -lib webidl -main Sample -dce full
Sample.hx:2: characters 8-20 : Module SampleModule does not define type SampleModule
Makefile:10: recipe for target 'js' failed
make: *** [js] Error 1

//////////

which step was wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.