Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7413] Android: Function variables for Windows methods cannot be successfully invoked.

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionInvalid
Resolution Date2012-02-08T17:20:47.000+0000
Affected Version/sRelease 1.8.0.1, Release 2.0.0, Release 1.8.1
Fix Version/sn/a
ComponentsAndroid
Labelsqe-and012312
ReporterDustin Hyde
AssigneeBill Dawson
Created2012-01-25T17:23:25.000+0000
Updated2013-05-01T20:06:59.000+0000

Description

The following app.js results in a runtime error in both runtimes:
var f = Ti.UI.createWindow().open;
f();
The error:
E/TiJSError(  764): (main) [512,1939] ----- Titanium Javascript Runtime Error -----
E/TiJSError(  764): (main) [1,1940] - In ti:/window.js:173,38
E/TiJSError(  764): (main) [0,1940] - Message: Uncaught TypeError: Cannot read property 'closed' of undefined
E/TiJSError(  764): (main) [0,1940] - Source: 		if (this.currentState != this.state.closed) {
E/V8Exception(  764): Exception occurred at ti:/window.js:173: Uncaught TypeError: Cannot read property 'closed' of undefined
It could be that this goes beyond windows methods... It's just that we know of this particular example.

Original Description

Running standard code causes internal runtime error. Log Attached. There is a similar crash in Rhino. Code works fine in iOS. Steps to Reproduce: 1. Run Code:
function Window1(){
	var self = {};
	
	var win =  Ti.UI.createWindow();
	var btn =  Ti.UI.createButton({
		title:'open',
		left:20,
		right:20,
		height:44
	});

	btn.addEventListener('click', function(){
		Window2().open();
	});

	win.add(btn);
	self.open = win.open;
	self.close = win.close;
	return self;
}

function Window2(){
	var self = {};
	
	var win = Ti.UI.createWindow({
		backgroundColor:'green'
	});
	win.addEventListener('open', function(){
		win.animate({
			left:10,
			duration:300
		}, function(){
			Ti.API.info('complete');
		});
	});
	var btn =  Ti.UI.createButton({title:'close', left:20,right:20,height:44});
	btn.addEventListener('click', function(){
		win.close();
	});
	win.add(btn);
	
	self.open = win.open;
	self.close = win.close;
	return self;
}

Window1().open();
Expected Result: App should run. Actual Result: Runtime error. Note: This test code exists in /titanium_mobile_tests_internal/FeatureTest/Window/Window_timob7246 Note: Will investigate behavior on other devices in future.

Attachments

FileDateSize
android v8 runtime error.txt2012-01-25T17:23:25.000+00006992

Comments

  1. Dustin Hyde 2012-01-30

    Same test code as TIMOB-7246.
  2. Bill Dawson 2012-02-08

    Changed the title and description to reflect underlying problem.
  3. Bill Dawson 2012-02-08

    self.open = win.open (and then calling self.open(); via Window1().open();) is not really valid Javascript. (I mean, it's syntactically okay, but the hoped-for behavior shouldn't occur.) If it's working in iOS, then it's really kinda just luck, i.e., setting a window's open method to a variable and having it work successfully must mean that the method itself somehow has a pre-bound context to it due to some kind of plumbing in our iOS implementation. This similar plain-old Javascript code makes the point:
       var o = {
       	x: 5,
       	f: function() {
       		console.log("this.x == " + this.x);
       	}
       };
       var f = o.f;
       f();
       
    When you take f away from o it becomes "this-less", so to speak. :) So the result is:
       this.x == undefined
       
    The same with window.open... When open is taken by itself, any code within it (when it is invoked) that tries to manipulate "its" window ... its this ... is going to fail. You can get the test to work by changing those lines that set self.open and self.close so they do something like:
       self.open = function(){win.open();};
       
  4. Dustin Hyde 2013-05-01

    Closing.

JSON Source