Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-6819] Android: V8 Only Supports String and Primitive Constants

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionInvalid
Resolution Date2017-06-05T23:31:44.000+0000
Affected Version/sRelease 1.8.0.1
Fix Version/sn/a
ComponentsAndroid
Labelstbs-1.8.2
ReporterDawson Toth
AssigneeEric Merriman
Created2011-12-21T11:22:10.000+0000
Updated2017-06-05T23:31:44.000+0000

Description

Problem

With V8, any constant that isn't a String or primitive doesn't seem to be accessible from JavaScript -- they come across as undefined.

Is Rhino Broken [in this way] Too?

Nope.

Reproduction

Create a module (where "titanium" is an alias to the titanium.py in your 1.8.0.1 folder): {quote}titanium create --type=module --name=booltest --id=ti.booltest --platform=android{quote} My original test requires the following two file changes in the module. It tests for access to Boolean constants. See my comments down below for a more comprehensive, albiet less humorous, test. Update the module's source file to the following:
package ti.booltest;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;

@Kroll.module(name="Booltest", id="ti.booltest")
public class BooltestModule extends KrollModule
{
        @Kroll.constant public static final Boolean THE_SKY_IS_BLUE = true;

        public BooltestModule()
        {
                super();
        }
}
Update the app.js to the following:
var win = Ti.UI.createWindow({
        backgroundColor: '#fff'
});
win.add(Ti.UI.createLabel({
        text: 'Papa! Papa! Is the sky blue?\n\n'
                + (require('ti.booltest').THE_SKY_IS_BLUE ? 'Yes! Just like your eyes, son.' : 'No! And while we\'re at it, Santa...')
}));
win.open();
Run the code on your device. You should see confirmation that the sky is, indeed, blue (in the form of a gripping father-son narrative). If you see slander against Santa, than we still have a problem that needs to be fixed.

Comments

  1. Dawson Toth 2011-12-21

    While we're at it, let's test all the available constants.

    Expanded Results

    All the following work on Rhino. But ONLY STRINGS work on V8.
       package ti.booltest;
       
       import org.appcelerator.kroll.KrollModule;
       import org.appcelerator.kroll.annotations.Kroll;
       
       import org.appcelerator.titanium.TiApplication;
       
       @Kroll.module(name="Booltest", id="ti.booltest")
       public class BooltestModule extends KrollModule
       {
       	/*
       	 * This test marches through the various available types in Java
       	 * to see if they can be used as constants in a Titanium Mobile app.
       	 * 
       	 * They follow the naming convention "d" + either "w" for the wrapped
       	 * form or "p" for the primitive form + the variable type.
       	 */
       
       	@Kroll.constant public static final Boolean dbool = true;
       	@Kroll.constant public static final Integer dint = 2;
       	@Kroll.constant public static final Short dshort = 3;
       	@Kroll.constant public static final Byte dbyte = 0x4;
       	@Kroll.constant public static final Float dfloat = 5.5f;
       	@Kroll.constant public static final Double ddouble = 6.6d;
       	@Kroll.constant public static final Long dlong = 7l;
       	@Kroll.constant public static final String dstr = "So how 'bout them Bears?";
       	
       	public BooltestModule()
       	{
       		super();
       	}
       }
       
       var win = Ti.UI.createWindow({
           backgroundColor: '#fff',
           layout: 'vertical'
       });
       var constants = [
           'dbool', 'dint', 'dshort', 'dbyte', 'dfloat', 'ddouble', 'dlong', 'dstr'
       ];
       var BoolTest = require('ti.booltest');
       for (var c in constants) {
           win.add(Ti.UI.createLabel({
               text: BoolTest[constants[c]] || (constants[c] + ' FAILED!'),
               height: '12dp', top: '1dp'
           }));
       }
       win.open();
       
  2. Dawson Toth 2011-12-21

    Sorry for the misleading title. After further investigation, Rhino supports any sort of constants I throw at it. But V8 does not support any wrapped type variable (like Boolean, Integer, Float) EXCEPT for String. The primitive forms work great (int, bool, short, etc).

JSON Source