소스 검색

[hxcpp] Export spcial cpp types as fsUnkown to cppia. Change definition of cpp.Star to allow null setting. Add cpp.Native class for some easier access to cpp.Star pointers. Bump hxcpp_api_version to 400.

Hugh Sanderson 7 년 전
부모
커밋
151a5028c0
5개의 변경된 파일123개의 추가작업 그리고 16개의 파일을 삭제
  1. 1 1
      src/compiler/main.ml
  2. 21 13
      src/generators/gencpp.ml
  3. 1 1
      std/cpp/ConstStar.hx
  4. 99 0
      std/cpp/Native.hx
  5. 1 1
      std/cpp/Star.hx

+ 1 - 1
src/compiler/main.ml

@@ -252,7 +252,7 @@ module Initialize = struct
 					add_std "php";
 				"php"
 			| Cpp ->
-				Common.define_value com Define.HxcppApiLevel "332";
+				Common.define_value com Define.HxcppApiLevel "400";
 				add_std "cpp";
 				if Common.defined com Define.Cppia then
 					classes := (Path.parse_path "cpp.cppia.HostClasses" ) :: !classes;

+ 21 - 13
src/generators/gencpp.ml

@@ -1861,9 +1861,9 @@ let rec cpp_type_of ctx haxe_type =
       | (["cpp"],"Struct"), [param] ->
             TCppStruct(cpp_type_of ctx param)
       | (["cpp"],"Star"), [param] ->
-            TCppStar(cpp_type_of ctx param,false)
+            TCppStar(cpp_type_of_pointer ctx param,false)
       | (["cpp"],"ConstStar"), [param] ->
-            TCppStar(cpp_type_of ctx param,true)
+            TCppStar(cpp_type_of_pointer ctx param,true)
 
       | ([],"Array"), [p] ->
          let arrayOf = cpp_type_of ctx p in
@@ -1901,7 +1901,10 @@ let rec cpp_type_of ctx haxe_type =
         TCppObject
      else
         baseType
-
+   and cpp_type_of_pointer ctx p =
+     match p with
+     | TAbstract ({ a_path = ([],"Null") },[t]) -> cpp_type_of ctx t
+     | x ->  cpp_type_of ctx x
    (* Optional types are Dynamic if they norally could not be null *)
    and cpp_fun_arg_type_of ctx tvar opt =
       match opt with
@@ -2322,8 +2325,7 @@ let cpp_enum_name_of field =
       keyword_remap field.ef_name
 ;;
 
-let is_gc_element ctx member_type =
-  Common.defined ctx.ctx_common Define.HxcppGcGenerational &&
+let is_object_element ctx member_type =
   match member_type with
    | TCppDynamic
    | TCppObject
@@ -2343,7 +2345,10 @@ let is_gc_element ctx member_type =
 
 ;;
 
-
+let is_gc_element ctx member_type =
+  Common.defined ctx.ctx_common Define.HxcppGcGenerational &&
+  (is_object_element ctx member_type)
+;;
 
 let retype_expression ctx request_type function_args function_type expression_tree forInjection =
    let rev_closures = ref [] in
