Browse Source

[cpp] Require explicit casting to go from cpp.Star to cpp.Reference. Add cpp.Native.addressOf to get cpp.Star from address. Closes https://github.com/HaxeFoundation/hxcpp/issues/741

hughsando 6 năm trước cách đây
mục cha
commit
7132528cb9
2 tập tin đã thay đổi với 17 bổ sung3 xóa
  1. 8 3
      src/generators/gencpp.ml
  2. 9 0
      std/cpp/Native.hx

+ 8 - 3
src/generators/gencpp.ml

@@ -2663,7 +2663,13 @@ let retype_expression ctx request_type function_args function_type expression_tr
 
                | CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "hx::AddressOf" ->
                     let arg = retype TCppUnchanged (List.hd args) in
-                    CppAddressOf(arg), TCppRawPointer("", arg.cpptype)
+                    let rawType = match arg.cpptype with | TCppReference(x) -> x | x -> x in
+                    CppAddressOf(arg), TCppRawPointer("", rawType)
+
+               | CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "hx::StarOf" ->
+                    let arg = retype TCppUnchanged (List.hd args) in
+                    let rawType = match arg.cpptype with | TCppReference(x) -> x | x -> x in
+                    CppAddressOf(arg), TCppStar(rawType,false)
 
                | CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "_hx_create_array_length" ->
                   let retypedArgs = List.map (retype TCppDynamic ) args in
@@ -3201,8 +3207,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
 
 
          | TCppStar(t,const), TCppInst _
-         | TCppStar(t,const), TCppStruct _
-         | TCppStar(t,const), TCppReference _ ->
+         | TCppStar(t,const), TCppStruct _ ->
              mk_cppexpr (CppDereference(cppExpr)) return_type
 
          | TCppInst _, TCppStar(p,const)

+ 9 - 0
std/cpp/Native.hx

@@ -76,7 +76,16 @@ extern class Native
          nativeFree(cast ptr);
    }
 
+   @:native("hx::StarOf")
+   public static function addressOf<T>(inVariable:Reference<T>) : Star<T>;
+
    #else
+
+   public static inline function addressOf<T>(inVariable:Reference<T>) : Star<T>
+   {
+      throw "Native.addressOf not available in cppia";
+   }
+
    public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
    {
       throw "Native.set not available in cppia";