Browse Source

[cpp] Avoid dynamic args when calling objc. Add some NSString and NSData externs

hughsando 9 years ago
parent
commit
a223de380a
3 changed files with 103 additions and 5 deletions
  1. 12 5
      src/generators/gencpp.ml
  2. 46 0
      std/cpp/NSData.hx
  3. 45 0
      std/cpp/NSString.hx

+ 12 - 5
src/generators/gencpp.ml

@@ -1805,6 +1805,8 @@ let rec cpp_type_of ctx haxe_type =
       | Some _ -> cpp_type_of_null ctx tvar.t_type
       | Some _ -> cpp_type_of_null ctx tvar.t_type
       | _ -> cpp_type_of ctx tvar.t_type
       | _ -> cpp_type_of ctx tvar.t_type
 
 
+   and cpp_tfun_arg_type_of ctx opt t =
+      if opt then cpp_type_of_null ctx t else cpp_type_of ctx t
 
 
    and cpp_function_type_of ctx function_type abi =
    and cpp_function_type_of ctx function_type abi =
       let abi = (match follow abi with
       let abi = (match follow abi with
@@ -2433,11 +2435,6 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
                CppNullAccess, TCppDynamic
                CppNullAccess, TCppDynamic
             else begin
             else begin
                let cppType = cpp_type_of expr.etype in
                let cppType = cpp_type_of expr.etype in
-               (*
-               let retypedArgs = List.map2 (fun arg (var,opt) ->
-                   retype (cpp_fun_arg_type_of ctx var opt) arg
-                   ) args, func.tf_args in
-               *)
                let retypedArgs = List.map (retype TCppDynamic ) args in
                let retypedArgs = List.map (retype TCppDynamic ) args in
                match retypedFunc.cppexpr with
                match retypedFunc.cppexpr with
                |  CppFunction(FuncFromStaticFunction ,returnType) ->
                |  CppFunction(FuncFromStaticFunction ,returnType) ->
@@ -2469,6 +2466,16 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
                   *)
                   *)
 
 
                (* Other functions ... *)
                (* Other functions ... *)
+
+                 (* todo - non objc? *)
+               | CppFunction( FuncInstance(_,true,{cf_type=TFun(arg_types,_)} ) as func, returnType )
+               | CppFunction( FuncStatic(_,true,{cf_type=TFun(arg_types,_)} ) as func, returnType ) ->
+                  (* retype args specifically (not just CppDynamic) *)
+                  let retypedArgs = List.map2 (fun arg (_,opt,t) ->
+                      retype (cpp_tfun_arg_type_of ctx opt t) arg
+                      ) args arg_types in
+                  CppCall(func,retypedArgs), returnType
+ 
                |  CppFunction(func,returnType) ->
                |  CppFunction(func,returnType) ->
                      CppCall(func,retypedArgs), returnType
                      CppCall(func,retypedArgs), returnType
 
 

+ 46 - 0
std/cpp/NSData.hx

@@ -0,0 +1,46 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+@:native("NSData") @:objc extern class NSDataData { }
+
+@:objc
+extern abstract NSData( NSDataData )
+{
+   @:native("_hx_bytes_to_nsdata") @:extern static function BytesToNSData(b:haxe.io.BytesData) : NSData return null;
+   @:native("_hx_bytes_to_nsdata") @:extern static function BytesToNSDataData(b:haxe.io.BytesData) : NSDataData return null;
+   @:native("_hx_nsdata_to_bytes") @:extern static function NSDataDataToBytes(d:NSDataData) : haxe.io.BytesData return null;
+
+
+   inline function new(d:NSDataData) this = d;
+
+   @:from @:extern
+   static public inline function fromBytesData(d:haxe.io.BytesData):NSData return new NSData( BytesToNSDataData(d) );
+
+   @:from @:extern
+   static public inline function fromBytes(d:haxe.io.Bytes):NSData return new NSData( BytesToNSDataData(d.getData()) );
+
+   @:to @:extern
+   public inline function toBytes():haxe.io.BytesData return NSDataDataToBytes(this);
+
+}
+

+ 45 - 0
std/cpp/NSString.hx

@@ -0,0 +1,45 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+@:native("NSString") @:objc extern class NSStringData { }
+
+@:objc
+extern abstract NSString( NSStringData )
+{
+   @:native(" (NSString *)") @:extern static function StringToNSString(s:String) : NSString return null;
+   @:native(" (NSString *)") @:extern static function StringToNSStringData(s:String) : NSStringData return null;
+   @:native("String") @:extern static function NSStringDataToString(s:NSStringData) : String return null;
+
+
+   inline function new(s:NSStringData) this = s;
+
+
+   @:from @:extern
+   static public inline function fromString(s:String):NSString return new NSString( StringToNSStringData(s) );
+
+
+   @:to @:extern
+   public inline function toString():String return NSStringDataToString(this);
+
+}
+