@@ -3089,6 +3094,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
             else (match return_type with
                | TCppObjC(k) -> CppCastObjC(baseCpp,k), return_type
                | TCppPointer(_,_)
+               | TCppStar(_)
                | TCppInst(_) -> CppCast(baseCpp,return_type), return_type
                | TCppString -> CppCastScalar(baseCpp,"::String"), return_type
                | TCppCode(t) when baseStr <> (tcpp_to_string t)  ->
@@ -3223,7 +3229,8 @@ let retype_expression ctx request_type function_args function_type expression_tr
 
 
          | TCppStar(t,const), TCppInst _
-         | TCppStar(t,const), TCppStruct _ ->
+         | TCppStar(t,const), TCppStruct _
+         | TCppStar(t,const), TCppReference _ ->
              mk_cppexpr (CppDereference(cppExpr)) return_type
 
          | TCppInst _, TCppStar(p,const)
@@ -6032,12 +6039,13 @@ let generate_class_files baseCtx super_deps constructor_deps class_def inScripta
          output_cpp "};\n\n";
       end;
 
-      let storage field = match type_string field.cf_type with
-         | "bool" -> "hx::fsBool"
-         | "int" -> "hx::fsInt"
-         | "Float" -> "hx::fsFloat"
-         | "::String" -> "hx::fsString"
-         | str -> "hx::fsObject" ^ " /*" ^ str ^ "*/ "
+      let storage field = match (cpp_type_of ctx field.cf_type) with
+         | TCppScalar("bool") -> "hx::fsBool"
+         | TCppScalar("int") -> "hx::fsInt"
+         | TCppScalar("Float") -> "hx::fsFloat"
+         | TCppString -> "hx::fsString"
+         | o when is_object_element ctx o -> "hx::fsObject" ^ " /* " ^ (tcpp_to_string o ) ^ " */ "
+         | u -> "hx::fsUnknown" ^ " /* " ^ (tcpp_to_string u) ^ " */ "
          in
       let dump_member_storage = (fun field ->
          output_cpp ("\t{" ^ (storage field) ^ ",(int)offsetof(" ^ class_name ^"," ^ (keyword_remap field.cf_name) ^")," ^

+ 1 - 1
std/cpp/ConstStar.hx

@@ -22,6 +22,6 @@
 package cpp;
 
 // Allows haxe to type result correctly, and hxcpp can recognise this use the correct type
-typedef ConstStar<T> = T;
+typedef ConstStar<T> = Null<T>;
 
 

+ 99 - 0
std/cpp/Native.hx

@@ -0,0 +1,99 @@
+/*
+ * Copyright (C)2005-2018 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package cpp;
+
+@:include("stdlib.h")
+extern class Native
+{
+   @:native("malloc")
+   public static function nativeMalloc(bytes:Int) : cpp.Star<cpp.Void> return null;
+   @:native("calloc")
+   public static function nativeCalloc(bytes:Int) : cpp.Star<cpp.Void> return null;
+   @:native("realloc")
+   public static function nativeRealloc(inPtr:cpp.Star<cpp.Void>,bytes:Int) : cpp.RawPointer<cpp.Void> return null;
+   @:native("free")
+   public static function nativeFree(ptr:cpp.Star<cpp.Void>) : Void { }
+   @:native("memcpy")
+   public static function nativeMemcpy(dest:cpp.Star<cpp.Void>, src:cpp.Star<cpp.Void>, bytes:Int) : Void { }
+
+   @:native("hx::ClassSizeOf") @:templatedCall
+   public static function sizeof<T>(t:T) : Int { }
+
+   #if !cppia
+   @:generic
+   public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
+   {
+      var ref: cpp.Reference<T> = ptr;
+      ref = value;
+   }
+   @:generic
+   public static inline function get<T>(ptr:cpp.Star<T>) : T
+   {
+      var ref: cpp.Reference<T> = ptr;
+      return ref;
+   }
+
+   @:generic
+   public static inline function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void
+      nativeMemcpy(cast dest,cast src, bytes);
+
+   @:generic
+   public static inline function malloc<T>(bytes:Int) : cpp.Star<T>
+      return cast nativeMalloc(bytes);
+
+   @:generic
+   public static inline function calloc<T>(bytes:Int) : cpp.Star<T>
+      return cast nativeCalloc(bytes);
+
+   @:generic
+   public static inline function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T>
+      return cast nativeRealloc(cast ioPtr, bytes);
+
+   @:generic
+   public static inline function free<T>(ptr:cpp.Star<T>) : Void
+   {
+      if (ptr!=null)
+         nativeFree(cast ptr);
+   }
+
+   #else
+   public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
+   {
+      throw "Native.set not available in cppia";
+   }
+   public static inline function get<T>(ptr:cpp.Star<T>) : T
+   {
+      throw "Native.get not available in cppia";
+      var d:Dynamic = null;
+      return d;
+   }
+
+   public static function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void { }
+   public static function malloc<T>(bytes:Int) : cpp.Star<T> return null;
+   public static function calloc<T>(bytes:Int) : cpp.Star<T> return null;
+   public static function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T> return null;
+   public static function free<T>(ptr:cpp.Star<T>) : Void { }
+   #end
+
+}
+

+ 1 - 1
std/cpp/Star.hx

@@ -22,6 +22,6 @@
 package cpp;
 
 // Allows haxe to type result correctly, and hxcpp can recognise this use the correct type
-typedef Star<T> = T;
+typedef Star<T> = Null<T>;