How can i do the same in haxe ?
Actrually this code doesn’t export my ScreenPressor
class Main
{
static inline var X = 960;
static inline var Y = 540;
static var ScreenPressor;
static function main()
{
ScreenPressor = new ScreenPressor(X,Y);
}
}
I’d love to keep this thread alive as I was trying, and mostly failing, to do something similar at the weekend.
I’m new to WASM, Emscripten and indeed the wonderful hxcpp. I found it straightforward to generate and run the WASM, but I also found configuring the compiler and exposing methods to Javascript harder, and I have not yet succeeded. Unfortunately I didn’t record my initial attempts fully but I did discover that:
The main static method of a WASM app compiled with the emscripten toolchain seems to be automatically exposed to javascript (as Module._main(scope, arguments)) so you can call and pass arguments to it, which, in turn can be accessed from within the main method using Sys.args – this might be sufficient for many use cases.
package hxcpp;
import hxcpp.ScreenPressor;
@:buildXml("
<linker id='exe' exe='emcc'>
<flag value='--bind' />
<flag value='-s' />
<flag value='-O3' />
<flag value='TOTAL_MEMORY=33554432' />
<flag value='-s' />
<flag value='WASM=1' />
</linker>
")
@:cppFileCode('
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(my_module) {
class_<hxcpp::ScreenPressor>("ScreenPressor")
.constructor<int, int>()
.function("DecompressFrame", &hxcpp::ScreenPressor::DecompressI)
;
}
')
class Main
{
static inline var X = 960;
static inline var Y = 540;
static var SP:ScreenPressor;
static function main()
{
SP = new ScreenPressor(X,Y);
trace('hello');
}
}
You are right, i used both @:buildXml and @:cppFileCode.
I’m acualy fighting against a cpp error
Error: ./src/hxcpp/Main.cpp:25:63: error: no member named ‘DecompressI’ in ‘hx::ObjectPtrhxcpp::ScreenPressor_obj’
.function(“DecompressFrame”, &hxcpp::ScreenPressor::DecompressI)
~~~~~~~~~~~~~~~~~~~~~~^
In file included from ./src/hxcpp/Main.cpp:18:
/home/tamina/projects/emsdk/emscripten/1.38.5/system/include/emscripten/bind.h:433:24: error: no matching constructor for initialization of ‘hx::ObjectPtr< hxcpp::ScreenPressor_obj>’
return new ClassType(std::forward(args)…);
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/home/tamina/projects/emsdk/emscripten/1.38.5/system/include/emscripten/bind.h:1177:28: note: in instantiation of function template specialization ‘emscrip ten::internal::operator_new<hx::ObjectPtrhxcpp::ScreenPressor_obj, int, int>’ requested here
&internal::operator_new<ClassType, ConstructorArgs…>,
^
./src/hxcpp/Main.cpp:24:12: note: in instantiation of function template specialization ‘emscripten::class_<hx::ObjectPtrhxcpp::ScreenPressor_obj, emscrip ten::internal::NoBaseClass>::constructor<int, int>’ requested here
.constructor<int, int>()
Hi,
I didn’t tried to compile your sources, but one thing looks wrong as for me.
Inside cppFileCode macro you trying to call DecompressI through ScreenPressor, but I believe it should be ScreenPressor_obj.
So, try something like this:
@:cppFileCode('
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(my_module) {
class_<hxcpp::ScreenPressor_obj>("ScreenPressor") // not sure if name inside "..." shold be replaced too
.constructor<int, int>()
.function("DecompressFrame", &hxcpp::ScreenPressor_obj::DecompressI)
;
}
')
Error: In file included from ./src/hxcpp/Main.cpp:18:
/home/tamina/projects/emsdk/emscripten/1.38.5/system/include/emscripten/bind.h:452:13: error: no suitable member ‘operator delete’ in ‘ScreenPressor_obj’
delete ptr;
^
/home/tamina/projects/emsdk/emscripten/1.38.5/system/include/emscripten/bind.h:1126:32: note: in instantiation of function template specialization ‘emscripten::internal::raw_destructorhxcpp::ScreenPressor_obj’ requested here
auto destructor = &raw_destructor;
^
./src/hxcpp/Main.cpp:23:3: note: in instantiation of member function ‘emscripten::class_<hxcpp::ScreenPressor_obj, emscripten::internal::NoBaseClass>::class_’ requested here
class_hxcpp::ScreenPressor_obj(“ScreenPressor”)
^
/home/tamina/haxelib/hxcpp/3,4,188/include/hx/Object.h:190:9: note: member ‘operator delete’ declared here
void operator delete( void *, bool) { }
Now it’s another problem. Haxe generates C++ classes without destructors but looks like Emscripten needs them.
Not sure if this is a solution, but try to put this thing into ScreenPressor.hx:
@:headerClassCode('
~ScreenPressor() { //or ScreenPressor_obj, not sure
//emty destructor
}
')
class ScreenPressor {
....
}
Or you can try to wrap your ScreenPressor class and implement destructor in wrapper. Something like you did before.
You you may not get normal linkage or name resolution for inline modifiers. The compiler may never have to lay down a declaration. See if you can drop the inline from inline void DecompressI.
Yeah, bumped into the inline issue with simple COBOL to C interfacing. And your problem is Haxe to C++ to Javascript to … how many layers?
Getting as far as you have is a testament to the quality of the toolchain, a tool chain, in this case, with many many links, the weakest still being pretty strong, it seems.