Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7006] Android: App.Properties - storing integer larger than 2147483647 causes exception

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusClosed
ResolutionInvalid
Resolution Date2012-01-05T14:14:35.000+0000
Affected Version/sRelease 1.7.5
Fix Version/sn/a
ComponentsAndroid
Labelslook1
ReporterKeith Gable
AssigneePaul Dowsett
Created2011-12-30T10:40:16.000+0000
Updated2017-03-21T21:09:26.000+0000

Description

Problem

The largest integer that can be saved without causing an exception is 2147483647, which is (2^31)-1. Note that large numbers can be successfully saved on: * iOS (according to reporter) * Android + Titanium 1.8.0.1 + v8 * Android + Titanium 1.8.0.1 + rhino

Test case

Ti.App.Properties.setInt("lowInt", 2147483647);
Ti.API.info("****** lowInt Saved ******");
Ti.App.Properties.setInt("highInt", 2147483648);
Ti.API.info("****** highInt Saved ******");
Ti.App.Properties.setInt("veryHighInt", 1325269931753);
Ti.API.info("****** veryHighInt Saved ******");

Logs

  332            KrollContext  D  (kroll$1: app://app.js) [184,376] Running evaluated script: file:///android_asset/Resources/app.js
  332                   TiAPI  I  (kroll$1: app://app.js) [64,440] ****** lowInt Saved ******
  332               TiJSError  E  (kroll$1: app://app.js) [217,657] ----- Titanium Javascript Runtime Error -----
  332               TiJSError  E  (kroll$1: app://app.js) [1,658] - In file:///android_asset/Resources/app.js:110,0
  332               TiJSError  E  (kroll$1: app://app.js) [3,661] - Message: Cannot convert 2147483648 to java.lang.Integer
  332               TiJSError  E  (kroll$1: app://app.js) [1,662] - Source: null
  332             KrollMethod  E  (kroll$1: app://app.js) [400,1062] Exception calling kroll method setInt, invocation: null ]
  332             KrollMethod  E  org.mozilla.javascript.EvaluatorException: Cannot convert 2147483648 to java.lang.Integer (file:///andro
                                  id_asset/Resources/app.js#110)
  332             KrollMethod  E  	at org.appcelerator.titanium.TiContext.runtimeError(TiContext.java:304)
  332             KrollMethod  E  	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:946)
  332             KrollMethod  E  	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1001)
  332             KrollMethod  E  	at org.mozilla.javascript.Context.reportRuntimeError2(Context.java:971)
  332             KrollMethod  E  	at org.mozilla.javascript.NativeJavaObject.reportConversionError(NativeJavaObject.java:896)
  332             KrollMethod  E  	at org.mozilla.javascript.NativeJavaObject.toInteger(NativeJavaObject.java:887)
  332             KrollMethod  E  	at org.mozilla.javascript.NativeJavaObject.coerceToNumber(NativeJavaObject.java:766)
  332             KrollMethod  E  	at org.mozilla.javascript.NativeJavaObject.coerceTypeImpl(NativeJavaObject.java:553)
  332             KrollMethod  E  	at org.mozilla.javascript.Context.jsToJava(Context.java:1729)
  332             KrollMethod  E  	at org.appcelerator.kroll.KrollConverter.convertJavascript(KrollConverter.java:329)
  332             KrollMethod  E  	at ti.modules.titanium.app.properties.PropertiesModuleBindingGen$11.invoke(PropertiesModuleBindingGen.j
                                  ava:563)
  332             KrollMethod  E  	at org.appcelerator.kroll.KrollMethod.call(KrollMethod.java:51)
  332             KrollMethod  E  	at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1701)
  332             KrollMethod  E  	at script(file:///android_asset/Resources/app.js:110)
  332             KrollMethod  E  	at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:854)
  332             KrollMethod  E  	at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
  332             KrollMethod  E  	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
  332             KrollMethod  E  	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3177)
  332             KrollMethod  E  	at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
  332             KrollMethod  E  	at org.mozilla.javascript.Context.evaluateReader(Context.java:1142)
  332             KrollMethod  E  	at org.appcelerator.titanium.kroll.KrollContext$DefaultEvaluator.evaluateFile(KrollContext.java:106)
  332             KrollMethod  E  	at org.appcelerator.titanium.kroll.KrollContext.evaluateScript(KrollContext.java:353)
  332             KrollMethod  E  	at org.appcelerator.titanium.kroll.KrollContext.handleEvalFile(KrollContext.java:382)
  332             KrollMethod  E  	at org.appcelerator.titanium.kroll.KrollContext.handleMessage(KrollContext.java:218)
  332             KrollMethod  E  	at org.appcelerator.titanium.TiMessageQueue.handleMessage(TiMessageQueue.java:223)
  332             KrollMethod  E  	at android.os.Handler.dispatchMessage(Handler.java:95)
  332             KrollMethod  E  	at android.os.Looper.loop(Looper.java:123)
  332             KrollMethod  E  	at org.appcelerator.titanium.kroll.KrollHandlerThread.run(KrollHandlerThread.java:86)
  332            KrollContext  E  (kroll$1: app://app.js) [15,1077] Error evaluating source: Cannot convert 2147483648 to java.lang.Intege
                                  r (file:///android_asset/Resources/app.js#110)
  332            KrollContext  E  org.mozilla.javascript.EvaluatorException: Cannot convert 2147483648 to java.lang.Integer (file:///andro
                                  id_asset/Resources/app.js#110)
  332            KrollContext  E  	at org.appcelerator.titanium.TiContext.runtimeError(TiContext.java:304)
  332            KrollContext  E  	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:946)
  332            KrollContext  E  	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1001)
  332            KrollContext  E  	at org.mozilla.javascript.Context.reportRuntimeError2(Context.java:971)
  332            KrollContext  E  	at org.mozilla.javascript.NativeJavaObject.reportConversionError(NativeJavaObject.java:896)
  332            KrollContext  E  	at org.mozilla.javascript.NativeJavaObject.toInteger(NativeJavaObject.java:887)
  332            KrollContext  E  	at org.mozilla.javascript.NativeJavaObject.coerceToNumber(NativeJavaObject.java:766)
  332            KrollContext  E  	at org.mozilla.javascript.NativeJavaObject.coerceTypeImpl(NativeJavaObject.java:553)
  332            KrollContext  E  	at org.mozilla.javascript.Context.jsToJava(Context.java:1729)
  332            KrollContext  E  	at org.appcelerator.kroll.KrollConverter.convertJavascript(KrollConverter.java:329)
  332            KrollContext  E  	at ti.modules.titanium.app.properties.PropertiesModuleBindingGen$11.invoke(PropertiesModuleBindingGen.j
                                  ava:563)
  332            KrollContext  E  	at org.appcelerator.kroll.KrollMethod.call(KrollMethod.java:51)
  332            KrollContext  E  	at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1701)
  332            KrollContext  E  	at script(file:///android_asset/Resources/app.js:110)
  332            KrollContext  E  	at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:854)
  332            KrollContext  E  	at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
  332            KrollContext  E  	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
  332            KrollContext  E  	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3177)
  332            KrollContext  E  	at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
  332            KrollContext  E  	at org.mozilla.javascript.Context.evaluateReader(Context.java:1142)
  332            KrollContext  E  	at org.appcelerator.titanium.kroll.KrollContext$DefaultEvaluator.evaluateFile(KrollContext.java:106)
  332            KrollContext  E  	at org.appcelerator.titanium.kroll.KrollContext.evaluateScript(KrollContext.java:353)
  332            KrollContext  E  	at org.appcelerator.titanium.kroll.KrollContext.handleEvalFile(KrollContext.java:382)
  332            KrollContext  E  	at org.appcelerator.titanium.kroll.KrollContext.handleMessage(KrollContext.java:218)
  332            KrollContext  E  	at org.appcelerator.titanium.TiMessageQueue.handleMessage(TiMessageQueue.java:223)
  332            KrollContext  E  	at android.os.Handler.dispatchMessage(Handler.java:95)
  332            KrollContext  E  	at android.os.Looper.loop(Looper.java:123)
  332            KrollContext  E  	at org.appcelerator.titanium.kroll.KrollHandlerThread.run(KrollHandlerThread.java:86)
  332               TiJSError  E  (kroll$1: app://app.js) [9,1086] ----- Titanium Javascript Runtime Error -----
  332               TiJSError  E  (kroll$1: app://app.js) [5,1091] - In file:///android_asset/Resources/app.js:110,0
  332               TiJSError  E  (kroll$1: app://app.js) [0,1091] - Message: Cannot convert 2147483648 to java.lang.Integer (file:///andr
                                  oid_asset/Resources/app.js#110)
  332               TiJSError  E  (kroll$1: app://app.js) [1,1092] - Source: null
  332          TiRootActivity  I  (main) [0,0] checkpoint, on root activity resume. context = org.appcelerator.titanium.TiContext@44f20810

Workaround

Use Ti.App.Properties.\[g|s\]etDouble():
Ti.App.Properties.setDouble("veryHighInt", 1325269931753);
Ti.API.info("****** veryHighInt Saved ******");
Ti.App.Properties.setDouble("veryHighIntPlusOneThousand", Ti.App.Properties.getDouble("veryHighInt") + 1000);
Ti.API.info("****** veryHighIntPlusOneThousand Saved ******");
Ti.API.info("----> veryHighIntPlusOneThousand = " + Ti.App.Properties.getDouble("veryHighIntPlusOneThousand"));
  576            KrollContext  D  (kroll$1: app://app.js) [46,701] Running evaluated script: file:///android_asset/Resources/app.js
  576                   TiAPI  I  (kroll$1: app://app.js) [38,739] ****** veryHighInt Saved ******
  576                   TiAPI  I  (kroll$1: app://app.js) [11,750] ****** veryHighIntPlusOneThousand Saved ******
  576                   TiAPI  I  (kroll$1: app://app.js) [3,753] ----> veryHighIntPlusOneThousand = 1325269932753

Comments

  1. Keith Gable 2011-12-30

    A possible workaround is to divide getTime() by 1000 to get the number of seconds, and then use parseInt() to remove the floating point portion. Then you lose millisecond precision, though.
       Ti.App.Properties.setInt("my_key_here", parseInt(instance_of_date.getTime() / 1000));
       
    So maybe this issue is that Java ints aren't big enough but Objective C ints are. Obviously this implementation difference should never be an issue in JavaScript.
  2. Paul Dowsett 2011-12-30

    For the reasons explained in TC-524, would you please edit the ticket to includes a test case that produces the included error? Thanks
  3. Paul Dowsett 2011-12-30

    Keith Did you seriously test this issue on all those devices listed in the environment field, or are you simply listing all the devices you have available? Please only include the devices that you have physically tested. Also, if you have tested it in an emulator, you could include details of that. Thanks
  4. Paul Dowsett 2012-01-05

    Keith Would you please provide the information I've requested, so I may escalate this ticket? Thanks
  5. Keith Gable 2012-01-05

    I have personally tested it on a Samsung Infuse 4G on AT&T running Gingerbread and my QA team tested it on whatever devices they tested it on (which is probably the aforementioned list but may not be the entire list, since the code failed miserably, they may not have tested it everywhere). "org.mozilla.javascript.EvaluatorException: Cannot convert 1325269931753 to java.lang.Integer" does not strike me as a device-specific problem however.
  6. Paul Dowsett 2012-01-05

    The use case is invalid. Blain Harmon's response below. However, am going to raise a apidoc ticket as this should be documented. I do believe that the difference is that iOS is simply letting the value overflow, so that the value stored with setInt would be some truncated, rolled over value. The reason I say that is that the data type used during setInt is indeed a 32 bit signed integer, and Apple's iOS docs say nothing of exceptions (The underlying method that causes the conversion is [NSNumber intValue]). Either way, An Int is a specific data type, of a 32 bit signed integer. HOWEVER, I think the issue here is one of documentation, to specify that int is the aforementioned specific data type. If you wish to store a larger value, setDouble exists for that very reason.
  7. Lee Morris 2017-03-21

    Closing ticket as invalid.

JSON Source