Bug 4333 - btouch cannot bind to NSString* const fields
Summary: btouch cannot bind to NSString* const fields
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2012-04-07 23:34 UTC by Tyson
Modified: 2012-04-09 18:19 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
The full binding project and zbar lib file. (1.18 MB, application/x-zip-compressed)
2012-04-07 23:34 UTC, Tyson
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Tyson 2012-04-07 23:34:09 UTC
Created attachment 1630 [details]
The full binding project and zbar lib file.

The exact scenario is documented here: http://docs.xamarin.com/ios/advanced_topics/binding_objective-c_types#Binding_Fields

However it does not seem to work. It crashes with an unhandled exception:

Target GenerateBindings:
        Tool /Developer/MonoTouch/usr/bin/btouch execution started with arguments: /d:DEBUG /e ApiDefinition.cs /s:StructsAndEnums.cs /tmpdir:obj/Debug/ios/ /sourceonly:obj/Debug/ios//sources.list 
        
        Unhandled Exception: System.ArgumentOutOfRangeException: Argument is out of range.
        Parameter name: startIndex
          at System.String.Substring (Int32 startIndex) [0x00000] in <filename unknown>:0 
          at Generator.Generate (System.Type type) [0x00000] in <filename unknown>:0 
          at Generator.Go () [0x00000] in <filename unknown>:0 
          at BindingTouch.Main (System.String[] args) [0x00000] in <filename unknown>:0 
        [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range.
        Parameter name: startIndex
          at System.String.Substring (Int32 startIndex) [0x00000] in <filename unknown>:0 
          at Generator.Generate (System.Type type) [0x00000] in <filename unknown>:0 
          at Generator.Go () [0x00000] in <filename unknown>:0 
          at BindingTouch.Main (System.String[] args) [0x00000] in <filename unknown>:0 

Commenting out the two offending lines and the whole binding compiles successfully:

    [Static]
    interface ZBarSDK
    {
        // extern NSString* const ZBarReaderControllerResults;
        // [Field ("ZBarReaderControllerResults")]
        //  NSString BarcodeResultsKey { get; }
    }
Comment 1 Tyson 2012-04-07 23:40:23 UTC
Also, I have posted a question to SO. It basically repeats what is said here though:
http://stackoverflow.com/questions/10055053/exposing-an-obj-c-const-nsstring-via-a-monotouch-binding
Comment 2 Sebastien Pouliot 2012-04-09 08:56:24 UTC
I can duplicate the same exception (here with line numbers) using the attached project.

Unhandled Exception: System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: startIndex
  at System.String.Substring (Int32 startIndex) [0x00036] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/String.cs:326 
  at Generator.Generate (System.Type type) [0x0073f] in /Developer/MonoTouch/Source/maccore/src/generator.cs:2311 
  at Generator.Go () [0x00930] in /Developer/MonoTouch/Source/maccore/src/generator.cs:1344 
  at BindingTouch.Main (System.String[] args) [0x00804] in /Developer/MonoTouch/Source/maccore/src/btouch.cs:239 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: startIndex
  at System.String.Substring (Int32 startIndex) [0x00036] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/String.cs:326 
  at Generator.Generate (System.Type type) [0x0073f] in /Developer/MonoTouch/Source/maccore/src/generator.cs:2311 
  at Generator.Go () [0x00930] in /Developer/MonoTouch/Source/maccore/src/generator.cs:1344 
  at BindingTouch.Main (System.String[] args) [0x00804] in /Developer/MonoTouch/Source/maccore/src/btouch.cs:239
Comment 3 Sebastien Pouliot 2012-04-09 09:12:09 UTC
It seems* that generator requires the namespace to start with "MonoTouch.", e.g. "MonoTouch.ZBar", to work properly. The immediate workaround is to renamed your namespaces to the later.

* at least with fields (maybe other parts were fixed long ago to allow other prefixes). I'll look into it.
Comment 4 Sebastien Pouliot 2012-04-09 10:52:02 UTC
The previous comment will fix the `btouch` part, i.e. the code will be generated (no exception). However that generated code does not compile.

First the manual property of the same name (in ZBarSDK.cs) must be commented or it will conflict with the generated one.

Second, the [Field] must tell where the symbol can be loaded. Since the library will be linked statiscally it should be __Internal, like this:

    [Static]
    interface ZBarSDK {
        [Field ("ZBarReaderControllerResults", "__Internal")]
        NSString BarcodeResultsKey { get; }
    }

ref: http://docs.xamarin.com/ios/advanced_topics/binding_objective-c_types#Binding_Fields

Finally it seems the generator is not complete, wrt fields, for user-generated bindings. IOW it still won't compile with the above changes. i.e.

    obj/Debug/ios/ZBar/ZBarSDK.g.cs(45,82): error CS0117: `MonoTouch.Constants' does not contain a definition for `__InternalLibrary'

Such MonoTouch.Constants.* are already pre-defined for MonoTouch's binded libraries/framework. However no generated code is currently provided for user's bindings.

From monotouch-bindings (in GIT) only a single binding (CorePlot) uses [Field] and they are all commented. I suspect a bug was never filled for this :-(
Comment 5 Sebastien Pouliot 2012-04-09 15:12:07 UTC
Two fixes were made in the generator:

029a36aae4472e286332d27bb6d10eb80c8b4d3e fix the ArgumentOutOfRangeException
a9c37e79c3645f9a7afe9685147916d47a617d2a will make your project build correctly if you add the __Internal (see previous comment).

I'll be testing both patches before including them in master and 5.2-series
Comment 6 Sebastien Pouliot 2012-04-09 18:19:32 UTC
Fixed. Thanks a lot for the test project.

master: 44383748a756b05ba3cd5051563c65bb20bf385f
5.2-series: 24c9201a76b13dd1194085230d4b93b68057f9db

QA: you can test this using the attached code (and fix from comment #4) or by building coreplot bindings (and removing the `#if false` around the [Field] items).