Skip to content

Commit

Permalink
Allow optional detaching of main thread after Native Init call. for H…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh Sanderson committed Apr 25, 2024
1 parent 7a7a17a commit 4693f95
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/hx/Native.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace hx

};

HXCPP_CLASS_ATTRIBUTES const char *Init();
HXCPP_CLASS_ATTRIBUTES const char *Init(bool stayAttached=true);
HXCPP_CLASS_ATTRIBUTES void PushTopOfStack(void *);
HXCPP_CLASS_ATTRIBUTES void PopTopOfStack();
HXCPP_CLASS_ATTRIBUTES void GcAddOffsetRoot(void *inRoot, int inOffset);
Expand Down
11 changes: 10 additions & 1 deletion src/hx/StdLibs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,25 @@ Array<unsigned char> __hxcpp_resource_bytes(String inName)
extern "C" void __hxcpp_lib_main();
namespace hx
{
const char *Init()
static std::string initReturnBuffer;
const char *Init(bool stayAttached)
{
try
{
__hxcpp_lib_main();
if (!stayAttached)
SetTopOfStack(0,true);
return 0;
}
catch(Dynamic e)
{
HX_TOP_OF_STACK
if (!stayAttached)
{
initReturnBuffer = e->toString().utf8_str();
SetTopOfStack(0,true);
return initReturnBuffer.c_str();
}
return e->toString().utf8_str();
}
}
Expand Down
55 changes: 40 additions & 15 deletions test/extern-use/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,62 @@ struct MyStruct

extern void __hxcpp_collect(bool inMajor);

bool checkAttachNone(const char *where)
{
if (hx::GcGetThreadAttachedCount()!=0)
{
printf("Bad attach count - something attached: %s\n",where);
return false;
}
return true;
}

int main(int argc, char **argv)
{
MyStruct *myStruct = new MyStruct();


const char *err = hx::Init();
if (err)
{
printf("Could not initialize library: %s\n", err);
if (!checkAttachNone("before creation"))
return -1;
}
else

{
hx::NativeAttach autoAttach;
const char *err = hx::Init(false);
if (err)
{
printf("Could not initialize library: %s\n", err);
return -1;
}
}

api::HaxeObject *obj = api::HaxeApi::createBase();
obj->setName("child");

myStruct->haxeRef = obj;
obj->setName("Name");
if (!checkAttachNone("after init"))
return -1;

api::HaxeObject *child = obj->createChild();
{
hx::NativeAttach autoAttach;

api::HaxeObject *obj = api::HaxeApi::createBase();
obj->setName("child");

myStruct->haxeRef = obj;
obj->setName("Name");

api::HaxeObject *child = obj->createChild();
}


if (!checkAttachNone("after interaction"))
return -1;

{
hx::NativeAttach autoAttach;
__hxcpp_collect(true);
}

if (!checkAttachNone("after collect"))
return -1;

{
hx::NativeAttach autoAttach;
if (myStruct->haxeRef->getName()!="Name")
Expand All @@ -61,11 +88,9 @@ int main(int argc, char **argv)
return -1;
}
}
if (hx::GcGetThreadAttachedCount()!=0)
{
printf("Bad clear attach count\n");

if (!checkAttachNone("after clear"))
return -1;
}

printf("all good\n");
return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/std/Test.hx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import cpp.link.StaticSqlite;
@:buildXml('<include name="${HXCPP}/src/hx/libs/ssl/Build.xml"/>')
extern class SslTest
{
@:extern @:native("_hx_ssl_init")
@:native("_hx_ssl_init")
extern public static function socket_init():Void;
}

Expand Down Expand Up @@ -140,7 +140,7 @@ class Test
for( row in rset )
{
var pass:Dynamic = row.password;
var password = Std.is(pass, haxe.io.BytesData) ? haxe.io.Bytes.ofData(pass) : pass;
var password = Std.isOfType(pass, haxe.io.BytesData) ? haxe.io.Bytes.ofData(pass) : pass;
var md5 = haxe.crypto.Md5.make(password).toHex().substr(0,8);
v(" user "+row.name+" is "+row.age+" years old, password:" + md5);
if (md5!="5f80e231" && md5!="8ed0b363")
Expand Down
1 change: 0 additions & 1 deletion test/std/compile32.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
-D HXCPP_M32
-D HXCPP_DEBUGGER
-cp ../unit
-lib hx4compat
1 change: 0 additions & 1 deletion test/std/compile64.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
-cpp cpp64
-D HXCPP_M64
-cp ../unit
-lib hx4compat

0 comments on commit 4693f95

Please sign in to comment.